modifiers.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 `parent_scope_inst`, and updates `introducer`.
  10. //
  11. // `parent_scope_inst` may be nullopt for a declaration in a block scope.
  12. auto CheckAccessModifiersOnDecl(Context& context,
  13. DeclIntroducerState& introducer,
  14. std::optional<SemIR::Inst> parent_scope_inst)
  15. -> void;
  16. // Reports a diagnostic if the method function modifiers `abstract`, `virtual`,
  17. // or `impl` are present but not permitted on a function declaration in
  18. // `parent_scope_inst`.
  19. //
  20. // `parent_scope_inst` may be nullopt for a declaration in a block scope.
  21. auto CheckMethodModifiersOnFunction(
  22. Context& context, DeclIntroducerState& introducer,
  23. SemIR::InstId parent_scope_inst_id,
  24. std::optional<SemIR::Inst> parent_scope_inst) -> void;
  25. // Like `LimitModifiersOnDecl`, except says which modifiers are forbidden, and a
  26. // `context_string` (and optional `context_loc_id`) specifying the context in
  27. // which those modifiers are forbidden.
  28. // TODO: Take another look at diagnostic phrasing for callers.
  29. auto ForbidModifiersOnDecl(Context& context, DeclIntroducerState& introducer,
  30. KeywordModifierSet forbidden,
  31. llvm::StringRef context_string,
  32. SemIR::LocId context_loc_id = SemIR::LocId::Invalid)
  33. -> void;
  34. // Reports a diagnostic (using `decl_kind`) if modifiers on this declaration are
  35. // not in `allowed`. Updates `introducer`.
  36. inline auto LimitModifiersOnDecl(Context& context,
  37. DeclIntroducerState& introducer,
  38. KeywordModifierSet allowed) -> void {
  39. ForbidModifiersOnDecl(context, introducer, ~allowed, "");
  40. }
  41. inline auto LimitModifiersOnNotDefinition(Context& context,
  42. DeclIntroducerState& introducer,
  43. KeywordModifierSet allowed) -> void {
  44. ForbidModifiersOnDecl(context, introducer, ~allowed, ", only definition");
  45. }
  46. // Restricts the `extern` modifier to only be used on namespace-scoped
  47. // declarations. Diagnoses and cleans up:
  48. // - `extern library` on a definition.
  49. // - `extern` on a scoped entity.
  50. //
  51. // `parent_scope_inst` may be nullopt for a declaration in a block scope.
  52. auto RestrictExternModifierOnDecl(Context& context,
  53. DeclIntroducerState& introducer,
  54. std::optional<SemIR::Inst> parent_scope_inst,
  55. bool is_definition) -> void;
  56. // Report a diagonostic if `default` and `final` modifiers are used on
  57. // declarations where they are not allowed. Right now they are only allowed
  58. // inside interfaces.
  59. //
  60. // `parent_scope_inst` may be nullopt for a declaration in a block scope.
  61. auto RequireDefaultFinalOnlyInInterfaces(
  62. Context& context, DeclIntroducerState& introducer,
  63. std::optional<SemIR::Inst> parent_scope_inst) -> void;
  64. } // namespace Carbon::Check
  65. #endif // CARBON_TOOLCHAIN_CHECK_MODIFIERS_H_