Changeset 0720e049 for src/Parser/ExpressionNode.cc
- Timestamp:
- Aug 11, 2017, 10:33:37 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 54cd58b0
- Parents:
- 3d4b23fa (diff), 59a75cb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ExpressionNode.cc
r3d4b23fa r0720e049 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 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> 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 21 17 #include <sstream> 22 18 … … 26 22 #include "SynTree/Expression.h" 27 23 #include "SynTree/Declaration.h" 28 #include "Common/UnimplementedError.h"29 24 #include "parserutility.h" 30 #include "Common/utility.h"31 25 32 26 using namespace std; … … 46 40 // type. 47 41 48 Type::Qualifiers emptyQualifiers;// no qualifiers on constants42 extern const Type::Qualifiers noQualifiers; // no qualifiers on constants 49 43 50 44 static inline bool checkU( char c ) { return c == 'u' || c == 'U'; } … … 55 49 static inline bool checkX( char c ) { return c == 'x' || c == 'X'; } 56 50 57 Expression * build_constantInteger( const std::string & str ) {51 Expression * build_constantInteger( const std::string & str ) { 58 52 static const BasicType::Kind kind[2][3] = { 59 53 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt }, … … 62 56 bool dec = true, Unsigned = false; // decimal, unsigned constant 63 57 int size; // 0 => int, 1 => long, 2 => long long 64 unsigned long long int v; 58 unsigned long long int v; // converted integral value 65 59 size_t last = str.length() - 1; // last character of constant 66 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 67 72 if ( str[0] == '0' ) { // octal/hex constant ? 68 73 dec = false; … … 118 123 } // if 119 124 120 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str, v ) ); 125 ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) ); 126 CLEANUP: 121 127 delete &str; // created by lex 122 128 return ret; 123 129 } // build_constantInteger 124 130 125 Expression * build_constantFloat( const std::string & str ) {131 Expression * build_constantFloat( const std::string & str ) { 126 132 static const BasicType::Kind kind[2][3] = { 127 133 { BasicType::Float, BasicType::Double, BasicType::LongDouble }, … … 153 159 } // if 154 160 155 Expression * ret = new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str, v ) );161 Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) ); 156 162 delete &str; // created by lex 157 163 return ret; 158 164 } // build_constantFloat 159 165 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] ) );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] ) ); 162 168 delete &str; // created by lex 163 169 return ret; 164 170 } // build_constantChar 165 171 166 ConstantExpr * build_constantStr( const std::string & str ) {172 ConstantExpr * build_constantStr( const std::string & str ) { 167 173 // string should probably be a primitive type 168 ArrayType * at = new ArrayType( emptyQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),169 new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), 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 '"' 170 176 false, false ); 171 // constant 0 is ignored for pure string value 172 ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); 177 ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value 173 178 delete &str; // created by lex 174 179 return ret; 175 180 } // 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 lex181 return ret;182 } // build_constantChar183 181 184 182 Expression * build_field_name_FLOATINGconstant( const std::string & str ) { … … 209 207 } // build_field_name_fraction_constants 210 208 211 212 213 209 Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) { 214 210 if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str ); … … 225 221 } // build_field_name_REALDECIMALconstant 226 222 227 NameExpr * build_varref( const string * name ) {228 NameExpr * expr = new NameExpr( *name, nullptr );223 NameExpr * build_varref( const string * name ) { 224 NameExpr * expr = new NameExpr( *name, nullptr ); 229 225 delete name; 230 226 return expr; 231 } 232 233 static const char *OperName[] = { 227 } // build_varref 228 229 230 static const char * OperName[] = { // must harmonize with OperKinds 234 231 // diadic 235 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "? *?", "?/?", "?%?", "||", "&&",232 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?\\?", "?*?", "?/?", "?%?", "||", "&&", 236 233 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", 237 "?=?", "?@=?", "? *=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",234 "?=?", "?@=?", "?\\=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", 238 235 "?[?]", "...", 239 236 // monadic 240 237 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 241 }; 242 243 Expression * build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) {244 Type * targetType = maybeMoveBuildType( decl_node );238 }; // OperName 239 240 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) { 241 Type * targetType = maybeMoveBuildType( decl_node ); 245 242 if ( dynamic_cast< VoidType * >( targetType ) ) { 246 243 delete targetType; … … 249 246 return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType ); 250 247 } // if 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( "*?" ) ); 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( "*?" ) ); 260 263 deref->location = expr_node->location; 261 264 deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) ); 262 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );263 return ret; 264 } 265 266 Expression * build_addressOf( ExpressionNode *expr_node ) {265 UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref ); 266 return ret; 267 } // build_pfieldSel 268 269 Expression * build_addressOf( ExpressionNode * expr_node ) { 267 270 return new AddressExpr( maybeMoveBuild< Expression >(expr_node) ); 268 } 269 Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { 271 } // build_addressOf 272 273 Expression * build_sizeOfexpr( ExpressionNode * expr_node ) { 270 274 return new SizeofExpr( maybeMoveBuild< Expression >(expr_node) ); 271 } 272 Expression *build_sizeOftype( DeclarationNode *decl_node ) { 275 } // build_sizeOfexpr 276 277 Expression * build_sizeOftype( DeclarationNode * decl_node ) { 273 278 return new SizeofExpr( maybeMoveBuildType( decl_node ) ); 274 } 275 Expression *build_alignOfexpr( ExpressionNode *expr_node ) { 279 } // build_sizeOftype 280 281 Expression * build_alignOfexpr( ExpressionNode * expr_node ) { 276 282 return new AlignofExpr( maybeMoveBuild< Expression >(expr_node) ); 277 } 278 Expression *build_alignOftype( DeclarationNode *decl_node ) { 283 } // build_alignOfexpr 284 285 Expression * build_alignOftype( DeclarationNode * decl_node ) { 279 286 return new AlignofExpr( maybeMoveBuildType( decl_node) ); 280 } 281 Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { 287 } // build_alignOftype 288 289 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) { 282 290 Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() ); 283 291 delete member; 284 292 return ret; 285 } 286 287 Expression * build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ) {293 } // build_offsetOf 294 295 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) { 288 296 return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind ); 289 } 290 291 Expression * build_unary_val( OperKinds op, ExpressionNode *expr_node ) {297 } // build_and_or 298 299 Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) { 292 300 std::list< Expression * > args; 293 301 args.push_back( maybeMoveBuild< Expression >(expr_node) ); 294 302 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 295 } 296 Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { 303 } // build_unary_val 304 305 Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) { 297 306 std::list< Expression * > args; 298 307 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) ); 299 308 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 300 } 301 Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 309 } // build_unary_ptr 310 311 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 302 312 std::list< Expression * > args; 303 313 args.push_back( maybeMoveBuild< Expression >(expr_node1) ); 304 314 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 305 315 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 306 } 307 Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 316 } // build_binary_val 317 318 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 308 319 std::list< Expression * > args; 309 320 args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) ); 310 321 args.push_back( maybeMoveBuild< Expression >(expr_node2) ); 311 322 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 312 } 313 314 Expression * build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ) {323 } // build_binary_ptr 324 325 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) { 315 326 return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) ); 316 } 317 318 Expression * build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {327 } // build_cond 328 329 Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) { 319 330 return new CommaExpr( maybeMoveBuild< Expression >(expr_node1), maybeMoveBuild< Expression >(expr_node2) ); 320 } 321 322 Expression * build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) {331 } // build_comma 332 333 Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node ) { 323 334 return new AttrExpr( var, maybeMoveBuild< Expression >(expr_node) ); 324 } 325 Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { 335 } // build_attrexpr 336 337 Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node ) { 326 338 return new AttrExpr( var, maybeMoveBuildType( decl_node ) ); 327 } 328 329 Expression * build_tuple( ExpressionNode * expr_node ) {339 } // build_attrtype 340 341 Expression * build_tuple( ExpressionNode * expr_node ) { 330 342 std::list< Expression * > exprs; 331 343 buildMoveList( expr_node, exprs ); 332 344 return new UntypedTupleExpr( exprs );; 333 } 334 335 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {345 } // build_tuple 346 347 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 336 348 std::list< Expression * > args; 337 349 buildMoveList( expr_node, args ); 338 350 return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr ); 339 } 340 341 Expression * build_range( ExpressionNode * low, ExpressionNode *high ) {351 } // build_func 352 353 Expression * build_range( ExpressionNode * low, ExpressionNode * high ) { 342 354 return new RangeExpr( maybeMoveBuild< Expression >( low ), maybeMoveBuild< Expression >( high ) ); 343 } 344 345 Expression * build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) {355 } // build_range 356 357 Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) { 346 358 return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) ); 347 } 348 349 Expression * build_valexpr( StatementNode *s ) {359 } // build_asmexpr 360 361 Expression * build_valexpr( StatementNode * s ) { 350 362 return new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(s) ) ); 351 } 352 Expression *build_typevalue( DeclarationNode *decl ) { 363 } // build_valexpr 364 365 Expression * build_typevalue( DeclarationNode * decl ) { 353 366 return new TypeExpr( maybeMoveBuildType( decl ) ); 354 } 355 356 Expression * build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) {367 } // build_typevalue 368 369 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) { 357 370 Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type 358 371 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type … … 380 393 assert( false ); 381 394 } // if 382 } 395 } // build_compoundLiteral 383 396 384 397 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.