1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef AST_H
21 #define AST_H
22 
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include "entity.h"
26 
27 typedef struct expression_base_t                     expression_base_t;
28 typedef struct literal_expression_t                  literal_expression_t;
29 typedef struct string_literal_expression_t           string_literal_expression_t;
30 typedef struct funcname_expression_t                 funcname_expression_t;
31 typedef struct compound_literal_expression_t         compound_literal_expression_t;
32 typedef struct reference_expression_t                reference_expression_t;
33 typedef struct call_argument_t                       call_argument_t;
34 typedef struct call_expression_t                     call_expression_t;
35 typedef struct binary_expression_t                   binary_expression_t;
36 typedef struct unary_expression_t                    unary_expression_t;
37 typedef struct select_expression_t                   select_expression_t;
38 typedef struct array_access_expression_t             array_access_expression_t;
39 typedef struct typeprop_expression_t                 typeprop_expression_t;
40 typedef struct conditional_expression_t              conditional_expression_t;
41 typedef struct statement_expression_t                statement_expression_t;
42 typedef struct designator_t                          designator_t;
43 typedef struct offsetof_expression_t                 offsetof_expression_t;
44 typedef struct va_start_expression_t                 va_start_expression_t;
45 typedef struct va_arg_expression_t                   va_arg_expression_t;
46 typedef struct va_copy_expression_t                  va_copy_expression_t;
47 typedef struct builtin_constant_expression_t         builtin_constant_expression_t;
48 typedef struct builtin_types_compatible_expression_t builtin_types_compatible_expression_t;
49 typedef struct classify_type_expression_t            classify_type_expression_t;
50 typedef struct label_address_expression_t            label_address_expression_t;
51 typedef union  expression_t                          expression_t;
52 
53 typedef struct initializer_base_t                    initializer_base_t;
54 typedef struct initializer_list_t                    initializer_list_t;
55 typedef struct initializer_value_t                   initializer_value_t;
56 typedef struct initializer_designator_t              initializer_designator_t;
57 typedef union  initializer_t                         initializer_t;
58 
59 typedef struct statement_base_t                      statement_base_t;
60 typedef struct compound_statement_t                  compound_statement_t;
61 typedef struct return_statement_t                    return_statement_t;
62 typedef struct if_statement_t                        if_statement_t;
63 typedef struct switch_statement_t                    switch_statement_t;
64 typedef struct declaration_statement_t               declaration_statement_t;
65 typedef struct expression_statement_t                expression_statement_t;
66 typedef struct computed_goto_statement_t             computed_goto_statement_t;
67 typedef struct goto_statement_t                      goto_statement_t;
68 typedef struct label_statement_t                     label_statement_t;
69 typedef struct case_label_statement_t                case_label_statement_t;
70 typedef struct do_while_statement_t                  do_while_statement_t;
71 typedef struct for_statement_t                       for_statement_t;
72 typedef struct asm_argument_t                        asm_argument_t;
73 typedef struct asm_clobber_t                         asm_clobber_t;
74 typedef struct asm_label_t                           asm_label_t;
75 typedef struct asm_statement_t                       asm_statement_t;
76 typedef struct ms_try_statement_t                    ms_try_statement_t;
77 typedef struct leave_statement_t                     leave_statement_t;
78 typedef union  statement_t                           statement_t;
79 
80 typedef struct translation_unit_t                    translation_unit_t;
81 
82 /**
83  * Initialize the AST construction.
84  */
85 void init_ast(void);
86 
87 /**
88  * Free the AST.
89  */
90 void exit_ast(void);
91 
92 void print_expression(const expression_t *expression);
93 void print_initializer(const initializer_t *initializer);
94 void print_ast(const translation_unit_t *unit);
95 void print_indent(void);
96 void print_declaration(const entity_t *entity);
97 void print_entity(const entity_t *entity);
98 void change_indent(int delta);
99 
100 typedef enum expression_classification_t {
101 	EXPR_CLASS_VARIABLE,
102 	EXPR_CLASS_ERROR,
103 	EXPR_CLASS_CONSTANT
104 } expression_classification_t;
105 
106 /**
107  * Returns true when an initializer contains only constants/linker_constant
108  * values.
109  */
110 expression_classification_t is_constant_initializer(const initializer_t *initializer);
111 
112 /**
113  * Returns true if a given expression is a compile time
114  * constant.
115  *
116  * @param expression  the expression to check
117  */
118 expression_classification_t is_constant_expression(const expression_t *expression);
119 
120 /**
121  * Checks if an expression is a constant/known value to the linker. Examples:
122  *  - all constant/linker constant expression casted to a pointer type
123  *  - "&x", with x being a global variable.
124  *  - "array" or "a.array" in case array is an array and array and a,
125  *  respectively is an object with link time constant address
126  */
127 expression_classification_t is_linker_constant(const expression_t *expression);
128 
129 long fold_constant_to_int(const expression_t *expression);
130 bool fold_constant_to_bool(const expression_t *expression);
131 bool constant_is_negative(const expression_t *constant);
132 
133 /**
134  * the type of a literal is usually the biggest type that can hold the value.
135  * Since this is backend dependent the parses needs this call exposed.
136  * Works for EXPR_LITERAL_* expressions.
137  */
138 void determine_literal_type(literal_expression_t *literal);
139 
140 #endif
141