Changes in src/Parser/DeclarationNode.cc [faddbd8:5b639ee]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (50 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rfaddbd8 r5b639ee 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Oct 3 18:03:08 201613 // Update Count : 65112 // Last Modified On : Mon Sep 12 21:03:18 2016 13 // Update Count : 491 14 14 // 15 15 … … 31 31 32 32 // These must remain in the same order as the corresponding DeclarationNode enumerations. 33 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };34 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };35 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };36 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };37 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };38 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };39 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" };40 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };41 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };33 const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" }; 34 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" }; 35 const char *DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" }; 36 const char *DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" }; 37 const char *DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" }; 38 const char *DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" }; 39 const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" }; 40 const char *DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" }; 41 const char *DeclarationNode::builtinTypeName[] = { "__builtin_va_list" }; 42 42 43 43 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 46 46 47 47 DeclarationNode::DeclarationNode() : 48 type( nullptr),48 type( 0 ), 49 49 storageClass( NoStorageClass ), 50 50 isInline( false ), 51 51 isNoreturn( false ), 52 bitfieldWidth( nullptr),53 initializer( nullptr),52 bitfieldWidth( 0 ), 53 initializer( 0 ), 54 54 hasEllipsis( false ), 55 55 linkage( ::linkage ), 56 56 extension( false ) { 57 58 // variable.name = nullptr; 59 variable.tyClass = NoTypeClass; 57 variable.tyClass = DeclarationNode::Otype; 60 58 variable.assertions = nullptr; 61 59 62 // attr.name = nullptr;63 60 attr.expr = nullptr; 64 61 attr.type = nullptr; … … 66 63 67 64 DeclarationNode::~DeclarationNode() { 68 // delete attr.name;69 65 delete attr.expr; 70 66 delete attr.type; 71 72 // delete variable.name;73 delete variable.assertions;74 75 67 delete type; 76 68 delete bitfieldWidth; … … 78 70 } 79 71 80 DeclarationNode * DeclarationNode::clone() const {81 DeclarationNode * newnode = new DeclarationNode;72 DeclarationNode *DeclarationNode::clone() const { 73 DeclarationNode *newnode = new DeclarationNode; 82 74 newnode->type = maybeClone( type ); 83 newnode->name = name ? new string( *name ) : nullptr;75 newnode->name = name; 84 76 newnode->storageClass = storageClass; 85 77 newnode->isInline = isInline; … … 91 83 newnode->linkage = linkage; 92 84 93 // newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr; 85 newnode->variable.assertions = maybeClone( variable.assertions ); 86 newnode->variable.name = variable.name; 94 87 newnode->variable.tyClass = variable.tyClass; 95 newnode->variable.assertions = maybeClone( variable.assertions ); 96 97 // newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr; 88 98 89 newnode->attr.expr = maybeClone( attr.expr ); 99 90 newnode->attr.type = maybeClone( attr.type ); … … 107 98 void DeclarationNode::print( std::ostream &os, int indent ) const { 108 99 os << string( indent, ' ' ); 109 if ( name ) {110 os << *name << ": ";100 if ( name == "" ) { 101 os << "unnamed: "; 111 102 } else { 112 os << "unnamed: ";103 os << name << ": "; 113 104 } // if 114 105 115 106 if ( linkage != LinkageSpec::Cforall ) { 116 os << LinkageSpec:: linkageName( linkage ) << " ";107 os << LinkageSpec::toString( linkage ) << " "; 117 108 } // if 118 109 … … 131 122 } // if 132 123 133 if ( initializer ) {124 if ( initializer != 0 ) { 134 125 os << endl << string( indent + 2, ' ' ) << "with initializer "; 135 126 initializer->printOneLine( os ); … … 148 139 } 149 140 150 DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) { 151 DeclarationNode * newnode = new DeclarationNode; 152 newnode->name = name; 141 DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) { 142 DeclarationNode *newnode = new DeclarationNode; 143 newnode->name = assign_strptr( name ); 144 153 145 newnode->type = new TypeData( TypeData::Function ); 154 146 newnode->type->function.params = param; 155 147 newnode->type->function.newStyle = newStyle; 156 148 newnode->type->function.body = body; 157 // ignore unnamed routine declarations: void p( int (*)(int) ); 158 if ( newnode->name ) { 159 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); 160 } // if 149 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 161 150 162 151 if ( body ) { … … 166 155 if ( ret ) { 167 156 newnode->type->base = ret->type; 168 ret->type = nullptr;157 ret->type = 0; 169 158 delete ret; 170 159 } // if … … 174 163 175 164 DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { 176 DeclarationNode * newnode = new DeclarationNode;165 DeclarationNode *newnode = new DeclarationNode; 177 166 newnode->type = new TypeData(); 178 167 newnode->type->qualifiers[ q ] = 1; … … 180 169 } // DeclarationNode::newQualifier 181 170 182 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {183 DeclarationNode * newnode = new DeclarationNode;171 DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) { 172 DeclarationNode *newnode = new DeclarationNode; 184 173 newnode->type = new TypeData( TypeData::Unknown ); 185 174 newnode->type->forall = forall; … … 188 177 189 178 DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { 190 DeclarationNode * newnode = new DeclarationNode; 179 DeclarationNode *newnode = new DeclarationNode; 180 //switch (sc) { 181 // case Inline: newnode->isInline = true; break; 182 // case Noreturn: newnode->isNoreturn = true; break; 183 // default: newnode->storageClass = sc; break; 184 //} 191 185 newnode->storageClass = sc; 192 186 return newnode; … … 194 188 195 189 DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { 196 DeclarationNode * newnode = new DeclarationNode;190 DeclarationNode *newnode = new DeclarationNode; 197 191 newnode->type = new TypeData( TypeData::Basic ); 198 192 newnode->type->basictype = bt; … … 201 195 202 196 DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { 203 DeclarationNode * newnode = new DeclarationNode;197 DeclarationNode *newnode = new DeclarationNode; 204 198 newnode->type = new TypeData( TypeData::Basic ); 205 199 newnode->type->complextype = ct; … … 208 202 209 203 DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { 210 DeclarationNode * newnode = new DeclarationNode;204 DeclarationNode *newnode = new DeclarationNode; 211 205 newnode->type = new TypeData( TypeData::Basic ); 212 206 newnode->type->signedness = sn; … … 215 209 216 210 DeclarationNode * DeclarationNode::newLength( Length lnth ) { 217 DeclarationNode * newnode = new DeclarationNode;211 DeclarationNode *newnode = new DeclarationNode; 218 212 newnode->type = new TypeData( TypeData::Basic ); 219 213 newnode->type->length = lnth; … … 221 215 } // DeclarationNode::newLength 222 216 223 DeclarationNode * DeclarationNode::newFromTypedef( st ring *name ) {224 DeclarationNode * newnode = new DeclarationNode;217 DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) { 218 DeclarationNode *newnode = new DeclarationNode; 225 219 newnode->type = new TypeData( TypeData::SymbolicInst ); 226 newnode->type->symbolic.name = name;220 newnode->type->symbolic.name = assign_strptr( name ); 227 221 newnode->type->symbolic.isTypedef = true; 228 newnode->type->symbolic.params = nullptr;222 newnode->type->symbolic.params = 0; 229 223 return newnode; 230 224 } // DeclarationNode::newFromTypedef 231 225 232 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const st ring * name, ExpressionNode * actuals, DeclarationNode *fields, bool body ) {233 DeclarationNode * newnode = new DeclarationNode;226 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) { 227 DeclarationNode *newnode = new DeclarationNode; 234 228 newnode->type = new TypeData( TypeData::Aggregate ); 235 229 newnode->type->aggregate.kind = kind; 236 if ( name ) { 237 newnode->type->aggregate.name = name; 238 } else { // anonymous aggregate ? 239 newnode->type->aggregate.name = new string( anonymous.newName() ); 230 newnode->type->aggregate.name = assign_strptr( name ); 231 if ( newnode->type->aggregate.name == "" ) { // anonymous aggregate ? 232 newnode->type->aggregate.name = anonymous.newName(); 240 233 } // if 241 234 newnode->type->aggregate.actuals = actuals; … … 245 238 } // DeclarationNode::newAggregate 246 239 247 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) { 248 DeclarationNode * newnode = new DeclarationNode; 240 DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) { 241 DeclarationNode *newnode = new DeclarationNode; 242 newnode->name = assign_strptr( name ); 249 243 newnode->type = new TypeData( TypeData::Enum ); 250 if ( name ) { 251 newnode->type->enumeration.name = name; 252 } else { // anonymous aggregate ? 253 newnode->type->enumeration.name = new string( anonymous.newName() ); 244 newnode->type->enumeration.name = newnode->name; 245 if ( newnode->type->enumeration.name == "" ) { // anonymous enumeration ? 246 newnode->type->enumeration.name = DeclarationNode::anonymous.newName(); 254 247 } // if 255 248 newnode->type->enumeration.constants = constants; … … 257 250 } // DeclarationNode::newEnum 258 251 259 DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode *constant ) {260 DeclarationNode * newnode = new DeclarationNode;261 newnode->name = name;252 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) { 253 DeclarationNode *newnode = new DeclarationNode; 254 newnode->name = assign_strptr( name ); 262 255 newnode->enumeratorValue.reset( constant ); 263 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );256 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID ); 264 257 return newnode; 265 258 } // DeclarationNode::newEnumConstant 266 259 267 DeclarationNode * DeclarationNode::newName( string *name ) {268 DeclarationNode * newnode = new DeclarationNode;269 newnode->name = name;260 DeclarationNode *DeclarationNode::newName( std::string *name ) { 261 DeclarationNode *newnode = new DeclarationNode; 262 newnode->name = assign_strptr( name ); 270 263 return newnode; 271 264 } // DeclarationNode::newName 272 265 273 DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode *params ) {274 DeclarationNode * newnode = new DeclarationNode;266 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) { 267 DeclarationNode *newnode = new DeclarationNode; 275 268 newnode->type = new TypeData( TypeData::SymbolicInst ); 276 newnode->type->symbolic.name = name;269 newnode->type->symbolic.name = assign_strptr( name ); 277 270 newnode->type->symbolic.isTypedef = false; 278 271 newnode->type->symbolic.actuals = params; … … 280 273 } // DeclarationNode::newFromTypeGen 281 274 282 DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) { 283 DeclarationNode * newnode = new DeclarationNode; 284 newnode->type = nullptr; 285 assert( ! newnode->name ); 286 // newnode->variable.name = name; 287 newnode->name = name; 275 DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) { 276 DeclarationNode *newnode = new DeclarationNode; 277 newnode->name = assign_strptr( name ); 278 newnode->type = new TypeData( TypeData::Variable ); 288 279 newnode->variable.tyClass = tc; 289 newnode->variable. assertions = nullptr;280 newnode->variable.name = newnode->name; 290 281 return newnode; 291 282 } // DeclarationNode::newTypeParam 292 283 293 DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode *asserts ) {294 DeclarationNode * newnode = new DeclarationNode;284 DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) { 285 DeclarationNode *newnode = new DeclarationNode; 295 286 newnode->type = new TypeData( TypeData::Aggregate ); 296 newnode->type->aggregate.name = name;297 287 newnode->type->aggregate.kind = Trait; 298 288 newnode->type->aggregate.params = params; 299 289 newnode->type->aggregate.fields = asserts; 290 newnode->type->aggregate.name = assign_strptr( name ); 300 291 return newnode; 301 292 } // DeclarationNode::newTrait 302 293 303 DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode *params ) {304 DeclarationNode * newnode = new DeclarationNode;294 DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) { 295 DeclarationNode *newnode = new DeclarationNode; 305 296 newnode->type = new TypeData( TypeData::AggregateInst ); 306 297 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); 307 298 newnode->type->aggInst.aggregate->aggregate.kind = Trait; 308 newnode->type->aggInst.aggregate->aggregate.name = name;299 newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name ); 309 300 newnode->type->aggInst.params = params; 310 301 return newnode; 311 302 } // DeclarationNode::newTraitUse 312 303 313 DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) { 314 DeclarationNode * newnode = new DeclarationNode; 304 DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) { 305 DeclarationNode *newnode = new DeclarationNode; 306 newnode->name = assign_strptr( name ); 315 307 newnode->type = new TypeData( TypeData::Symbolic ); 316 308 newnode->type->symbolic.isTypedef = false; 317 309 newnode->type->symbolic.params = typeParams; 318 newnode->type->symbolic.name = n ame;310 newnode->type->symbolic.name = newnode->name; 319 311 return newnode; 320 312 } // DeclarationNode::newTypeDecl 321 313 322 DeclarationNode * DeclarationNode::newPointer( DeclarationNode *qualifiers ) {323 DeclarationNode * newnode = new DeclarationNode;314 DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) { 315 DeclarationNode *newnode = new DeclarationNode; 324 316 newnode->type = new TypeData( TypeData::Pointer ); 325 317 return newnode->addQualifiers( qualifiers ); 326 318 } // DeclarationNode::newPointer 327 319 328 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode *qualifiers, bool isStatic ) {329 DeclarationNode * newnode = new DeclarationNode;320 DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) { 321 DeclarationNode *newnode = new DeclarationNode; 330 322 newnode->type = new TypeData( TypeData::Array ); 331 323 newnode->type->array.dimension = size; 332 324 newnode->type->array.isStatic = isStatic; 333 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) {325 if ( newnode->type->array.dimension == 0 || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) { 334 326 newnode->type->array.isVarLen = false; 335 327 } else { … … 339 331 } // DeclarationNode::newArray 340 332 341 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {342 DeclarationNode * newnode = new DeclarationNode;333 DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) { 334 DeclarationNode *newnode = new DeclarationNode; 343 335 newnode->type = new TypeData( TypeData::Array ); 344 newnode->type->array.dimension = nullptr;336 newnode->type->array.dimension = 0; 345 337 newnode->type->array.isStatic = false; 346 338 newnode->type->array.isVarLen = true; … … 348 340 } 349 341 350 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode *size ) {351 DeclarationNode * newnode = new DeclarationNode;342 DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) { 343 DeclarationNode *newnode = new DeclarationNode; 352 344 newnode->bitfieldWidth = size; 353 345 return newnode; 354 346 } 355 347 356 DeclarationNode * DeclarationNode::newTuple( DeclarationNode *members ) {357 DeclarationNode * newnode = new DeclarationNode;348 DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) { 349 DeclarationNode *newnode = new DeclarationNode; 358 350 newnode->type = new TypeData( TypeData::Tuple ); 359 351 newnode->type->tuple = members; … … 361 353 } 362 354 363 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode *expr ) {364 DeclarationNode * newnode = new DeclarationNode;355 DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) { 356 DeclarationNode *newnode = new DeclarationNode; 365 357 newnode->type = new TypeData( TypeData::Typeof ); 366 358 newnode->type->typeexpr = expr; … … 369 361 370 362 DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { 371 DeclarationNode * newnode = new DeclarationNode;363 DeclarationNode *newnode = new DeclarationNode; 372 364 newnode->type = new TypeData( TypeData::Builtin ); 373 365 newnode->builtin = bt; … … 375 367 } // DeclarationNode::newBuiltinType 376 368 377 DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) { 378 DeclarationNode * newnode = new DeclarationNode; 379 newnode->type = nullptr; 380 // newnode->attr.name = name; 381 newnode->name = name; 369 DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) { 370 DeclarationNode *newnode = new DeclarationNode; 371 newnode->type = new TypeData( TypeData::Attr ); 372 newnode->attr.name = assign_strptr( name ); 382 373 newnode->attr.expr = expr; 383 374 return newnode; 384 375 } 385 376 386 DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) { 387 DeclarationNode * newnode = new DeclarationNode; 388 newnode->type = nullptr; 389 // newnode->attr.name = name; 390 newnode->name = name; 377 DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) { 378 DeclarationNode *newnode = new DeclarationNode; 379 newnode->type = new TypeData( TypeData::Attr ); 380 newnode->attr.name = assign_strptr( name ); 391 381 newnode->attr.type = type; 392 382 return newnode; 383 } 384 385 static void addQualifiersToType( TypeData *&src, TypeData *dst ) { 386 if ( src && dst ) { 387 if ( src->forall && dst->kind == TypeData::Function ) { 388 if ( dst->forall ) { 389 dst->forall->appendList( src->forall ); 390 } else { 391 dst->forall = src->forall; 392 } // if 393 src->forall = 0; 394 } // if 395 if ( dst->base ) { 396 addQualifiersToType( src, dst->base ); 397 } else if ( dst->kind == TypeData::Function ) { 398 dst->base = src; 399 src = 0; 400 } else { 401 dst->qualifiers |= src->qualifiers; 402 } // if 403 } // if 393 404 } 394 405 … … 399 410 } // appendError 400 411 401 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData *dst ) {412 void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) { 402 413 TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization 403 414 … … 411 422 } // DeclarationNode::checkQualifiers 412 423 413 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {424 void DeclarationNode::checkStorageClasses( DeclarationNode *q ) { 414 425 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { 415 426 if ( storageClass == q->storageClass ) { // duplicate qualifier … … 423 434 } // DeclarationNode::copyStorageClasses 424 435 425 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode *q ) {436 DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) { 426 437 isInline = isInline || q->isInline; 427 438 isNoreturn = isNoreturn || q->isNoreturn; … … 434 445 } // DeclarationNode::copyStorageClasses 435 446 436 static void addQualifiersToType( TypeData *&src, TypeData * dst ) { 437 if ( src->forall && dst->kind == TypeData::Function ) { 438 if ( dst->forall ) { 439 dst->forall->appendList( src->forall ); 447 DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) { 448 if ( q ) { 449 checkStorageClasses( q ); 450 copyStorageClasses( q ); 451 if ( q->type ) { 452 if ( ! type ) { 453 type = new TypeData; 454 } else { 455 checkQualifiers( q->type, type ); 456 } // if 457 addQualifiersToType( q->type, type ); 458 if ( q->type && q->type->forall ) { 459 if ( type->forall ) { 460 type->forall->appendList( q->type->forall ); 461 } else { 462 if ( type->kind == TypeData::Aggregate ) { 463 type->aggregate.params = q->type->forall; 464 // change implicit typedef from TYPEDEFname to TYPEGENname 465 typedefTable.changeKind( type->aggregate.name, TypedefTable::TG ); 466 } else { 467 type->forall = q->type->forall; 468 } // if 469 } // if 470 q->type->forall = 0; 471 } // if 472 } // if 473 } // if 474 delete q; 475 return this; 476 } 477 478 static void addTypeToType( TypeData *&src, TypeData *&dst ) { 479 if ( src && dst ) { 480 if ( src->forall && dst->kind == TypeData::Function ) { 481 if ( dst->forall ) { 482 dst->forall->appendList( src->forall ); 483 } else { 484 dst->forall = src->forall; 485 } // if 486 src->forall = 0; 487 } // if 488 if ( dst->base ) { 489 addTypeToType( src, dst->base ); 440 490 } else { 441 dst->forall = src->forall; 442 } // if 443 src->forall = nullptr; 444 } // if 445 if ( dst->base ) { 446 addQualifiersToType( src, dst->base ); 447 } else if ( dst->kind == TypeData::Function ) { 448 dst->base = src; 449 src = nullptr; 450 } else { 451 dst->qualifiers |= src->qualifiers; 452 } // if 453 } // addQualifiersToType 454 455 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { 456 if ( ! q ) { delete q; return this; } 457 458 checkStorageClasses( q ); 459 copyStorageClasses( q ); 460 461 if ( ! q->type ) { 462 delete q; 463 return this; 464 } // if 465 466 if ( ! type ) { 467 type = q->type; // reuse this structure 468 q->type = nullptr; 469 delete q; 470 return this; 471 } // if 472 473 checkQualifiers( q->type, type ); 474 addQualifiersToType( q->type, type ); 475 476 if ( q->type->forall ) { 477 if ( type->forall ) { 478 type->forall->appendList( q->type->forall ); 479 } else { 480 if ( type->kind == TypeData::Aggregate ) { 481 type->aggregate.params = q->type->forall; 482 // change implicit typedef from TYPEDEFname to TYPEGENname 483 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); 484 } else { 485 type->forall = q->type->forall; 486 } // if 487 } // if 488 q->type->forall = nullptr; 489 } // if 490 delete q; 491 return this; 492 } // addQualifiers 493 494 static void addTypeToType( TypeData *&src, TypeData *&dst ) { 495 if ( src->forall && dst->kind == TypeData::Function ) { 496 if ( dst->forall ) { 497 dst->forall->appendList( src->forall ); 498 } else { 499 dst->forall = src->forall; 500 } // if 501 src->forall = nullptr; 502 } // if 503 if ( dst->base ) { 504 addTypeToType( src, dst->base ); 505 } else { 506 switch ( dst->kind ) { 507 case TypeData::Unknown: 508 src->qualifiers |= dst->qualifiers; 509 dst = src; 510 src = nullptr; 511 break; 512 case TypeData::Basic: 513 dst->qualifiers |= src->qualifiers; 514 if ( src->kind != TypeData::Unknown ) { 515 assert( src->kind == TypeData::Basic ); 516 517 if ( dst->basictype == DeclarationNode::NoBasicType ) { 518 dst->basictype = src->basictype; 519 } else if ( src->basictype != DeclarationNode::NoBasicType ) 520 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 521 522 if ( dst->complextype == DeclarationNode::NoComplexType ) { 523 dst->complextype = src->complextype; 524 } else if ( src->complextype != DeclarationNode::NoComplexType ) 525 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 526 527 if ( dst->signedness == DeclarationNode::NoSignedness ) { 528 dst->signedness = src->signedness; 529 } else if ( src->signedness != DeclarationNode::NoSignedness ) 530 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 531 532 if ( dst->length == DeclarationNode::NoLength ) { 533 dst->length = src->length; 534 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { 535 dst->length = DeclarationNode::LongLong; 536 } else if ( src->length != DeclarationNode::NoLength ) 537 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 538 } // if 539 break; 540 default: 541 switch ( src->kind ) { 542 case TypeData::Aggregate: 543 case TypeData::Enum: 544 dst->base = new TypeData( TypeData::AggregateInst ); 545 dst->base->aggInst.aggregate = src; 546 if ( src->kind == TypeData::Aggregate ) { 547 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 491 switch ( dst->kind ) { 492 case TypeData::Unknown: 493 src->qualifiers |= dst->qualifiers; 494 dst = src; 495 src = 0; 496 break; 497 case TypeData::Basic: 498 dst->qualifiers |= src->qualifiers; 499 if ( src->kind != TypeData::Unknown ) { 500 assert( src->kind == TypeData::Basic ); 501 502 if ( dst->basictype == DeclarationNode::NoBasicType ) { 503 dst->basictype = src->basictype; 504 } else if ( src->basictype != DeclarationNode::NoBasicType ) 505 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); 506 507 if ( dst->complextype == DeclarationNode::NoComplexType ) { 508 dst->complextype = src->complextype; 509 } else if ( src->complextype != DeclarationNode::NoComplexType ) 510 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); 511 512 if ( dst->signedness == DeclarationNode::NoSignedness ) { 513 dst->signedness = src->signedness; 514 } else if ( src->signedness != DeclarationNode::NoSignedness ) 515 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); 516 517 if ( dst->length == DeclarationNode::NoLength ) { 518 dst->length = src->length; 519 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { 520 dst->length = DeclarationNode::LongLong; 521 } else if ( src->length != DeclarationNode::NoLength ) 522 throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); 548 523 } // if 549 dst->base->qualifiers |= src->qualifiers;550 src = nullptr;551 524 break; 552 525 default: 553 if ( dst->forall ) { 554 dst->forall->appendList( src->forall ); 555 } else { 556 dst->forall = src->forall; 557 } // if 558 src->forall = nullptr; 559 dst->base = src; 560 src = nullptr; 526 switch ( src->kind ) { 527 case TypeData::Aggregate: 528 case TypeData::Enum: 529 dst->base = new TypeData( TypeData::AggregateInst ); 530 dst->base->aggInst.aggregate = src; 531 if ( src->kind == TypeData::Aggregate ) { 532 dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); 533 } // if 534 dst->base->qualifiers |= src->qualifiers; 535 src = 0; 536 break; 537 default: 538 if ( dst->forall ) { 539 dst->forall->appendList( src->forall ); 540 } else { 541 dst->forall = src->forall; 542 } // if 543 src->forall = 0; 544 dst->base = src; 545 src = 0; 546 } // switch 561 547 } // switch 562 } // switch563 } // if 564 } 565 566 DeclarationNode * DeclarationNode::addType( DeclarationNode *o ) {548 } // if 549 } // if 550 } 551 552 DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) { 567 553 if ( o ) { 568 554 checkStorageClasses( o ); … … 580 566 type = o->type; 581 567 } // if 582 o->type = nullptr;568 o->type = 0; 583 569 } else { 584 570 addTypeToType( o->type, type ); … … 598 584 } 599 585 600 DeclarationNode * DeclarationNode::addTypedef() {601 TypeData * newtype = new TypeData( TypeData::Symbolic );602 newtype->symbolic.params = nullptr;586 DeclarationNode *DeclarationNode::addTypedef() { 587 TypeData *newtype = new TypeData( TypeData::Symbolic ); 588 newtype->symbolic.params = 0; 603 589 newtype->symbolic.isTypedef = true; 604 newtype->symbolic.name = name ? new string( *name ) : nullptr;590 newtype->symbolic.name = name; 605 591 newtype->base = type; 606 592 type = newtype; … … 608 594 } 609 595 610 DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { 611 if ( variable.tyClass != NoTypeClass ) { 612 if ( variable.assertions ) { 613 variable.assertions->appendList( assertions ); 614 } else { 615 variable.assertions = assertions; 616 } // if 617 return this; 618 } // if 619 596 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) { 620 597 assert( type ); 621 598 switch ( type->kind ) { … … 627 604 } // if 628 605 break; 606 case TypeData::Variable: 607 if ( variable.assertions ) { 608 variable.assertions->appendList( assertions ); 609 } else { 610 variable.assertions = assertions; 611 } // if 612 break; 629 613 default: 630 614 assert( false ); … … 634 618 } 635 619 636 DeclarationNode * DeclarationNode::addName( string * newname ) { 637 assert( ! name ); 638 name = newname; 639 return this; 640 } 641 642 DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { 620 DeclarationNode *DeclarationNode::addName( std::string *newname ) { 621 name = assign_strptr( newname ); 622 return this; 623 } 624 625 DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) { 643 626 bitfieldWidth = size; 644 627 return this; 645 628 } 646 629 647 DeclarationNode * DeclarationNode::addVarArgs() {630 DeclarationNode *DeclarationNode::addVarArgs() { 648 631 assert( type ); 649 632 hasEllipsis = true; … … 651 634 } 652 635 653 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode *body ) {636 DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) { 654 637 assert( type ); 655 638 assert( type->kind == TypeData::Function ); 656 assert( ! type->function.body);639 assert( type->function.body == 0 ); 657 640 type->function.body = body; 658 641 type->function.hasBody = true; … … 660 643 } 661 644 662 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode *list ) {645 DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) { 663 646 assert( type ); 664 647 assert( type->kind == TypeData::Function ); 665 assert( ! type->function.oldDeclList);648 assert( type->function.oldDeclList == 0 ); 666 649 type->function.oldDeclList = list; 667 650 return this; 668 651 } 669 652 670 static void setBase( TypeData *&type, TypeData * newType ) {653 static void setBase( TypeData *&type, TypeData *newType ) { 671 654 if ( type ) { 672 TypeData * prevBase = type;673 TypeData * curBase = type->base;674 while ( curBase != nullptr) {655 TypeData *prevBase = type; 656 TypeData *curBase = type->base; 657 while ( curBase != 0 ) { 675 658 prevBase = curBase; 676 659 curBase = curBase->base; … … 682 665 } 683 666 684 DeclarationNode * DeclarationNode::addPointer( DeclarationNode *p ) {667 DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) { 685 668 if ( p ) { 686 669 assert( p->type->kind == TypeData::Pointer ); 687 670 setBase( type, p->type ); 688 p->type = nullptr;671 p->type = 0; 689 672 delete p; 690 673 } // if … … 692 675 } 693 676 694 DeclarationNode * DeclarationNode::addArray( DeclarationNode *a ) {677 DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) { 695 678 if ( a ) { 696 679 assert( a->type->kind == TypeData::Array ); 697 680 setBase( type, a->type ); 698 a->type = nullptr;681 a->type = 0; 699 682 delete a; 700 683 } // if … … 702 685 } 703 686 704 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode *p ) {687 DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) { 705 688 if ( p ) { 706 689 assert( p->type->kind == TypeData::Pointer ); … … 720 703 p->type->base = type; 721 704 } // switch 722 type = nullptr;705 type = 0; 723 706 } // if 724 707 delete this; … … 729 712 } 730 713 731 static TypeData * findLast( TypeData *a ) {714 static TypeData *findLast( TypeData *a ) { 732 715 assert( a ); 733 TypeData * cur = a;716 TypeData *cur = a; 734 717 while ( cur->base ) { 735 718 cur = cur->base; … … 738 721 } 739 722 740 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode *a ) {723 DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) { 741 724 if ( a ) { 742 725 assert( a->type->kind == TypeData::Array ); 743 TypeData * lastArray = findLast( a->type );726 TypeData *lastArray = findLast( a->type ); 744 727 if ( type ) { 745 728 switch ( type->kind ) { … … 756 739 lastArray->base = type; 757 740 } // switch 758 type = nullptr;741 type = 0; 759 742 } // if 760 743 delete this; … … 765 748 } 766 749 767 DeclarationNode * DeclarationNode::addParamList( DeclarationNode *params ) {768 TypeData * ftype = new TypeData( TypeData::Function );750 DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) { 751 TypeData *ftype = new TypeData( TypeData::Function ); 769 752 ftype->function.params = params; 770 753 setBase( type, ftype ); … … 772 755 } 773 756 774 static TypeData * addIdListToType( TypeData * type, DeclarationNode *ids ) {757 static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) { 775 758 if ( type ) { 776 759 if ( type->kind != TypeData::Function ) { … … 781 764 return type; 782 765 } else { 783 TypeData * newtype = new TypeData( TypeData::Function );766 TypeData *newtype = new TypeData( TypeData::Function ); 784 767 newtype->function.idList = ids; 785 768 return newtype; 786 769 } // if 787 } // addIdListToType788 789 DeclarationNode * DeclarationNode::addIdList( DeclarationNode *ids ) {770 } 771 772 DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) { 790 773 type = addIdListToType( type, ids ); 791 774 return this; 792 775 } 793 776 794 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { 777 DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) { 778 //assert 795 779 initializer = init; 796 780 return this; 797 781 } 798 782 799 DeclarationNode * DeclarationNode::cloneType( string * newName ) { 800 DeclarationNode * newnode = new DeclarationNode; 783 DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) { 784 DeclarationNode *newnode = new DeclarationNode; 785 TypeData *srcType = type; 786 while ( srcType->base ) { 787 srcType = srcType->base; 788 } // while 789 newnode->type = maybeClone( srcType ); 790 if ( newnode->type->kind == TypeData::AggregateInst ) { 791 // don't duplicate members 792 if ( newnode->type->aggInst.aggregate->kind == TypeData::Enum ) { 793 delete newnode->type->aggInst.aggregate->enumeration.constants; 794 newnode->type->aggInst.aggregate->enumeration.constants = 0; 795 } else { 796 assert( newnode->type->aggInst.aggregate->kind == TypeData::Aggregate ); 797 delete newnode->type->aggInst.aggregate->aggregate.fields; 798 newnode->type->aggInst.aggregate->aggregate.fields = 0; 799 } // if 800 } // if 801 newnode->type->forall = maybeClone( type->forall ); 802 assert( storageClass == NoStorageClass ); 803 newnode->copyStorageClasses( this ); 804 newnode->name = assign_strptr( newName ); 805 return newnode; 806 } 807 808 DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) { 809 if ( o ) { 810 o->copyStorageClasses( this ); 811 if ( type ) { 812 TypeData *srcType = type; 813 while ( srcType->base ) { 814 srcType = srcType->base; 815 } // while 816 TypeData *newType = srcType->clone(); 817 if ( newType->kind == TypeData::AggregateInst ) { 818 // don't duplicate members 819 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 820 delete newType->aggInst.aggregate->enumeration.constants; 821 newType->aggInst.aggregate->enumeration.constants = 0; 822 } else { 823 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 824 delete newType->aggInst.aggregate->aggregate.fields; 825 newType->aggInst.aggregate->aggregate.fields = 0; 826 } // if 827 } // if 828 newType->forall = maybeClone( type->forall ); 829 if ( ! o->type ) { 830 o->type = newType; 831 } else { 832 addTypeToType( newType, o->type ); 833 delete newType; 834 } // if 835 } // if 836 } // if 837 return o; 838 } 839 840 DeclarationNode *DeclarationNode::cloneType( string *newName ) { 841 DeclarationNode *newnode = new DeclarationNode; 801 842 newnode->type = maybeClone( type ); 802 843 assert( storageClass == NoStorageClass ); 803 844 newnode->copyStorageClasses( this ); 804 assert( newName ); 805 newnode->name = newName; 806 return newnode; 807 } 808 809 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { 810 if ( ! o ) return nullptr; 811 812 o->copyStorageClasses( this ); 845 newnode->name = assign_strptr( newName ); 846 return newnode; 847 } 848 849 DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) { 850 if ( o ) { 851 assert( storageClass == NoStorageClass ); 852 o->copyStorageClasses( this ); 853 if ( type ) { 854 TypeData *newType = type->clone(); 855 if ( ! o->type ) { 856 o->type = newType; 857 } else { 858 addTypeToType( newType, o->type ); 859 delete newType; 860 } // if 861 } // if 862 } // if 863 delete o; 864 return o; 865 } 866 867 DeclarationNode *DeclarationNode::extractAggregate() const { 813 868 if ( type ) { 814 TypeData * srcType = type; 815 816 while ( srcType->base ) { 817 srcType = srcType->base; 818 } // while 819 820 TypeData * newType = srcType->clone(); 821 if ( newType->kind == TypeData::AggregateInst ) { 822 // don't duplicate members 823 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { 824 delete newType->aggInst.aggregate->enumeration.constants; 825 newType->aggInst.aggregate->enumeration.constants = nullptr; 826 } else { 827 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); 828 delete newType->aggInst.aggregate->aggregate.fields; 829 newType->aggInst.aggregate->aggregate.fields = nullptr; 830 } // if 831 } // if 832 833 newType->forall = maybeClone( type->forall ); 834 if ( ! o->type ) { 835 o->type = newType; 836 } else { 837 addTypeToType( newType, o->type ); 838 delete newType; 839 } // if 840 } // if 841 return o; 842 } 843 844 DeclarationNode * DeclarationNode::extractAggregate() const { 845 if ( type ) { 846 TypeData * ret = typeextractAggregate( type ); 869 TypeData *ret = typeextractAggregate( type ); 847 870 if ( ret ) { 848 DeclarationNode * newnode = new DeclarationNode;871 DeclarationNode *newnode = new DeclarationNode; 849 872 newnode->type = ret; 850 873 return newnode; 851 874 } // if 852 875 } // if 853 return nullptr;854 } 855 856 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {876 return 0; 877 } 878 879 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) { 857 880 SemanticError errors; 858 881 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); 859 const DeclarationNode * cur = firstNode; 860 882 const DeclarationNode *cur = firstNode; 861 883 while ( cur ) { 862 884 try { 863 if ( DeclarationNode * extr = cur->extractAggregate() ) {885 if ( DeclarationNode *extr = cur->extractAggregate() ) { 864 886 // handle the case where a structure declaration is contained within an object or type declaration 865 Declaration * decl = extr->build();887 Declaration *decl = extr->build(); 866 888 if ( decl ) { 867 * out++ = decl;889 *out++ = decl; 868 890 } // if 869 891 delete extr; 870 892 } // if 871 872 Declaration * decl = cur->build(); 893 Declaration *decl = cur->build(); 873 894 if ( decl ) { 874 * out++ = decl; 875 } // if 876 } catch( SemanticError &e ) { 877 errors.append( e ); 878 } // try 879 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 880 } // while 881 882 if ( ! errors.isEmpty() ) { 883 throw errors; 884 } // if 885 } // buildList 886 887 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 888 SemanticError errors; 889 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 890 const DeclarationNode * cur = firstNode; 891 while ( cur ) { 892 try { 893 Declaration * decl = cur->build(); 894 if ( decl ) { 895 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 896 * out++ = dwt; 897 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 898 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 899 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 900 delete agg; 901 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 902 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 903 * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); 904 } // if 895 *out++ = decl; 905 896 } // if 906 897 } catch( SemanticError &e ) { … … 912 903 throw errors; 913 904 } // if 914 } // buildList915 916 void build TypeList( const DeclarationNode * firstNode, std::list<Type * > &outputList ) {905 } 906 907 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) { 917 908 SemanticError errors; 918 std::back_insert_iterator< std::list< Type * > > out( outputList ); 919 const DeclarationNode * cur = firstNode; 920 909 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 910 const DeclarationNode *cur = firstNode; 921 911 while ( cur ) { 922 912 try { 923 * out++ = cur->buildType(); 913 Declaration *decl = cur->build(); 914 if ( decl ) { 915 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 916 *out++ = dwt; 917 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) { 918 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 919 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); 920 delete agg; 921 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) { 922 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 923 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 ); 924 } // if 925 } // if 924 926 } catch( SemanticError &e ) { 925 927 errors.append( e ); … … 927 929 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 928 930 } // while 929 930 931 if ( ! errors.isEmpty() ) { 931 932 throw errors; 932 933 } // if 933 } // buildTypeList 934 935 Declaration * DeclarationNode::build() const { 934 } 935 936 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) { 937 SemanticError errors; 938 std::back_insert_iterator< std::list< Type * > > out( outputList ); 939 const DeclarationNode *cur = firstNode; 940 while ( cur ) { 941 try { 942 *out++ = cur->buildType(); 943 } catch( SemanticError &e ) { 944 errors.append( e ); 945 } // try 946 cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); 947 } // while 948 if ( ! errors.isEmpty() ) { 949 throw errors; 950 } // if 951 } 952 953 Declaration *DeclarationNode::build() const { 936 954 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this ); 937 938 // if ( variable.name ) {939 if ( variable.tyClass != NoTypeClass ) {940 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };941 // TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );942 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );943 buildList( variable.assertions, ret->get_assertions() );944 return ret;945 } // if946 947 955 if ( type ) { 948 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 949 } // if 950 956 if ( type->kind == TypeData::Variable ) { 957 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; 958 TypeDecl * ret = new TypeDecl( variable.name, DeclarationNode::NoStorageClass, 0, kindMap[ variable.tyClass ] ); 959 buildList( variable.assertions, ret->get_assertions() ); 960 return ret; 961 } else { 962 return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension ); 963 } // if 964 } // if 951 965 if ( ! isInline && ! isNoreturn ) { 952 assertf( name, "ObjectDecl are assumed to have names\n" ); 953 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 954 } // if 955 966 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension ); 967 } // if 956 968 throw SemanticError( "invalid function specifier ", this ); 957 969 } 958 970 959 Type * DeclarationNode::buildType() const {971 Type *DeclarationNode::buildType() const { 960 972 assert( type ); 961 962 if ( attr.expr ) {963 // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );964 return new AttrType( buildQualifiers( type ), *name, attr.expr->build() );965 } else if ( attr.type ) {966 // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );967 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType() );968 } // if969 973 970 974 switch ( type->kind ) { 971 975 case TypeData::Enum: 972 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );976 return new EnumInstType( buildQualifiers( type ), type->enumeration.name ); 973 977 case TypeData::Aggregate: { 974 ReferenceToType * ret;978 ReferenceToType *ret; 975 979 switch ( type->aggregate.kind ) { 976 980 case DeclarationNode::Struct: 977 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );981 ret = new StructInstType( buildQualifiers( type ), type->aggregate.name ); 978 982 break; 979 983 case DeclarationNode::Union: 980 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );984 ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name ); 981 985 break; 982 986 case DeclarationNode::Trait: 983 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );987 ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name ); 984 988 break; 985 989 default: … … 990 994 } 991 995 case TypeData::Symbolic: { 992 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );996 TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false ); 993 997 buildList( type->symbolic.actuals, ret->get_parameters() ); 998 return ret; 999 } 1000 case TypeData::Attr: { 1001 assert( type->kind == TypeData::Attr ); 1002 // assert( type->attr ); 1003 AttrType * ret; 1004 if ( attr.expr ) { 1005 ret = new AttrType( buildQualifiers( type ), attr.name, attr.expr->build() ); 1006 } else { 1007 assert( attr.type ); 1008 ret = new AttrType( buildQualifiers( type ), attr.name, attr.type->buildType() ); 1009 } // if 994 1010 return ret; 995 1011 }
Note:
See TracChangeset
for help on using the changeset viewer.