entity.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. struct Scope;
  2. struct Checker;
  3. struct Type;
  4. struct DeclInfo;
  5. // typedef enum BuiltinProcId BuiltinProcId;
  6. #define ENTITY_KINDS \
  7. ENTITY_KIND(Invalid) \
  8. ENTITY_KIND(Constant) \
  9. ENTITY_KIND(Variable) \
  10. ENTITY_KIND(TypeName) \
  11. ENTITY_KIND(Procedure) \
  12. ENTITY_KIND(Builtin) \
  13. ENTITY_KIND(ImportName) \
  14. ENTITY_KIND(LibraryName) \
  15. ENTITY_KIND(Nil) \
  16. ENTITY_KIND(Label)
  17. enum EntityKind {
  18. #define ENTITY_KIND(k) GB_JOIN2(Entity_, k),
  19. ENTITY_KINDS
  20. #undef ENTITY_KIND
  21. Entity_Count,
  22. };
  23. String const entity_strings[] = {
  24. #define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1},
  25. ENTITY_KINDS
  26. #undef ENTITY_KIND
  27. };
  28. enum EntityFlag {
  29. EntityFlag_Visited = 1<<0,
  30. EntityFlag_Used = 1<<1,
  31. EntityFlag_Using = 1<<2,
  32. EntityFlag_Field = 1<<3,
  33. EntityFlag_Param = 1<<4,
  34. EntityFlag_VectorElem = 1<<5,
  35. EntityFlag_Ellipsis = 1<<6,
  36. EntityFlag_NoAlias = 1<<7,
  37. EntityFlag_TypeField = 1<<8,
  38. EntityFlag_Value = 1<<9,
  39. EntityFlag_Sret = 1<<10,
  40. EntityFlag_BitFieldValue = 1<<11,
  41. };
  42. // Zero value means the overloading process is not yet done
  43. enum OverloadKind {
  44. Overload_Unknown,
  45. Overload_No,
  46. Overload_Yes,
  47. };
  48. enum EntityAliasKind {
  49. EntityAlias_Invalid,
  50. EntityAlias_Type,
  51. EntityAlias_Entity,
  52. };
  53. // An Entity is a named "thing" in the language
  54. struct Entity {
  55. EntityKind kind;
  56. u64 id;
  57. u32 flags;
  58. Token token;
  59. Scope * scope;
  60. Type * type;
  61. AstNode * identifier; // Can be NULL
  62. DeclInfo * parent_proc_decl; // NULL if in file/global scope
  63. // TODO(bill): Cleanup how `using` works for entities
  64. Entity * using_parent;
  65. AstNode * using_expr;
  66. union {
  67. struct {
  68. ExactValue value;
  69. } Constant;
  70. struct {
  71. i32 field_index;
  72. i32 field_src_index;
  73. bool is_immutable;
  74. bool is_thread_local;
  75. ExactValue default_value;
  76. bool default_is_nil;
  77. } Variable;
  78. struct {
  79. bool is_type_alias;
  80. } TypeName;
  81. struct {
  82. OverloadKind overload_kind;
  83. String link_name;
  84. u64 tags;
  85. bool is_foreign;
  86. Entity * foreign_library;
  87. AstNode * foreign_library_ident;
  88. } Procedure;
  89. struct {
  90. i32 id;
  91. } Builtin;
  92. struct {
  93. String path;
  94. String name;
  95. Scope *scope;
  96. bool used;
  97. } ImportName;
  98. struct {
  99. String path;
  100. String name;
  101. bool used;
  102. } LibraryName;
  103. i32 Nil;
  104. struct {
  105. String name;
  106. AstNode *node;
  107. } Label;
  108. };
  109. };
  110. gb_global Entity *e_context = NULL;
  111. bool is_entity_kind_exported(EntityKind kind) {
  112. switch (kind) {
  113. case Entity_Builtin:
  114. case Entity_ImportName:
  115. case Entity_LibraryName:
  116. case Entity_Nil:
  117. return false;
  118. }
  119. return true;
  120. }
  121. bool is_entity_exported(Entity *e) {
  122. // TODO(bill): Determine the actual exportation rules for imports of entities
  123. GB_ASSERT(e != NULL);
  124. if (!is_entity_kind_exported(e->kind)) {
  125. return false;
  126. }
  127. String name = e->token.string;
  128. if (name.len == 0) {
  129. return false;
  130. }
  131. return name[0] != '_';
  132. }
  133. gb_global u64 global_entity_id = 0;
  134. Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token, Type *type) {
  135. Entity *entity = gb_alloc_item(a, Entity);
  136. entity->kind = kind;
  137. entity->scope = scope;
  138. entity->token = token;
  139. entity->type = type;
  140. entity->id = ++global_entity_id;
  141. return entity;
  142. }
  143. Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type, bool is_immutable) {
  144. Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type);
  145. entity->Variable.is_immutable = is_immutable;
  146. return entity;
  147. }
  148. Entity *make_entity_using_variable(gbAllocator a, Entity *parent, Token token, Type *type) {
  149. GB_ASSERT(parent != NULL);
  150. token.pos = parent->token.pos;
  151. Entity *entity = alloc_entity(a, Entity_Variable, parent->scope, token, type);
  152. entity->using_parent = parent;
  153. entity->parent_proc_decl = parent->parent_proc_decl;
  154. entity->flags |= EntityFlag_Using;
  155. return entity;
  156. }
  157. Entity *make_entity_constant(gbAllocator a, Scope *scope, Token token, Type *type, ExactValue value) {
  158. Entity *entity = alloc_entity(a, Entity_Constant, scope, token, type);
  159. entity->Constant.value = value;
  160. return entity;
  161. }
  162. Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *type) {
  163. Entity *entity = alloc_entity(a, Entity_TypeName, scope, token, type);
  164. return entity;
  165. }
  166. Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool is_using, bool is_immutable) {
  167. Entity *entity = make_entity_variable(a, scope, token, type, is_immutable);
  168. entity->flags |= EntityFlag_Used;
  169. if (is_using) entity->flags |= EntityFlag_Using;
  170. entity->flags |= EntityFlag_Param;
  171. return entity;
  172. }
  173. Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool is_using, i32 field_src_index) {
  174. Entity *entity = make_entity_variable(a, scope, token, type, false);
  175. entity->Variable.field_src_index = field_src_index;
  176. entity->Variable.field_index = field_src_index;
  177. if (is_using) entity->flags |= EntityFlag_Using;
  178. entity->flags |= EntityFlag_Field;
  179. return entity;
  180. }
  181. Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) {
  182. Entity *entity = make_entity_variable(a, scope, token, type, false);
  183. entity->Variable.field_src_index = field_src_index;
  184. entity->Variable.field_index = field_src_index;
  185. entity->flags |= EntityFlag_Field;
  186. entity->flags |= EntityFlag_VectorElem;
  187. return entity;
  188. }
  189. Entity *make_entity_procedure(gbAllocator a, Scope *scope, Token token, Type *signature_type, u64 tags) {
  190. Entity *entity = alloc_entity(a, Entity_Procedure, scope, token, signature_type);
  191. entity->Procedure.tags = tags;
  192. return entity;
  193. }
  194. Entity *make_entity_builtin(gbAllocator a, Scope *scope, Token token, Type *type, i32 id) {
  195. Entity *entity = alloc_entity(a, Entity_Builtin, scope, token, type);
  196. entity->Builtin.id = id;
  197. return entity;
  198. }
  199. Entity *make_entity_import_name(gbAllocator a, Scope *scope, Token token, Type *type,
  200. String path, String name, Scope *import_scope) {
  201. Entity *entity = alloc_entity(a, Entity_ImportName, scope, token, type);
  202. entity->ImportName.path = path;
  203. entity->ImportName.name = name;
  204. entity->ImportName.scope = import_scope;
  205. return entity;
  206. }
  207. Entity *make_entity_library_name(gbAllocator a, Scope *scope, Token token, Type *type,
  208. String path, String name) {
  209. Entity *entity = alloc_entity(a, Entity_LibraryName, scope, token, type);
  210. entity->LibraryName.path = path;
  211. entity->LibraryName.name = name;
  212. return entity;
  213. }
  214. Entity *make_entity_nil(gbAllocator a, String name, Type *type) {
  215. Token token = make_token_ident(name);
  216. Entity *entity = alloc_entity(a, Entity_Nil, NULL, token, type);
  217. return entity;
  218. }
  219. Entity *make_entity_label(gbAllocator a, Scope *scope, Token token, Type *type,
  220. AstNode *node) {
  221. Entity *entity = alloc_entity(a, Entity_Label, scope, token, type);
  222. entity->Label.node = node;
  223. return entity;
  224. }
  225. Entity *make_entity_dummy_variable(gbAllocator a, Scope *scope, Token token) {
  226. token.string = str_lit("_");
  227. return make_entity_variable(a, scope, token, NULL, false);
  228. }