entity_with_params_base.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_SEM_IR_ENTITY_WITH_PARAMS_BASE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_ENTITY_WITH_PARAMS_BASE_H_
  6. #include "toolchain/sem_ir/ids.h"
  7. namespace Carbon::SemIR {
  8. // Common entity fields.
  9. //
  10. // `EntityWithParamsBase` would be a base class of entities like `Function`,
  11. // except that then we couldn't use named initialization (or would need to
  12. // disable warnings about mixing named and unnamed initialization) due to how
  13. // C++ handles initialization of base structs. Instead, this is composed with a
  14. // `Fields` struct to provide an entity's actual struct.
  15. //
  16. // For example:
  17. // struct FunctionFields {
  18. // ... data members ...
  19. // };
  20. //
  21. // struct Function : public EntityWithParamsBase,
  22. // public FunctionFields, public Printable<Function> {
  23. // ... methods ...
  24. // };
  25. //
  26. // This achieves a few things:
  27. // - Allows named initialization, such as:
  28. // `{{.name_id = ...}, {.function_field = ...}}`
  29. // - Makes `entity.name_id` access work.
  30. // - Allows passing a `EntityWithParamsBase*` when only common fields are
  31. // needed.
  32. // - Does all this in a way that's vanilla C++.
  33. struct EntityWithParamsBase {
  34. auto PrintBaseFields(llvm::raw_ostream& out) const -> void {
  35. out << "name: " << name_id << ", parent_scope: " << parent_scope_id;
  36. }
  37. // When merging a declaration and definition, prefer things which would point
  38. // at the definition for diagnostics.
  39. auto MergeDefinition(const EntityWithParamsBase& definition) -> void {
  40. first_param_node_id = definition.first_param_node_id;
  41. last_param_node_id = definition.last_param_node_id;
  42. implicit_param_refs_id = definition.implicit_param_refs_id;
  43. param_refs_id = definition.param_refs_id;
  44. definition_id = definition.definition_id;
  45. }
  46. // Returns the instruction for the first declaration.
  47. auto first_decl_id() const -> SemIR::InstId {
  48. if (non_owning_decl_id.is_valid()) {
  49. return non_owning_decl_id;
  50. }
  51. CARBON_CHECK(first_owning_decl_id.is_valid());
  52. return first_owning_decl_id;
  53. }
  54. // Returns the instruction for the latest declaration.
  55. auto latest_decl_id() const -> SemIR::InstId {
  56. if (definition_id.is_valid()) {
  57. return definition_id;
  58. }
  59. if (first_owning_decl_id.is_valid()) {
  60. return first_owning_decl_id;
  61. }
  62. return non_owning_decl_id;
  63. }
  64. // Determines whether this entity has any parameter lists.
  65. auto has_parameters() const -> bool {
  66. return implicit_param_refs_id.is_valid() || param_refs_id.is_valid();
  67. }
  68. // The following members always have values, and do not change throughout the
  69. // lifetime of the entity.
  70. // The class name.
  71. NameId name_id;
  72. // The parent scope.
  73. NameScopeId parent_scope_id;
  74. // If this is a generic function, information about the generic.
  75. GenericId generic_id;
  76. // Parse tree bounds for the parameters, including both implicit and explicit
  77. // parameters. These will be compared to match between declaration and
  78. // definition.
  79. Parse::NodeId first_param_node_id;
  80. Parse::NodeId last_param_node_id;
  81. // A block containing a single reference instruction per implicit parameter.
  82. InstBlockId implicit_param_refs_id;
  83. // A block containing a single reference instruction per parameter.
  84. InstBlockId param_refs_id;
  85. // True if declarations are `extern`.
  86. bool is_extern;
  87. // For an `extern library` declaration, the library name.
  88. SemIR::LibraryNameId extern_library_id;
  89. // The non-owning declaration of the entity, if present. This will be a
  90. // <entity>Decl.
  91. InstId non_owning_decl_id;
  92. // The first owning declaration of the entity, if present. This will be a
  93. // <entity>Decl. It may either be a forward declaration, or the same as
  94. // `definition_id`.
  95. InstId first_owning_decl_id;
  96. // The following members are set at the `{` of the definition.
  97. // The definition of the entity. This will be a <entity>Decl.
  98. InstId definition_id = InstId::Invalid;
  99. };
  100. } // namespace Carbon::SemIR
  101. #endif // CARBON_TOOLCHAIN_SEM_IR_ENTITY_WITH_PARAMS_BASE_H_