1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ExpressionNode.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 13:17:07 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Aug 25 21:39:40 2016 |
---|
13 | // Update Count : 503 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | #include <cctype> |
---|
18 | #include <climits> |
---|
19 | #include <cstdio> |
---|
20 | #include <algorithm> |
---|
21 | #include <sstream> |
---|
22 | |
---|
23 | #include "ParseNode.h" |
---|
24 | #include "TypeData.h" |
---|
25 | #include "SynTree/Constant.h" |
---|
26 | #include "SynTree/Expression.h" |
---|
27 | #include "SynTree/Declaration.h" |
---|
28 | #include "Common/UnimplementedError.h" |
---|
29 | #include "parseutility.h" |
---|
30 | #include "Common/utility.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.get_name() ), extension( other.extension ) {} |
---|
35 | |
---|
36 | //############################################################################## |
---|
37 | |
---|
38 | // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns: |
---|
39 | // |
---|
40 | // prefix action constant action suffix |
---|
41 | // |
---|
42 | // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty: |
---|
43 | // |
---|
44 | // constant BEGIN CONT ... |
---|
45 | // <CONT>(...)? BEGIN 0 ... // possible empty suffix |
---|
46 | // |
---|
47 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their |
---|
48 | // type. |
---|
49 | |
---|
50 | static Type::Qualifiers emptyQualifiers; // no qualifiers on constants |
---|
51 | |
---|
52 | static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } |
---|
53 | static inline bool checkL( char c ) { return c == 'l' || c == 'L'; } |
---|
54 | static inline bool checkF( char c ) { return c == 'f' || c == 'F'; } |
---|
55 | static inline bool checkD( char c ) { return c == 'd' || c == 'D'; } |
---|
56 | static inline bool checkI( char c ) { return c == 'i' || c == 'I'; } |
---|
57 | static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } |
---|
58 | |
---|
59 | Expression *build_constantInteger( const std::string & str ) { |
---|
60 | static const BasicType::Kind kind[2][3] = { |
---|
61 | { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, |
---|
62 | { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt }, |
---|
63 | }; |
---|
64 | bool dec = true, Unsigned = false; // decimal, unsigned constant |
---|
65 | int size; // 0 => int, 1 => long, 2 => long long |
---|
66 | unsigned long long v; // converted integral value |
---|
67 | size_t last = str.length() - 1; // last character of constant |
---|
68 | |
---|
69 | if ( str[0] == '0' ) { // octal/hex constant ? |
---|
70 | dec = false; |
---|
71 | if ( last != 0 && checkX( str[1] ) ) { // hex constant ? |
---|
72 | sscanf( (char *)str.c_str(), "%llx", &v ); |
---|
73 | //printf( "%llx %llu\n", v, v ); |
---|
74 | } else { // octal constant |
---|
75 | sscanf( (char *)str.c_str(), "%llo", &v ); |
---|
76 | //printf( "%llo %llu\n", v, v ); |
---|
77 | } // if |
---|
78 | } else { // decimal constant ? |
---|
79 | sscanf( (char *)str.c_str(), "%llu", &v ); |
---|
80 | //printf( "%llu %llu\n", v, v ); |
---|
81 | } // if |
---|
82 | |
---|
83 | if ( v <= INT_MAX ) { // signed int |
---|
84 | size = 0; |
---|
85 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int |
---|
86 | size = 0; |
---|
87 | Unsigned = true; // unsigned |
---|
88 | } else if ( v <= LONG_MAX ) { // signed long int |
---|
89 | size = 1; |
---|
90 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int |
---|
91 | size = 1; |
---|
92 | Unsigned = true; // unsigned long int |
---|
93 | } else if ( v <= LLONG_MAX ) { // signed long long int |
---|
94 | size = 2; |
---|
95 | } else { // unsigned long long int |
---|
96 | size = 2; |
---|
97 | Unsigned = true; // unsigned long long int |
---|
98 | } // if |
---|
99 | |
---|
100 | if ( checkU( str[last] ) ) { // suffix 'u' ? |
---|
101 | Unsigned = true; |
---|
102 | if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'l' ? |
---|
103 | size = 1; |
---|
104 | if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ? |
---|
105 | size = 2; |
---|
106 | } // if |
---|
107 | } // if |
---|
108 | } else if ( checkL( str[ last ] ) ) { // suffix 'l' ? |
---|
109 | size = 1; |
---|
110 | if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ? |
---|
111 | size = 2; |
---|
112 | if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ? |
---|
113 | Unsigned = true; |
---|
114 | } // if |
---|
115 | } else { |
---|
116 | if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ? |
---|
117 | Unsigned = true; |
---|
118 | } // if |
---|
119 | } // if |
---|
120 | } // if |
---|
121 | |
---|
122 | Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) ); |
---|
123 | delete &str; // created by lex |
---|
124 | return ret; |
---|
125 | } // build_constantInteger |
---|
126 | |
---|
127 | Expression *build_constantFloat( const std::string & str ) { |
---|
128 | static const BasicType::Kind kind[2][3] = { |
---|
129 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
130 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
131 | }; |
---|
132 | |
---|
133 | bool complx = false; // real, complex |
---|
134 | int size = 1; // 0 => float, 1 => double (default), 2 => long double |
---|
135 | // floating-point constant has minimum of 2 characters: 1. or .1 |
---|
136 | size_t last = str.length() - 1; |
---|
137 | |
---|
138 | if ( checkI( str[last] ) ) { // imaginary ? |
---|
139 | complx = true; |
---|
140 | last -= 1; // backup one character |
---|
141 | } // if |
---|
142 | |
---|
143 | if ( checkF( str[last] ) ) { // float ? |
---|
144 | size = 0; |
---|
145 | } else if ( checkD( str[last] ) ) { // double ? |
---|
146 | size = 1; |
---|
147 | } else if ( checkL( str[last] ) ) { // long double ? |
---|
148 | size = 2; |
---|
149 | } // if |
---|
150 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? |
---|
151 | complx = true; |
---|
152 | } // if |
---|
153 | |
---|
154 | Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) ); |
---|
155 | delete &str; // created by lex |
---|
156 | return ret; |
---|
157 | } // build_constantFloat |
---|
158 | |
---|
159 | Expression *build_constantChar( const std::string & str ) { |
---|
160 | Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) ); |
---|
161 | delete &str; // created by lex |
---|
162 | return ret; |
---|
163 | } // build_constantChar |
---|
164 | |
---|
165 | ConstantExpr *build_constantStr( const std::string & str ) { |
---|
166 | // string should probably be a primitive type |
---|
167 | ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ), |
---|
168 | new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ), |
---|
169 | toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' |
---|
170 | false, false ); |
---|
171 | ConstantExpr * ret = new ConstantExpr( Constant( at, str ) ); |
---|
172 | delete &str; // created by lex |
---|
173 | return ret; |
---|
174 | } // build_constantStr |
---|
175 | |
---|
176 | NameExpr * build_varref( const string *name, bool labelp ) { |
---|
177 | NameExpr *expr = new NameExpr( *name, nullptr ); |
---|
178 | delete name; |
---|
179 | return expr; |
---|
180 | } |
---|
181 | |
---|
182 | static const char *OperName[] = { |
---|
183 | // diadic |
---|
184 | "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", |
---|
185 | "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", |
---|
186 | "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", |
---|
187 | "?[?]", "...", |
---|
188 | // monadic |
---|
189 | "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" |
---|
190 | }; |
---|
191 | |
---|
192 | Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { |
---|
193 | Type *targetType = maybeMoveBuildType( decl_node ); |
---|
194 | if ( dynamic_cast< VoidType * >( targetType ) ) { |
---|
195 | delete targetType; |
---|
196 | return new CastExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
197 | } else { |
---|
198 | return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); |
---|
199 | } // if |
---|
200 | } |
---|
201 | |
---|
202 | Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) { |
---|
203 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeMoveBuild< Expression >(expr_node) ); |
---|
204 | delete member; |
---|
205 | return ret; |
---|
206 | } |
---|
207 | |
---|
208 | Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) { |
---|
209 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
210 | deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
211 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); |
---|
212 | delete member; |
---|
213 | return ret; |
---|
214 | } |
---|
215 | |
---|
216 | Expression *build_addressOf( ExpressionNode *expr_node ) { |
---|
217 | return new AddressExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
218 | } |
---|
219 | Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { |
---|
220 | return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
221 | } |
---|
222 | Expression *build_sizeOftype( DeclarationNode *decl_node ) { |
---|
223 | return new SizeofExpr( maybeMoveBuildType( decl_node ) ); |
---|
224 | } |
---|
225 | Expression *build_alignOfexpr( ExpressionNode *expr_node ) { |
---|
226 | return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) ); |
---|
227 | } |
---|
228 | Expression *build_alignOftype( DeclarationNode *decl_node ) { |
---|
229 | return new AlignofExpr( maybeMoveBuildType( decl_node) ); |
---|
230 | } |
---|
231 | Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { |
---|
232 | Expression* ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); |
---|
233 | delete member; |
---|
234 | return ret; |
---|
235 | } |
---|
236 | |
---|
237 | Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) { |
---|
238 | return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); |
---|
239 | } |
---|
240 | |
---|
241 | Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { |
---|
242 | std::list< Expression * > args; |
---|
243 | args.push_back( maybeMoveBuild< Expression >(expr_node) ); |
---|
244 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
245 | } |
---|
246 | Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { |
---|
247 | std::list< Expression * > args; |
---|
248 | args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) ); |
---|
249 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
250 | } |
---|
251 | Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
252 | std::list< Expression * > args; |
---|
253 | args.push_back( maybeMoveBuild< Expression >(expr_node1) ); |
---|
254 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
255 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
256 | } |
---|
257 | Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
258 | std::list< Expression * > args; |
---|
259 | args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) ); |
---|
260 | args.push_back( maybeMoveBuild< Expression >(expr_node2) ); |
---|
261 | return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); |
---|
262 | } |
---|
263 | |
---|
264 | Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) { |
---|
265 | return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); |
---|
266 | } |
---|
267 | |
---|
268 | Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { |
---|
269 | return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) ); |
---|
270 | } |
---|
271 | |
---|
272 | Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { |
---|
273 | return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) ); |
---|
274 | } |
---|
275 | Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { |
---|
276 | return new AttrExpr( var, maybeMoveBuildType( decl_node ) ); |
---|
277 | } |
---|
278 | |
---|
279 | Expression *build_tuple( ExpressionNode * expr_node ) { |
---|
280 | TupleExpr *ret = new TupleExpr(); |
---|
281 | buildMoveList( expr_node, ret->get_exprs() ); |
---|
282 | return ret; |
---|
283 | } |
---|
284 | |
---|
285 | Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) { |
---|
286 | std::list< Expression * > args; |
---|
287 | buildMoveList( expr_node, args ); |
---|
288 | return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr ); |
---|
289 | } |
---|
290 | |
---|
291 | Expression *build_range( ExpressionNode * low, ExpressionNode *high ) { |
---|
292 | return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) ); |
---|
293 | } |
---|
294 | |
---|
295 | Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { |
---|
296 | return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) ); |
---|
297 | } |
---|
298 | |
---|
299 | Expression *build_valexpr( StatementNode *s ) { |
---|
300 | return new UntypedValofExpr( maybeMoveBuild< Statement >(s), nullptr ); |
---|
301 | } |
---|
302 | Expression *build_typevalue( DeclarationNode *decl ) { |
---|
303 | return new TypeExpr( maybeMoveBuildType( decl ) ); |
---|
304 | } |
---|
305 | |
---|
306 | Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { |
---|
307 | Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type |
---|
308 | if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type |
---|
309 | return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) ); |
---|
310 | // these types do not have associated type information |
---|
311 | } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl ) ) { |
---|
312 | return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
313 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl ) ) { |
---|
314 | return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
315 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl ) ) { |
---|
316 | return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) ); |
---|
317 | } else { |
---|
318 | assert( false ); |
---|
319 | } // if |
---|
320 | } |
---|
321 | |
---|
322 | // Local Variables: // |
---|
323 | // tab-width: 4 // |
---|
324 | // mode: c++ // |
---|
325 | // compile-command: "make install" // |
---|
326 | // End: // |
---|