scope.h (2228B)
1 #ifndef HAREC_SCOPE_H 2 #define HAREC_SCOPE_H 3 #include "expr.h" 4 #include "identifier.h" 5 6 #define SCOPE_BUCKETS 4096 7 8 enum object_type { 9 O_BIND, 10 O_CONST, 11 O_DECL, 12 O_SCAN, 13 O_TYPE, 14 }; 15 16 struct scope_object { 17 enum object_type otype; 18 // name is the name of the object within this scope (for lookups) 19 // ident is the global identifier (these may be different in some cases) 20 struct identifier name, ident; 21 bool threadlocal; 22 23 const struct type *type; 24 struct expression *value; // For O_CONST 25 26 struct scope_object *lnext; // Linked list 27 struct scope_object *mnext; // Hash map 28 }; 29 30 enum scope_class { 31 SCOPE_COMPOUND, 32 SCOPE_ENUM, 33 SCOPE_FUNC, 34 SCOPE_LOOP, 35 SCOPE_MATCH, 36 SCOPE_SUBUNIT, 37 SCOPE_UNIT, 38 SCOPE_DEFINES, 39 }; 40 41 struct expression; 42 43 struct yield { 44 struct expression **expression; 45 struct yield *next; 46 }; 47 48 struct scope { 49 enum scope_class class; 50 const char *label; 51 struct scope *parent; 52 53 const struct type *hint; 54 struct type_tagged_union *results; 55 struct yield *yields; 56 57 // Linked list in insertion order 58 // Used for function parameters and enum values, where order matters 59 struct scope_object *objects; 60 struct scope_object **next; 61 62 // Hash map in reverse insertion order 63 // Used for lookups, and accounts for shadowing 64 struct scope_object *buckets[SCOPE_BUCKETS]; 65 }; 66 67 struct scopes { 68 struct scope *scope; 69 struct scopes *next; 70 }; 71 72 struct scope *scope_push(struct scope **stack, enum scope_class class); 73 struct scope *scope_pop(struct scope **stack); 74 75 struct scope *scope_lookup_ancestor(struct scope *scope, 76 enum scope_class class, const char *label); 77 78 void scope_free(struct scope *scope); 79 void scope_free_all(struct scopes *scopes); 80 81 void scope_object_init(struct scope_object *obj, enum object_type otype, 82 const struct identifier *ident, const struct identifier *name, 83 const struct type *type, struct expression *value); 84 85 void scope_insert_from_object(struct scope *scope, struct scope_object *object); 86 87 struct scope_object *scope_insert( 88 struct scope *scope, enum object_type otype, 89 const struct identifier *ident, const struct identifier *name, 90 const struct type *type, struct expression *value); 91 92 struct scope_object *scope_lookup(struct scope *scope, 93 const struct identifier *ident); 94 95 #endif