ast.h (16775B)
1 /* ast.h */ 2 #ifndef AST_H 3 #define AST_H 4 #include "token.h" 5 #include <stddef.h> 6 #include <stdint.h> 7 /* Node Types */ 8 typedef struct ast_node ast_node_t; 9 10 typedef enum { 11 // Expression nodes 12 AST_INTU32, 13 AST_INTU64, 14 AST_INTS32, 15 AST_INTS64, 16 AST_FLOAT, 17 AST_DOUBLE, 18 AST_STRING, 19 AST_CHAR, 20 AST_VAR, 21 AST_SUBSCRIPT, 22 AST_DIRECT_COMPONENT_SELECTION, 23 AST_INDIRECT_COMPONENT_SELECTION, 24 AST_FUNCTION_CALL, 25 AST_POSTFIX_INC, 26 AST_POSTFIX_DEC, 27 AST_UNARY_PLUS, 28 AST_UNARY_MINUS, 29 AST_LOGICAL_NOT, 30 AST_BITWISE_NOT, 31 AST_ADDRESS_OF, 32 AST_INDIRECTION, 33 AST_SIZEOF, 34 AST_PRE_INC, 35 AST_PRE_DEC, 36 AST_CAST, 37 AST_MUL, 38 AST_DIV, 39 AST_MOD, 40 AST_ADD, 41 AST_SUB, 42 AST_LEFT_SHIFT, 43 AST_RIGHT_SHIFT, 44 AST_LESS_THAN, 45 AST_GREATER_THAN, 46 AST_LESS_THAN_OR_EQUAL, 47 AST_GREATER_THAN_OR_EQUAL, 48 AST_EQUAL, 49 AST_NOT_EQUAL, 50 AST_BITWISE_AND, 51 AST_BITWISE_XOR, 52 AST_BITWISE_OR, 53 AST_LOGICAL_AND, 54 AST_LOGICAL_OR, 55 AST_CONDITIONAL, 56 AST_ASSIGN, 57 AST_ADD_ASSIGN, 58 AST_SUB_ASSIGN, 59 AST_MUL_ASSIGN, 60 AST_DIV_ASSIGN, 61 AST_MOD_ASSIGN, 62 AST_LEFT_SHIFT_ASSIGN, 63 AST_RIGHT_SHIFT_ASSIGN, 64 AST_AND_ASSIGN, 65 AST_XOR_ASSIGN, 66 AST_OR_ASSIGN, 67 AST_COMMA, 68 // Statement nodes 69 AST_EXPRESSION_STATEMENT, 70 AST_LABEL_STATEMENT, 71 AST_CASE_STATEMENT, 72 AST_DEFAULT_STATEMENT, 73 AST_COMPOUND_STATEMENT, 74 AST_IF_STATEMENT, 75 AST_WHILE_STATEMENT, 76 AST_DO_WHILE_STATEMENT, 77 AST_FOR_STATEMENT, 78 AST_GOTO_STATEMENT, 79 AST_CONTINUE_STATEMENT, 80 AST_BREAK_STATEMENT, 81 AST_RETURN_STATEMENT, 82 AST_SWITCH_STATEMENT, 83 AST_NULL_STATEMENT, 84 // Declaration nodes 85 AST_VAR_DECL, 86 AST_FUN_DECL, 87 AST_PARAM, 88 AST_FUN_DEF, 89 AST_STRUCT_DECL, 90 AST_UNION_DECL, 91 AST_ENUM_DECL, 92 AST_TYPEDEF, 93 AST_TYPE, 94 AST_MEMBER, 95 AST_ENUM_MEMBER, 96 // Translation unit 97 AST_TRANSLATION_UNIT 98 } ast_node_type_t; 99 100 /* Node Creation (Expression Nodes) */ 101 // Expression nodes (primary) 102 ast_node_t *ast_create_ints32_node(int32_t value, int line, int column); // 1 103 ast_node_t *ast_create_ints64_node(int64_t value, int line, int column); // 1L 104 ast_node_t *ast_create_intu32_node(uint32_t value, int line, int column); // 1u 105 ast_node_t *ast_create_intu64_node(uint64_t value, int line, int column); // 1ul 106 ast_node_t *ast_create_float_node(float value, int line, int column); // 1.0f 107 ast_node_t *ast_create_double_node(double value, int line, int column); // 1.0 108 ast_node_t *ast_create_string_node(char *value, int line, int column); // "string" 109 ast_node_t *ast_create_char_node(char value, int line, int column); // 'c' 110 ast_node_t *ast_create_var_node(char *value, int line, int column); // Variable 111 // Nothing needed for paren as the child can just be placed in the parent node directly 112 // Expression nodes (postfix) 113 ast_node_t *ast_create_subscript_node(ast_node_t *expr, ast_node_t *subscript, int line, int column); // expr[subscript] 114 ast_node_t *ast_create_direct_component_selection_node(ast_node_t *expr, char *component, int line, int column); // .component 115 ast_node_t *ast_create_indirect_component_selection_node(ast_node_t *expr, char *component, int line, int column); // ->component 116 ast_node_t *ast_create_function_call_node(ast_node_t *expr, ast_node_t **args, size_t num_args, int line, int column); // expr(arg1, arg2, ...) 117 ast_node_t *ast_create_postfix_inc_node(ast_node_t *expr, int line, int column); // expr++ 118 ast_node_t *ast_create_postfix_dec_node(ast_node_t *expr, int line, int column); // expr-- 119 // Expression nodes (unary) 120 ast_node_t *ast_create_unary_plus_node(ast_node_t *expr, int line, int column); // +expr 121 ast_node_t *ast_create_unary_minus_node(ast_node_t *expr, int line, int column); // -expr 122 ast_node_t *ast_create_logical_not_node(ast_node_t *expr, int line, int column); // !expr 123 ast_node_t *ast_create_bitwise_not_node(ast_node_t *expr, int line, int column); // ~expr 124 ast_node_t *ast_create_address_of_node(ast_node_t *expr, int line, int column); // &expr 125 ast_node_t *ast_create_indirection_node(ast_node_t *expr, int line, int column); // *expr 126 ast_node_t *ast_create_sizeof_node(ast_node_t *expr, int line, int column); // sizeof(expr) 127 ast_node_t *ast_create_pre_inc_node(ast_node_t *expr, int line, int column); // ++expr 128 ast_node_t *ast_create_pre_dec_node(ast_node_t *expr, int line, int column); // --expr 129 // Expression nodes (cast) 130 ast_node_t *ast_create_cast_node(ast_node_t *type, ast_node_t *expr, int line, int column); // (type)expr 131 // Expression nodes (multiplicative) 132 ast_node_t *ast_create_mul_node(ast_node_t *left, ast_node_t *right, int line, int column); // left * right 133 ast_node_t *ast_create_div_node(ast_node_t *left, ast_node_t *right, int line, int column); // left / right 134 ast_node_t *ast_create_mod_node(ast_node_t *left, ast_node_t *right, int line, int column); // left % right 135 // Expression nodes (additive) 136 ast_node_t *ast_create_add_node(ast_node_t *left, ast_node_t *right, int line, int column); // left + right 137 ast_node_t *ast_create_sub_node(ast_node_t *left, ast_node_t *right, int line, int column); // left - right 138 // Expression nodes (shift) 139 ast_node_t *ast_create_left_shift_node(ast_node_t *left, ast_node_t *right, int line, int column); // left << right 140 ast_node_t *ast_create_right_shift_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >> right 141 // Expression nodes (relational) 142 ast_node_t *ast_create_less_than_node(ast_node_t *left, ast_node_t *right, int line, int column); // left < right 143 ast_node_t *ast_create_greater_than_node(ast_node_t *left, ast_node_t *right, int line, int column); // left > right 144 ast_node_t *ast_create_less_than_or_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left <= right 145 ast_node_t *ast_create_greater_than_or_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >= right 146 // Expression nodes (equality) 147 ast_node_t *ast_create_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left == right 148 ast_node_t *ast_create_not_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left != right 149 // Expression nodes (bitwise and) 150 ast_node_t *ast_create_bitwise_and_node(ast_node_t *left, ast_node_t *right, int line, int column); // left & right 151 // Expression nodes (bitwise xor) 152 ast_node_t *ast_create_bitwise_xor_node(ast_node_t *left, ast_node_t *right, int line, int column); // left ^ right 153 // Expression nodes (bitwise or) 154 ast_node_t *ast_create_bitwise_or_node(ast_node_t *left, ast_node_t *right, int line, int column); // left | right 155 // Expression nodes (logical and) 156 ast_node_t *ast_create_logical_and_node(ast_node_t *left, ast_node_t *right, int line, int column); // left && right 157 // Expression nodes (logical or) 158 ast_node_t *ast_create_logical_or_node(ast_node_t *left, ast_node_t *right, int line, int column); // left || right 159 // Expression nodes (conditional) 160 ast_node_t *ast_create_conditional_node(ast_node_t *condition, ast_node_t *true_expr, ast_node_t *false_expr, int line, int column); // condition ? true_expr : false_expr 161 // Expression nodes (assignment) 162 ast_node_t *ast_create_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left = right 163 ast_node_t *ast_create_add_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left += right 164 ast_node_t *ast_create_sub_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left -= right 165 ast_node_t *ast_create_mul_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left *= right 166 ast_node_t *ast_create_div_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left /= right 167 ast_node_t *ast_create_mod_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left %= right 168 ast_node_t *ast_create_left_shift_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left <<= right 169 ast_node_t *ast_create_right_shift_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >>= right 170 ast_node_t *ast_create_and_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left &= right 171 ast_node_t *ast_create_xor_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left ^= right 172 ast_node_t *ast_create_or_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left |= right 173 // Expression nodes (comma) 174 ast_node_t *ast_create_comma_node(ast_node_t *left, ast_node_t *right, int line, int column); // left, right 175 176 /* Node Creation (Statement Nodes) */ 177 // Statement nodes (expression) 178 ast_node_t *ast_create_expression_statement_node(ast_node_t *expr); // expr; 179 // Statement nodes (label) 180 ast_node_t *ast_create_label_statement_node(char *label, int line, int column); // label: 181 ast_node_t *ast_create_case_statement_node(ast_node_t *expr, int line, int column); // case expr: 182 ast_node_t *ast_create_default_statement_node(int line, int column); // default: 183 // Statement nodes (compound) 184 ast_node_t *ast_create_compound_statement_node(ast_node_t **decls_and_stmts, size_t num_decls_and_stmts, int line, int column); // { decls_and_stmts } 185 // Statement nodes (conditional) 186 ast_node_t *ast_create_if_statement_node(ast_node_t *condition, ast_node_t *true_stmt, ast_node_t *false_stmt, int line, int column); // if (condition) true_stmt else false_stmt 187 // Statement nodes (iteration) 188 ast_node_t *ast_create_while_statement_node(ast_node_t *condition, ast_node_t *stmt, int line, int column); // while (condition) stmt 189 ast_node_t *ast_create_do_while_statement_node(ast_node_t *stmt, ast_node_t *condition, int line, int column); // do stmt while (condition) 190 ast_node_t *ast_create_for_statement_node(ast_node_t *init, ast_node_t *condition, ast_node_t *update, ast_node_t *stmt, int line, int column); // for (init; condition; update) stmt 191 // Statement nodes (jump) 192 ast_node_t *ast_create_goto_statement_node(char *label, int line, int column); // goto label; 193 ast_node_t *ast_create_continue_statement_node(int line, int column); // continue; 194 ast_node_t *ast_create_break_statement_node(int line, int column); // break; 195 ast_node_t *ast_create_return_statement_node(ast_node_t *expr, int line, int column); // return expr; 196 // Statement nodes (multiway branch) 197 ast_node_t *ast_create_switch_statement_node(ast_node_t *expr, ast_node_t *stmt, int line, int column); // switch (expr) stmt 198 // Statement nodes (null) 199 ast_node_t *ast_create_null_statement_node(int line, int column); // ; 200 201 /* Node Creation (Declaration Nodes) */ 202 // Declaration nodes (simple) 203 ast_node_t *ast_create_var_decl_node(ast_node_t *type, char *id, int line, int column); // type id 204 ast_node_t *ast_create_fun_decl_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, int line, int column); // type id(params) 205 // Declaration nodes (parameter) 206 ast_node_t *ast_create_param_node(ast_node_t *type, char *id, int line, int column); // type id 207 // Declaration nodes (function) 208 ast_node_t *ast_create_fun_def_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, ast_node_t *stmt, int line, int column); // type id(params) stmt 209 // Declaration nodes (struct) 210 ast_node_t *ast_create_struct_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int column); // struct id { members } (reuses param node for members) 211 // Declaration nodes (union) 212 ast_node_t *ast_create_union_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int column); // union id { members } (reuses param node for members) 213 // Declaration nodes (enum) 214 ast_node_t *ast_create_enum_decl_node(char *id, ast_node_t **enums, size_t num_enums, int line, int column); // enum id { enums } 215 // Declaration nodes (Typedef) 216 ast_node_t *ast_create_typedef_node(ast_node_t *type, char *id, int line, int column); // typedef type id 217 // Types/members 218 ast_node_t *ast_create_type_node(token_t *type[], size_t num_type, int line, int column); // type 219 ast_node_t *ast_create_member_node(ast_node_t *type, char *id, int line, int column); // type id 220 ast_node_t *ast_create_enum_member_node(char *id, ast_node_t *value, int line, int column); // id = value 221 222 /* Node Creation (Translation Unit) */ 223 ast_node_t *ast_create_translation_unit(ast_node_t **nodes, size_t num_nodes, int line, int column); 224 225 /* Node Destruction */ 226 void ast_destroy_node(ast_node_t *node); 227 228 /* Node Information (General) */ 229 ast_node_type_t ast_get_node_type(ast_node_t *node); 230 int ast_get_node_line(ast_node_t *node); 231 int ast_get_node_column(ast_node_t *node); 232 233 /* Node Information (Expression Nodes) */ 234 // Expression nodes (primary) 235 int64_t ast_get_int_value(ast_node_t *node); 236 double ast_get_double_value(ast_node_t *node); 237 char *ast_get_string_value(ast_node_t *node); 238 char ast_get_char_value(ast_node_t *node); 239 char *ast_get_var_value(ast_node_t *node); 240 // Expression nodes (postfix) 241 ast_node_t *ast_get_subscript_expr(ast_node_t *node); 242 ast_node_t *ast_get_subscript_idx(ast_node_t *node); 243 char *ast_get_component(ast_node_t *node); 244 ast_node_t *ast_get_function_call_expr(ast_node_t *node); 245 ast_node_t **ast_get_function_call_args(ast_node_t *node, size_t *num_args); 246 ast_node_t *ast_get_postfix_expr(ast_node_t *node); 247 // Expression nodes (unary) 248 ast_node_t *ast_get_unary_expr(ast_node_t *node); 249 // Expression nodes (cast) 250 ast_node_t *ast_get_cast_type(ast_node_t *node); 251 ast_node_t *ast_get_cast_expr(ast_node_t *node); 252 // Expression nodes (binary) 253 ast_node_t *ast_get_left_expr(ast_node_t *node); 254 ast_node_t *ast_get_right_expr(ast_node_t *node); 255 // Expression nodes (conditional) 256 ast_node_t *ast_get_conditional_condition(ast_node_t *node); 257 ast_node_t *ast_get_conditional_true_expr(ast_node_t *node); 258 ast_node_t *ast_get_conditional_false_expr(ast_node_t *node); 259 260 /* Node Information (Statement Nodes) */ 261 // Statement nodes (expression) 262 ast_node_t *ast_get_expression_statement_expr(ast_node_t *node); 263 // Statement nodes (label) 264 char *ast_get_label_statement_label(ast_node_t *node); // label 265 ast_node_t *ast_get_case_statement_expr(ast_node_t *node); // expr 266 // Statement nodes (compound) 267 ast_node_t **ast_get_compound_statement_contents(ast_node_t *node, size_t *num_contents); // decls_and_stmts 268 // Statement nodes (conditional) 269 ast_node_t *ast_get_if_statement_condition(ast_node_t *node); // condition 270 ast_node_t *ast_get_if_statement_true_stmt(ast_node_t *node); // true_stmt 271 ast_node_t *ast_get_if_statement_false_stmt(ast_node_t *node); // false_stmt 272 // Statement nodes (iteration) 273 ast_node_t *ast_get_iteration_statement_condition(ast_node_t *node); // condition 274 ast_node_t *ast_get_iteration_statement_stmt(ast_node_t *node); // stmt 275 ast_node_t *ast_get_for_statement_init(ast_node_t *node); // init 276 ast_node_t *ast_get_for_statement_update(ast_node_t *node); // update 277 // Statement nodes (jump) 278 char *ast_get_goto_statement_label(ast_node_t *node); // label 279 // Statement nodes (multiway branch) 280 ast_node_t *ast_get_switch_statement_expr(ast_node_t *node); // expr 281 ast_node_t *ast_get_switch_statement_stmt(ast_node_t *node); // stmt 282 283 /* Node Information (Declaration Nodes) */ 284 // Declaration nodes (simple) 285 ast_node_t *ast_get_var_decl_type(ast_node_t *node); 286 char *ast_get_var_decl_id(ast_node_t *node); 287 ast_node_t *ast_get_fun_decl_type(ast_node_t *node); 288 char *ast_get_fun_decl_id(ast_node_t *node); 289 ast_node_t **ast_get_fun_decl_params(ast_node_t *node, size_t *num_params); 290 // Declaration nodes (parameter) 291 ast_node_t *ast_get_param_type(ast_node_t *node); 292 char *ast_get_param_id(ast_node_t *node); 293 // Declaration nodes (function) 294 ast_node_t *ast_get_fun_def_type(ast_node_t *node); 295 char *ast_get_fun_def_id(ast_node_t *node); 296 ast_node_t **ast_get_fun_def_params(ast_node_t *node, size_t *num_params); 297 ast_node_t *ast_get_fun_def_stmt(ast_node_t *node); 298 // Declaration nodes (struct) 299 char *ast_get_struct_decl_id(ast_node_t *node); 300 ast_node_t **ast_get_struct_decl_members(ast_node_t *node, size_t *num_members); 301 // Declaration nodes (union) 302 char *ast_get_union_decl_id(ast_node_t *node); 303 ast_node_t **ast_get_union_decl_members(ast_node_t *node, size_t *num_members); 304 // Declaration nodes (enum) 305 char *ast_get_enum_decl_id(ast_node_t *node); 306 ast_node_t **ast_get_enum_decl_enums(ast_node_t *node, size_t *num_enums); 307 // Declaration nodes (Typedef) 308 ast_node_t *ast_get_typedef_type(ast_node_t *node); 309 char *ast_get_typedef_id(ast_node_t *node); 310 // Types/members 311 token_t **ast_get_type(ast_node_t *node, size_t *num_type); 312 ast_node_t *ast_get_member_type(ast_node_t *node); 313 char *ast_get_member_id(ast_node_t *node); 314 ast_node_t *ast_get_enum_member_expr(ast_node_t *node); 315 316 /* Node Information (Translation Unit) */ 317 ast_node_t **ast_get_translation_unit(ast_node_t *node, size_t *num_nodes); 318 319 #endif 320