Changes in src/Parser/ExpressionNode.cc [65cdc1e:a67b60e]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r65cdc1e ra67b60e 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed Aug 2 11:12:00 2017 13 // Update Count : 568 14 // 15 16 #include <climits> // access INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, LLONG_MAX 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 21:08:15 2017 13 // Update Count : 542 14 // 15 16 #include <cassert> 17 #include <cctype> 18 #include <climits> 19 #include <cstdio> 20 #include <algorithm> 17 21 #include <sstream> 18 22 … … 22 26 #include "SynTree/Expression.h" 23 27 #include "SynTree/Declaration.h" 28 #include "Common/UnimplementedError.h" 24 29 #include "parserutility.h" 30 #include "Common/utility.h" 25 31 26 32 using namespace std; … … 40 46 // type. 41 47 42 extern const Type::Qualifiers noQualifiers;// no qualifiers on constants48 Type::Qualifiers emptyQualifiers; // no qualifiers on constants 43 49 44 50 static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } … … 49 55 static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } 50 56 51 Expression * build_constantInteger( const std::string & str ) {57 Expression *build_constantInteger( const std::string & str ) { 52 58 static const BasicType::Kind kind[2][3] = { 53 59 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, … … 56 62 bool dec = true, Unsigned = false; // decimal, unsigned constant 57 63 int size; // 0 => int, 1 => long, 2 => long long 58 unsigned long long int v; // converted integral value64 unsigned long long int v; // converted integral value 59 65 size_t last = str.length() - 1; // last character of constant 60 Expression * ret; 61 62 // special constants 63 if ( str == "0" ) { 64 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) ); 65 goto CLEANUP; 66 } // if 67 if ( str == "1" ) { 68 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) ); 69 goto CLEANUP; 70 } // if 71 66 72 67 if ( str[0] == '0' ) { // octal/hex constant ? 73 68 dec = false; … … 123 118 } // if 124 119 125 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) ); 126 CLEANUP: 120 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str, v ) ); 127 121 delete &str; // created by lex 128 122 return ret; 129 123 } // build_constantInteger 130 124 131 Expression * build_constantFloat( const std::string & str ) {125 Expression *build_constantFloat( const std::string & str ) { 132 126 static const BasicType::Kind kind[2][3] = { 133 127 { BasicType::Float, BasicType::Double, BasicType::LongDouble }, … … 159 153 } // if 160 154 161 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );155 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str, v ) ); 162 156 delete &str; // created by lex 163 157 return ret; 164 158 } // build_constantFloat 165 159 166 Expression * build_constantChar( const std::string & str ) {167 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );160 Expression *build_constantChar( const std::string & str ) { 161 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); 168 162 delete &str; // created by lex 169 163 return ret; 170 164 } // build_constantChar 171 165 172 ConstantExpr * build_constantStr( const std::string & str ) {166 ConstantExpr *build_constantStr( const std::string & str ) { 173 167 // string should probably be a primitive type 174 ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),175 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'168 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ), 169 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"' 176 170 false, false ); 177 ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value 171 // constant 0 is ignored for pure string value 172 ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); 178 173 delete &str; // created by lex 179 174 return ret; 180 175 } // build_constantStr 176 177 Expression *build_constantZeroOne( const std::string & str ) { 178 Expression * ret = new ConstantExpr( Constant( str == "0" ? (Type *)new ZeroType( emptyQualifiers ) : (Type*)new OneType( emptyQualifiers ), str, 179 str == "0" ? (unsigned long long int)0 : (unsigned long long int)1 ) ); 180 delete &str; // created by lex 181 return ret; 182 } // build_constantChar 181 183 182 184 Expression * build_field_name_FLOATINGconstant( const std::string & str ) { … … 207 209 } // build_field_name_fraction_constants 208 210 211 212 209 213 Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) { 210 214 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str ); … … 221 225 } // build_field_name_REALDECIMALconstant 222 226 223 NameExpr * build_varref( const string * name ) {224 NameExpr * expr = new NameExpr( *name, nullptr );227 NameExpr * build_varref( const string *name ) { 228 NameExpr *expr = new NameExpr( *name, nullptr ); 225 229 delete name; 226 230 return expr; 227 } // build_varref 228 229 230 static const char * OperName[] = { // must harmonize with OperKinds 231 } 232 233 static const char *OperName[] = { 231 234 // diadic 232 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "? \\?", "?*?", "?/?", "?%?", "||", "&&",235 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", 233 236 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", 234 "?=?", "?@=?", "? \\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",237 "?=?", "?@=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", 235 238 "?[?]", "...", 236 239 // monadic 237 240 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 238 }; // OperName239 240 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node ) {241 Type * targetType = maybeMoveBuildType( decl_node );241 }; 242 243 Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { 244 Type *targetType = maybeMoveBuildType( decl_node ); 242 245 if ( dynamic_cast< VoidType * >( targetType ) ) { 243 246 delete targetType; … … 246 249 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); 247 250 } // if 248 } // build_cast 249 250 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 251 Type * targetType = maybeMoveBuildType( decl_node ); 252 Expression * castArg = maybeMoveBuild< Expression >( expr_node ); 253 return new VirtualCastExpr( castArg, targetType ); 254 } // build_virtual_cast 255 256 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) { 257 UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 258 return ret; 259 } // build_fieldSel 260 261 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) { 262 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) ); 251 } 252 253 Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) { 254 UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 255 return ret; 256 } 257 258 Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) { 259 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 263 260 deref->location = expr_node->location; 264 261 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); 265 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );266 return ret; 267 } // build_pfieldSel268 269 Expression * build_addressOf( ExpressionNode *expr_node ) {262 UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref ); 263 return ret; 264 } 265 266 Expression *build_addressOf( ExpressionNode *expr_node ) { 270 267 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) ); 271 } // build_addressOf 272 273 Expression * build_sizeOfexpr( ExpressionNode * expr_node ) { 268 } 269 Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { 274 270 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) ); 275 } // build_sizeOfexpr 276 277 Expression * build_sizeOftype( DeclarationNode * decl_node ) { 271 } 272 Expression *build_sizeOftype( DeclarationNode *decl_node ) { 278 273 return new SizeofExpr( maybeMoveBuildType( decl_node ) ); 279 } // build_sizeOftype 280 281 Expression * build_alignOfexpr( ExpressionNode * expr_node ) { 274 } 275 Expression *build_alignOfexpr( ExpressionNode *expr_node ) { 282 276 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) ); 283 } // build_alignOfexpr 284 285 Expression * build_alignOftype( DeclarationNode * decl_node ) { 277 } 278 Expression *build_alignOftype( DeclarationNode *decl_node ) { 286 279 return new AlignofExpr( maybeMoveBuildType( decl_node) ); 287 } // build_alignOftype 288 289 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 280 } 281 Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { 290 282 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 291 283 delete member; 292 284 return ret; 293 } // build_offsetOf294 295 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode *expr_node2, bool kind ) {285 } 286 287 Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) { 296 288 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); 297 } // build_and_or298 299 Expression * build_unary_val( OperKinds op, ExpressionNode *expr_node ) {289 } 290 291 Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { 300 292 std::list< Expression * > args; 301 293 args.push_back( maybeMoveBuild< Expression >(expr_node) ); 302 294 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 303 } // build_unary_val 304 305 Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { 295 } 296 Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { 306 297 std::list< Expression * > args; 307 298 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) ); 308 299 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 309 } // build_unary_ptr 310 311 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 300 } 301 Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 312 302 std::list< Expression * > args; 313 303 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 314 304 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 315 305 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 316 } // build_binary_val 317 318 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 306 } 307 Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 319 308 std::list< Expression * > args; 320 309 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) ); 321 310 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 322 311 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 323 } // build_binary_ptr324 325 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode *expr_node3 ) {312 } 313 314 Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) { 326 315 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); 327 } // build_cond328 329 Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode *expr_node2 ) {316 } 317 318 Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 330 319 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) ); 331 } // build_comma332 333 Expression * build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {320 } 321 322 Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { 334 323 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) ); 335 } // build_attrexpr 336 337 Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) { 324 } 325 Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { 338 326 return new AttrExpr( var, maybeMoveBuildType( decl_node ) ); 339 } // build_attrtype340 341 Expression * build_tuple( ExpressionNode * expr_node ) {327 } 328 329 Expression *build_tuple( ExpressionNode * expr_node ) { 342 330 std::list< Expression * > exprs; 343 331 buildMoveList( expr_node, exprs ); 344 332 return new UntypedTupleExpr( exprs );; 345 } // build_tuple346 347 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {333 } 334 335 Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 348 336 std::list< Expression * > args; 349 337 buildMoveList( expr_node, args ); 350 338 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr ); 351 } // build_func352 353 Expression * build_range( ExpressionNode * low, ExpressionNode *high ) {339 } 340 341 Expression *build_range( ExpressionNode * low, ExpressionNode *high ) { 354 342 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) ); 355 } // build_range356 357 Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode *operand ) {343 } 344 345 Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { 358 346 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) ); 359 } // build_asmexpr360 361 Expression * build_valexpr( StatementNode *s ) {347 } 348 349 Expression *build_valexpr( StatementNode *s ) { 362 350 return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) ); 363 } // build_valexpr 364 365 Expression * build_typevalue( DeclarationNode * decl ) { 351 } 352 Expression *build_typevalue( DeclarationNode *decl ) { 366 353 return new TypeExpr( maybeMoveBuildType( decl ) ); 367 } // build_typevalue368 369 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode *kids ) {354 } 355 356 Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { 370 357 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type 371 358 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type … … 393 380 assert( false ); 394 381 } // if 395 } // build_compoundLiteral382 } 396 383 397 384 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.