modifiers.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CHECK_MODIFIERS_H_
  5. #define CARBON_TOOLCHAIN_CHECK_MODIFIERS_H_
  6. #include "toolchain/check/context.h"
  7. namespace Carbon::Check {
  8. // Reports a diagnostic if access control modifiers on this are not allowed for
  9. // a declaration in `target_scope_id`, and updates the declaration state in
  10. // `context`.
  11. //
  12. // `target_scope_id` may be Invalid for a declaration in a block scope.
  13. auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind,
  14. SemIR::NameScopeId target_scope_id) -> void;
  15. // Reports a diagnostic if the method function modifiers `abstract`, `virtual`,
  16. // or `impl` are present but not permitted on a function declaration in
  17. // `target_scope_id`.
  18. //
  19. // `target_scope_id` may be Invalid for a declaration in a block scope.
  20. auto CheckMethodModifiersOnFunction(Context& context,
  21. SemIR::NameScopeId target_scope_id) -> void;
  22. // Like `LimitModifiersOnDecl`, except says which modifiers are forbidden, and a
  23. // `context_string` (and optional `context_node`) specifying the context in
  24. // which those modifiers are forbidden.
  25. // TODO: Take another look at diagnostic phrasing for callers.
  26. auto ForbidModifiersOnDecl(Context& context, KeywordModifierSet forbidden,
  27. Lex::TokenKind decl_kind,
  28. llvm::StringRef context_string,
  29. Parse::NodeId context_node = Parse::NodeId::Invalid)
  30. -> void;
  31. // Reports a diagnostic (using `decl_kind`) if modifiers on this declaration are
  32. // not in `allowed`. Updates the declaration state in
  33. // `context.decl_state_stack()`.
  34. inline auto LimitModifiersOnDecl(Context& context, KeywordModifierSet allowed,
  35. Lex::TokenKind decl_kind) -> void {
  36. ForbidModifiersOnDecl(context, ~allowed, decl_kind, "");
  37. }
  38. // If the `extern` modifier is present, diagnoses and updates the declaration
  39. // state to remove it. Only called for declarations with definitions.
  40. auto ForbidExternModifierOnDefinition(Context& context,
  41. Lex::TokenKind decl_kind) -> void;
  42. // Report a diagonostic if `default` and `final` modifiers are used on
  43. // declarations where they are not allowed. Right now they are only allowed
  44. // inside interfaces.
  45. //
  46. // `target_scope_id` may be Invalid for a declaration in a block scope.
  47. auto RequireDefaultFinalOnlyInInterfaces(Context& context,
  48. Lex::TokenKind decl_kind,
  49. SemIR::NameScopeId target_scope_id)
  50. -> void;
  51. } // namespace Carbon::Check
  52. #endif // CARBON_TOOLCHAIN_CHECK_MODIFIERS_H_