Changes in src/Parser/ExpressionNode.cc [65cdc1e:a5f0529]
- File:
-
- 1 edited
-
src/Parser/ExpressionNode.cc (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r65cdc1e ra5f0529 10 10 // Created On : Sat May 16 13:17:07 2015 11 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 12 // Last Modified On : Tus Jul 25 10:11:00 2017 13 // Update Count : 551 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 noQualifiers; // 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( noQualifiers, 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 }, … … 164 158 } // build_constantFloat 165 159 166 Expression * build_constantChar( const std::string & str ) {160 Expression *build_constantChar( const std::string & str ) { 167 161 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) ); 168 162 delete &str; // created by lex … … 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( noQualifiers, 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( noQualifiers ) : (Type*)new OneType( noQualifiers ), 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_varref228 229 230 static const char * OperName[] = { // must harmonize with OperKinds231 } 232 233 // Must harmonize with OperKinds. 234 static const char *OperName[] = { 231 235 // diadic 232 236 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", … … 236 240 // monadic 237 241 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 238 }; // OperName239 240 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node ) {241 Type * targetType = maybeMoveBuildType( decl_node );242 }; 243 244 Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { 245 Type *targetType = maybeMoveBuildType( decl_node ); 242 246 if ( dynamic_cast< VoidType * >( targetType ) ) { 243 247 delete targetType; … … 246 250 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); 247 251 } // 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 ); 252 } 253 254 255 Expression *build_virtual_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { 256 Type *targetType = maybeMoveBuildType( decl_node ); 257 Expression *castArg = maybeMoveBuild< Expression >( expr_node ); 253 258 return new VirtualCastExpr( castArg, targetType ); 254 } // build_virtual_cast255 256 Expression * build_fieldSel( ExpressionNode * expr_node, Expression *member ) {257 UntypedMemberExpr * ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );258 return ret; 259 } // build_fieldSel260 261 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression *member ) {262 UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );259 } 260 261 Expression *build_fieldSel( ExpressionNode *expr_node, Expression *member ) { 262 UntypedMemberExpr *ret = new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) ); 263 return ret; 264 } 265 266 Expression *build_pfieldSel( ExpressionNode *expr_node, Expression *member ) { 267 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 263 268 deref->location = expr_node->location; 264 269 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 ) {270 UntypedMemberExpr *ret = new UntypedMemberExpr( member, deref ); 271 return ret; 272 } 273 274 Expression *build_addressOf( ExpressionNode *expr_node ) { 270 275 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) ); 271 } // build_addressOf 272 273 Expression * build_sizeOfexpr( ExpressionNode * expr_node ) { 276 } 277 Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { 274 278 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) ); 275 } // build_sizeOfexpr 276 277 Expression * build_sizeOftype( DeclarationNode * decl_node ) { 279 } 280 Expression *build_sizeOftype( DeclarationNode *decl_node ) { 278 281 return new SizeofExpr( maybeMoveBuildType( decl_node ) ); 279 } // build_sizeOftype 280 281 Expression * build_alignOfexpr( ExpressionNode * expr_node ) { 282 } 283 Expression *build_alignOfexpr( ExpressionNode *expr_node ) { 282 284 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) ); 283 } // build_alignOfexpr 284 285 Expression * build_alignOftype( DeclarationNode * decl_node ) { 285 } 286 Expression *build_alignOftype( DeclarationNode *decl_node ) { 286 287 return new AlignofExpr( maybeMoveBuildType( decl_node) ); 287 } // build_alignOftype 288 289 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 288 } 289 Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { 290 290 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 291 291 delete member; 292 292 return ret; 293 } // build_offsetOf294 295 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode *expr_node2, bool kind ) {293 } 294 295 Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) { 296 296 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 ) {297 } 298 299 Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { 300 300 std::list< Expression * > args; 301 301 args.push_back( maybeMoveBuild< Expression >(expr_node) ); 302 302 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 303 } // build_unary_val 304 305 Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { 303 } 304 Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { 306 305 std::list< Expression * > args; 307 306 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) ); 308 307 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 ) { 308 } 309 Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 312 310 std::list< Expression * > args; 313 311 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 314 312 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 315 313 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 ) { 314 } 315 Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 319 316 std::list< Expression * > args; 320 317 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) ); 321 318 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 322 319 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 ) {320 } 321 322 Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) { 326 323 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 ) {324 } 325 326 Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 330 327 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) ); 331 } // build_comma332 333 Expression * build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {328 } 329 330 Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { 334 331 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) ); 335 } // build_attrexpr 336 337 Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) { 332 } 333 Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { 338 334 return new AttrExpr( var, maybeMoveBuildType( decl_node ) ); 339 } // build_attrtype340 341 Expression * build_tuple( ExpressionNode * expr_node ) {335 } 336 337 Expression *build_tuple( ExpressionNode * expr_node ) { 342 338 std::list< Expression * > exprs; 343 339 buildMoveList( expr_node, exprs ); 344 340 return new UntypedTupleExpr( exprs );; 345 } // build_tuple346 347 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {341 } 342 343 Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 348 344 std::list< Expression * > args; 349 345 buildMoveList( expr_node, args ); 350 346 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr ); 351 } // build_func352 353 Expression * build_range( ExpressionNode * low, ExpressionNode *high ) {347 } 348 349 Expression *build_range( ExpressionNode * low, ExpressionNode *high ) { 354 350 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) ); 355 } // build_range356 357 Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode *operand ) {351 } 352 353 Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { 358 354 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) ); 359 } // build_asmexpr360 361 Expression * build_valexpr( StatementNode *s ) {355 } 356 357 Expression *build_valexpr( StatementNode *s ) { 362 358 return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) ); 363 } // build_valexpr 364 365 Expression * build_typevalue( DeclarationNode * decl ) { 359 } 360 Expression *build_typevalue( DeclarationNode *decl ) { 366 361 return new TypeExpr( maybeMoveBuildType( decl ) ); 367 } // build_typevalue368 369 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode *kids ) {362 } 363 364 Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { 370 365 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type 371 366 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type … … 393 388 assert( false ); 394 389 } // if 395 } // build_compoundLiteral390 } 396 391 397 392 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.