Changeset 3078643 for src/Parser/ExpressionNode.cc
- Timestamp:
- Aug 10, 2016, 2:29:44 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- be0a9d8, ef42e764
- Parents:
- f18a711 (diff), a563f01 (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
rf18a711 r3078643 10 10 // Created On : Sat May 16 13:17:07 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Aug 5 07:56:23201613 // Update Count : 37512 // Last Modified On : Wed Aug 10 11:07:38 2016 13 // Update Count : 486 14 14 // 15 15 16 16 #include <cassert> 17 17 #include <cctype> 18 #include <climits> 19 #include <cstdio> 18 20 #include <algorithm> 19 21 #include <sstream> 20 #include <cstdio>21 22 22 23 #include "ParseNode.h" … … 31 32 using namespace std; 32 33 33 ExpressionNode::ExpressionNode() : ParseNode() {} 34 35 ExpressionNode::ExpressionNode( const string *name ) : ParseNode( name ) {} 36 37 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ), extension( other.extension ) { 38 if ( other.argName ) { 39 argName = other.argName->clone(); 40 } else { 41 argName = 0; 42 } // if 43 } 44 45 ExpressionNode * ExpressionNode::set_argName( const std::string *aName ) { 46 argName = new VarRefNode( aName ); 47 return this; 48 } 49 50 ExpressionNode * ExpressionNode::set_argName( ExpressionNode *aDesignator ) { 51 argName = aDesignator; 52 return this; 53 } 54 55 void ExpressionNode::printDesignation( std::ostream &os, int indent ) const { 56 if ( argName ) { 57 os << string( indent, ' ' ) << "(designated by: "; 58 argName->printOneLine( os, indent ); 59 os << ")" << std::endl; 60 } // if 61 } 62 63 //############################################################################## 64 65 NullExprNode::NullExprNode() {} 66 67 NullExprNode *NullExprNode::clone() const { 68 return new NullExprNode(); 69 } 70 71 void NullExprNode::print( std::ostream & os, int indent ) const { 72 printDesignation( os ); 73 os << "null expression"; 74 } 75 76 void NullExprNode::printOneLine( std::ostream & os, int indent ) const { 77 printDesignation( os ); 78 os << "null"; 79 } 80 81 Expression *NullExprNode::build() const { 82 return 0; 83 } 84 85 // CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ) { 86 // return new CommaExprNode( this, exp ); 87 // } 88 89 //############################################################################## 90 91 ConstantNode::ConstantNode( ConstantExpr *expr ) : expr( expr ) { 92 } // ConstantNode::ConstantNode 93 94 ConstantNode *ConstantNode::appendstr( const std::string *newValue ) { 95 assert( newValue != 0 ); 96 97 string value = expr->get_constant()->get_value(); 98 99 // "abc" "def" "ghi" => "abcdefghi", remove new text from quotes and insert before last quote in old string. 100 value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) ); 101 expr->get_constant()->set_value( value ); 102 103 delete newValue; // allocated by lexer 104 return this; 105 } 106 107 void ConstantNode::printOneLine( std::ostream &os, int indent ) const { 108 // os << string( indent, ' ' ); 109 // printDesignation( os ); 110 111 // switch ( type ) { 112 // case Integer: 113 // case Float: 114 // os << value ; 115 // break; 116 // case Character: 117 // os << "'" << value << "'"; 118 // break; 119 // case String: 120 // os << '"' << value << '"'; 121 // break; 122 // } // switch 123 124 // os << ' '; 125 } 126 127 void ConstantNode::print( std::ostream &os, int indent ) const { 128 printOneLine( os, indent ); 129 os << endl; 130 } 131 132 Expression *ConstantNode::build() const { 133 return expr->clone(); 134 } 135 136 //############################################################################## 137 138 VarRefNode::VarRefNode() : isLabel( false ) {} 139 140 VarRefNode::VarRefNode( const string *name_, bool labelp ) : ExpressionNode( name_ ), isLabel( labelp ) {} 141 142 VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) { 143 } 144 145 Expression *VarRefNode::build() const { 146 return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) ); 147 } 148 149 void VarRefNode::printOneLine( std::ostream &os, int indent ) const { 150 printDesignation( os ); 151 os << get_name() << ' '; 152 } 153 154 void VarRefNode::print( std::ostream &os, int indent ) const { 155 printDesignation( os ); 156 os << string( indent, ' ' ) << "Referencing: "; 157 os << "Variable: " << get_name(); 158 os << endl; 159 } 160 161 //############################################################################## 162 163 DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex ) : isArrayIndex( isArrayIndex ) { 164 set_argName( expr ); 165 assert( get_argName() ); 166 167 if ( ! isArrayIndex ) { 168 if ( VarRefNode * var = dynamic_cast< VarRefNode * >( expr ) ) { 169 170 stringstream ss( var->get_name() ); 171 double value; 172 if ( ss >> value ) { 173 // this is a floating point constant. It MUST be 174 // ".0" or ".1", otherwise the program is invalid 175 if ( ! (var->get_name() == ".0" || var->get_name() == ".1") ) { 176 throw SemanticError( "invalid designator name: " + var->get_name() ); 177 } // if 178 var->set_name( var->get_name().substr(1) ); 34 ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.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( 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; 179 106 } // if 180 107 } // if 181 } // if 182 } 183 184 DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) { 185 } 186 187 class DesignatorFixer : public Mutator { 188 public: 189 virtual Expression* mutate( NameExpr *nameExpr ) { 190 if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) { 191 Constant val( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nameExpr->get_name() ); 192 delete nameExpr; 193 return new ConstantExpr( val ); 194 } 195 return nameExpr; 196 } 197 }; 198 199 Expression *DesignatorNode::build() const { 200 Expression * ret = maybeBuild<Expression>(get_argName()); 201 202 if ( isArrayIndex ) { 203 // need to traverse entire structure and change any instances of 0 or 1 to 204 // ConstantExpr 205 DesignatorFixer fixer; 206 ret = ret->acceptMutator( fixer ); 207 } // if 208 209 return ret; 210 } 211 212 void DesignatorNode::printOneLine( std::ostream &os, int indent ) const { 213 if ( get_argName() ) { 214 if ( isArrayIndex ) { 215 os << "["; 216 get_argName()->printOneLine( os, indent ); 217 os << "]"; 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 218 115 } else { 219 os << "."; 220 get_argName()->printOneLine( os, indent ); 221 } 222 } // if 223 } 224 225 void DesignatorNode::print( std::ostream &os, int indent ) const { 226 if ( get_argName() ) { 227 if ( isArrayIndex ) { 228 os << "["; 229 get_argName()->print( os, indent ); 230 os << "]"; 231 } else { 232 os << "."; 233 get_argName()->print( os, indent ); 234 } 235 } // if 236 } 237 238 //############################################################################## 239 240 static const char *opName[] = { 241 "TupleC", "Comma", "TupleFieldSel", // "TuplePFieldSel", // n-adic 242 // triadic 243 "Cond", "NCond", 116 if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ? 117 Unsigned = true; 118 } // if 119 } // if 120 } // if 121 122 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ) ); 123 } // build_constantInteger 124 125 Expression *build_constantFloat( std::string & str ) { 126 static const BasicType::Kind kind[2][3] = { 127 { BasicType::Float, BasicType::Double, BasicType::LongDouble }, 128 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, 129 }; 130 131 bool complx = false; // real, complex 132 int size = 1; // 0 => float, 1 => double (default), 2 => long double 133 // floating-point constant has minimum of 2 characters: 1. or .1 134 size_t last = str.length() - 1; 135 136 if ( checkI( str[last] ) ) { // imaginary ? 137 complx = true; 138 last -= 1; // backup one character 139 } // if 140 141 if ( checkF( str[last] ) ) { // float ? 142 size = 0; 143 } else if ( checkD( str[last] ) ) { // double ? 144 size = 1; 145 } else if ( checkL( str[last] ) ) { // long double ? 146 size = 2; 147 } // if 148 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ? 149 complx = true; 150 } // if 151 152 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ) ); 153 } // build_constantFloat 154 155 Expression *build_constantChar( std::string & str ) { 156 return new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ) ); 157 } // build_constantChar 158 159 ConstantExpr *build_constantStr( std::string & str ) { 160 // string should probably be a primitive type 161 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ), 162 new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ), 163 toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' 164 false, false ); 165 return new ConstantExpr( Constant( at, str ) ); 166 } // build_constantStr 167 168 //############################################################################## 169 170 NameExpr * build_varref( const string *name, bool labelp ) { 171 return new NameExpr( *name, nullptr ); 172 } 173 174 //############################################################################## 175 176 static const char *OperName[] = { 244 177 // diadic 245 "SizeOf", "AlignOf", "OffsetOf", " Attr", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",178 "SizeOf", "AlignOf", "OffsetOf", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&", 246 179 "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?", 247 180 "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", 248 "?[?]", " FieldSel", "PFieldSel", "...",181 "?[?]", "...", 249 182 // monadic 250 183 "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&" 251 184 }; 252 185 253 OperatorNode::OperatorNode( Type t ) : type( t ) {} 254 255 OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) { 256 } 257 258 OperatorNode::~OperatorNode() {} 259 260 OperatorNode::Type OperatorNode::get_type( void ) const { 261 return type; 262 } 263 264 void OperatorNode::printOneLine( std::ostream &os, int indent ) const { 265 printDesignation( os ); 266 os << opName[ type ] << ' '; 267 } 268 269 void OperatorNode::print( std::ostream &os, int indent ) const{ 270 printDesignation( os ); 271 os << string( indent, ' ' ) << "Operator: " << opName[type] << endl; 272 return; 273 } 274 275 const char *OperatorNode::get_typename( void ) const{ 276 return opName[ type ]; 277 } 278 279 //############################################################################## 280 281 CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) { 282 } 283 284 CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) { 285 } 286 287 CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ): 288 function( f ), arguments( args ) { 289 } 290 291 CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2): 292 function( f ), arguments( arg1 ) { 293 arguments->set_link( arg2 ); 294 } 295 296 CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ), arguments( 0 ) { 297 ParseNode *cur = other.arguments; 298 while ( cur ) { 299 if ( arguments ) { 300 arguments->set_link( cur->clone() ); 301 } else { 302 arguments = ( ExpressionNode*)cur->clone(); 303 } // if 304 cur = cur->get_link(); 305 } 306 } 307 308 CompositeExprNode::~CompositeExprNode() { 309 delete function; 310 delete arguments; 311 } 312 313 314 Expression *build_cast( TypeValueNode * arg, ExpressionNode *expr_node ) { 315 DeclarationNode *decl_node = arg->get_decl(); 316 186 //############################################################################## 187 188 Expression *build_cast( DeclarationNode *decl_node, ExpressionNode *expr_node ) { 317 189 Type *targetType = decl_node->buildType(); 318 if ( dynamic_cast< VoidType * >( targetType ) ) {190 if ( dynamic_cast< VoidType * >( targetType ) ) { 319 191 delete targetType; 320 192 return new CastExpr( maybeBuild<Expression>(expr_node) ); … … 324 196 } 325 197 326 Expression *build_fieldSel( ExpressionNode *expr_node, VarRefNode *member ) { 327 NameExpr* memberExpr = dynamic_cast<NameExpr*> ( maybeBuild<Expression>( member) ); 328 assert( memberExpr ); 329 UntypedMemberExpr *ret = new UntypedMemberExpr( memberExpr->get_name(), maybeBuild<Expression>(expr_node) ); 198 Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ) { 199 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), maybeBuild<Expression>(expr_node) ); 330 200 delete member; 331 201 return ret; 332 202 } 333 203 334 Expression *build_pfieldSel( ExpressionNode *expr_node, VarRefNode *member ) { 335 NameExpr* memberExpr = dynamic_cast<NameExpr*> ( maybeBuild<Expression>( member) ); 336 assert( memberExpr ); 204 Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ) { 337 205 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 338 206 deref->get_args().push_back( maybeBuild<Expression>(expr_node) ); 339 UntypedMemberExpr *ret = new UntypedMemberExpr( member Expr->get_name(), deref );207 UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref ); 340 208 delete member; 341 209 return ret; … … 345 213 return new AddressExpr( maybeBuild<Expression>(expr_node) ); 346 214 } 347 Expression *build_sizeOf( ExpressionNode *expr_node ) { 348 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( expr_node ) ) { 349 return new SizeofExpr( arg->get_decl()->buildType() ); 350 } else { 351 return new SizeofExpr( maybeBuild<Expression>(expr_node) ); 352 } // if 353 } 354 Expression *build_alignOf( ExpressionNode *expr_node ) { 355 if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( expr_node ) ) { 356 return new AlignofExpr( arg->get_decl()->buildType() ); 357 } else { 358 return new AlignofExpr( maybeBuild<Expression>(expr_node) ); 359 } // if 360 } 361 Expression *build_offsetOf( TypeValueNode * arg, VarRefNode *member ) { 362 NameExpr *memberExpr = dynamic_cast<NameExpr *>( maybeBuild<Expression>( member ) ); 363 assert( memberExpr ); 364 return new UntypedOffsetofExpr( arg->get_decl()->buildType(), memberExpr->get_name() ); 215 Expression *build_sizeOfexpr( ExpressionNode *expr_node ) { 216 return new SizeofExpr( maybeBuild<Expression>(expr_node) ); 217 } 218 Expression *build_sizeOftype( DeclarationNode *decl_node ) { 219 return new SizeofExpr( decl_node->buildType() ); 220 } 221 Expression *build_alignOfexpr( ExpressionNode *expr_node ) { 222 return new AlignofExpr( maybeBuild<Expression>(expr_node) ); 223 } 224 Expression *build_alignOftype( DeclarationNode *decl_node ) { 225 return new AlignofExpr( decl_node->buildType() ); 226 } 227 Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ) { 228 return new UntypedOffsetofExpr( decl_node->buildType(), member->get_name() ); 365 229 } 366 230 … … 369 233 } 370 234 371 Expression *build_opr1( OperatorNode::Type op, ExpressionNode *expr_node ) { 235 Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) { 236 std::list<Expression *> args; 237 args.push_back( maybeBuild<Expression>(expr_node) ); 238 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 239 } 240 Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) { 372 241 std::list<Expression *> args; 373 242 args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node) ) ); 374 return new UntypedExpr( new NameExpr( opName[op ] ), args );375 } 376 Expression *build_ opr2( OperatorNode::Typeop, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {243 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 244 } 245 Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 377 246 std::list<Expression *> args; 378 247 args.push_back( maybeBuild<Expression>(expr_node1) ); 379 248 args.push_back( maybeBuild<Expression>(expr_node2) ); 380 return new UntypedExpr( new NameExpr( opName[ op ] ), args ); 249 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 250 } 251 Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) { 252 std::list<Expression *> args; 253 args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node1) ) ); 254 args.push_back( maybeBuild<Expression>(expr_node2) ); 255 return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args ); 381 256 } 382 257 … … 389 264 } 390 265 391 CompositeExprNode2::CompositeExprNode2( Expression *expr ) : expr( expr ) {} 392 CompositeExprNode2::CompositeExprNode2( const CompositeExprNode2 &other ) : expr( other.expr->clone() ) {} 393 CompositeExprNode2::~CompositeExprNode2() { delete expr; } 394 void CompositeExprNode2::print( std::ostream &, int indent ) const { assert( false ); } 395 void CompositeExprNode2::printOneLine( std::ostream &, int indent ) const { assert( false ); } 396 397 398 Expression *CompositeExprNode::build() const { 399 OperatorNode *op; 400 std::list<Expression *> args; 401 402 buildList( get_args(), args ); 403 404 if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator 405 return new UntypedExpr( maybeBuild<Expression>(function), args, maybeBuild< Expression >( get_argName() )); 406 } // if 407 408 switch ( op->get_type() ) { 409 case OperatorNode::Assign: 410 case OperatorNode::MulAssn: 411 case OperatorNode::DivAssn: 412 case OperatorNode::ModAssn: 413 case OperatorNode::PlusAssn: 414 case OperatorNode::MinusAssn: 415 case OperatorNode::LSAssn: 416 case OperatorNode::RSAssn: 417 case OperatorNode::AndAssn: 418 case OperatorNode::ERAssn: 419 case OperatorNode::OrAssn: 420 assert( ! args.empty() ); 421 args.front() = new AddressExpr( args.front() ); 422 case OperatorNode::UnPlus: 423 case OperatorNode::UnMinus: 424 case OperatorNode::PointTo: 425 case OperatorNode::Neg: 426 case OperatorNode::BitNeg: 427 case OperatorNode::LabelAddress: 428 return new UntypedExpr( new NameExpr( opName[ op->get_type() ] ), args ); 429 430 case OperatorNode::Attr: 431 { 432 VarRefNode *var = dynamic_cast<VarRefNode *>( get_args() ); 433 assert( var ); 434 if ( ! get_args()->get_link() ) { 435 return new AttrExpr( maybeBuild<Expression>(var), ( Expression*)0); 436 } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link() ) ) { 437 return new AttrExpr( maybeBuild<Expression>(var), arg->get_decl()->buildType() ); 438 } else { 439 return new AttrExpr( maybeBuild<Expression>(var), args.back() ); 440 } // if 441 } 442 case OperatorNode::Cond: 443 { 444 assert( args.size() == 3); 445 std::list< Expression * >::const_iterator i = args.begin(); 446 Expression *arg1 = notZeroExpr( *i++ ); 447 Expression *arg2 = *i++; 448 Expression *arg3 = *i++; 449 return new ConditionalExpr( arg1, arg2, arg3 ); 450 } 451 case OperatorNode::NCond: 452 throw UnimplementedError( "GNU 2-argument conditional expression" ); 453 // Tuples 454 case OperatorNode::TupleC: 455 { 456 TupleExpr *ret = new TupleExpr(); 457 std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) ); 458 return ret; 459 } 460 default: 461 assert( ((void)"CompositeExprNode::build", false) ); 462 return 0; 463 } // switch 464 } 465 466 void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const { 467 printDesignation( os ); 468 os << "( "; 469 function->printOneLine( os, indent ); 470 for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) { 471 cur->printOneLine( os, indent ); 472 } // for 473 os << ") "; 474 } 475 476 void CompositeExprNode::print( std::ostream &os, int indent ) const { 477 printDesignation( os ); 478 os << string( indent, ' ' ) << "Application of: " << endl; 479 function->print( os, indent + ParseNode::indent_by ); 480 481 os << string( indent, ' ' ) ; 482 if ( arguments ) { 483 os << "... on arguments: " << endl; 484 arguments->printList( os, indent + ParseNode::indent_by ); 485 } else 486 os << "... on no arguments: " << endl; 487 } 488 489 void CompositeExprNode::set_function( ExpressionNode *f ) { 490 function = f; 491 } 492 493 void CompositeExprNode::set_args( ExpressionNode *args ) { 494 arguments = args; 495 } 496 497 ExpressionNode *CompositeExprNode::get_function( void ) const { 498 return function; 499 } 500 501 ExpressionNode *CompositeExprNode::get_args( void ) const { 502 return arguments; 503 } 504 505 void CompositeExprNode::add_arg( ExpressionNode *arg ) { 506 if ( arguments ) 507 arguments->set_link( arg ); 508 else 509 set_args( arg ); 510 } 511 512 //############################################################################## 513 514 Expression *AsmExprNode::build() const { 515 return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)maybeBuild<Expression>(constraint), maybeBuild<Expression>(operand) ); 516 } 517 518 void AsmExprNode::print( std::ostream &os, int indent ) const { 519 os << string( indent, ' ' ) << "Assembler Expression:" << endl; 520 if ( inout ) { 521 os << string( indent, ' ' ) << "inout: " << std::endl; 522 inout->print( os, indent + 2 ); 523 } // if 524 if ( constraint ) { 525 os << string( indent, ' ' ) << "constraint: " << std::endl; 526 constraint->print( os, indent + 2 ); 527 } // if 528 if ( operand ) { 529 os << string( indent, ' ' ) << "operand: " << std::endl; 530 operand->print( os, indent + 2 ); 531 } // if 532 } 533 534 void AsmExprNode::printOneLine( std::ostream &os, int indent ) const { 535 printDesignation( os ); 536 os << "( "; 537 if ( inout ) inout->printOneLine( os, indent + 2 ); 538 os << ", "; 539 if ( constraint ) constraint->printOneLine( os, indent + 2 ); 540 os << ", "; 541 if ( operand ) operand->printOneLine( os, indent + 2 ); 542 os << ") "; 266 Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ) { 267 return new AttrExpr( var, maybeBuild<Expression>(expr_node) ); 268 } 269 Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ) { 270 return new AttrExpr( var, decl_node->buildType() ); 271 } 272 273 Expression *build_tuple( ExpressionNode * expr_node ) { 274 TupleExpr *ret = new TupleExpr(); 275 buildList( expr_node, ret->get_exprs() ); 276 return ret; 277 } 278 279 Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ) { 280 std::list<Expression *> args; 281 282 buildList( expr_node, args ); 283 return new UntypedExpr( maybeBuild<Expression>(function), args, nullptr ); 284 } 285 286 Expression *build_range( ExpressionNode * low, ExpressionNode *high ) { 287 Expression *low_cexpr = maybeBuild<Expression>( low ); 288 Expression *high_cexpr = maybeBuild<Expression>( high ); 289 return new RangeExpr( low_cexpr, high_cexpr ); 290 } 291 292 //############################################################################## 293 294 Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ) { 295 return new AsmExpr( maybeBuild< Expression >( inout ), constraint, maybeBuild<Expression>(operand) ); 543 296 } 544 297 … … 551 304 //############################################################################## 552 305 553 ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {} 554 555 ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) { 556 } 557 558 ValofExprNode::~ValofExprNode() { 559 delete body; 560 } 561 562 void ValofExprNode::print( std::ostream &os, int indent ) const { 563 printDesignation( os ); 564 os << string( indent, ' ' ) << "Valof Expression:" << std::endl; 565 get_body()->print( os, indent + 4); 566 } 567 568 void ValofExprNode::printOneLine( std::ostream &, int indent ) const { 569 assert( false ); 570 } 571 572 Expression *ValofExprNode::build() const { 573 return new UntypedValofExpr ( maybeBuild<Statement>(get_body()), maybeBuild< Expression >( get_argName() ) ); 574 } 575 576 //############################################################################## 577 578 ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { 579 if ( init_ == 0 ) 580 init = 0; 581 else { 582 DeclarationNode *decl; 583 ExpressionNode *exp; 584 585 if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) 586 init = new StatementNode( decl ); 587 else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) 588 init = new StatementNode( StatementNode::Exp, exp ); 589 else 590 throw SemanticError("Error in for control expression"); 591 } 592 } 593 594 ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other ) 595 : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) { 596 } 597 598 ForCtlExprNode::~ForCtlExprNode() { 599 delete init; 600 delete condition; 601 delete change; 602 } 603 604 Expression *ForCtlExprNode::build() const { 605 // this shouldn't be used! 606 assert( false ); 607 return 0; 608 } 609 610 void ForCtlExprNode::print( std::ostream &os, int indent ) const{ 611 os << string( indent,' ' ) << "For Control Expression -- :" << endl; 612 613 os << string( indent + 2, ' ' ) << "initialization:" << endl; 614 if ( init != 0 ) 615 init->printList( os, indent + 4 ); 616 617 os << string( indent + 2, ' ' ) << "condition: " << endl; 618 if ( condition != 0 ) 619 condition->print( os, indent + 4 ); 620 os << string( indent + 2, ' ' ) << "increment: " << endl; 621 if ( change != 0 ) 622 change->print( os, indent + 4 ); 623 } 624 625 void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const { 626 assert( false ); 627 } 628 629 //############################################################################## 630 631 TypeValueNode::TypeValueNode( DeclarationNode *decl ) : decl( decl ) { 632 } 633 634 TypeValueNode::TypeValueNode( const TypeValueNode &other ) : ExpressionNode( other ), decl( maybeClone( other.decl ) ) { 635 } 636 637 Expression *TypeValueNode::build() const { 306 Expression *build_valexpr( StatementNode *s ) { 307 return new UntypedValofExpr( maybeBuild<Statement>(s), nullptr ); 308 } 309 310 //############################################################################## 311 312 // ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) { 313 // if ( init_ == 0 ) 314 // init = 0; 315 // else { 316 // DeclarationNode *decl; 317 // ExpressionNode *exp; 318 319 // if (( decl = dynamic_cast<DeclarationNode *>(init_) ) != 0) 320 // init = new StatementNode( decl ); 321 // else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0) 322 // init = new StatementNode( StatementNode::Exp, exp ); 323 // else 324 // throw SemanticError("Error in for control expression"); 325 // } 326 // } 327 328 // ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other ) 329 // : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) { 330 // } 331 332 // ForCtlExprNode::~ForCtlExprNode() { 333 // delete init; 334 // delete condition; 335 // delete change; 336 // } 337 338 // Expression *ForCtlExprNode::build() const { 339 // // this shouldn't be used! 340 // assert( false ); 341 // return 0; 342 // } 343 344 // void ForCtlExprNode::print( std::ostream &os, int indent ) const{ 345 // os << string( indent,' ' ) << "For Control Expression -- :" << endl; 346 347 // os << string( indent + 2, ' ' ) << "initialization:" << endl; 348 // if ( init != 0 ) 349 // init->printList( os, indent + 4 ); 350 351 // os << string( indent + 2, ' ' ) << "condition: " << endl; 352 // if ( condition != 0 ) 353 // condition->print( os, indent + 4 ); 354 // os << string( indent + 2, ' ' ) << "increment: " << endl; 355 // if ( change != 0 ) 356 // change->print( os, indent + 4 ); 357 // } 358 359 // void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const { 360 // assert( false ); 361 // } 362 363 //############################################################################## 364 365 Expression *build_typevalue( DeclarationNode *decl ) { 638 366 return new TypeExpr( decl->buildType() ); 639 367 } 640 368 641 void TypeValueNode::print( std::ostream &os, int indent ) const { 642 os << std::string( indent, ' ' ) << "Type:"; 643 get_decl()->print( os, indent + 2); 644 } 645 646 void TypeValueNode::printOneLine( std::ostream &os, int indent ) const { 647 os << "Type:"; 648 get_decl()->print( os, indent + 2); 649 } 650 651 652 CompoundLiteralNode::CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids ) : type( type ), kids( kids ) {} 653 CompoundLiteralNode::CompoundLiteralNode( const CompoundLiteralNode &other ) : ExpressionNode( other ), type( other.type ), kids( other.kids ) {} 654 655 CompoundLiteralNode::~CompoundLiteralNode() { 656 delete kids; 657 delete type; 658 } 659 660 CompoundLiteralNode *CompoundLiteralNode::clone() const { 661 return new CompoundLiteralNode( *this ); 662 } 663 664 void CompoundLiteralNode::print( std::ostream &os, int indent ) const { 665 os << string( indent,' ' ) << "CompoundLiteralNode:" << endl; 666 667 os << string( indent + 2, ' ' ) << "type:" << endl; 668 if ( type != 0 ) 669 type->print( os, indent + 4 ); 670 671 os << string( indent + 2, ' ' ) << "initialization:" << endl; 672 if ( kids != 0 ) 673 kids->printList( os, indent + 4 ); 674 } 675 676 void CompoundLiteralNode::printOneLine( std::ostream &os, int indent ) const { 677 os << "( "; 678 if ( type ) type->print( os ); 679 os << ", "; 680 if ( kids ) kids->printOneLine( os ); 681 os << ") "; 682 } 683 684 Expression *CompoundLiteralNode::build() const { 685 Declaration * newDecl = maybeBuild<Declaration>(type); // compound literal type 369 //############################################################################## 370 371 Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ) { 372 Declaration * newDecl = maybeBuild<Declaration>(decl_node); // compound literal type 686 373 if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type 687 374 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeBuild<Initializer>(kids) ); … … 698 385 } 699 386 700 ExpressionNode *flattenCommas( ExpressionNode *list ) {701 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) {702 OperatorNode *op;703 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) {704 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )705 composite->add_arg( next );706 return flattenCommas( composite->get_args() );707 } // if708 } // if709 710 if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )711 list->set_next( flattenCommas( next ) );712 713 return list;714 }715 716 ExpressionNode *tupleContents( ExpressionNode *tuple ) {717 if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) {718 OperatorNode *op = 0;719 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) )720 return composite->get_args();721 } // if722 return tuple;723 }724 725 387 // Local Variables: // 726 388 // tab-width: 4 //
Note: See TracChangeset
for help on using the changeset viewer.