harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

scope.h (2205B)


      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 
     22 	const struct type *type;
     23 	struct expression *value; // For O_CONST
     24 
     25 	struct scope_object *lnext; // Linked list
     26 	struct scope_object *mnext; // Hash map
     27 };
     28 
     29 enum scope_class {
     30 	SCOPE_COMPOUND,
     31 	SCOPE_ENUM,
     32 	SCOPE_FUNC,
     33 	SCOPE_LOOP,
     34 	SCOPE_MATCH,
     35 	SCOPE_SUBUNIT,
     36 	SCOPE_UNIT,
     37 };
     38 
     39 struct expression;
     40 
     41 struct yield {
     42 	struct expression **expression;
     43 	struct yield *next;
     44 };
     45 
     46 struct scope {
     47 	enum scope_class class;
     48 	const char *label;
     49 	struct scope *parent;
     50 
     51 	const struct type *hint;
     52 	struct type_tagged_union *results;
     53 	struct yield *yields;
     54 
     55 	// Linked list in insertion order
     56 	// Used for function parameters and enum values, where order matters
     57 	struct scope_object *objects;
     58 	struct scope_object **next;
     59 
     60 	// Hash map in reverse insertion order
     61 	// Used for lookups, and accounts for shadowing
     62 	struct scope_object *buckets[SCOPE_BUCKETS];
     63 };
     64 
     65 struct scopes {
     66 	struct scope *scope;
     67 	struct scopes *next;
     68 };
     69 
     70 struct scope *scope_push(struct scope **stack, enum scope_class class);
     71 struct scope *scope_pop(struct scope **stack);
     72 
     73 struct scope *scope_lookup_ancestor(struct scope *scope,
     74 		enum scope_class class, const char *label);
     75 
     76 void scope_free(struct scope *scope);
     77 void scope_free_all(struct scopes *scopes);
     78 
     79 void scope_object_init(struct scope_object *obj, enum object_type otype,
     80 	const struct identifier *ident, const struct identifier *name,
     81 	const struct type *type, struct expression *value);
     82 
     83 void scope_insert_from_object(struct scope *scope, struct scope_object *object);
     84 
     85 const struct scope_object *scope_insert(
     86 	struct scope *scope, enum object_type otype,
     87 	const struct identifier *ident, const struct identifier *name,
     88 	const struct type *type, struct expression *value);
     89 
     90 const struct scope_object *scope_lookup(struct scope *scope,
     91 	const struct identifier *ident);
     92 
     93 #endif