harec

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

check.h (3295B)


      1 #ifndef HARE_CHECK_H
      2 #define HARE_CHECK_H
      3 #include <stdbool.h>
      4 #include "identifier.h"
      5 #include "scope.h"
      6 #include "types.h"
      7 #include "type_store.h"
      8 
      9 struct expression;
     10 
     11 #define MODCACHE_BUCKETS 256
     12 
     13 struct modcache {
     14 	struct identifier ident;
     15 	struct scope *scope;
     16 	struct modcache *next;
     17 };
     18 
     19 struct ast_expression;
     20 struct ast_unit;
     21 
     22 struct errors {
     23 	struct location loc;
     24 	char *msg;
     25 	struct errors *next;
     26 };
     27 
     28 struct context {
     29 	struct modcache **modcache;
     30 	struct type_store *store;
     31 	const struct type *fntype;
     32 	struct identifier *ns;
     33 	bool is_test;
     34 	struct scope *unit;
     35 	struct scope *scope;
     36 	struct scope *defines;
     37 	bool deferring;
     38 	int id;
     39 	struct errors *errors;
     40 	struct errors **next;
     41 };
     42 
     43 struct constant_decl {
     44 	const struct type *type;
     45 	const struct expression *value;
     46 };
     47 
     48 enum func_decl_flags {
     49 	FN_FINI = 1 << 0,
     50 	FN_INIT = 1 << 1,
     51 	FN_TEST = 1 << 2,
     52 };
     53 
     54 struct function_decl {
     55 	const struct type *type;
     56 	struct expression *body;
     57 	struct scope *scope;
     58 	unsigned int flags; // enum function_flags
     59 };
     60 
     61 struct global_decl {
     62 	const struct type *type;
     63 	struct expression *value; // EXPR_CONSTANT
     64 	bool threadlocal;
     65 };
     66 
     67 enum declaration_type {
     68 	DECL_CONST,
     69 	DECL_FUNC,
     70 	DECL_GLOBAL,
     71 	DECL_TYPE,
     72 };
     73 
     74 struct declaration {
     75 	enum declaration_type type;
     76 	struct identifier ident;
     77 	struct location loc;
     78 	char *symbol;
     79 	bool exported;
     80 	union {
     81 		struct constant_decl constant;
     82 		struct function_decl func;
     83 		struct global_decl global;
     84 		const struct type *_type;
     85 	};
     86 };
     87 
     88 struct declarations {
     89 	struct declaration *decl;
     90 	struct declarations *next;
     91 };
     92 
     93 struct unit {
     94 	struct identifier *ns;
     95 	struct declarations *declarations;
     96 	struct identifiers *imports;
     97 };
     98 
     99 enum idecl_type {
    100 	IDECL_DECL,
    101 	IDECL_ENUM_FLD,
    102 };
    103 
    104 // Keeps track of enum specific context required for enum field resolution
    105 struct incomplete_enum_field {
    106 	struct ast_enum_field *field;
    107 	struct scope *enum_scope;
    108 };
    109 
    110 // Keeps track of context required to resolve a declaration or an enum field
    111 // Extends the scope_object struct so it can be inserted into a scope
    112 struct incomplete_declaration {
    113 	struct scope_object obj;
    114 	struct scope *imports; // the scope of this declaration's subunit
    115 	enum idecl_type type;
    116 	bool in_progress;
    117 	union {
    118 		struct ast_decl decl;
    119 		struct incomplete_enum_field *field;
    120 	};
    121 };
    122 
    123 void mkident(struct context *ctx, struct identifier *out,
    124 		const struct identifier *in, const char *symbol);
    125 
    126 typedef void (*resolvefn)(struct context *,
    127 		struct incomplete_declaration *idecl);
    128 
    129 void resolve_dimensions(struct context *ctx,
    130 		struct incomplete_declaration *idecl);
    131 
    132 void resolve_type(struct context *ctx,
    133 		struct incomplete_declaration *idecl);
    134 
    135 void resolve_decl(struct context *ctx,
    136 		struct incomplete_declaration *idecl);
    137 
    138 void wrap_resolver(struct context *ctx,
    139 	const struct scope_object *obj, resolvefn resolver);
    140 
    141 struct scope *check(struct type_store *ts,
    142 	bool is_test,
    143 	struct ast_global_decl *defines,
    144 	const struct ast_unit *aunit,
    145 	struct unit *unit);
    146 
    147 struct scope *check_internal(struct type_store *ts,
    148 	struct modcache **cache,
    149 	bool is_test,
    150 	struct ast_global_decl *defines,
    151 	const struct ast_unit *aunit,
    152 	struct unit *unit,
    153 	bool scan_only);
    154 
    155 void check_expression(struct context *ctx,
    156 	const struct ast_expression *aexpr,
    157 	struct expression *expr,
    158 	const struct type *hint);
    159 
    160 #endif