Changeset 17cd4eb
- Timestamp:
- Jan 7, 2015, 6:04:42 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0b8cd72
- Parents:
- d9a0e76
- Files:
-
- 26 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/prelude.cf
rd9a0e76 r17cd4eb 8 8 // Created On : Sat Nov 29 07:23:41 2014 9 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Mon Dec 15 16:20:30 201411 // Update Count : 2 710 // Last Modified On : Wed Jan 7 17:42:15 2015 11 // Update Count : 28 12 12 // 13 13 14 14 // Following line added from stddef.h by build 15 15 16 typedef long int ptrdiff_t; 16 17 -
translator/CodeGen/CodeGenerator2.cc
rd9a0e76 r17cd4eb 20 20 21 21 namespace CodeGen { 22 23 int CodeGenerator2::tabsize = 4; 24 25 CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before(os), after() { } 26 27 CodeGenerator2::CodeGenerator2(std::ostream &os, std::string init, int indent, bool infunp ) 28 : cur_indent( indent ), insideFunction( infunp ), before(os), after() 29 { 30 //before << std::string( init ); 31 } 32 33 CodeGenerator2::CodeGenerator2(std::ostream &os, char *init, int indent, bool infunp ) 34 : cur_indent( indent ), insideFunction( infunp ), before(os), after() 35 { 36 //before << std::string( init ); 37 } 38 39 CodeGenerator2::~CodeGenerator2() { 40 after.freeze( false ); 41 } 42 43 string 44 mangleName( DeclarationWithType *decl ) { 45 if( decl->get_mangleName() != "" ) { 46 return decl->get_mangleName(); 47 } else { 48 return decl->get_name(); 49 } 50 } 51 52 //*** Declarations 53 void CodeGenerator2::visit(FunctionDecl *functionDecl){ 54 55 handleStorageClass( functionDecl ); 56 before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); 57 58 // how to get this to the Functype? 59 std::list< Declaration * > olds = functionDecl->get_oldDecls(); 60 if( ! olds.empty() ) { 61 before << " /* function has old declaration */"; 62 } 63 64 // acceptAll( functionDecl->get_oldDecls(), *this ); 65 if( functionDecl->get_statements() ) { 66 functionDecl->get_statements()->accept(*this); 67 } 68 } 69 70 void CodeGenerator2::visit(ObjectDecl *objectDecl){ 71 72 handleStorageClass( objectDecl ); 73 before << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 22 int CodeGenerator2::tabsize = 4; 23 24 CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before( os ), after() { } 25 26 CodeGenerator2::CodeGenerator2( std::ostream &os, std::string init, int indent, bool infunp ) 27 : cur_indent( indent ), insideFunction( infunp ), before( os ) 28 { 29 //before << std::string( init ); 30 } 31 32 CodeGenerator2::CodeGenerator2( std::ostream &os, char *init, int indent, bool infunp ) 33 : cur_indent( indent ), insideFunction( infunp ), before( os ) 34 { 35 //before << std::string( init ); 36 } 37 38 string mangleName( DeclarationWithType *decl ) { 39 if ( decl->get_mangleName() != "" ) { 40 return decl->get_mangleName(); 41 } else { 42 return decl->get_name(); 43 } // if 44 } 45 46 //*** Declarations 47 void CodeGenerator2::visit( FunctionDecl *functionDecl ) { 48 handleStorageClass( functionDecl ); 49 before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); 50 51 // how to get this to the Functype? 52 std::list< Declaration * > olds = functionDecl->get_oldDecls(); 53 if ( ! olds.empty() ) { 54 before << " /* function has old declaration */"; 55 } // if 56 57 // acceptAll( functionDecl->get_oldDecls(), *this ); 58 if ( functionDecl->get_statements() ) { 59 functionDecl->get_statements()->accept(*this ); 60 } // if 61 } 62 63 void CodeGenerator2::visit( ObjectDecl *objectDecl ) { 64 handleStorageClass( objectDecl ); 65 before << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 74 66 75 if( objectDecl->get_init() ) { 76 before << " = "; 77 objectDecl->get_init()->accept( *this ); 78 } 79 if( objectDecl->get_bitfieldWidth() ) { 80 before << ":"; 81 objectDecl->get_bitfieldWidth()->accept( *this ); 82 } 83 } 84 85 void 86 CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) 87 { 88 if ( aggDecl->get_name() != "" ) 89 before << aggDecl->get_name(); 67 if ( objectDecl->get_init() ) { 68 before << " = "; 69 objectDecl->get_init()->accept( *this ); 70 } // if 71 if ( objectDecl->get_bitfieldWidth() ) { 72 before << ":"; 73 objectDecl->get_bitfieldWidth()->accept( *this ); 74 } // if 75 } 76 77 void CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) { 78 if ( aggDecl->get_name() != "" ) 79 before << aggDecl->get_name(); 90 80 91 std::list< Declaration * > &memb = aggDecl->get_members(); 92 93 if( ! memb.empty() ){ 94 before << endl << string(cur_indent, ' ') << "{" << endl; 95 96 cur_indent += CodeGenerator2::tabsize; 97 for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++){ 98 before << string(cur_indent, ' '); 99 (*i)->accept(*this); 100 before << ";" << endl; 101 } 102 103 cur_indent -= CodeGenerator2::tabsize; 104 105 before << string(cur_indent, ' ') << "}"; 106 } 107 } 108 109 void CodeGenerator2::visit( StructDecl *structDecl ){ 110 before << "struct "; 111 handleAggregate( structDecl ); 112 } 113 114 void 115 CodeGenerator2::visit(UnionDecl *aggregateDecl) 116 { 117 before << "union "; 118 handleAggregate( aggregateDecl ); 119 } 120 121 void 122 CodeGenerator2::visit(EnumDecl *aggDecl) 123 { 124 before << "enum "; 125 126 if ( aggDecl->get_name() != "" ) 127 before << aggDecl->get_name(); 81 std::list< Declaration * > &memb = aggDecl->get_members(); 82 83 if ( ! memb.empty() ) { 84 before << endl << string( cur_indent, ' ' ) << "{" << endl; 85 86 cur_indent += CodeGenerator2::tabsize; 87 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 88 before << string( cur_indent, ' ' ); 89 (*i)->accept(*this ); 90 before << ";" << endl; 91 } 92 93 cur_indent -= CodeGenerator2::tabsize; 94 95 before << string( cur_indent, ' ' ) << "}"; 96 } // if 97 } 98 99 void CodeGenerator2::visit( StructDecl *structDecl ) { 100 before << "struct "; 101 handleAggregate( structDecl ); 102 } 103 104 void CodeGenerator2::visit( UnionDecl *aggregateDecl ) { 105 before << "union "; 106 handleAggregate( aggregateDecl ); 107 } 108 109 void CodeGenerator2::visit( EnumDecl *aggDecl ) { 110 before << "enum "; 111 112 if ( aggDecl->get_name() != "" ) 113 before << aggDecl->get_name(); 128 114 129 std::list< Declaration* > &memb = aggDecl->get_members(); 130 131 if( ! memb.empty() ){ 132 before << endl << "{" << endl; 133 134 cur_indent += CodeGenerator2::tabsize; 135 for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++){ 136 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 137 assert( obj ); 138 before << string(cur_indent, ' ') << mangleName( obj ); 139 if( obj->get_init() ) { 140 before << " = "; 141 obj->get_init()->accept(*this); 115 std::list< Declaration* > &memb = aggDecl->get_members(); 116 117 if ( ! memb.empty() ) { 118 before << endl << "{" << endl; 119 120 cur_indent += CodeGenerator2::tabsize; 121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 122 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 123 assert( obj ); 124 before << string( cur_indent, ' ' ) << mangleName( obj ); 125 if ( obj->get_init() ) { 126 before << " = "; 127 obj->get_init()->accept(*this ); 128 } // if 129 before << "," << endl; 130 } 131 132 cur_indent -= CodeGenerator2::tabsize; 133 134 before << "}" << endl; 135 } // if 136 } 137 138 void CodeGenerator2::visit( ContextDecl *aggregateDecl ) {} 139 140 void CodeGenerator2::visit( TypedefDecl *typeDecl ) { 141 before << "typedef "; 142 before << genType( typeDecl->get_base(), typeDecl->get_name() ); 143 } 144 145 void CodeGenerator2::visit( TypeDecl *typeDecl ) { 146 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 147 // still to be done 148 before << "extern unsigned long " << typeDecl->get_name(); 149 if ( typeDecl->get_base() ) { 150 before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; 151 } // if 152 } 153 154 void CodeGenerator2::visit( SingleInit *init ) { 155 init->get_value()->accept( *this ); 156 } 157 158 void CodeGenerator2::visit( ListInit *init ) { 159 before << "{ "; 160 genCommaList( init->begin_initializers(), init->end_initializers() ); 161 before << " }"; 162 } 163 164 void CodeGenerator2::visit( Constant *constant ) { 165 before << constant->get_value() ; 166 } 167 168 //*** Expressions 169 void CodeGenerator2::visit( ApplicationExpr *applicationExpr ) { 170 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 171 OperatorInfo opInfo; 172 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 173 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); 174 switch ( opInfo.type ) { 175 case OT_PREFIXASSIGN: 176 case OT_POSTFIXASSIGN: 177 case OT_INFIXASSIGN: 178 { 179 assert( arg != applicationExpr->get_args().end() ); 180 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 181 182 *arg = addrExpr->get_arg(); 183 } else { 184 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 185 newExpr->get_args().push_back( *arg ); 186 *arg = newExpr; 187 } // if 188 break; 189 } 190 191 default: 192 // do nothing 193 ; 194 } 195 196 switch ( opInfo.type ) { 197 case OT_INDEX: 198 assert( applicationExpr->get_args().size() == 2 ); 199 (*arg++)->accept( *this ); 200 before << "["; 201 (*arg)->accept( *this ); 202 before << "]"; 203 break; 204 205 case OT_CALL: 206 // there are no intrinsic definitions of the function call operator 207 assert( false ); 208 break; 209 210 case OT_PREFIX: 211 case OT_PREFIXASSIGN: 212 assert( applicationExpr->get_args().size() == 1 ); 213 before << "("; 214 before << opInfo.symbol; 215 (*arg)->accept( *this ); 216 before << ")"; 217 break; 218 219 case OT_POSTFIX: 220 case OT_POSTFIXASSIGN: 221 assert( applicationExpr->get_args().size() == 1 ); 222 (*arg)->accept( *this ); 223 before << opInfo.symbol; 224 break; 225 226 case OT_INFIX: 227 case OT_INFIXASSIGN: 228 assert( applicationExpr->get_args().size() == 2 ); 229 before << "("; 230 (*arg++)->accept( *this ); 231 before << opInfo.symbol; 232 (*arg)->accept( *this ); 233 before << ")"; 234 break; 235 236 case OT_CONSTANT: 237 // there are no intrinsic definitions of 0 or 1 as functions 238 assert( false ); 239 } 240 } else { 241 varExpr->accept( *this ); 242 before << "("; 243 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 244 before << ")"; 245 } // if 246 } else { 247 applicationExpr->get_function()->accept( *this ); 248 before << "("; 249 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 250 before << ")"; 251 } // if 252 } 253 254 void CodeGenerator2::visit( UntypedExpr *untypedExpr ) { 255 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 256 OperatorInfo opInfo; 257 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { 258 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); 259 switch ( opInfo.type ) { 260 case OT_INDEX: 261 assert( untypedExpr->get_args().size() == 2 ); 262 (*arg++)->accept( *this ); 263 before << "["; 264 (*arg)->accept( *this ); 265 before << "]"; 266 break; 267 268 case OT_CALL: 269 assert( false ); 270 break; 271 272 case OT_PREFIX: 273 case OT_PREFIXASSIGN: 274 assert( untypedExpr->get_args().size() == 1 ); 275 before << "("; 276 before << opInfo.symbol; 277 (*arg)->accept( *this ); 278 before << ")"; 279 break; 280 281 case OT_POSTFIX: 282 case OT_POSTFIXASSIGN: 283 assert( untypedExpr->get_args().size() == 1 ); 284 (*arg)->accept( *this ); 285 before << opInfo.symbol; 286 break; 287 288 case OT_INFIX: 289 case OT_INFIXASSIGN: 290 assert( untypedExpr->get_args().size() == 2 ); 291 before << "("; 292 (*arg++)->accept( *this ); 293 before << opInfo.symbol; 294 (*arg)->accept( *this ); 295 before << ")"; 296 break; 297 298 case OT_CONSTANT: 299 // there are no intrinsic definitions of 0 or 1 as functions 300 assert( false ); 301 } 302 } else { 303 nameExpr->accept( *this ); 304 before << "("; 305 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 306 before << ")"; 307 } // if 308 } else { 309 untypedExpr->get_function()->accept( *this ); 310 before << "("; 311 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 312 before << ")"; 313 } // if 314 } 315 316 void CodeGenerator2::visit( NameExpr *nameExpr ) { 317 OperatorInfo opInfo; 318 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { 319 assert( opInfo.type == OT_CONSTANT ); 320 before << opInfo.symbol; 321 } else { 322 before << nameExpr->get_name(); 323 } // if 324 } 325 326 void CodeGenerator2::visit( AddressExpr *addressExpr ) { 327 before << "(&"; 328 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 329 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 330 before << mangleName( variableExpr->get_var() ); 331 } else { 332 addressExpr->get_arg()->accept( *this ); 333 } // if 334 before << ")"; 335 } 336 337 void CodeGenerator2::visit( CastExpr *castExpr ) { 338 before << "(("; 339 if ( castExpr->get_results().empty() ) { 340 before << "void" ; 341 } else { 342 before << genType( castExpr->get_results().front(), "" ); 343 } // if 344 before << ")"; 345 castExpr->get_arg()->accept( *this ); 346 before << ")"; 347 } 348 349 void CodeGenerator2::visit( UntypedMemberExpr *memberExpr ) { 350 assert( false ); 351 } 352 353 void CodeGenerator2::visit( MemberExpr *memberExpr ) { 354 memberExpr->get_aggregate()->accept( *this ); 355 before << "." << mangleName( memberExpr->get_member() ); 356 } 357 358 void CodeGenerator2::visit( VariableExpr *variableExpr ) { 359 OperatorInfo opInfo; 360 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { 361 before << opInfo.symbol; 362 } else { 363 before << mangleName( variableExpr->get_var() ); 364 } // if 365 } 366 367 void CodeGenerator2::visit( ConstantExpr *constantExpr ) { 368 assert( constantExpr->get_constant() ); 369 constantExpr->get_constant()->accept( *this ); 370 } 371 372 void CodeGenerator2::visit( SizeofExpr *sizeofExpr ) { 373 before << "sizeof("; 374 if ( sizeofExpr->get_isType() ) { 375 before << genType( sizeofExpr->get_type(), "" ); 376 } else { 377 sizeofExpr->get_expr()->accept( *this ); 378 } // if 379 before << ")"; 380 } 381 382 void CodeGenerator2::visit( LogicalExpr *logicalExpr ) { 383 before << "("; 384 logicalExpr->get_arg1()->accept( *this ); 385 if ( logicalExpr->get_isAnd() ) { 386 before << " && "; 387 } else { 388 before << " || "; 389 } // if 390 logicalExpr->get_arg2()->accept( *this ); 391 before << ")"; 392 } 393 394 void CodeGenerator2::visit( ConditionalExpr *conditionalExpr ) { 395 before << "("; 396 conditionalExpr->get_arg1()->accept( *this ); 397 before << " ? "; 398 conditionalExpr->get_arg2()->accept( *this ); 399 before << " : "; 400 conditionalExpr->get_arg3()->accept( *this ); 401 before << ")"; 402 } 403 404 void CodeGenerator2::visit( CommaExpr *commaExpr ) { 405 before << "("; 406 commaExpr->get_arg1()->accept( *this ); 407 before << " , "; 408 commaExpr->get_arg2()->accept( *this ); 409 before << ")"; 410 } 411 412 void CodeGenerator2::visit( TupleExpr *tupleExpr ) {} 413 414 void CodeGenerator2::visit( TypeExpr *typeExpr ) {} 415 416 417 //*** Statements 418 void CodeGenerator2::visit( CompoundStmt *compoundStmt ) { 419 std::list<Statement*> ks = compoundStmt->get_kids(); 420 421 before << endl << string( cur_indent, ' ' ) << "{" << endl; 422 423 cur_indent += CodeGenerator2::tabsize; 424 425 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { 426 before << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 427 (*i)->accept(*this ); 428 shift_left(); 429 before << endl; 142 430 } 143 before << "," << endl; 144 } 145 146 cur_indent -= CodeGenerator2::tabsize; 147 148 before << "}" << endl; 149 } 150 } 151 152 void 153 CodeGenerator2::visit(ContextDecl *aggregateDecl) 154 { 155 } 156 157 void 158 CodeGenerator2::visit(TypedefDecl *typeDecl) 159 { 160 before << "typedef "; 161 before << genType( typeDecl->get_base(), typeDecl->get_name() ); 162 } 163 164 void 165 CodeGenerator2::visit(TypeDecl *typeDecl) 166 { 167 // really, we should mutate this into something that isn't a TypeDecl 168 // but that requires large-scale changes, still to be done 169 before << "extern unsigned long " << typeDecl->get_name(); 170 if( typeDecl->get_base() ) { 171 before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; 172 } 173 } 174 175 //*** Initializer 176 void 177 CodeGenerator2::visit(SingleInit *init) 178 { 179 init->get_value()->accept( *this ); 180 } 181 182 void 183 CodeGenerator2::visit(ListInit *init) 184 { 185 before << "{ "; 186 genCommaList( init->begin_initializers(), init->end_initializers() ); 187 before << " }"; 188 } 189 190 //*** Constant 191 void CodeGenerator2::visit(Constant *constant) { 192 before << constant->get_value() ; 193 } 194 195 //*** Expressions 196 void 197 CodeGenerator2::visit(ApplicationExpr *applicationExpr) 198 { 199 if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 200 OperatorInfo opInfo; 201 if( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 202 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); 203 switch( opInfo.type ) { 204 case OT_PREFIXASSIGN: 205 case OT_POSTFIXASSIGN: 206 case OT_INFIXASSIGN: 207 { 208 assert( arg != applicationExpr->get_args().end() ); 209 if( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 210 211 *arg = addrExpr->get_arg(); 212 } else { 213 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 214 newExpr->get_args().push_back( *arg ); 215 *arg = newExpr; 216 } 217 break; 218 } 219 220 default: 221 // do nothing 222 ; 223 } 224 225 switch( opInfo.type ) { 226 case OT_INDEX: 227 assert( applicationExpr->get_args().size() == 2 ); 228 (*arg++)->accept( *this ); 229 before << "["; 230 (*arg)->accept( *this ); 231 before << "]"; 232 break; 233 234 case OT_CALL: 235 // there are no intrinsic definitions of the function call operator 236 assert( false ); 237 break; 238 239 case OT_PREFIX: 240 case OT_PREFIXASSIGN: 241 assert( applicationExpr->get_args().size() == 1 ); 242 before << "("; 243 before << opInfo.symbol; 244 (*arg)->accept( *this ); 245 before << ")"; 246 break; 247 248 case OT_POSTFIX: 249 case OT_POSTFIXASSIGN: 250 assert( applicationExpr->get_args().size() == 1 ); 251 (*arg)->accept( *this ); 252 before << opInfo.symbol; 253 break; 254 255 case OT_INFIX: 256 case OT_INFIXASSIGN: 257 assert( applicationExpr->get_args().size() == 2 ); 258 before << "("; 259 (*arg++)->accept( *this ); 260 before << opInfo.symbol; 261 (*arg)->accept( *this ); 262 before << ")"; 263 break; 264 265 case OT_CONSTANT: 266 // there are no intrinsic definitions of 0 or 1 as functions 267 assert( false ); 268 } 269 } else { 270 varExpr->accept( *this ); 271 before << "("; 272 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 273 before << ")"; 274 } 275 } else { 276 applicationExpr->get_function()->accept( *this ); 277 before << "("; 278 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 279 before << ")"; 280 } 281 } 282 283 void 284 CodeGenerator2::visit(UntypedExpr *untypedExpr) 285 { 286 if( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 287 OperatorInfo opInfo; 288 if( operatorLookup( nameExpr->get_name(), opInfo ) ) { 289 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); 290 switch( opInfo.type ) { 291 case OT_INDEX: 292 assert( untypedExpr->get_args().size() == 2 ); 293 (*arg++)->accept( *this ); 294 before << "["; 295 (*arg)->accept( *this ); 296 before << "]"; 297 break; 298 299 case OT_CALL: 300 assert( false ); 301 break; 302 303 case OT_PREFIX: 304 case OT_PREFIXASSIGN: 305 assert( untypedExpr->get_args().size() == 1 ); 306 before << "("; 307 before << opInfo.symbol; 308 (*arg)->accept( *this ); 309 before << ")"; 310 break; 311 312 case OT_POSTFIX: 313 case OT_POSTFIXASSIGN: 314 assert( untypedExpr->get_args().size() == 1 ); 315 (*arg)->accept( *this ); 316 before << opInfo.symbol; 317 break; 318 319 case OT_INFIX: 320 case OT_INFIXASSIGN: 321 assert( untypedExpr->get_args().size() == 2 ); 322 before << "("; 323 (*arg++)->accept( *this ); 324 before << opInfo.symbol; 325 (*arg)->accept( *this ); 326 before << ")"; 327 break; 328 329 case OT_CONSTANT: 330 // there are no intrinsic definitions of 0 or 1 as functions 331 assert( false ); 332 } 333 } else { 334 nameExpr->accept( *this ); 335 before << "("; 336 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 337 before << ")"; 338 } 339 } else { 340 untypedExpr->get_function()->accept( *this ); 341 before << "("; 342 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 343 before << ")"; 344 } 345 } 346 347 void 348 CodeGenerator2::visit(NameExpr *nameExpr) 349 { 350 OperatorInfo opInfo; 351 if( operatorLookup( nameExpr->get_name(), opInfo ) ) { 352 assert( opInfo.type == OT_CONSTANT ); 353 before << opInfo.symbol; 354 } else { 355 before << nameExpr->get_name(); 356 } 357 } 358 359 void 360 CodeGenerator2::visit(AddressExpr *addressExpr) 361 { 362 before << "(&"; 363 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 364 if( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 365 before << mangleName( variableExpr->get_var() ); 366 } else { 367 addressExpr->get_arg()->accept( *this ); 368 } 369 before << ")"; 370 } 371 372 void 373 CodeGenerator2::visit(CastExpr *castExpr) 374 { 375 before << "(("; 376 if( castExpr->get_results().empty() ) { 377 before << "void" ; 378 } else { 379 before << genType( castExpr->get_results().front(), "" ); 380 } 381 before << ")"; 382 castExpr->get_arg()->accept( *this ); 383 before << ")"; 384 } 385 386 void 387 CodeGenerator2::visit(UntypedMemberExpr *memberExpr) 388 { 389 assert( false ); 390 } 391 392 void 393 CodeGenerator2::visit(MemberExpr *memberExpr) 394 { 395 memberExpr->get_aggregate()->accept( *this ); 396 before << "." << mangleName( memberExpr->get_member() ); 397 } 398 399 void 400 CodeGenerator2::visit(VariableExpr *variableExpr) 401 { 402 OperatorInfo opInfo; 403 if( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { 404 before << opInfo.symbol; 405 } else { 406 before << mangleName( variableExpr->get_var() ); 407 } 408 } 409 410 void 411 CodeGenerator2::visit(ConstantExpr *constantExpr) 412 { 413 assert( constantExpr->get_constant() ); 414 constantExpr->get_constant()->accept( *this ); 415 } 416 417 void 418 CodeGenerator2::visit(SizeofExpr *sizeofExpr) 419 { 420 before << "sizeof("; 421 if( sizeofExpr->get_isType() ) { 422 before << genType( sizeofExpr->get_type(), "" ); 423 } else { 424 sizeofExpr->get_expr()->accept( *this ); 425 } 426 before << ")"; 427 } 428 429 void 430 CodeGenerator2::visit(LogicalExpr *logicalExpr) 431 { 432 before << "("; 433 logicalExpr->get_arg1()->accept( *this ); 434 if( logicalExpr->get_isAnd() ) { 435 before << " && "; 436 } else { 437 before << " || "; 438 } 439 logicalExpr->get_arg2()->accept( *this ); 440 before << ")"; 441 } 442 443 void 444 CodeGenerator2::visit(ConditionalExpr *conditionalExpr) 445 { 446 before << "("; 447 conditionalExpr->get_arg1()->accept( *this ); 448 before << " ? "; 449 conditionalExpr->get_arg2()->accept( *this ); 450 before << " : "; 451 conditionalExpr->get_arg3()->accept( *this ); 452 before << ")"; 453 } 454 455 void 456 CodeGenerator2::visit(CommaExpr *commaExpr) 457 { 458 before << "("; 459 commaExpr->get_arg1()->accept( *this ); 460 before << " , "; 461 commaExpr->get_arg2()->accept( *this ); 462 before << ")"; 463 } 464 465 void 466 CodeGenerator2::visit(TupleExpr *tupleExpr) 467 { 468 } 469 470 void 471 CodeGenerator2::visit(TypeExpr *typeExpr) 472 { 473 } 474 475 476 //*** Statements 477 void CodeGenerator2::visit(CompoundStmt *compoundStmt){ 478 std::list<Statement*> ks = compoundStmt->get_kids(); 479 480 before << endl << string(cur_indent,' ') << "{" << endl; 481 482 cur_indent += CodeGenerator2::tabsize; 483 484 for(std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++){ 485 before << string(cur_indent, ' ') << printLabels( (*i)->get_labels() ) ; 486 (*i)->accept(*this); 487 shift_left(); 488 before << endl; 489 } 490 cur_indent -= CodeGenerator2::tabsize; 491 492 before << string(cur_indent,' ') << "}" << endl; 493 } 494 495 void CodeGenerator2::visit(ExprStmt *exprStmt){ 496 if(exprStmt != 0){ 497 exprStmt->get_expr()->accept( *this ); 498 shift_left(); 499 before << ";" ; 500 } 501 } 502 503 void CodeGenerator2::visit(IfStmt *ifStmt){ 504 before << "if ("; 505 ifStmt->get_condition()->accept(*this); 506 after << ")" << endl ; 507 shift_left(); 508 509 cur_indent += CodeGenerator2::tabsize; 510 before << string(cur_indent, ' '); 511 ifStmt->get_thenPart()->accept(*this); 512 cur_indent -= CodeGenerator2::tabsize; 513 shift_left(); before << endl; 514 515 if(ifStmt->get_elsePart() != 0){ 516 before << string(cur_indent, ' ') << " else " << endl ; 517 518 cur_indent += CodeGenerator2::tabsize; 519 ifStmt->get_elsePart()->accept(*this); 520 cur_indent -= CodeGenerator2::tabsize; 521 } 522 } 523 524 void CodeGenerator2::visit(SwitchStmt *switchStmt){ 525 //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( switchStmt->get_labels() ) 526 before << "switch (" ; 527 switchStmt->get_condition()->accept(*this); 528 after << ")" << std::endl ; 529 shift_left(); 530 531 before << string(cur_indent, ' ') << "{" << std::endl; 532 cur_indent += CodeGenerator2::tabsize; 533 534 std::list< Statement * > stmts = switchStmt->get_branches(); 535 bool lastBreak = false; 536 537 // horrible, horrible hack 538 if( dynamic_cast<BranchStmt *>(stmts.back()) != 0 ) { 539 lastBreak = true; 540 stmts.pop_back(); 541 } 542 acceptAll(stmts, *this ); 543 if ( lastBreak ) { 544 Statement *st = switchStmt->get_branches().back(); 545 before << CodeGenerator2::printLabels(st->get_labels()); 546 st->accept(*this); 547 } 431 cur_indent -= CodeGenerator2::tabsize; 432 433 before << string( cur_indent, ' ' ) << "}" << endl; 434 } 435 436 void CodeGenerator2::visit( ExprStmt *exprStmt ) { 437 if ( exprStmt != 0 ) { 438 exprStmt->get_expr()->accept( *this ); 439 shift_left(); 440 before << ";" ; 441 } // if 442 } 443 444 void CodeGenerator2::visit( IfStmt *ifStmt ) { 445 before << "if ("; 446 ifStmt->get_condition()->accept(*this ); 447 after += ")\n"; 448 shift_left(); 449 450 cur_indent += CodeGenerator2::tabsize; 451 before << string( cur_indent, ' ' ); 452 ifStmt->get_thenPart()->accept(*this ); 453 cur_indent -= CodeGenerator2::tabsize; 454 shift_left(); before << endl; 455 456 if ( ifStmt->get_elsePart() != 0) { 457 before << string( cur_indent, ' ' ) << " else " << endl ; 458 459 cur_indent += CodeGenerator2::tabsize; 460 ifStmt->get_elsePart()->accept(*this ); 461 cur_indent -= CodeGenerator2::tabsize; 462 } // if 463 } 464 465 void CodeGenerator2::visit( SwitchStmt *switchStmt ) { 466 //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( switchStmt->get_labels() ) 467 before << "switch (" ; 468 switchStmt->get_condition()->accept(*this ); 469 after += ")\n"; 470 shift_left(); 471 472 before << string( cur_indent, ' ' ) << "{" << std::endl; 473 cur_indent += CodeGenerator2::tabsize; 474 475 std::list< Statement * > stmts = switchStmt->get_branches(); 476 bool lastBreak = false; 477 478 // horrible, horrible hack 479 if ( dynamic_cast<BranchStmt *>( stmts.back()) != 0 ) { 480 lastBreak = true; 481 stmts.pop_back(); 482 } // if 483 acceptAll( stmts, *this ); 484 if ( lastBreak ) { 485 Statement *st = switchStmt->get_branches().back(); 486 before << CodeGenerator2::printLabels( st->get_labels()); 487 st->accept(*this ); 488 } // if 548 489 549 cur_indent -= CodeGenerator2::tabsize; 550 551 before << /* "\r" << */ string(cur_indent, ' ') << "}" << endl ; 552 } 553 554 void CodeGenerator2::visit(CaseStmt *caseStmt){ 555 before << string(cur_indent, ' '); 556 if (caseStmt->isDefault()) 557 before << "default " ; 558 else { 559 before << "case " ; 560 caseStmt->get_condition()->accept(*this); 561 } 562 after << ":" << std::endl ; 563 shift_left(); 564 565 std::list<Statement *> sts = caseStmt->get_statements(); 566 567 cur_indent += CodeGenerator2::tabsize; 568 for(std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++){ 569 before << /* "\r" << */ string(cur_indent, ' ') << printLabels( (*i)->get_labels() ) ; 570 (*i)->accept(*this); 571 shift_left(); 572 before << ";" << endl; 573 } 574 cur_indent -= CodeGenerator2::tabsize; 575 } 576 577 void CodeGenerator2::visit(BranchStmt *branchStmt){ 578 switch(branchStmt->get_type()){ 579 case BranchStmt::Goto: 580 if( ! branchStmt->get_target().empty() ) 581 before << "goto " << branchStmt->get_target(); 582 else { 583 if ( branchStmt->get_computedTarget() != 0 ) { 584 before << "goto *"; 585 branchStmt->get_computedTarget()->accept(*this); 490 cur_indent -= CodeGenerator2::tabsize; 491 492 before << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl ; 493 } 494 495 void CodeGenerator2::visit( CaseStmt *caseStmt ) { 496 before << string( cur_indent, ' ' ); 497 if ( caseStmt->isDefault()) 498 before << "default " ; 499 else { 500 before << "case " ; 501 caseStmt->get_condition()->accept(*this ); 502 } // if 503 after += ":\n"; 504 shift_left(); 505 506 std::list<Statement *> sts = caseStmt->get_statements(); 507 508 cur_indent += CodeGenerator2::tabsize; 509 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 510 before << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 511 (*i)->accept(*this ); 512 shift_left(); 513 before << ";" << endl; 586 514 } 587 } 588 break; 589 case BranchStmt::Break: 590 before << "break"; 591 break; 592 case BranchStmt::Continue: 593 before << "continue"; 594 break; 595 } 596 before << ";"; 597 } 598 599 600 void CodeGenerator2::visit(ReturnStmt *returnStmt){ 601 before << "return "; 602 603 // xxx -- check for null expression; 604 if ( returnStmt->get_expr() ){ 605 returnStmt->get_expr()->accept( *this ); 606 } 607 after << ";"; 608 } 609 610 void CodeGenerator2::visit(WhileStmt *whileStmt){ 611 if( whileStmt->get_isDoWhile() ) 612 before << "do" ; 613 else { 614 before << "while(" ; 615 whileStmt->get_condition()->accept(*this); 616 after << ")"; 617 } 618 after << "{" << endl ; 619 shift_left(); 620 621 whileStmt->get_body()->accept( *this ); 622 623 before << /* "\r" << */ string(cur_indent, ' ') << "}" ; 624 625 if( whileStmt->get_isDoWhile() ){ 626 before << " while(" ; 627 whileStmt->get_condition()->accept(*this); 628 after << ");"; 629 } 630 631 after << endl; 632 } 633 634 void CodeGenerator2::visit(ForStmt *forStmt){ 635 before << "for ("; 636 637 if( forStmt->get_initialization() != 0 ) 638 forStmt->get_initialization()->accept( *this ); 639 else 640 before << ";"; 641 shift_left(); 642 643 if( forStmt->get_condition() != 0 ) 644 forStmt->get_condition()->accept( *this ); 645 shift_left(); before << ";"; 646 647 if( forStmt->get_increment() != 0 ) 648 forStmt->get_increment()->accept( *this ); 649 shift_left(); before << ")" << endl; 650 651 if( forStmt->get_body() != 0 ){ 652 cur_indent += CodeGenerator2::tabsize; 653 before << string(cur_indent, ' ') << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() ); 654 forStmt->get_body()->accept( *this ); 655 cur_indent -= CodeGenerator2::tabsize; 656 } 657 } 658 659 void CodeGenerator2::visit(NullStmt *nullStmt){ 660 //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( nullStmt->get_labels() ); 661 before << "/* null statement */ ;"; 662 } 663 664 void CodeGenerator2::visit(DeclStmt *declStmt){ 665 declStmt->get_decl()->accept( *this ); 515 cur_indent -= CodeGenerator2::tabsize; 516 } 517 518 void CodeGenerator2::visit( BranchStmt *branchStmt ) { 519 switch ( branchStmt->get_type()) { 520 case BranchStmt::Goto: 521 if ( ! branchStmt->get_target().empty() ) 522 before << "goto " << branchStmt->get_target(); 523 else { 524 if ( branchStmt->get_computedTarget() != 0 ) { 525 before << "goto *"; 526 branchStmt->get_computedTarget()->accept(*this ); 527 } // if 528 } // if 529 break; 530 case BranchStmt::Break: 531 before << "break"; 532 break; 533 case BranchStmt::Continue: 534 before << "continue"; 535 break; 536 } 537 before << ";"; 538 } 539 540 541 void CodeGenerator2::visit( ReturnStmt *returnStmt ) { 542 before << "return "; 543 544 // xxx -- check for null expression; 545 if ( returnStmt->get_expr() ) { 546 returnStmt->get_expr()->accept( *this ); 547 } // if 548 after += ";"; 549 } 550 551 void CodeGenerator2::visit( WhileStmt *whileStmt ) { 552 if ( whileStmt->get_isDoWhile() ) 553 before << "do" ; 554 else { 555 before << "while(" ; 556 whileStmt->get_condition()->accept(*this ); 557 after += ")"; 558 } // if 559 after += "{\n"; 560 shift_left(); 561 562 whileStmt->get_body()->accept( *this ); 563 564 before << /* "\r" << */ string( cur_indent, ' ' ) << "}" ; 565 566 if ( whileStmt->get_isDoWhile() ) { 567 before << " while(" ; 568 whileStmt->get_condition()->accept(*this ); 569 after += ");"; 570 } // if 571 572 after += "\n"; 573 } 574 575 void CodeGenerator2::visit( ForStmt *forStmt ) { 576 before << "for ("; 577 578 if ( forStmt->get_initialization() != 0 ) 579 forStmt->get_initialization()->accept( *this ); 580 else 581 before << ";"; 582 shift_left(); 583 584 if ( forStmt->get_condition() != 0 ) 585 forStmt->get_condition()->accept( *this ); 586 shift_left(); before << ";"; 587 588 if ( forStmt->get_increment() != 0 ) 589 forStmt->get_increment()->accept( *this ); 590 shift_left(); before << ")" << endl; 591 592 if ( forStmt->get_body() != 0 ) { 593 cur_indent += CodeGenerator2::tabsize; 594 before << string( cur_indent, ' ' ) << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() ); 595 forStmt->get_body()->accept( *this ); 596 cur_indent -= CodeGenerator2::tabsize; 597 } // if 598 } 599 600 void CodeGenerator2::visit( NullStmt *nullStmt ) { 601 //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( nullStmt->get_labels() ); 602 before << "/* null statement */ ;"; 603 } 604 605 void CodeGenerator2::visit( DeclStmt *declStmt ) { 606 declStmt->get_decl()->accept( *this ); 666 607 667 if( doSemicolon( declStmt->get_decl() ) ) { 668 after << ";"; 669 } 670 shift_left(); 671 } 672 673 std::string CodeGenerator2::printLabels ( std::list< Label > &l ) { 674 std::string str(""); 675 l.unique(); 676 677 for( std::list< Label >::iterator i = l.begin(); i != l.end(); i++) 678 str += *i + ": "; 679 680 return str; 681 } 682 683 void CodeGenerator2::shift_left(){ 684 before << string(after.str(), after.pcount()); 685 686 after.freeze( false ); 687 after.seekp(0); 688 } 689 690 void 691 CodeGenerator2::handleStorageClass( Declaration *decl ) 692 { 693 switch( decl->get_storageClass() ) { 694 case Declaration::NoStorageClass: 695 break; 696 case Declaration::Auto: 697 break; 698 case Declaration::Static: 699 before << "static "; 700 break; 701 case Declaration::Extern: 702 before << "extern "; 703 break; 704 case Declaration::Register: 705 before << "register "; 706 break; 707 case Declaration::Fortran: 708 before << "fortran "; 709 break; 710 } 711 } 712 608 if ( doSemicolon( declStmt->get_decl() ) ) { 609 after += ";"; 610 } // if 611 shift_left(); 612 } 613 614 std::string CodeGenerator2::printLabels( std::list< Label > &l ) { 615 std::string str( "" ); 616 l.unique(); 617 618 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ ) 619 str += *i + ": "; 620 621 return str; 622 } 623 624 void CodeGenerator2::shift_left() { 625 before << after; 626 after = ""; 627 } 628 629 void CodeGenerator2::handleStorageClass( Declaration *decl ) { 630 switch ( decl->get_storageClass() ) { 631 case Declaration::NoStorageClass: 632 break; 633 case Declaration::Auto: 634 break; 635 case Declaration::Static: 636 before << "static "; 637 break; 638 case Declaration::Extern: 639 before << "extern "; 640 break; 641 case Declaration::Register: 642 before << "register "; 643 break; 644 case Declaration::Fortran: 645 before << "fortran "; 646 break; 647 } 648 } 713 649 } // namespace CodeGen 714 715 -
translator/CodeGen/CodeGenerator2.h
rd9a0e76 r17cd4eb 10 10 11 11 namespace CodeGen { 12 class CodeGenerator2 : public Visitor { 13 public: 14 static int tabsize; 12 15 13 class CodeGenerator2 : public Visitor 14 { 15 public: 16 static int tabsize; 16 CodeGenerator2( std::ostream &os ); 17 CodeGenerator2( std::ostream &os, std::string, int indent = 0, bool infun = false ); 18 CodeGenerator2( std::ostream &os, char *, int indent = 0, bool infun = false ); 17 19 18 CodeGenerator2( std::ostream &os ); 19 CodeGenerator2(std::ostream &os, std::string, int indent = 0, bool infun = false ); 20 CodeGenerator2(std::ostream &os, char *, int indent = 0, bool infun = false ); 20 CodeGenerator2( CodeGenerator2 & ); 21 21 22 CodeGenerator2( CodeGenerator2 & ); 22 //*** Declaration 23 virtual void visit( StructDecl * ); 24 virtual void visit( FunctionDecl * ); 25 virtual void visit( ObjectDecl * ); 26 virtual void visit( UnionDecl *aggregateDecl ); 27 virtual void visit( EnumDecl *aggregateDecl ); 28 virtual void visit( ContextDecl *aggregateDecl ); 29 virtual void visit( TypedefDecl *typeDecl ); 30 virtual void visit( TypeDecl *typeDecl ); 23 31 24 virtual ~CodeGenerator2(); 32 //*** Initializer 33 virtual void visit( SingleInit * ); 34 virtual void visit( ListInit * ); 25 35 26 //*** Declaration 27 virtual void visit(StructDecl *); 28 virtual void visit(FunctionDecl *); 29 virtual void visit(ObjectDecl *); 30 virtual void visit(UnionDecl *aggregateDecl); 31 virtual void visit(EnumDecl *aggregateDecl); 32 virtual void visit(ContextDecl *aggregateDecl); 33 virtual void visit(TypedefDecl *typeDecl); 34 virtual void visit(TypeDecl *typeDecl); 36 //*** Constant 37 virtual void visit( Constant * ); 35 38 36 //*** Initializer 37 virtual void visit(SingleInit *); 38 virtual void visit(ListInit *); 39 //*** Expression 40 virtual void visit( ApplicationExpr *applicationExpr ); 41 virtual void visit( UntypedExpr *untypedExpr ); 42 virtual void visit( NameExpr *nameExpr ); 43 virtual void visit( AddressExpr *addressExpr ); 44 virtual void visit( CastExpr *castExpr ); 45 virtual void visit( UntypedMemberExpr *memberExpr ); 46 virtual void visit( MemberExpr *memberExpr ); 47 virtual void visit( VariableExpr *variableExpr ); 48 virtual void visit( ConstantExpr *constantExpr ); 49 virtual void visit( SizeofExpr *sizeofExpr ); 50 virtual void visit( LogicalExpr *logicalExpr ); 51 virtual void visit( ConditionalExpr *conditionalExpr ); 52 virtual void visit( CommaExpr *commaExpr ); 53 virtual void visit( TupleExpr *tupleExpr ); 54 virtual void visit( TypeExpr *typeExpr ); 39 55 40 //*** Constant 41 virtual void visit(Constant *); 56 //*** Statements 57 virtual void visit( CompoundStmt * ); 58 virtual void visit( ExprStmt * ); 59 virtual void visit( IfStmt * ); 60 virtual void visit( SwitchStmt * ); 61 virtual void visit( CaseStmt * ); 62 virtual void visit( BranchStmt * ); 63 virtual void visit( ReturnStmt * ); 64 virtual void visit( WhileStmt * ); 65 virtual void visit( ForStmt * ); 66 virtual void visit( NullStmt * ); 67 virtual void visit( DeclStmt * ); 42 68 43 //*** Expression 44 virtual void visit(ApplicationExpr *applicationExpr); 45 virtual void visit(UntypedExpr *untypedExpr); 46 virtual void visit(NameExpr *nameExpr); 47 virtual void visit(AddressExpr *addressExpr); 48 virtual void visit(CastExpr *castExpr); 49 virtual void visit(UntypedMemberExpr *memberExpr); 50 virtual void visit(MemberExpr *memberExpr); 51 virtual void visit(VariableExpr *variableExpr); 52 virtual void visit(ConstantExpr *constantExpr); 53 virtual void visit(SizeofExpr *sizeofExpr); 54 virtual void visit(LogicalExpr *logicalExpr); 55 virtual void visit(ConditionalExpr *conditionalExpr); 56 virtual void visit(CommaExpr *commaExpr); 57 virtual void visit(TupleExpr *tupleExpr); 58 virtual void visit(TypeExpr *typeExpr); 69 std::string get_string( void ); 70 void add_string_left( std::string s ) { before << s; } 71 void shift_left(); 72 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 73 private: 74 int cur_indent; 75 bool insideFunction; 76 std::ostream &before; 77 std::string after; 59 78 60 //*** Statements 61 virtual void visit(CompoundStmt *); 62 virtual void visit(ExprStmt *); 63 virtual void visit(IfStmt *); 64 virtual void visit(SwitchStmt *); 65 virtual void visit(CaseStmt *); 66 virtual void visit(BranchStmt *); 67 virtual void visit(ReturnStmt *); 68 virtual void visit(WhileStmt *); 69 virtual void visit(ForStmt *); 70 virtual void visit(NullStmt *); 71 virtual void visit(DeclStmt *); 72 73 std::string get_string(void); 74 void add_string_left(std::string s) { before << s; } 75 void shift_left(); 76 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 77 78 private: 79 int cur_indent; 80 bool insideFunction; 81 std::ostream &before; 82 std::ostrstream after; 83 84 static std::string printLabels ( std::list < Label > & ); 85 void handleStorageClass( Declaration *decl ); 86 void handleAggregate( AggregateDecl *aggDecl ); 87 void handleTypedef( NamedTypeDecl *namedType ); 79 static std::string printLabels ( std::list < Label > & ); 80 void handleStorageClass( Declaration *decl ); 81 void handleAggregate( AggregateDecl *aggDecl ); 82 void handleTypedef( NamedTypeDecl *namedType ); 88 83 89 84 }; 90 85 91 template< class Iterator > 92 void 93 CodeGenerator2::genCommaList( Iterator begin, Iterator end ) 94 { 95 if( begin == end ) return; 96 97 for( ;; ) { 98 (*begin++)->accept( *this ); 99 100 if( begin == end ) return; 101 102 before << ", "; 86 template< class Iterator > 87 void CodeGenerator2::genCommaList( Iterator begin, Iterator end ) { 88 if ( begin == end ) return; 89 90 for ( ;; ) { 91 (*begin++)->accept( *this ); 92 if ( begin == end ) return; 93 before << ", "; 94 } 103 95 } 104 }105 96 106 inline bool107 doSemicolon( Declaration* decl ) 108 { 109 if( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { 110 return !func->get_statements();97 inline bool doSemicolon( Declaration* decl ) { 98 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { 99 return !func->get_statements(); 100 } 101 return true; 111 102 } 112 return true;113 }114 115 103 } // namespace CodeGen 116 104 -
translator/CodeGen/GenType.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: GenType.cc,v 1.6 2005/08/29 20:14:12 rcbilson Exp $5 *6 */7 8 1 #include <strstream> 9 2 #include <cassert> … … 16 9 17 10 namespace CodeGen { 11 class GenType : public Visitor { 12 public: 13 GenType( const std::string &typeString ); 14 std::string get_typeString() const { return typeString; } 15 void set_typeString( const std::string &newValue ) { typeString = newValue; } 16 17 virtual void visit( FunctionType *funcType ); 18 virtual void visit( VoidType *voidType ); 19 virtual void visit( BasicType *basicType ); 20 virtual void visit( PointerType *pointerType ); 21 virtual void visit( ArrayType *arrayType ); 22 virtual void visit( StructInstType *structInst ); 23 virtual void visit( UnionInstType *unionInst ); 24 virtual void visit( EnumInstType *enumInst ); 25 virtual void visit( TypeInstType *typeInst ); 26 27 private: 28 void handleQualifiers( Type *type ); 29 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); 30 31 std::string typeString; 32 }; 18 33 19 class GenType : public Visitor 20 { 21 public: 22 GenType( const std::string &typeString ); 23 std::string get_typeString() const { return typeString; } 24 void set_typeString( const std::string &newValue ) { typeString = newValue; } 34 std::string genType( Type *type, const std::string &baseString ) { 35 GenType gt( baseString ); 36 type->accept( gt ); 37 return gt.get_typeString(); 38 } 39 40 GenType::GenType( const std::string &typeString ) : typeString( typeString ) {} 41 42 void GenType::visit( VoidType *voidType ) { 43 typeString = "void " + typeString; 44 handleQualifiers( voidType ); 45 } 46 47 void GenType::visit( BasicType *basicType ) { 48 BasicType::Kind kind = basicType->get_kind(); 49 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); 50 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; 51 handleQualifiers( basicType ); 52 } 53 54 void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { 55 std::ostrstream os; 56 if ( typeString != "" ) { 57 if ( typeString[ 0 ] == '*' ) { 58 os << "(" << typeString << ")"; 59 } else { 60 os << typeString; 61 } // if 62 } // if 63 os << "["; 64 65 if ( isStatic ) { 66 os << "static "; 67 } // if 68 if ( qualifiers.isConst ) { 69 os << "const "; 70 } // if 71 if ( qualifiers.isVolatile ) { 72 os << "volatile "; 73 } // if 74 if ( qualifiers.isRestrict ) { 75 os << "__restrict "; 76 } // if 77 if ( isVarLen ) { 78 os << "*"; 79 } // if 80 if ( dimension != 0 ) { 81 CodeGenerator2 cg( os ); 82 dimension->accept( cg ); 83 } // if 84 os << "]"; 85 86 typeString = std::string( os.str(), os.pcount() ); 25 87 26 virtual void visit( FunctionType *funcType ); 27 virtual void visit( VoidType *voidType ); 28 virtual void visit( BasicType *basicType ); 29 virtual void visit( PointerType *pointerType ); 30 virtual void visit( ArrayType *arrayType ); 31 virtual void visit( StructInstType *structInst ); 32 virtual void visit( UnionInstType *unionInst ); 33 virtual void visit( EnumInstType *enumInst ); 34 virtual void visit( TypeInstType *typeInst ); 88 base->accept( *this ); 89 } 90 91 void GenType::visit( PointerType *pointerType ) { 92 assert( pointerType->get_base() != 0); 93 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { 94 genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); 95 } else { 96 handleQualifiers( pointerType ); 97 if ( typeString[ 0 ] == '?' ) { 98 typeString = "* " + typeString; 99 } else { 100 typeString = "*" + typeString; 101 } // if 102 pointerType->get_base()->accept( *this ); 103 } // if 104 } 105 106 void GenType::visit( ArrayType *arrayType ){ 107 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); 108 } 109 110 void GenType::visit( FunctionType *funcType ) { 111 std::ostrstream os; 112 113 if ( typeString != "" ) { 114 if ( typeString[ 0 ] == '*' ) { 115 os << "(" << typeString << ")"; 116 } else { 117 os << typeString; 118 } // if 119 } // if 35 120 36 private: 37 void handleQualifiers( Type *type ); 38 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); 121 /************* parameters ***************/ 122 123 const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); 124 125 if ( pars.empty() ) { 126 if ( funcType->get_isVarArgs() ) { 127 os << "()"; 128 } else { 129 os << "(void)"; 130 } // if 131 } else { 132 CodeGenerator2 cg( os ); 133 os << "(" ; 134 135 cg.genCommaList( pars.begin(), pars.end() ); 136 137 if ( funcType->get_isVarArgs() ){ 138 os << ", ..."; 139 } // if 140 os << ")"; 141 } // if 39 142 40 std::string typeString; 41 }; 143 typeString = std::string( os.str(), os.pcount() ); 42 144 43 std::string 44 genType( Type *type, const std::string &baseString ) 45 { 46 GenType gt( baseString ); 47 type->accept( gt ); 48 return gt.get_typeString(); 49 } 145 if ( funcType->get_returnVals().size() == 0 ) { 146 typeString = "void " + typeString; 147 } else { 148 funcType->get_returnVals().front()->get_type()->accept( *this ); 149 } // if 150 } 50 151 51 GenType::GenType( const std::string &typeString ) 52 : typeString( typeString ) 53 { 54 }152 void GenType::visit( StructInstType *structInst ) { 153 typeString = "struct " + structInst->get_name() + " " + typeString; 154 handleQualifiers( structInst ); 155 } 55 156 56 void GenType::visit(VoidType *voidType){57 typeString = "void" + typeString;58 handleQualifiers( voidType);59 }157 void GenType::visit( UnionInstType *unionInst ) { 158 typeString = "union " + unionInst->get_name() + " " + typeString; 159 handleQualifiers( unionInst ); 160 } 60 161 61 void GenType::visit(BasicType *basicType) 62 { 63 std::string typeWords; 64 switch(basicType->get_kind()){ 65 case BasicType::Bool: 66 typeWords = "_Bool"; 67 break; 68 case BasicType::Char: 69 case BasicType::SignedChar: 70 typeWords = "char"; 71 break; 72 case BasicType::UnsignedChar: 73 typeWords = "unsigned char"; 74 break; 75 case BasicType::ShortSignedInt: 76 typeWords = "short"; 77 break; 78 case BasicType::ShortUnsignedInt: 79 typeWords = "short unsigned"; 80 break; 81 case BasicType::SignedInt: 82 typeWords = "int"; 83 break; 84 case BasicType::UnsignedInt: 85 typeWords = "unsigned int"; 86 break; 87 case BasicType::LongSignedInt: 88 typeWords = "long int"; 89 break; 90 case BasicType::LongUnsignedInt: 91 typeWords = "long unsigned int"; 92 break; 93 case BasicType::LongLongSignedInt: 94 typeWords = "long long int"; 95 break; 96 case BasicType::LongLongUnsignedInt: 97 typeWords = "long long unsigned int"; 98 break; 99 case BasicType::Float: 100 typeWords = "float"; 101 break; 102 case BasicType::Double: 103 typeWords = "double"; 104 break; 105 case BasicType::LongDouble: 106 typeWords = "long double"; 107 break; 108 case BasicType::FloatComplex: 109 typeWords = "float _Complex"; 110 break; 111 case BasicType::DoubleComplex: 112 typeWords = "double _Complex"; 113 break; 114 case BasicType::LongDoubleComplex: 115 typeWords = "long double _Complex"; 116 break; 117 case BasicType::FloatImaginary: 118 typeWords = "float _Imaginary"; 119 break; 120 case BasicType::DoubleImaginary: 121 typeWords = "double _Imaginary"; 122 break; 123 case BasicType::LongDoubleImaginary: 124 typeWords = "long double _Imaginary"; 125 break; 126 default: 127 assert( false ); 128 } 129 typeString = typeWords + " " + typeString; 130 handleQualifiers( basicType ); 131 } 162 void GenType::visit( EnumInstType *enumInst ) { 163 typeString = "enum " + enumInst->get_name() + " " + typeString; 164 handleQualifiers( enumInst ); 165 } 132 166 133 void 134 GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) 135 { 136 std::ostrstream os; 137 if( typeString != "" ) { 138 if( typeString[ 0 ] == '*' ) { 139 os << "(" << typeString << ")"; 140 } else { 141 os << typeString; 167 void GenType::visit( TypeInstType *typeInst ) { 168 typeString = typeInst->get_name() + " " + typeString; 169 handleQualifiers( typeInst ); 142 170 } 143 }144 os << "[";145 if( isStatic ) {146 os << "static ";147 }148 if( qualifiers.isConst ) {149 os << "const ";150 }151 if( qualifiers.isVolatile ) {152 os << "volatile ";153 }154 if( isVarLen ) {155 os << "*";156 }157 if( dimension != 0 ) {158 CodeGenerator2 cg( os );159 dimension->accept( cg );160 }161 os << "]";162 163 typeString = std::string( os.str(), os.pcount() );164 165 base->accept ( *this );166 }167 171 168 void GenType::visit(PointerType *pointerType) {169 assert(pointerType->get_base() != 0); 170 if( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { 171 genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); 172 } else{173 handleQualifiers( pointerType );174 if( typeString[ 0 ] == '?' ) { 175 typeString = "* " + typeString; 176 } else { 177 typeString = "*" + typeString; 172 void GenType::handleQualifiers( Type *type ) { 173 if ( type->get_isConst() ) { 174 typeString = "const " + typeString; 175 } // if 176 if ( type->get_isVolatile() ) { 177 typeString = "volatile " + typeString; 178 } // if 179 if ( type->get_isRestrict() ) { 180 typeString = "__restrict " + typeString; 181 } // if 178 182 } 179 pointerType->get_base()->accept ( *this );180 }181 }182 183 void GenType::visit(ArrayType *arrayType){184 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );185 }186 187 void GenType::visit(FunctionType *funcType) {188 std::ostrstream os;189 190 if( typeString != "" ) {191 if( typeString[ 0 ] == '*' ) {192 os << "(" << typeString << ")";193 } else {194 os << typeString;195 }196 }197 198 /************* parameters ***************/199 200 const std::list<DeclarationWithType*> &pars = funcType->get_parameters();201 202 if( pars.empty() ) {203 if( funcType->get_isVarArgs() ) {204 os << "()";205 } else {206 os << "(void)";207 }208 } else {209 CodeGenerator2 cg( os );210 os << "(" ;211 212 cg.genCommaList( pars.begin(), pars.end() );213 214 if( funcType->get_isVarArgs() ){215 os << ", ...";216 }217 os << ")";218 }219 220 typeString = std::string( os.str(), os.pcount() );221 222 if( funcType->get_returnVals().size() == 0 ) {223 typeString = "void " + typeString;224 } else {225 226 funcType->get_returnVals().front()->get_type()->accept( *this );227 228 }229 230 }231 232 void GenType::visit( StructInstType *structInst )233 {234 typeString = "struct " + structInst->get_name() + " " + typeString;235 handleQualifiers( structInst );236 }237 238 void239 GenType::visit( UnionInstType *unionInst )240 {241 typeString = "union " + unionInst->get_name() + " " + typeString;242 handleQualifiers( unionInst );243 }244 245 void246 GenType::visit( EnumInstType *enumInst )247 {248 typeString = "enum " + enumInst->get_name() + " " + typeString;249 handleQualifiers( enumInst );250 }251 252 void253 GenType::visit( TypeInstType *typeInst )254 {255 typeString = typeInst->get_name() + " " + typeString;256 handleQualifiers( typeInst );257 }258 259 void260 GenType::handleQualifiers( Type *type )261 {262 if( type->get_isConst() ) {263 typeString = "const " + typeString;264 }265 if( type->get_isVolatile() ) {266 typeString = "volatile " + typeString;267 }268 if( type->get_isRestrict() ) {269 typeString = "__restrict " + typeString;270 }271 }272 273 183 } // namespace CodeGen -
translator/CodeGen/GenType.h
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: GenType.h,v 1.2 2005/08/29 20:14:12 rcbilson Exp $5 *6 */7 8 1 #ifndef CODEGEN_GENTYPE_H 9 2 #define CODEGEN_GENTYPE_H … … 13 6 14 7 namespace CodeGen { 15 16 std::string genType( Type *type, const std::string &baseString ); 17 8 std::string genType( Type *type, const std::string &baseString ); 18 9 } // namespace CodeGen 19 10 20 #endif / * #ifndef CODEGEN_GENTYPE_H */11 #endif // CODEGEN_GENTYPE_H -
translator/CodeGen/Generate.cc
rd9a0e76 r17cd4eb 12 12 13 13 namespace CodeGen { 14 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) { 15 CodeGen::CodeGenerator2 cgv( os ); 14 16 15 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) { 16 17 CodeGen::CodeGenerator2 cgv( os ); 18 19 for(std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end(); i++) 20 { 21 if( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || !LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) { 22 (*i)->accept(cgv); 23 cgv.shift_left(); 24 if( doSemicolon( *i ) ) { 25 os << ";"; 26 } 27 os << std::endl; 28 } 29 } 30 31 } 32 17 for ( std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end(); i++ ) { 18 if ( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) { 19 (*i)->accept(cgv); 20 cgv.shift_left(); 21 if ( doSemicolon( *i ) ) { 22 os << ";"; 23 } // if 24 os << std::endl; 25 } // if 26 } // for 27 } 33 28 } // namespace CodeGen 34 35 -
translator/GenPoly/CopyParams.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: CopyParams.cc,v 1.3 2005/08/29 20:14:13 rcbilson Exp $5 *6 */7 8 1 #include <set> 9 2 #include <map> … … 19 12 20 13 namespace GenPoly { 14 class CopyParams : public Visitor { 15 public: 16 CopyParams(); 17 18 virtual void visit( FunctionDecl *funcDecl ); 19 virtual void visit( AddressExpr *addrExpr ); 21 20 22 class CopyParams : public Visitor 23 { 24 public: 25 CopyParams(); 26 27 virtual void visit( FunctionDecl *funcDecl ); 28 virtual void visit( AddressExpr *addrExpr ); 21 private: 22 std::set< UniqueId > modVars; 23 UniqueName namer; 24 }; 29 25 30 private: 31 std::set< UniqueId > modVars;32 UniqueName namer;33 }; 26 void copyParams( std::list< Declaration* > &translationUnit ) { 27 CopyParams copier; 28 acceptAll( translationUnit, copier ); 29 } 34 30 35 void 36 copyParams( std::list< Declaration* > &translationUnit ) 37 { 38 CopyParams copier; 39 acceptAll( translationUnit, copier ); 40 } 31 CopyParams::CopyParams() : namer( "_cp" ) {} 41 32 42 CopyParams::CopyParams() 43 : namer( "_cp" ) 44 { 45 } 33 static const std::list< Label > noLabels; 46 34 47 static const std::list< Label > noLabels; 35 void CopyParams::visit( FunctionDecl *funcDecl ) { 36 if ( funcDecl->get_statements() ) { 37 funcDecl->get_statements()->accept( *this ); 38 39 if ( ! modVars.empty() ) { 40 std::map< std::string, DeclarationWithType* > assignOps; 41 // assume the assignment operator is the first assert param after any "type" parameter 42 for ( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 43 if ( (*tyVar)->get_kind() == TypeDecl::Any ) { 44 assert( !(*tyVar)->get_assertions().empty() ); 45 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 46 } // if 47 } // for 48 for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 49 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 50 if ( var != modVars.end() ) { 51 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 52 assert( typeInst ); 53 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 54 if ( assignOp != assignOps.end() ) { 55 DeclarationWithType *oldParam = *param; 56 *param = (*param)->clone(); 57 (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); 58 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 59 assign->get_args().push_back( new VariableExpr( oldParam ) ); 60 assign->get_args().push_back( new VariableExpr( *param ) ); 61 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 62 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 63 } // if 64 modVars.erase( var ); 65 } // if 66 } // for 67 } // if 68 } // if 69 } 48 70 49 void 50 CopyParams::visit( FunctionDecl *funcDecl ) 51 { 52 if( funcDecl->get_statements() ) { 53 funcDecl->get_statements()->accept( *this ); 54 55 if( !modVars.empty() ) { 56 std::map< std::string, DeclarationWithType* > assignOps; 57 // assume that the assignment operator is the first assert param after any "type" parameter 58 for( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 59 if( (*tyVar)->get_kind() == TypeDecl::Any ) { 60 assert( !(*tyVar)->get_assertions().empty() ); 61 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 62 } 63 } 64 for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 65 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 66 if( var != modVars.end() ) { 67 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 68 assert( typeInst ); 69 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 70 if( assignOp != assignOps.end() ) { 71 DeclarationWithType *oldParam = *param; 72 *param = (*param)->clone(); 73 (*param)->set_name( namer.newName() ); 74 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 75 assign->get_args().push_back( new VariableExpr( oldParam ) ); 76 assign->get_args().push_back( new VariableExpr( *param ) ); 77 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 78 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 79 } 80 modVars.erase( var ); 81 } 82 } 71 // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic 72 // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to 73 // make sure that all of the correct cases are identified where copies are necessary. 74 // 75 // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy 76 // every parameter of TypeInstType* when visiting the FunctionDecl. 77 void CopyParams::visit( AddressExpr *addrExpr ) { 78 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) { 79 if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) { 80 modVars.insert( varExpr->get_var()->get_uniqueId() ); 81 } // if 82 } // if 83 83 } 84 }85 }86 87 void88 CopyParams::visit( AddressExpr *addrExpr )89 {90 if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {91 if( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {92 modVars.insert( varExpr->get_var()->get_uniqueId() );93 }94 }95 }96 97 84 } // namespace GenPoly -
translator/Parser/DeclarationNode.cc
rd9a0e76 r17cd4eb 19 19 /* these must remain in the same order as the corresponding DeclarationNode enumerations */ 20 20 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue" }; 21 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", " bool", "complex", "imaginary" };21 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" }; 22 22 const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" }; 23 23 const char *DeclarationNode::tyConName[] = { "struct", "union", "context" }; … … 799 799 } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) { 800 800 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 801 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );801 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 802 802 delete agg; 803 803 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) { 804 804 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 805 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );805 *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); 806 806 } 807 807 } -
translator/Parser/InitializerNode.cc
rd9a0e76 r17cd4eb 43 43 if ( designator != 0 ) { 44 44 os << "designated by: ("; 45 ExpressionNode 45 ExpressionNode *curdes = designator; 46 46 while( curdes != 0) { 47 47 curdes->printOneLine(os); 48 48 curdes = (ExpressionNode *)(curdes->get_link()); 49 49 if ( curdes ) os << ", "; 50 } 50 } // while 51 51 os << ")"; 52 } 52 } // if 53 53 if ( expr ) expr->printOneLine(os); 54 54 } else { // It's an aggregate … … 57 57 next_init()->printOneLine(os); 58 58 if (aggregate) os << "--]"; 59 } 59 } // if 60 60 61 61 InitializerNode *moreInit; … … 88 88 if ( get_expression() != 0) 89 89 return new SingleInit( get_expression()->build(), designators ); 90 } 90 } // if 91 91 92 92 return 0; -
translator/Parser/ParseNode.h
rd9a0e76 r17cd4eb 456 456 ExpressionNode *get_expression() const { return expr; } 457 457 458 InitializerNode *set_designators( ExpressionNode *des ) { designator = des; 458 InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; } 459 459 ExpressionNode *get_designators() const { return designator; } 460 460 -
translator/Parser/TypeData.cc
rd9a0e76 r17cd4eb 560 560 if ( *i == DeclarationNode::Void ) { 561 561 if ( basic->typeSpec.size() != 1 || !basic->modifiers.empty() ) { 562 throw SemanticError( "invalid type specifier \"void\" in type ", this );562 throw SemanticError( "invalid type specifier \"void\" in type: ", this ); 563 563 } else { 564 564 return new VoidType( buildQualifiers() ); … … 571 571 case DeclarationNode::Float: 572 572 if ( sawDouble ) { 573 throw SemanticError( "invalid type specifier \"float\" in type ", this );573 throw SemanticError( "invalid type specifier \"float\" in type: ", this ); 574 574 } else { 575 575 switch ( ret ) { … … 581 581 break; 582 582 default: 583 throw SemanticError( "invalid type specifier \"float\" in type ", this );583 throw SemanticError( "invalid type specifier \"float\" in type: ", this ); 584 584 } 585 585 } … … 587 587 case DeclarationNode::Double: 588 588 if ( sawDouble ) { 589 throw SemanticError( "duplicate type specifier \"double\" in type ", this );589 throw SemanticError( "duplicate type specifier \"double\" in type: ", this ); 590 590 } else { 591 591 switch ( ret ) { … … 594 594 break; 595 595 default: 596 throw SemanticError( "invalid type specifier \"double\" in type ", this );596 throw SemanticError( "invalid type specifier \"double\" in type: ", this ); 597 597 } 598 598 } … … 609 609 break; 610 610 default: 611 throw SemanticError( "invalid type specifier \" complex\" in type", this );611 throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this ); 612 612 } 613 613 break; … … 623 623 break; 624 624 default: 625 throw SemanticError( "invalid type specifier \" imaginary\" in type", this );625 throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this ); 626 626 } 627 627 break; 628 628 629 629 default: 630 throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type ", this );630 throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this ); 631 631 } 632 632 } … … 666 666 break; 667 667 default: 668 throw SemanticError( "invalid type modifier \"long\" in type ", this );668 throw SemanticError( "invalid type modifier \"long\" in type: ", this ); 669 669 } 670 670 } … … 683 683 break; 684 684 default: 685 throw SemanticError( "invalid type modifier \"short\" in type ", this );685 throw SemanticError( "invalid type modifier \"short\" in type: ", this ); 686 686 } 687 687 } … … 692 692 ret = BasicType::SignedInt; 693 693 } else if ( sawSigned ) { 694 throw SemanticError( "duplicate type modifer \"signed\" in type ", this );694 throw SemanticError( "duplicate type modifer \"signed\" in type: ", this ); 695 695 } else { 696 696 switch ( ret ) { … … 702 702 break; 703 703 default: 704 throw SemanticError( "invalid type modifer \"signed\" in type ", this );704 throw SemanticError( "invalid type modifer \"signed\" in type: ", this ); 705 705 } 706 706 } … … 711 711 ret = BasicType::UnsignedInt; 712 712 } else if ( sawSigned ) { 713 throw SemanticError( "invalid type modifer \"unsigned\" in type ", this );713 throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this ); 714 714 } else { 715 715 switch ( ret ) { … … 727 727 break; 728 728 default: 729 throw SemanticError( "invalid type modifer \"unsigned\" in type ", this );729 throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this ); 730 730 } 731 731 } -
translator/Parser/cfa.y
rd9a0e76 r17cd4eb 10 10 * Created On : Sat Sep 1 20:22:55 2001 11 11 * Last Modified By : Peter A. Buhr 12 * Last Modified On : Tue Nov 25 23:52:54 201413 * Update Count : 89012 * Last Modified On : Wed Jan 7 09:19:57 2015 13 * Update Count : 906 14 14 */ 15 15 … … 294 294 /* ENUMERATIONconstant is not included here; it is treated as a variable with type 295 295 "enumeration constant". */ 296 INTEGERconstant { $$ = new ConstantNode(ConstantNode::Integer, $1); }297 | FLOATINGconstant { $$ = new ConstantNode(ConstantNode::Float, $1); }298 | CHARACTERconstant { $$ = new ConstantNode(ConstantNode::Character, $1); }296 INTEGERconstant { $$ = new ConstantNode(ConstantNode::Integer, $1); } 297 | FLOATINGconstant { $$ = new ConstantNode(ConstantNode::Float, $1); } 298 | CHARACTERconstant { $$ = new ConstantNode(ConstantNode::Character, $1); } 299 299 ; 300 300 … … 320 320 321 321 string_literal_list: /* juxtaposed strings are concatenated */ 322 STRINGliteral { $$ = new ConstantNode(ConstantNode::String, $1); }323 | string_literal_list STRINGliteral { $$ = $1->append( $2 ); }322 STRINGliteral { $$ = new ConstantNode(ConstantNode::String, $1); } 323 | string_literal_list STRINGliteral { $$ = $1->append( $2 ); } 324 324 ; 325 325 … … 352 352 { $$ = new CompositeExprNode($1, $3); } 353 353 | postfix_expression '.' no_attr_identifier 354 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), $1, 355 new VarRefNode($3)); } 354 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), $1, new VarRefNode($3)); } 356 355 | postfix_expression '.' '[' push field_list pop ']' /* CFA, tuple field selector */ 357 356 | postfix_expression ARROW no_attr_identifier 358 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), $1, 359 new VarRefNode($3)); } 357 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), $1, new VarRefNode($3)); } 360 358 | postfix_expression ARROW '[' push field_list pop ']' /* CFA, tuple field selector */ 361 359 | postfix_expression ICR … … 371 369 argument_expression 372 370 | argument_expression_list ',' argument_expression 373 { $$ = (ExpressionNode *)($1->set_link($3)); }371 { $$ = (ExpressionNode *)($1->set_link($3)); } 374 372 ; 375 373 … … 379 377 | assignment_expression 380 378 | no_attr_identifier ':' assignment_expression 381 379 { $$ = $3->set_asArgName($1); } 382 380 /* Only a list of no_attr_identifier_or_typedef_name is allowed in this context. However, there 383 381 is insufficient look ahead to distinguish between this list of parameter names and a tuple, … … 391 389 field_list: /* CFA, tuple field selector */ 392 390 field 393 | field_list ',' field 391 | field_list ',' field { $$ = (ExpressionNode *)$1->set_link( $3 ); } 394 392 ; 395 393 396 394 field: /* CFA, tuple field selector */ 397 395 no_attr_identifier 398 { $$ = new VarRefNode( $1 ); }396 { $$ = new VarRefNode( $1 ); } 399 397 | no_attr_identifier '.' field 400 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $3); }398 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $3); } 401 399 | no_attr_identifier '.' '[' push field_list pop ']' 402 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $5); }400 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $5); } 403 401 | no_attr_identifier ARROW field 404 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $3); }402 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $3); } 405 403 | no_attr_identifier ARROW '[' push field_list pop ']' 406 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $5); }404 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $5); } 407 405 ; 408 406 … … 440 438 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::AlignOf), new TypeValueNode($3)); } 441 439 | ANDAND no_attr_identifier /* GCC, address of label */ 442 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LabelAddress), 443 new VarRefNode($2, true)); } 440 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LabelAddress), new VarRefNode($2, true)); } 444 441 ; 445 442 446 443 unary_operator: 447 '&' 448 | '+' 449 | '-' 450 | '~' 444 '&' { $$ = new OperatorNode(OperatorNode::AddressOf); } 445 | '+' { $$ = new OperatorNode(OperatorNode::UnPlus); } 446 | '-' { $$ = new OperatorNode(OperatorNode::UnMinus); } 447 | '~' { $$ = new OperatorNode(OperatorNode::BitNeg); } 451 448 ; 452 449 … … 454 451 unary_expression 455 452 | '(' type_name_no_function ')' cast_expression 456 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), 457 new TypeValueNode($2), $4); } 453 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); } 458 454 | '(' type_name_no_function ')' tuple 459 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), 460 new TypeValueNode($2), $4); } 455 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); } 461 456 ; 462 457 … … 464 459 cast_expression 465 460 | multiplicative_expression '*' cast_expression 466 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mul),$1,$3); }461 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mul),$1,$3); } 467 462 | multiplicative_expression '/' cast_expression 468 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Div),$1,$3); }463 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Div),$1,$3); } 469 464 | multiplicative_expression '%' cast_expression 470 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mod),$1,$3); }465 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mod),$1,$3); } 471 466 ; 472 467 … … 474 469 multiplicative_expression 475 470 | additive_expression '+' multiplicative_expression 476 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Plus),$1,$3); }471 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Plus),$1,$3); } 477 472 | additive_expression '-' multiplicative_expression 478 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Minus),$1,$3); }473 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Minus),$1,$3); } 479 474 ; 480 475 … … 482 477 additive_expression 483 478 | shift_expression LS additive_expression 484 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LShift),$1,$3); }479 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LShift),$1,$3); } 485 480 | shift_expression RS additive_expression 486 481 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::RShift),$1,$3); } … … 558 553 conditional_expression 559 554 | unary_expression '=' assignment_expression 560 { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::Assign), $1, $3); }555 { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::Assign), $1, $3); } 561 556 | unary_expression assignment_operator assignment_expression 562 { $$ =new CompositeExprNode($2, $1, $3); }557 { $$ =new CompositeExprNode($2, $1, $3); } 563 558 | tuple assignment_opt /* CFA, tuple expression */ 564 559 { … … 1209 1204 typedefTable.enterScope(); 1210 1205 } 1211 type_parameter_list ')' /* CFA */1206 type_parameter_list ')' /* CFA */ 1212 1207 { 1213 1208 typedefTable.leaveScope(); … … 1630 1625 1631 1626 initializer_opt: 1632 /* empty */ { $$ = 0; }1633 | '=' initializer { $$ = $2; }1627 /* empty */ { $$ = 0; } 1628 | '=' initializer { $$ = $2; } 1634 1629 ; 1635 1630 1636 1631 initializer: 1637 assignment_expression { $$ = new InitializerNode($1); }1638 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode($2, true); }1632 assignment_expression { $$ = new InitializerNode($1); } 1633 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode($2, true); } 1639 1634 ; 1640 1635 1641 1636 initializer_list: 1642 1637 initializer 1643 | designation initializer 1644 | initializer_list ',' initializer 1638 | designation initializer { $$ = $2->set_designators( $1 ); } 1639 | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_link($3) ); } 1645 1640 | initializer_list ',' designation initializer 1646 1641 { $$ = (InitializerNode *)( $1->set_link( $4->set_designators($3) ) ); } 1647 1642 ; 1648 1643 … … 1659 1654 designation: 1660 1655 designator_list ':' /* ANSI99, CFA uses ":" instead of "=" */ 1661 | no_attr_identifier_or_typedef_name ':' 1662 1656 | no_attr_identifier_or_typedef_name ':' /* GCC, field name */ 1657 { $$ = new VarRefNode( $1 ); } 1663 1658 ; 1664 1659 1665 1660 designator_list: /* ANSI99 */ 1666 1661 designator 1667 | designator_list designator 1662 | designator_list designator { $$ = (ExpressionNode *)($1->set_link( $2 )); } 1668 1663 ; 1669 1664 1670 1665 designator: 1671 1666 '.' no_attr_identifier_or_typedef_name /* ANSI99, field name */ 1672 1667 { $$ = new VarRefNode( $2 ); } 1673 1668 | '[' push assignment_expression pop ']' /* ANSI99, single array element */ 1674 1669 /* assignment_expression used instead of constant_expression because of shift/reduce conflicts 1675 1670 with tuple. */ 1676 1671 { $$ = $3; } 1677 1672 | '[' push subrange pop ']' /* CFA, multiple array elements */ 1678 1673 { $$ = $3; } 1679 1674 | '[' push constant_expression ELLIPSIS constant_expression pop ']' /* GCC, multiple array elements */ 1680 1675 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range), $3, $5); } 1681 1676 | '.' '[' push field_list pop ']' /* CFA, tuple field selector */ 1682 1677 { $$ = $4; } 1683 1678 ; 1684 1679 … … 2492 2487 ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall 2493 2488 appear only in a declaration of a function parameter with an array type, and then only in the 2494 outermost array type derivation." 2495 */ 2489 outermost array type derivation." */ 2496 2490 2497 2491 array_parameter_1st_dimension: 2498 2492 '[' push pop ']' 2499 2493 { $$ = DeclarationNode::newArray( 0, 0, false ); } 2494 // multi_array_dimension handles the '[' '*' ']' case 2500 2495 | '[' push type_qualifier_list '*' pop ']' /* remaining ANSI99 */ 2501 2496 { $$ = DeclarationNode::newVarArray( $3 ); } 2497 | '[' push type_qualifier_list pop ']' 2498 { $$ = DeclarationNode::newArray( 0, $3, false ); } 2499 // multi_array_dimension handles the '[' assignment_expression ']' case 2502 2500 | '[' push type_qualifier_list assignment_expression pop ']' 2503 2501 { $$ = DeclarationNode::newArray( $4, $3, false ); } 2504 | '[' push STATIC assignment_expression pop ']' 2505 { $$ = DeclarationNode::newArray( $4, 0, true ); } 2506 | '[' push STATIC type_qualifier_list assignment_expression pop ']' 2502 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']' 2507 2503 { $$ = DeclarationNode::newArray( $5, $4, true ); } 2504 | '[' push type_qualifier_list STATIC assignment_expression pop ']' 2505 { $$ = DeclarationNode::newArray( $5, $3, true ); } 2508 2506 ; 2509 2507 -
translator/ResolvExpr/Resolver.cc
rd9a0e76 r17cd4eb 47 47 (*i)->print( std::cerr ); 48 48 (*i)->accept( resolver ); 49 } 49 } // for 50 50 #endif 51 51 } … … 81 81 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { 82 82 i->print( std::cout ); 83 } 84 } 83 } // for 84 } // if 85 85 #endif 86 86 assert( finder.get_alternatives().size() == 1 ); … … 98 98 } else { 99 99 return true; 100 } 100 } // if 101 101 } 102 102 … … 112 112 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { 113 113 i->print( std::cout ); 114 } 115 } 114 } // for 115 } // if 116 116 #endif 117 117 Expression *newExpr = 0; … … 124 124 newExpr = i->expr->clone(); 125 125 newEnv = &i->env; 126 } 127 } 128 } 126 } // if 127 } // if 128 } // for 129 129 if ( !newExpr ) { 130 130 throw SemanticError( "Too many interpretations for switch control expression", untyped ); 131 } 131 } // if 132 132 finishExpr( newExpr, *newEnv ); 133 133 return newExpr; … … 147 147 Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); 148 148 typeDecl->set_base( new_type ); 149 } 149 } // if 150 150 SymTab::Indexer::visit( typeDecl ); 151 151 } … … 163 163 for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { 164 164 functionReturn.push_back( (*i)->get_type() ); 165 } 165 } // for 166 166 SymTab::Indexer::visit( functionDecl ); 167 167 functionReturn = oldFunctionReturn; … … 173 173 delete exprStmt->get_expr(); 174 174 exprStmt->set_expr( newExpr ); 175 } 175 } // if 176 176 } 177 177 … … 196 196 delete forStmt->get_condition(); 197 197 forStmt->set_condition( newExpr ); 198 } 198 } // if 199 199 200 200 if ( forStmt->get_increment() ) { … … 202 202 delete forStmt->get_increment(); 203 203 forStmt->set_increment( newExpr ); 204 } 204 } // if 205 205 206 206 Visitor::visit( forStmt ); … … 236 236 delete castExpr; 237 237 returnStmt->set_expr( newExpr ); 238 } 238 } // if 239 239 } 240 240 241 241 void Resolver::visit( SingleInit *singleInit ) { 242 //if ( singleInit->get_value() ) {243 //CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );244 //Expression *newExpr = findSingleExpression( castExpr, *this );245 //delete castExpr;246 //singleInit->set_value( newExpr );247 // }248 //singleInit->get_value()->accept( *this );242 if ( singleInit->get_value() ) { 243 CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); 244 Expression *newExpr = findSingleExpression( castExpr, *this ); 245 delete castExpr; 246 singleInit->set_value( newExpr ); 247 } // if 248 singleInit->get_value()->accept( *this ); 249 249 } 250 250 -
translator/SymTab/Indexer.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: Indexer.cc,v 1.12 2005/08/29 20:14:17 rcbilson Exp $5 *6 */7 8 1 #include "SynTree/Declaration.h" 9 2 #include "SynTree/Type.h" … … 15 8 #include "utility.h" 16 9 17 #define debugPrint(x) if ( doDebug ) { std::cout << x; }10 #define debugPrint(x) if ( doDebug ) { std::cout << x; } 18 11 19 12 namespace SymTab { 20 21 Indexer::Indexer( bool useDebug ) 22 : doDebug( useDebug ) 23 { 24 } 25 26 Indexer::~Indexer() 27 { 28 } 29 30 void 31 Indexer::visit( ObjectDecl *objectDecl ) 32 { 33 maybeAccept( objectDecl->get_type(), *this ); 34 maybeAccept( objectDecl->get_init(), *this ); 35 maybeAccept( objectDecl->get_bitfieldWidth(), *this ); 36 if( objectDecl->get_name() != "" ) { 37 debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); 38 idTable.addDecl( objectDecl ); 39 } 40 } 41 42 void 43 Indexer::visit( FunctionDecl *functionDecl ) 44 { 45 if( functionDecl->get_name() == "" ) return; 46 debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); 47 idTable.addDecl( functionDecl ); 48 enterScope(); 49 maybeAccept( functionDecl->get_functionType(), *this ); 50 acceptAll( functionDecl->get_oldDecls(), *this ); 51 maybeAccept( functionDecl->get_statements(), *this ); 52 leaveScope(); 53 } 13 Indexer::Indexer( bool useDebug ) : doDebug( useDebug ) {} 14 15 Indexer::~Indexer() {} 16 17 void Indexer::visit( ObjectDecl *objectDecl ) { 18 maybeAccept( objectDecl->get_type(), *this ); 19 maybeAccept( objectDecl->get_init(), *this ); 20 maybeAccept( objectDecl->get_bitfieldWidth(), *this ); 21 if ( objectDecl->get_name() != "" ) { 22 debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); 23 idTable.addDecl( objectDecl ); 24 } 25 } 26 27 void Indexer::visit( FunctionDecl *functionDecl ) { 28 if ( functionDecl->get_name() == "" ) return; 29 debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); 30 idTable.addDecl( functionDecl ); 31 enterScope(); 32 maybeAccept( functionDecl->get_functionType(), *this ); 33 acceptAll( functionDecl->get_oldDecls(), *this ); 34 maybeAccept( functionDecl->get_statements(), *this ); 35 leaveScope(); 36 } 54 37 55 38 /******** … … 72 55 */ 73 56 74 void 75 Indexer::visit( TypeDecl *typeDecl ) 76 { 77 // see A NOTE ON THE ORDER OF TRAVERSAL, above 78 // note that assertions come after the type is added to the symtab, since they aren't part 79 // of the type proper and may depend on the type itself 80 enterScope(); 81 acceptAll( typeDecl->get_parameters(), *this ); 82 maybeAccept( typeDecl->get_base(), *this ); 83 leaveScope(); 84 debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); 85 typeTable.add( typeDecl ); 86 acceptAll( typeDecl->get_assertions(), *this ); 87 } 88 89 void 90 Indexer::visit( TypedefDecl *typeDecl ) 91 { 92 enterScope(); 93 acceptAll( typeDecl->get_parameters(), *this ); 94 maybeAccept( typeDecl->get_base(), *this ); 95 leaveScope(); 96 debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); 97 typeTable.add( typeDecl ); 98 } 99 100 void 101 Indexer::visit( StructDecl *aggregateDecl ) 102 { 103 // make up a forward declaration and add it before processing the members 104 StructDecl fwdDecl( aggregateDecl->get_name() ); 105 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 106 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); 107 structTable.add( &fwdDecl ); 108 109 enterScope(); 110 acceptAll( aggregateDecl->get_parameters(), *this ); 111 acceptAll( aggregateDecl->get_members(), *this ); 112 leaveScope(); 113 114 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); 115 // this addition replaces the forward declaration 116 structTable.add( aggregateDecl ); 117 } 118 119 void 120 Indexer::visit( UnionDecl *aggregateDecl ) 121 { 122 // make up a forward declaration and add it before processing the members 123 UnionDecl fwdDecl( aggregateDecl->get_name() ); 124 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 125 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); 126 unionTable.add( &fwdDecl ); 127 128 enterScope(); 129 acceptAll( aggregateDecl->get_parameters(), *this ); 130 acceptAll( aggregateDecl->get_members(), *this ); 131 leaveScope(); 132 133 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); 134 unionTable.add( aggregateDecl ); 135 } 136 137 void 138 Indexer::visit( EnumDecl *aggregateDecl ) 139 { 140 debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); 141 enumTable.add( aggregateDecl ); 142 // unlike structs, contexts, and unions, enums inject their members into the 143 // global scope 144 acceptAll( aggregateDecl->get_members(), *this ); 145 } 146 147 void 148 Indexer::visit( ContextDecl *aggregateDecl ) 149 { 150 enterScope(); 151 acceptAll( aggregateDecl->get_parameters(), *this ); 152 acceptAll( aggregateDecl->get_members(), *this ); 153 leaveScope(); 154 155 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); 156 contextTable.add( aggregateDecl ); 157 } 158 159 void 160 Indexer::visit( CompoundStmt *compoundStmt ) 161 { 162 enterScope(); 163 acceptAll( compoundStmt->get_kids(), *this ); 164 leaveScope(); 165 } 166 167 void 168 Indexer::visit( ContextInstType *contextInst ) 169 { 170 acceptAll( contextInst->get_parameters(), *this ); 171 acceptAll( contextInst->get_members(), *this ); 172 } 173 174 void 175 Indexer::visit( StructInstType *structInst ) 176 { 177 if( !structTable.lookup( structInst->get_name() ) ) { 178 debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); 179 structTable.add( structInst->get_name() ); 180 } 181 enterScope(); 182 acceptAll( structInst->get_parameters(), *this ); 183 leaveScope(); 184 } 185 186 void 187 Indexer::visit( UnionInstType *unionInst ) 188 { 189 if( !unionTable.lookup( unionInst->get_name() ) ) { 190 debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); 191 unionTable.add( unionInst->get_name() ); 192 } 193 enterScope(); 194 acceptAll( unionInst->get_parameters(), *this ); 195 leaveScope(); 196 } 197 198 void 199 Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const 200 { 201 idTable.lookupId( id, list ); 202 } 203 204 NamedTypeDecl * 205 Indexer::lookupType( const std::string &id ) const 206 { 207 return typeTable.lookup( id ); 208 } 209 210 StructDecl * 211 Indexer::lookupStruct( const std::string &id ) const 212 { 213 return structTable.lookup( id ); 214 } 215 216 EnumDecl * 217 Indexer::lookupEnum( const std::string &id ) const 218 { 219 return enumTable.lookup( id ); 220 } 221 222 UnionDecl * 223 Indexer::lookupUnion( const std::string &id ) const 224 { 225 return unionTable.lookup( id ); 226 } 227 228 ContextDecl * 229 Indexer::lookupContext( const std::string &id ) const 230 { 231 return contextTable.lookup( id ); 232 } 233 234 void 235 Indexer::enterScope() 236 { 237 if( doDebug ) { 238 std::cout << "--- Entering scope" << std::endl; 239 } 240 idTable.enterScope(); 241 typeTable.enterScope(); 242 structTable.enterScope(); 243 enumTable.enterScope(); 244 unionTable.enterScope(); 245 contextTable.enterScope(); 246 } 247 248 void 249 Indexer::leaveScope() 250 { 251 using std::cout; 252 using std::endl; 253 254 if( doDebug ) { 255 cout << "--- Leaving scope containing" << endl; 256 idTable.dump( cout ); 257 typeTable.dump( cout ); 258 structTable.dump( cout ); 259 enumTable.dump( cout ); 260 unionTable.dump( cout ); 261 contextTable.dump( cout ); 262 } 263 idTable.leaveScope(); 264 typeTable.leaveScope(); 265 structTable.leaveScope(); 266 enumTable.leaveScope(); 267 unionTable.leaveScope(); 268 contextTable.leaveScope(); 269 } 270 271 void 272 Indexer::print( std::ostream &os, int indent ) const 273 { 274 idTable.dump( os ); 275 typeTable.dump( os ); 276 structTable.dump( os ); 277 enumTable.dump( os ); 278 unionTable.dump( os ); 279 contextTable.dump( os ); 280 } 281 57 void Indexer::visit( TypeDecl *typeDecl ) { 58 // see A NOTE ON THE ORDER OF TRAVERSAL, above 59 // note that assertions come after the type is added to the symtab, since they aren't part 60 // of the type proper and may depend on the type itself 61 enterScope(); 62 acceptAll( typeDecl->get_parameters(), *this ); 63 maybeAccept( typeDecl->get_base(), *this ); 64 leaveScope(); 65 debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); 66 typeTable.add( typeDecl ); 67 acceptAll( typeDecl->get_assertions(), *this ); 68 } 69 70 void Indexer::visit( TypedefDecl *typeDecl ) { 71 enterScope(); 72 acceptAll( typeDecl->get_parameters(), *this ); 73 maybeAccept( typeDecl->get_base(), *this ); 74 leaveScope(); 75 debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); 76 typeTable.add( typeDecl ); 77 } 78 79 void Indexer::visit( StructDecl *aggregateDecl ) { 80 // make up a forward declaration and add it before processing the members 81 StructDecl fwdDecl( aggregateDecl->get_name() ); 82 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 83 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); 84 structTable.add( &fwdDecl ); 85 86 enterScope(); 87 acceptAll( aggregateDecl->get_parameters(), *this ); 88 acceptAll( aggregateDecl->get_members(), *this ); 89 leaveScope(); 90 91 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); 92 // this addition replaces the forward declaration 93 structTable.add( aggregateDecl ); 94 } 95 96 void Indexer::visit( UnionDecl *aggregateDecl ) { 97 // make up a forward declaration and add it before processing the members 98 UnionDecl fwdDecl( aggregateDecl->get_name() ); 99 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 100 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); 101 unionTable.add( &fwdDecl ); 102 103 enterScope(); 104 acceptAll( aggregateDecl->get_parameters(), *this ); 105 acceptAll( aggregateDecl->get_members(), *this ); 106 leaveScope(); 107 108 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); 109 unionTable.add( aggregateDecl ); 110 } 111 112 void Indexer::visit( EnumDecl *aggregateDecl ) { 113 debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); 114 enumTable.add( aggregateDecl ); 115 // unlike structs, contexts, and unions, enums inject their members into the global scope 116 acceptAll( aggregateDecl->get_members(), *this ); 117 } 118 119 void Indexer::visit( ContextDecl *aggregateDecl ) { 120 enterScope(); 121 acceptAll( aggregateDecl->get_parameters(), *this ); 122 acceptAll( aggregateDecl->get_members(), *this ); 123 leaveScope(); 124 125 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); 126 contextTable.add( aggregateDecl ); 127 } 128 129 void Indexer::visit( CompoundStmt *compoundStmt ) { 130 enterScope(); 131 acceptAll( compoundStmt->get_kids(), *this ); 132 leaveScope(); 133 } 134 135 void Indexer::visit( ContextInstType *contextInst ) { 136 acceptAll( contextInst->get_parameters(), *this ); 137 acceptAll( contextInst->get_members(), *this ); 138 } 139 140 void Indexer::visit( StructInstType *structInst ) { 141 if ( ! structTable.lookup( structInst->get_name() ) ) { 142 debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); 143 structTable.add( structInst->get_name() ); 144 } 145 enterScope(); 146 acceptAll( structInst->get_parameters(), *this ); 147 leaveScope(); 148 } 149 150 void Indexer::visit( UnionInstType *unionInst ) { 151 if ( ! unionTable.lookup( unionInst->get_name() ) ) { 152 debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); 153 unionTable.add( unionInst->get_name() ); 154 } 155 enterScope(); 156 acceptAll( unionInst->get_parameters(), *this ); 157 leaveScope(); 158 } 159 160 void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const { 161 idTable.lookupId( id, list ); 162 } 163 164 NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { 165 return typeTable.lookup( id ); 166 } 167 168 StructDecl *Indexer::lookupStruct( const std::string &id ) const { 169 return structTable.lookup( id ); 170 } 171 172 EnumDecl *Indexer::lookupEnum( const std::string &id ) const { 173 return enumTable.lookup( id ); 174 } 175 176 UnionDecl *Indexer::lookupUnion( const std::string &id ) const { 177 return unionTable.lookup( id ); 178 } 179 180 ContextDecl * Indexer::lookupContext( const std::string &id ) const { 181 return contextTable.lookup( id ); 182 } 183 184 void Indexer::enterScope() { 185 if ( doDebug ) { 186 std::cout << "--- Entering scope" << std::endl; 187 } 188 idTable.enterScope(); 189 typeTable.enterScope(); 190 structTable.enterScope(); 191 enumTable.enterScope(); 192 unionTable.enterScope(); 193 contextTable.enterScope(); 194 } 195 196 void Indexer::leaveScope() { 197 using std::cout; 198 using std::endl; 199 200 if ( doDebug ) { 201 cout << "--- Leaving scope containing" << endl; 202 idTable.dump( cout ); 203 typeTable.dump( cout ); 204 structTable.dump( cout ); 205 enumTable.dump( cout ); 206 unionTable.dump( cout ); 207 contextTable.dump( cout ); 208 } 209 idTable.leaveScope(); 210 typeTable.leaveScope(); 211 structTable.leaveScope(); 212 enumTable.leaveScope(); 213 unionTable.leaveScope(); 214 contextTable.leaveScope(); 215 } 216 217 void Indexer::print( std::ostream &os, int indent ) const { 218 idTable.dump( os ); 219 typeTable.dump( os ); 220 structTable.dump( os ); 221 enumTable.dump( os ); 222 unionTable.dump( os ); 223 contextTable.dump( os ); 224 } 282 225 } // namespace SymTab -
translator/SymTab/Indexer.h
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * A class that indexes the syntax tree. It is intended to be subclassed by a visitor class5 * that wants to use the index.6 *7 * $Id: Indexer.h,v 1.9 2005/08/29 20:14:18 rcbilson Exp $8 *9 */10 11 1 #ifndef SYMTAB_INDEXER_H 12 2 #define SYMTAB_INDEXER_H … … 22 12 23 13 namespace SymTab { 14 class Indexer : public Visitor { 15 public: 16 Indexer( bool useDebug = false ); 17 virtual ~Indexer(); 24 18 25 class Indexer : public Visitor 26 { 27 public: 28 Indexer( bool useDebug = false ); 29 virtual ~Indexer(); 19 //using Visitor::visit; 20 virtual void visit( ObjectDecl *objectDecl ); 21 virtual void visit( FunctionDecl *functionDecl ); 22 virtual void visit( TypeDecl *typeDecl ); 23 virtual void visit( TypedefDecl *typeDecl ); 24 virtual void visit( StructDecl *aggregateDecl ); 25 virtual void visit( UnionDecl *aggregateDecl ); 26 virtual void visit( EnumDecl *aggregateDecl ); 27 virtual void visit( ContextDecl *aggregateDecl ); 30 28 31 /// using Visitor::visit; 32 virtual void visit( ObjectDecl *objectDecl ); 33 virtual void visit( FunctionDecl *functionDecl ); 34 virtual void visit( TypeDecl *typeDecl ); 35 virtual void visit( TypedefDecl *typeDecl ); 36 virtual void visit( StructDecl *aggregateDecl ); 37 virtual void visit( UnionDecl *aggregateDecl ); 38 virtual void visit( EnumDecl *aggregateDecl ); 39 virtual void visit( ContextDecl *aggregateDecl ); 29 virtual void visit( CompoundStmt *compoundStmt ); 40 30 41 virtual void visit( CompoundStmt *compoundStmt ); 31 virtual void visit( ContextInstType *contextInst ); 32 virtual void visit( StructInstType *contextInst ); 33 virtual void visit( UnionInstType *contextInst ); 34 35 // when using an indexer manually (e.g., within a mutator traversal), it is necessary to tell the indexer 36 // explicitly when scopes begin and end 37 void enterScope(); 38 void leaveScope(); 42 39 43 virtual void visit( ContextInstType *contextInst ); 44 virtual void visit( StructInstType *contextInst ); 45 virtual void visit( UnionInstType *contextInst ); 40 void lookupId( const std::string &id, std::list< DeclarationWithType* >& ) const; 41 NamedTypeDecl *lookupType( const std::string &id ) const; 42 StructDecl *lookupStruct( const std::string &id ) const; 43 EnumDecl *lookupEnum( const std::string &id ) const; 44 UnionDecl *lookupUnion( const std::string &id ) const; 45 ContextDecl *lookupContext( const std::string &id ) const; 46 46 47 // when using an indexer manually (e.g., within a mutator traversal), it is 48 // necessary to tell the indexer explicitly when scopes begin and end 49 void enterScope(); 50 void leaveScope(); 51 52 void lookupId( const std::string &id, std::list< DeclarationWithType* >& ) const; 53 NamedTypeDecl *lookupType( const std::string &id ) const; 54 StructDecl *lookupStruct( const std::string &id ) const; 55 EnumDecl *lookupEnum( const std::string &id ) const; 56 UnionDecl *lookupUnion( const std::string &id ) const; 57 ContextDecl *lookupContext( const std::string &id ) const; 47 void print( std::ostream &os, int indent = 0 ) const; 48 private: 49 IdTable idTable; 50 TypeTable typeTable; 51 StructTable structTable; 52 EnumTable enumTable; 53 UnionTable unionTable; 54 ContextTable contextTable; 58 55 59 void print( std::ostream &os, int indent = 0 ) const; 60 61 private: 62 IdTable idTable; 63 TypeTable typeTable; 64 StructTable structTable; 65 EnumTable enumTable; 66 UnionTable unionTable; 67 ContextTable contextTable; 68 69 bool doDebug; // display debugging trace 70 }; 71 56 bool doDebug; // display debugging trace 57 }; 72 58 } // namespace SymTab 73 59 -
translator/SymTab/Validate.cc
rd9a0e76 r17cd4eb 185 185 if ( ! visitor.get_declsToAdd().empty() ) { 186 186 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() ); 187 } 187 } // if 188 188 i = next; 189 } 189 } // while 190 190 } 191 191 … … 206 206 if ( doDelete ) { 207 207 delete *i; 208 } 208 } // if 209 209 declList.erase( i ); 210 } 210 } // if 211 211 i = next; 212 } 212 } // while 213 213 } 214 214 … … 227 227 Visitor::visit( aggregateDecl ); 228 228 inStruct = false; 229 } 229 } // if 230 230 // Always remove the hoisted aggregate from the inner structure. 231 231 filter( aggregateDecl->get_members(), isStructOrUnion, false ); … … 275 275 // Set the type of each member of the enumeration to be EnumConstant 276 276 277 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {277 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { 278 278 ObjectDecl *obj = dynamic_cast< ObjectDecl * >( *i ); 279 279 assert( obj ); 280 280 obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false ), enumDecl->get_name() ) ); 281 } 281 } // for 282 282 Parent::visit( enumDecl ); 283 283 } … … 285 285 namespace { 286 286 template< typename DWTIterator > 287 void 288 fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) { 287 void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) { 289 288 // the only case in which "void" is valid is where it is the only one in the list; then 290 289 // it should be removed entirely … … 300 299 if ( i != end ) { 301 300 throw SemanticError( "invalid type void in function type ", func ); 302 } 301 } // if 303 302 } else { 304 303 ++i; 305 for ( ; i != end; ++i ) {304 for ( ; i != end; ++i ) { 306 305 FixFunction fixer; 307 306 *i = (*i )->acceptMutator( fixer ); 308 307 if ( fixer.get_isVoid() ) { 309 308 throw SemanticError( "invalid type void in function type ", func ); 310 } 311 } 312 } 309 } // if 310 } // for 311 } // if 313 312 } 314 313 } … … 326 325 } else { 327 326 indexer = this; 328 } 327 } // if 329 328 } 330 329 … … 336 335 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); 337 336 structInst->set_baseStruct( st ); 338 } 337 } // if 339 338 if ( ! st || st->get_members().empty() ) { 340 339 // use of forward declaration 341 340 forwardStructs[ structInst->get_name() ].push_back( structInst ); 342 } 341 } // if 343 342 } 344 343 … … 349 348 if ( un ) { 350 349 unionInst->set_baseUnion( un ); 351 } 350 } // if 352 351 if ( ! un || un->get_members().empty() ) { 353 352 // use of forward declaration 354 353 forwardUnions[ unionInst->get_name() ].push_back( unionInst ); 355 } 354 } // if 356 355 } 357 356 … … 361 360 if ( ! ctx ) { 362 361 throw SemanticError( "use of undeclared context " + contextInst->get_name() ); 363 } 364 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {365 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {362 } // if 363 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { 364 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { 366 365 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) { 367 366 cloneAll( otherCtx->get_members(), contextInst->get_members() ); 368 367 } else { 369 368 contextInst->get_members().push_back( (*assert )->clone() ); 370 } 371 } 372 } 369 } // if 370 } // for 371 } // for 373 372 applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) ); 374 373 } … … 378 377 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); 379 378 if ( fwds != forwardStructs.end() ) { 380 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {379 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 381 380 (*inst )->set_baseStruct( structDecl ); 382 } 381 } // for 383 382 forwardStructs.erase( fwds ); 384 } 385 } 383 } // if 384 } // if 386 385 Indexer::visit( structDecl ); 387 386 } … … 391 390 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); 392 391 if ( fwds != forwardUnions.end() ) { 393 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {392 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 394 393 (*inst )->set_baseUnion( unionDecl ); 395 } 394 } // for 396 395 forwardUnions.erase( fwds ); 397 } 398 } 396 } // if 397 } // if 399 398 Indexer::visit( unionDecl ); 400 399 } … … 404 403 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { 405 404 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); 406 } 407 } 405 } // if 406 } // if 408 407 } 409 408 … … 413 412 } else { 414 413 indexer = this; 415 } 414 } // if 416 415 } 417 416 418 417 void forallFixer( Type *func ) { 419 418 // Fix up assertions 420 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {419 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { 421 420 std::list< DeclarationWithType * > toBeDone, nextRound; 422 421 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); 423 422 while ( ! toBeDone.empty() ) { 424 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {423 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { 425 424 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) { 426 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {425 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { 427 426 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); 428 427 assert( dwt ); … … 449 448 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { 450 449 forallFixer( pointer->get_base() ); 451 } 450 } // if 452 451 Parent::visit( object ); 453 452 object->fixUniqueId(); … … 546 545 assignDecl->fixUniqueId(); 547 546 548 for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {547 for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) { 549 548 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) { 550 549 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) { … … 552 551 } else { 553 552 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); 554 } 555 } 556 } 553 } // if 554 } // if 555 } // for 557 556 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); 558 557 … … 594 593 declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) ); 595 594 structsDone.insert( structDecl->get_name() ); 596 } 595 } // if 597 596 } 598 597 … … 602 601 unionInst->set_baseUnion( unionDecl ); 603 602 declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) ); 604 } 603 } // if 605 604 } 606 605 … … 617 616 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) ); 618 617 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) ); 619 } 618 } // if 620 619 FunctionType *type = new FunctionType( Type::Qualifiers(), false ); 621 620 type->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) ); … … 628 627 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) { 629 628 if ( ! declsToAdd.empty() ) { 630 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {629 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) { 631 630 statements.insert( i, new DeclStmt( noLabels, *decl ) ); 632 } 631 } // for 633 632 declsToAdd.clear(); 634 } 633 } // if 635 634 } 636 635 637 636 void AddStructAssignment::visit( FunctionType *) { 638 // ensure that we don't add assignment ops for types defined 639 // as part of the function 637 // ensure that we don't add assignment ops for types defined as part of the function 640 638 } 641 639 642 640 void AddStructAssignment::visit( PointerType *) { 643 // ensure that we don't add assignment ops for types defined 644 // as part of the pointer 641 // ensure that we don't add assignment ops for types defined as part of the pointer 645 642 } 646 643 647 644 void AddStructAssignment::visit( ContextDecl *) { 648 // ensure that we don't add assignment ops for types defined 649 // as part of the context 645 // ensure that we don't add assignment ops for types defined as part of the context 650 646 } 651 647 … … 714 710 delete typeInst; 715 711 return ret; 716 } 712 } // if 717 713 return typeInst; 718 714 } … … 721 717 Declaration *ret = Mutator::mutate( tyDecl ); 722 718 typedefNames[ tyDecl->get_name() ] = tyDecl; 723 if ( AggregateDecl *aggDecl = dynamic_cast< AggregateDecl * >( tyDecl->get_base() ) ) { 724 tyDecl->set_base( 0 ); 725 delete tyDecl; 726 return aggDecl; 719 // When a typedef is a forward declaration: 720 // typedef struct screen SCREEN; 721 // the declaration portion must be retained: 722 // struct screen; 723 // because the expansion of the typedef is: 724 // void rtn( SCREEN *p ) => void rtn( struct screen *p ) 725 // hence the type-name "screen" must be defined. 726 // Note, qualifiers on the typedef are superfluous for the forward declaration. 727 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { 728 return new StructDecl( aggDecl->get_name() ); 729 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { 730 return new UnionDecl( aggDecl->get_name() ); 727 731 } else { 728 732 return ret; 729 } 733 } // if 730 734 } 731 735 … … 734 738 if ( i != typedefNames.end() ) { 735 739 typedefNames.erase( i ) ; 736 } 740 } // if 737 741 return typeDecl; 738 742 } … … 770 774 delete *i; 771 775 compoundStmt->get_kids().erase( i ); 772 } 773 } 776 } // if 777 } // if 774 778 i = next; 775 } 779 } // while 776 780 typedefNames = oldNames; 777 781 return ret; -
translator/SynTree/BasicType.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: BasicType.cc,v 1.10 2005/08/29 20:59:25 rcbilson Exp $5 *6 */7 8 1 #include <cassert> 9 2 … … 11 4 12 5 13 BasicType::BasicType( const Type::Qualifiers &tq, Kind bt ) 14 : Type( tq ), kind( bt ) 15 { 16 } 6 BasicType::BasicType( const Type::Qualifiers &tq, Kind bt ) : Type( tq ), kind( bt ) {} 17 7 18 void 19 BasicType::print( std::ostream &os, int indent ) const 20 { 8 void BasicType::print( std::ostream &os, int indent ) const { 21 9 static const char *kindNames[] = { 22 " bool", "char", "signed char", "unsigned char", "short signed int", "short unsigned int",10 "_Bool", "char", "signed char", "unsigned char", "short signed int", "short unsigned int", 23 11 "signed int", "unsigned int", "long signed int", "long unsigned int", "long long signed int", 24 "long long unsigned int", "float", "double", "long double", "float complex", "double complex",25 "long double complex", "float imaginary", "double imaginary", "long double imaginary"12 "long long unsigned int", "float", "double", "long double", "float _Complex", "double _Complex", 13 "long double _Complex", "float _Imaginary", "double _Imaginary", "long double _Imaginary" 26 14 }; 27 15 … … 30 18 } 31 19 32 bool 33 BasicType::isInteger() const 34 { 20 bool BasicType::isInteger() const { 35 21 switch( kind ) { 36 case Bool:37 case Char:38 case SignedChar:39 case UnsignedChar:40 case ShortSignedInt:41 case ShortUnsignedInt:42 case SignedInt:43 case UnsignedInt:44 case LongSignedInt:45 case LongUnsignedInt:46 case LongLongSignedInt:47 case LongLongUnsignedInt:22 case Bool: 23 case Char: 24 case SignedChar: 25 case UnsignedChar: 26 case ShortSignedInt: 27 case ShortUnsignedInt: 28 case SignedInt: 29 case UnsignedInt: 30 case LongSignedInt: 31 case LongUnsignedInt: 32 case LongLongSignedInt: 33 case LongLongUnsignedInt: 48 34 return true; 49 35 50 case Float:51 case Double:52 case LongDouble:53 case FloatComplex:54 case DoubleComplex:55 case LongDoubleComplex:56 case FloatImaginary:57 case DoubleImaginary:58 case LongDoubleImaginary:36 case Float: 37 case Double: 38 case LongDouble: 39 case FloatComplex: 40 case DoubleComplex: 41 case LongDoubleComplex: 42 case FloatImaginary: 43 case DoubleImaginary: 44 case LongDoubleImaginary: 59 45 return false; 60 46 61 case NUMBER_OF_BASIC_TYPES:47 case NUMBER_OF_BASIC_TYPES: 62 48 assert( false ); 63 49 } -
translator/SynTree/Declaration.h
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: Declaration.h,v 1.22 2005/08/29 20:59:25 rcbilson Exp $5 *6 */7 8 1 #ifndef DECLARATION_H 9 2 #define DECLARATION_H … … 15 8 16 9 17 class Declaration 18 { 19 public: 20 enum StorageClass 21 { 10 class Declaration { 11 public: 12 enum StorageClass { 22 13 NoStorageClass, 23 14 Auto, … … 27 18 Fortran 28 19 }; 29 20 30 21 Declaration( const std::string &name, StorageClass sc, LinkageSpec::Type linkage ); 31 22 Declaration( const Declaration &other ); … … 39 30 void set_linkage( LinkageSpec::Type newValue ) { linkage = newValue; } 40 31 UniqueId get_uniqueId() const { return uniqueId; } 41 32 42 33 void fixUniqueId( void ); 43 34 virtual Declaration *clone() const = 0; … … 46 37 virtual void print( std::ostream &os, int indent = 0 ) const = 0; 47 38 virtual void printShort( std::ostream &os, int indent = 0 ) const = 0; 48 39 49 40 static const char* storageClassName[]; 50 41 51 42 static void dumpIds( std::ostream &os ); 52 43 static Declaration *declFromId( UniqueId id ); 53 54 private: 44 private: 55 45 std::string name; 56 46 StorageClass storageClass; … … 59 49 }; 60 50 61 class DeclarationWithType : public Declaration 62 { 63 public: 51 class DeclarationWithType : public Declaration { 52 public: 64 53 DeclarationWithType( const std::string &name, StorageClass sc, LinkageSpec::Type linkage ); 65 54 DeclarationWithType( const DeclarationWithType &other ); … … 68 57 std::string get_mangleName() const { return mangleName; } 69 58 void set_mangleName( std::string newValue ) { mangleName = newValue; } 70 59 71 60 virtual DeclarationWithType *clone() const = 0; 72 61 virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; 73 62 74 63 virtual Type *get_type() const = 0; 75 64 virtual void set_type(Type *) = 0; 76 77 private: 78 // this represents the type with all types and typedefs expanded 79 // it is generated by SymTab::Validate::Pass2 65 private: 66 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 80 67 std::string mangleName; 81 68 }; 82 69 83 class ObjectDecl : public DeclarationWithType 84 { 70 class ObjectDecl : public DeclarationWithType { 85 71 typedef DeclarationWithType Parent; 86 87 public: 72 public: 88 73 ObjectDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, Expression *bitfieldWidth, Type *type, Initializer *init ); 89 74 ObjectDecl( const ObjectDecl &other ); … … 97 82 Expression *get_bitfieldWidth() const { return bitfieldWidth; } 98 83 void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } 99 84 100 85 virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); } 101 86 virtual void accept( Visitor &v ) { v.visit( this ); } … … 103 88 virtual void print( std::ostream &os, int indent = 0 ) const; 104 89 virtual void printShort( std::ostream &os, int indent = 0 ) const; 105 106 private: 90 private: 107 91 Type *type; 108 92 Initializer *init; … … 110 94 }; 111 95 112 class FunctionDecl : public DeclarationWithType 113 { 96 class FunctionDecl : public DeclarationWithType { 114 97 typedef DeclarationWithType Parent; 115 116 public: 98 public: 117 99 FunctionDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline ); 118 100 FunctionDecl( const FunctionDecl &other ); … … 130 112 std::list< std::string >& get_oldIdents() { return oldIdents; } 131 113 std::list< Declaration* >& get_oldDecls() { return oldDecls; } 132 114 133 115 virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); } 134 116 virtual void accept( Visitor &v ) { v.visit( this ); } … … 136 118 virtual void print( std::ostream &os, int indent = 0 ) const; 137 119 virtual void printShort( std::ostream &os, int indent = 0 ) const; 138 139 private: 120 private: 140 121 FunctionType *type; 141 122 CompoundStmt *statements; … … 145 126 }; 146 127 147 class NamedTypeDecl : public Declaration 148 { 128 class NamedTypeDecl : public Declaration { 149 129 typedef Declaration Parent; 150 151 public: 130 public: 152 131 NamedTypeDecl( const std::string &name, StorageClass sc, Type *type ); 153 132 NamedTypeDecl( const TypeDecl &other ); … … 162 141 virtual void print( std::ostream &os, int indent = 0 ) const; 163 142 virtual void printShort( std::ostream &os, int indent = 0 ) const; 164 165 protected: 143 protected: 166 144 virtual std::string typeString() const = 0; 167 168 private: 145 private: 169 146 Type *base; 170 147 std::list< TypeDecl* > parameters; … … 172 149 }; 173 150 174 class TypeDecl : public NamedTypeDecl 175 { 151 class TypeDecl : public NamedTypeDecl { 176 152 typedef NamedTypeDecl Parent; 177 178 public: 153 public: 179 154 enum Kind { Any, Dtype, Ftype }; 180 155 181 156 TypeDecl( const std::string &name, StorageClass sc, Type *type, Kind kind ); 182 157 TypeDecl( const TypeDecl &other ); 183 158 184 159 Kind get_kind() const { return kind; } 185 160 … … 187 162 virtual void accept( Visitor &v ) { v.visit( this ); } 188 163 virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } 189 190 private: 164 private: 191 165 virtual std::string typeString() const; 192 166 Kind kind; 193 167 }; 194 195 class TypedefDecl : public NamedTypeDecl 196 { 168 169 class TypedefDecl : public NamedTypeDecl { 197 170 typedef NamedTypeDecl Parent; 198 199 public: 171 public: 200 172 TypedefDecl( const std::string &name, StorageClass sc, Type *type ) : Parent( name, sc, type ) {} 201 173 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} 202 174 203 175 virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); } 204 176 virtual void accept( Visitor &v ) { v.visit( this ); } 205 177 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } 206 207 private: 208 virtual std::string typeString() const; 209 }; 210 211 class AggregateDecl : public Declaration 212 { 178 private: 179 virtual std::string typeString() const; 180 }; 181 182 class AggregateDecl : public Declaration { 213 183 typedef Declaration Parent; 214 215 public: 184 public: 216 185 AggregateDecl( const std::string &name ); 217 186 AggregateDecl( const AggregateDecl &other ); … … 220 189 std::list<Declaration*>& get_members() { return members; } 221 190 std::list<TypeDecl*>& get_parameters() { return parameters; } 222 223 virtual void print( std::ostream &os, int indent = 0 ) const; 224 virtual void printShort( std::ostream &os, int indent = 0 ) const; 225 226 protected: 191 192 virtual void print( std::ostream &os, int indent = 0 ) const; 193 virtual void printShort( std::ostream &os, int indent = 0 ) const; 194 protected: 227 195 virtual std::string typeString() const = 0; 228 229 private:196 197 private: 230 198 std::list<Declaration*> members; 231 199 std::list<TypeDecl*> parameters; 232 200 }; 233 201 234 class StructDecl : public AggregateDecl 235 { 236 typedef AggregateDecl Parent; 237 238 public: 202 class StructDecl : public AggregateDecl { 203 typedef AggregateDecl Parent; 204 public: 239 205 StructDecl( const std::string &name ) : Parent( name ) {} 240 206 StructDecl( const StructDecl &other ) : Parent( other ) {} 241 207 242 208 virtual StructDecl *clone() const { return new StructDecl( *this ); } 243 209 virtual void accept( Visitor &v ) { v.visit( this ); } 244 210 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } 245 211 246 private: 247 virtual std::string typeString() const; 248 }; 249 250 class UnionDecl : public AggregateDecl 251 { 252 typedef AggregateDecl Parent; 253 254 public: 212 private: 213 virtual std::string typeString() const; 214 }; 215 216 class UnionDecl : public AggregateDecl { 217 typedef AggregateDecl Parent; 218 public: 255 219 UnionDecl( const std::string &name ) : Parent( name ) {} 256 220 UnionDecl( const UnionDecl &other ) : Parent( other ) {} 257 221 258 222 virtual UnionDecl *clone() const { return new UnionDecl( *this ); } 259 223 virtual void accept( Visitor &v ) { v.visit( this ); } 260 224 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } 261 262 private: 263 virtual std::string typeString() const; 264 }; 265 266 class EnumDecl : public AggregateDecl 267 { 268 typedef AggregateDecl Parent; 269 270 public: 225 private: 226 virtual std::string typeString() const; 227 }; 228 229 class EnumDecl : public AggregateDecl { 230 typedef AggregateDecl Parent; 231 public: 271 232 EnumDecl( const std::string &name ) : Parent( name ) {} 272 233 EnumDecl( const EnumDecl &other ) : Parent( other ) {} 273 234 274 235 virtual EnumDecl *clone() const { return new EnumDecl( *this ); } 275 236 virtual void accept( Visitor &v ) { v.visit( this ); } 276 237 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } 277 278 private: 279 virtual std::string typeString() const; 280 }; 281 282 class ContextDecl : public AggregateDecl 283 { 284 typedef AggregateDecl Parent; 285 286 public: 238 private: 239 virtual std::string typeString() const; 240 }; 241 242 class ContextDecl : public AggregateDecl { 243 typedef AggregateDecl Parent; 244 public: 287 245 ContextDecl( const std::string &name ) : Parent( name ) {} 288 246 ContextDecl( const ContextDecl &other ) : Parent( other ) {} 289 247 290 248 virtual ContextDecl *clone() const { return new ContextDecl( *this ); } 291 249 virtual void accept( Visitor &v ) { v.visit( this ); } 292 250 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } 293 294 private: 295 virtual std::string typeString() const; 296 }; 297 298 299 300 #endif /* #ifndef DECLARATION_H */ 251 private: 252 virtual std::string typeString() const; 253 }; 254 255 #endif // DECLARATION_H -
translator/SynTree/NamedTypeDecl.cc
rd9a0e76 r17cd4eb 6 6 NamedTypeDecl::NamedTypeDecl( const std::string &name, StorageClass sc, Type *base ) 7 7 : Parent( name, sc, LinkageSpec::Cforall ), base( base ) 8 { 9 } 8 {} 10 9 11 10 NamedTypeDecl::NamedTypeDecl( const TypeDecl &other ) … … 16 15 } 17 16 18 NamedTypeDecl::~NamedTypeDecl() 19 { 17 NamedTypeDecl::~NamedTypeDecl() { 20 18 delete base; 21 19 deleteAll( parameters ); … … 23 21 } 24 22 25 void 26 NamedTypeDecl::print( std::ostream &os, int indent ) const 27 { 23 void NamedTypeDecl::print( std::ostream &os, int indent ) const { 28 24 using namespace std; 29 25 30 if ( get_name() != "" ) {26 if ( get_name() != "" ) { 31 27 os << get_name() << ": a "; 32 } 33 if ( get_storageClass() != NoStorageClass ) {28 } // if 29 if ( get_storageClass() != NoStorageClass ) { 34 30 os << storageClassName[ get_storageClass() ] << ' '; 35 } 31 } // if 36 32 os << typeString(); 37 if ( base ) {33 if ( base ) { 38 34 os << " for "; 39 35 base->print( os, indent ); 40 } 41 if ( !parameters.empty() ) {36 } // if 37 if ( !parameters.empty() ) { 42 38 os << endl << string( indent, ' ' ) << "with parameters" << endl; 43 39 printAll( parameters, os, indent+2 ); 44 } 45 if ( !assertions.empty() ) {40 } // if 41 if ( !assertions.empty() ) { 46 42 os << endl << string( indent, ' ' ) << "with assertions" << endl; 47 43 printAll( assertions, os, indent+2 ); 48 } 44 } // if 49 45 } 50 46 51 void 52 NamedTypeDecl::printShort( std::ostream &os, int indent ) const 53 { 47 void NamedTypeDecl::printShort( std::ostream &os, int indent ) const { 54 48 using namespace std; 55 49 56 if ( get_name() != "" ) {50 if ( get_name() != "" ) { 57 51 os << get_name() << ": a "; 58 } 59 if ( get_storageClass() != NoStorageClass ) {52 } // if 53 if ( get_storageClass() != NoStorageClass ) { 60 54 os << storageClassName[ get_storageClass() ] << ' '; 61 } 55 } // if 62 56 os << typeString(); 63 if ( base ) {57 if ( base ) { 64 58 os << " for "; 65 59 base->print( os, indent ); 66 } 67 if ( !parameters.empty() ) {60 } // if 61 if ( !parameters.empty() ) { 68 62 os << endl << string( indent, ' ' ) << "with parameters" << endl; 69 63 printAll( parameters, os, indent+2 ); 70 } 64 } // if 71 65 } 72 66 73 67 std::string TypedefDecl::typeString() const { return "typedef"; } 74 -
translator/SynTree/Type.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: Type.cc,v 1.6 2005/08/29 20:59:26 rcbilson Exp $5 *6 */7 8 1 #include "SynTree.h" 9 2 #include "Visitor.h" … … 12 5 #include "utility.h" 13 6 7 const char *BasicType::typeNames[BasicType::NUMBER_OF_BASIC_TYPES] = { 8 "_Bool", 9 "char", 10 "char", 11 "unsigned char", 12 "short", 13 "short unsigned", 14 "int", 15 "unsigned int", 16 "long int", 17 "long unsigned int", 18 "long long int", 19 "long long unsigned int", 20 "float", 21 "double", 22 "long double", 23 "float _Complex", 24 "double _Complex", 25 "long double _Complex", 26 "float _Imaginary", 27 "double _Imaginary", 28 "long double _Imaginary", 29 }; 14 30 15 Type::Type( const Qualifiers &tq ) 16 : tq( tq ) 17 { 18 } 31 Type::Type( const Qualifiers &tq ) : tq( tq ) {} 19 32 20 Type::Type( const Type &other ) 21 : tq( other.tq ) 22 { 33 Type::Type( const Type &other ) : tq( other.tq ) { 23 34 cloneAll( other.forall, forall ); 24 35 } 25 36 26 Type::~Type() 27 { 37 Type::~Type() { 28 38 deleteAll( forall ); 29 39 } 30 40 31 void 32 Type::print( std::ostream &os, int indent ) const 33 { 34 if( !forall.empty() ) { 41 void Type::print( std::ostream &os, int indent ) const { 42 if ( !forall.empty() ) { 35 43 os << "forall" << std::endl; 36 44 printAll( forall, os, indent + 4 ); 37 45 os << std::string( indent+2, ' ' ); 38 } 39 if ( tq.isConst ) {46 } // if 47 if ( tq.isConst ) { 40 48 os << "const "; 41 } 42 if ( tq.isVolatile ) {49 } // if 50 if ( tq.isVolatile ) { 43 51 os << "volatile "; 44 } 45 if ( tq.isRestrict ) {52 } // if 53 if ( tq.isRestrict ) { 46 54 os << "restrict "; 47 } 48 if ( tq.isLvalue ) {55 } // if 56 if ( tq.isLvalue ) { 49 57 os << "lvalue "; 50 } 58 } // if 51 59 } 52 -
translator/SynTree/Type.h
rd9a0e76 r17cd4eb 90 90 }; 91 91 92 static const char *typeNames[]; // string names for basic types, MUST MATCH with Kind 93 92 94 BasicType( const Type::Qualifiers &tq, Kind bt ); 93 95 -
translator/Tests/Parser/Makefile
rd9a0e76 r17cd4eb 4 4 TESTS = $(EXPECTED:Expected/%=%) 5 5 TEST_IN = $(TESTS:.tst=.c) 6 DIFF = /software/gnu/bin/diff#diff6 DIFF = diff 7 7 8 8 %.tst:%.c $(CFA) … … 15 15 $(DIFF) -B -w Expected/$$i $$i | tee -a report; \ 16 16 done 17 18 clean: 19 rm -f *.tst -
translator/examples/Makefile
rd9a0e76 r17cd4eb 20 20 21 21 ${EXEC1} : ${OBJECTS1} # link step 1st executable 22 ${CC} ${C CFLAGS} $^ -o $@ # additional object files before $^22 ${CC} ${CFLAGS} $^ -o $@ # additional object files before $^ 23 23 24 24 ${EXEC2} : ${OBJECTS2} # link step 2nd executable 25 ${CC} ${C CFLAGS} $^ -o $@ # additional object files before $^25 ${CC} ${CFLAGS} $^ -o $@ # additional object files before $^ 26 26 27 27 ${OBJECTS} : ${MAKEFILE_NAME} # OPTIONAL : changes to this file => recompile -
translator/examples/includes.c
rd9a0e76 r17cd4eb 25 25 //#include <wchar.h> // FAILS -- includes locale.h 26 26 //#include <wctype.h> // FAILS -- includes locale.h 27 #include <curses.h> 27 28 #else 28 #include < curses.h>29 #endif 29 #include <aio.h> 30 #endif // 0 30 31 31 32 // Local Variables: // -
translator/examples/swap.c
rd9a0e76 r17cd4eb 4 4 5 5 forall( type T ) 6 void swap( T left, T right ) { 7 T temp = left; 8 left = right; 9 right = temp; 6 T swap( T *left, T *right ) { 7 T temp; 8 temp = *left; 9 *left = *right; 10 *right = temp; 11 return *right; 10 12 } 11 13 12 14 int main() { 13 15 int x = 1, y = 2; 14 printf( "%d %d", x, y ); 15 swap( x, y ); 16 printf( "%d %d", x, y ); 16 printf( "%d %d\n", x, y ); 17 int w; 18 w = swap( &x, &y ); 19 printf( "%d %d %d\n", w, x, y ); 17 20 } 18 21 -
translator/examples/vector_test.c
rd9a0e76 r17cd4eb 22 22 sout << "Array elements:\n"; 23 23 // write_all( begin( vec ), end( vec ), sout ); 24 sout << "\n"; 25 #if 0 26 write_reverse( begin( vec ), end( vec ), sout ); 27 sout << "\n"; 28 29 #endif 24 // sout << "\n"; 30 25 for ( int index = 0; index <= last( vec ); index += 1 ) { 31 26 sout << vec[ index ] << " "; 32 27 } 33 28 sout << "\n"; 29 #if 1 30 sout << "Array elements reversed:\n"; 31 write_reverse( begin( vec ), end( vec ), sout ); 32 sout << "\n"; 33 #endif 34 34 } 35 35
Note: See TracChangeset
for help on using the changeset viewer.