type: Make an assert()ion independent of the last entry of an enum.
[cparser] / ast.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #ifndef AST_H
6 #define AST_H
7
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include "entity.h"
11
12 typedef struct expression_base_t                     expression_base_t;
13 typedef struct literal_expression_t                  literal_expression_t;
14 typedef struct string_literal_expression_t           string_literal_expression_t;
15 typedef struct funcname_expression_t                 funcname_expression_t;
16 typedef struct compound_literal_expression_t         compound_literal_expression_t;
17 typedef struct reference_expression_t                reference_expression_t;
18 typedef struct call_argument_t                       call_argument_t;
19 typedef struct call_expression_t                     call_expression_t;
20 typedef struct binary_expression_t                   binary_expression_t;
21 typedef struct unary_expression_t                    unary_expression_t;
22 typedef struct select_expression_t                   select_expression_t;
23 typedef struct array_access_expression_t             array_access_expression_t;
24 typedef struct typeprop_expression_t                 typeprop_expression_t;
25 typedef struct conditional_expression_t              conditional_expression_t;
26 typedef struct statement_expression_t                statement_expression_t;
27 typedef struct designator_t                          designator_t;
28 typedef struct offsetof_expression_t                 offsetof_expression_t;
29 typedef struct va_start_expression_t                 va_start_expression_t;
30 typedef struct va_arg_expression_t                   va_arg_expression_t;
31 typedef struct va_copy_expression_t                  va_copy_expression_t;
32 typedef struct builtin_constant_expression_t         builtin_constant_expression_t;
33 typedef struct builtin_types_compatible_expression_t builtin_types_compatible_expression_t;
34 typedef struct classify_type_expression_t            classify_type_expression_t;
35 typedef struct label_address_expression_t            label_address_expression_t;
36 typedef union  expression_t                          expression_t;
37
38 typedef struct initializer_base_t                    initializer_base_t;
39 typedef struct initializer_list_t                    initializer_list_t;
40 typedef struct initializer_value_t                   initializer_value_t;
41 typedef struct initializer_designator_t              initializer_designator_t;
42 typedef union  initializer_t                         initializer_t;
43
44 typedef struct statement_base_t                      statement_base_t;
45 typedef struct compound_statement_t                  compound_statement_t;
46 typedef struct return_statement_t                    return_statement_t;
47 typedef struct if_statement_t                        if_statement_t;
48 typedef struct switch_statement_t                    switch_statement_t;
49 typedef struct declaration_statement_t               declaration_statement_t;
50 typedef struct expression_statement_t                expression_statement_t;
51 typedef struct computed_goto_statement_t             computed_goto_statement_t;
52 typedef struct goto_statement_t                      goto_statement_t;
53 typedef struct label_statement_t                     label_statement_t;
54 typedef struct case_label_statement_t                case_label_statement_t;
55 typedef struct do_while_statement_t                  do_while_statement_t;
56 typedef struct for_statement_t                       for_statement_t;
57 typedef struct asm_argument_t                        asm_argument_t;
58 typedef struct asm_clobber_t                         asm_clobber_t;
59 typedef struct asm_label_t                           asm_label_t;
60 typedef struct asm_statement_t                       asm_statement_t;
61 typedef struct ms_try_statement_t                    ms_try_statement_t;
62 typedef struct leave_statement_t                     leave_statement_t;
63 typedef union  statement_t                           statement_t;
64
65 typedef struct translation_unit_t                    translation_unit_t;
66
67 /**
68  * Initialize the AST construction.
69  */
70 void init_ast(void);
71
72 /**
73  * Free the AST.
74  */
75 void exit_ast(void);
76
77 void print_expression(const expression_t *expression);
78 void print_initializer(const initializer_t *initializer);
79 void print_ast(const translation_unit_t *unit);
80 void print_indent(void);
81 void print_declaration(const entity_t *entity);
82 void print_entity(const entity_t *entity);
83 void change_indent(int delta);
84
85 typedef enum expression_classification_t {
86         EXPR_CLASS_VARIABLE,
87         EXPR_CLASS_ERROR,
88         EXPR_CLASS_CONSTANT,
89         EXPR_CLASS_INTEGER_CONSTANT
90 } expression_classification_t;
91
92 /**
93  * Returns true when an initializer contains only constants/linker_constant
94  * values.
95  */
96 expression_classification_t is_constant_initializer(const initializer_t *initializer);
97
98 /**
99  * Returns true if a given expression is a compile time
100  * constant.
101  *
102  * @param expression  the expression to check
103  */
104 expression_classification_t is_constant_expression(const expression_t *expression);
105
106 /**
107  * Checks if an expression is a constant/known value to the linker. Examples:
108  *  - all constant/linker constant expression casted to a pointer type
109  *  - "&x", with x being a global variable.
110  *  - "array" or "a.array" in case array is an array and array and a,
111  *  respectively is an object with link time constant address
112  */
113 expression_classification_t is_linker_constant(const expression_t *expression);
114
115 long fold_constant_to_int(const expression_t *expression);
116 bool fold_constant_to_bool(const expression_t *expression);
117 bool constant_is_negative(const expression_t *constant);
118
119 /**
120  * the type of a literal is usually the biggest type that can hold the value.
121  * Since this is backend dependent the parses needs this call exposed.
122  * Works for EXPR_LITERAL_* expressions.
123  */
124 void determine_literal_type(literal_expression_t *literal);
125
126 #endif