source: src/Parser/DeclarationNode.cc@ 258eb5c9

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 258eb5c9 was 1db21619, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add CFA flag, remove -p from cc1, typedef on functions become function declarations, move isInline/isNoreturn to Declaration, cleaned up label code

  • Property mode set to 100644
File size: 28.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
[1db21619]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 14 14:46:32 2015
13// Update Count : 126
[b87a5ed]14//
15
[51b73452]16#include <string>
17#include <list>
18#include <iterator>
19#include <algorithm>
20#include <cassert>
21
22#include "TypeData.h"
[bdd516a]23
[68cd1ce]24#include "SynTree/Declaration.h"
25#include "SynTree/Expression.h"
[51b73452]26
[de62360d]27#include "Parser.h"
28#include "TypedefTable.h"
29extern TypedefTable typedefTable;
30
[51b73452]31using namespace std;
32
[bdd516a]33// These must remain in the same order as the corresponding DeclarationNode enumerations.
[68cd1ce]34const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };
[bdd516a]35const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
[17cd4eb]36const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
[68cd1ce]37const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
38const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
[51b73452]39const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
40
41UniqueName DeclarationNode::anonymous( "__anonymous" );
42
43extern LinkageSpec::Type linkage; /* defined in cfa.y */
44
[3848e0e]45DeclarationNode *DeclarationNode::clone() const {
[b87a5ed]46 DeclarationNode *newnode = new DeclarationNode;
47 newnode->type = maybeClone( type );
48 newnode->name = name;
49 newnode->storageClasses = storageClasses;
50 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
51 newnode->hasEllipsis = hasEllipsis;
52 newnode->initializer = initializer;
53 newnode->next = maybeClone( next );
54 newnode->linkage = linkage;
55 return newnode;
[3848e0e]56}
57
58DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
59}
60
61DeclarationNode::~DeclarationNode() {
[b87a5ed]62 delete type;
63 delete bitfieldWidth;
64 delete initializer;
[3848e0e]65}
66
67bool DeclarationNode::get_hasEllipsis() const {
[b87a5ed]68 return hasEllipsis;
[3848e0e]69}
70
71void DeclarationNode::print( std::ostream &os, int indent ) const {
[59db689]72 os << string( indent, ' ' );
[b87a5ed]73 if ( name == "" ) {
74 os << "unnamed: ";
75 } else {
76 os << name << ": ";
[68cd1ce]77 } // if
[51b73452]78
[b87a5ed]79 if ( linkage != LinkageSpec::Cforall ) {
80 os << LinkageSpec::toString( linkage ) << " ";
[68cd1ce]81 } // if
[3848e0e]82
[68cd1ce]83 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
[b87a5ed]84 if ( type ) {
85 type->print( os, indent );
86 } else {
87 os << "untyped entity ";
[68cd1ce]88 } // if
[3848e0e]89
[b87a5ed]90 if ( bitfieldWidth ) {
[59db689]91 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]92 bitfieldWidth->printOneLine( os );
[68cd1ce]93 } // if
[3848e0e]94
[b87a5ed]95 if ( initializer != 0 ) {
[59db689]96 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]97 initializer->printOneLine( os );
[68cd1ce]98 } // if
[3848e0e]99
[b87a5ed]100 os << endl;
[51b73452]101}
102
[3848e0e]103void DeclarationNode::printList( std::ostream &os, int indent ) const {
[b87a5ed]104 ParseNode::printList( os, indent );
105 if ( hasEllipsis ) {
106 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]107 } // if
[51b73452]108}
109
[d9a0e76]110DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
[b87a5ed]111 DeclarationNode *newnode = new DeclarationNode;
112 newnode->name = assign_strptr( name );
[3848e0e]113
[b87a5ed]114 newnode->type = new TypeData( TypeData::Function );
115 newnode->type->function->params = param;
116 newnode->type->function->newStyle = newStyle;
117 newnode->type->function->body = body;
[3848e0e]118
[b87a5ed]119 if ( body ) {
120 newnode->type->function->hasBody = true;
[68cd1ce]121 } // if
[51b73452]122
[b87a5ed]123 if ( ret ) {
124 newnode->type->base = ret->type;
125 ret->type = 0;
126 delete ret;
[68cd1ce]127 } // if
[51b73452]128
[b87a5ed]129 return newnode;
[3848e0e]130}
131
132DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
[b87a5ed]133 DeclarationNode *newnode = new DeclarationNode;
134 newnode->type = new TypeData();
135 newnode->type->qualifiers.push_back( q );
136 return newnode;
[3848e0e]137}
138
[68cd1ce]139DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
[b87a5ed]140 DeclarationNode *newnode = new DeclarationNode;
141 newnode->storageClasses.push_back( sc );
142 return newnode;
[3848e0e]143}
144
145DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
[b87a5ed]146 DeclarationNode *newnode = new DeclarationNode;
147 newnode->type = new TypeData( TypeData::Basic );
148 newnode->type->basic->typeSpec.push_back( bt );
149 return newnode;
[3848e0e]150}
151
152DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
[b87a5ed]153 DeclarationNode *newnode = new DeclarationNode;
154 newnode->type = new TypeData( TypeData::Basic );
155 newnode->type->basic->modifiers.push_back( mod );
156 return newnode;
[3848e0e]157}
158
[59db689]159DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
[b87a5ed]160 DeclarationNode *newnode = new DeclarationNode;
161 newnode->type = new TypeData( TypeData::Unknown );
162 newnode->type->forall = forall;
163 return newnode;
[3848e0e]164}
165
[59db689]166DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
[b87a5ed]167 DeclarationNode *newnode = new DeclarationNode;
168 newnode->type = new TypeData( TypeData::SymbolicInst );
169 newnode->type->symbolic->name = assign_strptr( name );
170 newnode->type->symbolic->isTypedef = true;
171 newnode->type->symbolic->params = 0;
172 return newnode;
[3848e0e]173}
174
[2871210]175DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields ) {
[b87a5ed]176 DeclarationNode *newnode = new DeclarationNode;
177 newnode->type = new TypeData( TypeData::Aggregate );
178 newnode->type->aggregate->kind = kind;
179 newnode->type->aggregate->name = assign_strptr( name );
[2871210]180 if ( newnode->type->aggregate->name == "" ) { // anonymous aggregate ?
[b87a5ed]181 newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
[2871210]182 } else {
183 // SKULLDUGGERY: generate a typedef for the aggregate name so that the aggregate does not have to be qualified
184 // by "struct"
185 typedefTable.addToEnclosingScope( newnode->type->aggregate->name, TypedefTable::TD );
186 DeclarationNode *typedf = new DeclarationNode;
187 typedf->name = newnode->type->aggregate->name;
188 newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
[68cd1ce]189 } // if
[b87a5ed]190 newnode->type->aggregate->actuals = actuals;
[721f17a]191 newnode->type->aggregate->fields = fields;
[b87a5ed]192 return newnode;
[3848e0e]193}
194
195DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
[b87a5ed]196 DeclarationNode *newnode = new DeclarationNode;
197 newnode->name = assign_strptr( name );
198 newnode->type = new TypeData( TypeData::Enum );
199 newnode->type->enumeration->name = newnode->name;
[2871210]200 if ( newnode->type->enumeration->name == "" ) { // anonymous enumeration ?
[b87a5ed]201 newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
[2871210]202 } else {
203 // SKULLDUGGERY: generate a typedef for the enumeration name so that the enumeration does not have to be
204 // qualified by "enum"
205 typedefTable.addToEnclosingScope( newnode->type->enumeration->name, TypedefTable::TD );
206 DeclarationNode *typedf = new DeclarationNode;
207 typedf->name = newnode->type->enumeration->name;
208 newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
[68cd1ce]209 } // if
[b87a5ed]210 newnode->type->enumeration->constants = constants;
211 return newnode;
[3848e0e]212}
213
[59db689]214DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
[b87a5ed]215 DeclarationNode *newnode = new DeclarationNode;
216 newnode->name = assign_strptr( name );
217 // do something with the constant
218 return newnode;
[3848e0e]219}
220
[59db689]221DeclarationNode *DeclarationNode::newName( std::string *name ) {
[b87a5ed]222 DeclarationNode *newnode = new DeclarationNode;
223 newnode->name = assign_strptr( name );
224 return newnode;
[3848e0e]225}
226
[59db689]227DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
[b87a5ed]228 DeclarationNode *newnode = new DeclarationNode;
229 newnode->type = new TypeData( TypeData::SymbolicInst );
230 newnode->type->symbolic->name = assign_strptr( name );
231 newnode->type->symbolic->isTypedef = false;
232 newnode->type->symbolic->actuals = params;
233 return newnode;
[3848e0e]234}
235
[59db689]236DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {
[b87a5ed]237 DeclarationNode *newnode = new DeclarationNode;
238 newnode->name = assign_strptr( name );
239 newnode->type = new TypeData( TypeData::Variable );
240 newnode->type->variable->tyClass = tc;
241 newnode->type->variable->name = newnode->name;
242 return newnode;
[3848e0e]243}
244
245DeclarationNode *DeclarationNode::newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
[b87a5ed]246 DeclarationNode *newnode = new DeclarationNode;
247 newnode->type = new TypeData( TypeData::Aggregate );
248 newnode->type->aggregate->kind = Context;
249 newnode->type->aggregate->params = params;
[721f17a]250 newnode->type->aggregate->fields = asserts;
[b87a5ed]251 newnode->type->aggregate->name = assign_strptr( name );
252 return newnode;
[3848e0e]253}
254
255DeclarationNode *DeclarationNode::newContextUse( std::string *name, ExpressionNode *params ) {
[b87a5ed]256 DeclarationNode *newnode = new DeclarationNode;
257 newnode->type = new TypeData( TypeData::AggregateInst );
258 newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate );
259 newnode->type->aggInst->aggregate->aggregate->kind = Context;
260 newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
261 newnode->type->aggInst->params = params;
262 return newnode;
[3848e0e]263}
264
265DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
[b87a5ed]266 DeclarationNode *newnode = new DeclarationNode;
267 newnode->name = assign_strptr( name );
268 newnode->type = new TypeData( TypeData::Symbolic );
269 newnode->type->symbolic->isTypedef = false;
270 newnode->type->symbolic->params = typeParams;
271 newnode->type->symbolic->name = newnode->name;
272 return newnode;
[3848e0e]273}
274
275DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
[b87a5ed]276 DeclarationNode *newnode = new DeclarationNode;
277 newnode->type = new TypeData( TypeData::Pointer );
278 return newnode->addQualifiers( qualifiers );
[3848e0e]279}
280
281DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
[b87a5ed]282 DeclarationNode *newnode = new DeclarationNode;
283 newnode->type = new TypeData( TypeData::Array );
284 newnode->type->array->dimension = size;
285 newnode->type->array->isStatic = isStatic;
[71bd8c6]286 if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantNode *>( newnode->type->array->dimension ) ) {
287 newnode->type->array->isVarLen = false;
288 } else {
289 newnode->type->array->isVarLen = true;
290 } // if
[b87a5ed]291 return newnode->addQualifiers( qualifiers );
[3848e0e]292}
293
294DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
[b87a5ed]295 DeclarationNode *newnode = new DeclarationNode;
296 newnode->type = new TypeData( TypeData::Array );
297 newnode->type->array->dimension = 0;
298 newnode->type->array->isStatic = false;
299 newnode->type->array->isVarLen = true;
300 return newnode->addQualifiers( qualifiers );
[3848e0e]301}
302
303DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
[b87a5ed]304 DeclarationNode *newnode = new DeclarationNode;
305 newnode->bitfieldWidth = size;
306 return newnode;
[3848e0e]307}
308
309DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
[b87a5ed]310 DeclarationNode *newnode = new DeclarationNode;
311 newnode->type = new TypeData( TypeData::Tuple );
312 newnode->type->tuple->members = members;
313 return newnode;
[3848e0e]314}
315
316DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
[b87a5ed]317 DeclarationNode *newnode = new DeclarationNode;
318 newnode->type = new TypeData( TypeData::Typeof );
319 newnode->type->typeexpr->expr = expr;
320 return newnode;
[3848e0e]321}
322
323DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) {
[b87a5ed]324 DeclarationNode *newnode = new DeclarationNode;
325 newnode->type = new TypeData( TypeData::Attr );
326 newnode->type->attr->name = assign_strptr( name );
327 newnode->type->attr->expr = expr;
328 return newnode;
[3848e0e]329}
330
331DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) {
[b87a5ed]332 DeclarationNode *newnode = new DeclarationNode;
333 newnode->type = new TypeData( TypeData::Attr );
334 newnode->type->attr->name = assign_strptr( name );
335 newnode->type->attr->type = type;
336 return newnode;
[3848e0e]337}
338
339static void addQualifiersToType( TypeData *&src, TypeData *dst ) {
[b87a5ed]340 if ( src && dst ) {
341 if ( src->forall && dst->kind == TypeData::Function ) {
342 if ( dst->forall ) {
343 dst->forall->appendList( src->forall );
344 } else {
345 dst->forall = src->forall;
[68cd1ce]346 } // if
[b87a5ed]347 src->forall = 0;
[68cd1ce]348 } // if
[b87a5ed]349 if ( dst->base ) {
350 addQualifiersToType( src, dst->base );
351 } else if ( dst->kind == TypeData::Function ) {
352 dst->base = src;
353 src = 0;
354 } else {
355 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
[68cd1ce]356 } // if
357 } // if
[3848e0e]358}
[b87a5ed]359
[3848e0e]360DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
[b87a5ed]361 if ( q ) {
362 storageClasses.splice( storageClasses.end(), q->storageClasses );
363 if ( q->type ) {
364 if ( ! type ) {
365 type = new TypeData;
[68cd1ce]366 } // if
[b87a5ed]367 addQualifiersToType( q->type, type );
368 if ( q->type && q->type->forall ) {
369 if ( type->forall ) {
370 type->forall->appendList( q->type->forall );
371 } else {
[68cd1ce]372 if ( type->kind == TypeData::Aggregate ) {
373 type->aggregate->params = q->type->forall;
[721f17a]374 // change implicit typedef from TYPEDEFname to TYPEGENname
375 typedefTable.changeKind( type->aggregate->name, TypedefTable::TG );
[68cd1ce]376 } else {
377 type->forall = q->type->forall;
378 } // if
379 } // if
[b87a5ed]380 q->type->forall = 0;
[68cd1ce]381 } // if
382 } // if
383 } // if
[b87a5ed]384 delete q;
385 return this;
[3848e0e]386}
387
388DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
[b87a5ed]389 storageClasses = q->storageClasses;
390 return this;
[3848e0e]391}
392
393static void addTypeToType( TypeData *&src, TypeData *&dst ) {
[b87a5ed]394 if ( src && dst ) {
395 if ( src->forall && dst->kind == TypeData::Function ) {
396 if ( dst->forall ) {
397 dst->forall->appendList( src->forall );
398 } else {
399 dst->forall = src->forall;
[68cd1ce]400 } // if
[b87a5ed]401 src->forall = 0;
[68cd1ce]402 } // if
[b87a5ed]403 if ( dst->base ) {
404 addTypeToType( src, dst->base );
405 } else {
406 switch ( dst->kind ) {
407 case TypeData::Unknown:
408 src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
409 dst = src;
410 src = 0;
411 break;
412 case TypeData::Basic:
413 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
414 if ( src->kind != TypeData::Unknown ) {
415 assert( src->kind == TypeData::Basic );
416 dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
417 dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
[68cd1ce]418 } // if
[b87a5ed]419 break;
420 default:
421 switch ( src->kind ) {
422 case TypeData::Aggregate:
423 case TypeData::Enum:
424 dst->base = new TypeData( TypeData::AggregateInst );
425 dst->base->aggInst->aggregate = src;
426 if ( src->kind == TypeData::Aggregate ) {
427 dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
[68cd1ce]428 } // if
[b87a5ed]429 dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
430 src = 0;
431 break;
432 default:
433 if ( dst->forall ) {
434 dst->forall->appendList( src->forall );
435 } else {
436 dst->forall = src->forall;
[68cd1ce]437 } // if
[b87a5ed]438 src->forall = 0;
439 dst->base = src;
440 src = 0;
[68cd1ce]441 } // switch
442 } // switch
443 } // if
444 } // if
[3848e0e]445}
446
447DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
[b87a5ed]448 if ( o ) {
449 storageClasses.splice( storageClasses.end(), o->storageClasses );
450 if ( o->type ) {
451 if ( ! type ) {
452 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
453 type = new TypeData( TypeData::AggregateInst );
454 type->aggInst->aggregate = o->type;
455 if ( o->type->kind == TypeData::Aggregate ) {
456 type->aggInst->params = maybeClone( o->type->aggregate->actuals );
[68cd1ce]457 } // if
[b87a5ed]458 type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
459 } else {
460 type = o->type;
[68cd1ce]461 } // if
[b87a5ed]462 o->type = 0;
463 } else {
464 addTypeToType( o->type, type );
[68cd1ce]465 } // if
466 } // if
[b87a5ed]467 if ( o->bitfieldWidth ) {
468 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]469 } // if
[71bd8c6]470
471 // there may be typedefs chained onto the type
472 if ( o->get_link() ) {
473 set_link( o->get_link()->clone() );
[1db21619]474 } // if
[68cd1ce]475 } // if
[b87a5ed]476 delete o;
477 return this;
[3848e0e]478}
479
480DeclarationNode *DeclarationNode::addTypedef() {
[b87a5ed]481 TypeData *newtype = new TypeData( TypeData::Symbolic );
482 newtype->symbolic->params = 0;
483 newtype->symbolic->isTypedef = true;
484 newtype->symbolic->name = name;
485 newtype->base = type;
486 type = newtype;
487 return this;
[3848e0e]488}
489
[59db689]490DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
[b87a5ed]491 assert( type );
492 switch ( type->kind ) {
493 case TypeData::Symbolic:
494 if ( type->symbolic->assertions ) {
495 type->symbolic->assertions->appendList( assertions );
496 } else {
497 type->symbolic->assertions = assertions;
[68cd1ce]498 } // if
[b87a5ed]499 break;
500 case TypeData::Variable:
501 if ( type->variable->assertions ) {
502 type->variable->assertions->appendList( assertions );
503 } else {
504 type->variable->assertions = assertions;
[68cd1ce]505 } // if
[b87a5ed]506 break;
507 default:
508 assert( false );
[68cd1ce]509 } // switch
[b87a5ed]510
511 return this;
[51b73452]512}
513
[59db689]514DeclarationNode *DeclarationNode::addName( std::string *newname ) {
[b87a5ed]515 name = assign_strptr( newname );
516 return this;
[51b73452]517}
518
[3848e0e]519DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
[b87a5ed]520 bitfieldWidth = size;
521 return this;
[51b73452]522}
523
[3848e0e]524DeclarationNode *DeclarationNode::addVarArgs() {
[b87a5ed]525 assert( type );
526 hasEllipsis = true;
527 return this;
[51b73452]528}
529
[3848e0e]530DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
[b87a5ed]531 assert( type );
532 assert( type->kind == TypeData::Function );
533 assert( type->function->body == 0 );
534 type->function->body = body;
535 type->function->hasBody = true;
536 return this;
[51b73452]537}
538
[3848e0e]539DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
[b87a5ed]540 assert( type );
541 assert( type->kind == TypeData::Function );
542 assert( type->function->oldDeclList == 0 );
543 type->function->oldDeclList = list;
544 return this;
[51b73452]545}
546
[68cd1ce]547static void setBase( TypeData *&type, TypeData *newType ) {
[b87a5ed]548 if ( type ) {
549 TypeData *prevBase = type;
550 TypeData *curBase = type->base;
[a32b204]551 while ( curBase != 0 ) {
[b87a5ed]552 prevBase = curBase;
553 curBase = curBase->base;
[68cd1ce]554 } // while
[b87a5ed]555 prevBase->base = newType;
556 } else {
557 type = newType;
[68cd1ce]558 } // if
[3848e0e]559}
560
561DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
[b87a5ed]562 if ( p ) {
563 assert( p->type->kind == TypeData::Pointer );
564 setBase( type, p->type );
565 p->type = 0;
566 delete p;
[68cd1ce]567 } // if
[b87a5ed]568 return this;
[3848e0e]569}
570
571DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
[b87a5ed]572 if ( a ) {
573 assert( a->type->kind == TypeData::Array );
574 setBase( type, a->type );
575 a->type = 0;
576 delete a;
[68cd1ce]577 } // if
[b87a5ed]578 return this;
[51b73452]579}
580
[3848e0e]581DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
[b87a5ed]582 if ( p ) {
583 assert( p->type->kind == TypeData::Pointer );
584 if ( type ) {
585 switch ( type->kind ) {
586 case TypeData::Aggregate:
587 case TypeData::Enum:
588 p->type->base = new TypeData( TypeData::AggregateInst );
589 p->type->base->aggInst->aggregate = type;
590 if ( type->kind == TypeData::Aggregate ) {
591 p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
[68cd1ce]592 } // if
[b87a5ed]593 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
594 break;
595
596 default:
597 p->type->base = type;
[68cd1ce]598 } // switch
[b87a5ed]599 type = 0;
[68cd1ce]600 } // if
[b87a5ed]601 delete this;
602 return p;
603 } else {
604 return this;
[68cd1ce]605 } // if
[51b73452]606}
607
[3848e0e]608static TypeData *findLast( TypeData *a ) {
[b87a5ed]609 assert( a );
610 TypeData *cur = a;
[a32b204]611 while ( cur->base ) {
[b87a5ed]612 cur = cur->base;
[68cd1ce]613 } // while
[b87a5ed]614 return cur;
[3848e0e]615}
616
617DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
[b87a5ed]618 if ( a ) {
619 assert( a->type->kind == TypeData::Array );
620 TypeData *lastArray = findLast( a->type );
621 if ( type ) {
622 switch ( type->kind ) {
623 case TypeData::Aggregate:
624 case TypeData::Enum:
625 lastArray->base = new TypeData( TypeData::AggregateInst );
626 lastArray->base->aggInst->aggregate = type;
627 if ( type->kind == TypeData::Aggregate ) {
628 lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
[68cd1ce]629 } // if
[b87a5ed]630 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
631 break;
632 default:
633 lastArray->base = type;
[68cd1ce]634 } // switch
[b87a5ed]635 type = 0;
[68cd1ce]636 } // if
[b87a5ed]637 delete this;
638 return a;
639 } else {
640 return this;
[68cd1ce]641 } // if
[51b73452]642}
[3848e0e]643
644DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
[b87a5ed]645 TypeData *ftype = new TypeData( TypeData::Function );
646 ftype->function->params = params;
647 setBase( type, ftype );
648 return this;
[3848e0e]649}
650
651static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
[b87a5ed]652 if ( type ) {
653 if ( type->kind != TypeData::Function ) {
654 type->base = addIdListToType( type->base, ids );
655 } else {
656 type->function->idList = ids;
[68cd1ce]657 } // if
[b87a5ed]658 return type;
[3848e0e]659 } else {
[b87a5ed]660 TypeData *newtype = new TypeData( TypeData::Function );
661 newtype->function->idList = ids;
662 return newtype;
[68cd1ce]663 } // if
[3848e0e]664}
[b87a5ed]665
[3848e0e]666DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
[b87a5ed]667 type = addIdListToType( type, ids );
668 return this;
[3848e0e]669}
670
671DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
[b87a5ed]672 //assert
673 initializer = init;
674 return this;
[3848e0e]675}
676
677DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) {
[b87a5ed]678 DeclarationNode *newnode = new DeclarationNode;
679 TypeData *srcType = type;
[a32b204]680 while ( srcType->base ) {
[b87a5ed]681 srcType = srcType->base;
[68cd1ce]682 } // while
[b87a5ed]683 newnode->type = maybeClone( srcType );
684 if ( newnode->type->kind == TypeData::AggregateInst ) {
685 // don't duplicate members
686 if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) {
687 delete newnode->type->aggInst->aggregate->enumeration->constants;
688 newnode->type->aggInst->aggregate->enumeration->constants = 0;
689 } else {
690 assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate );
[721f17a]691 delete newnode->type->aggInst->aggregate->aggregate->fields;
692 newnode->type->aggInst->aggregate->aggregate->fields = 0;
[68cd1ce]693 } // if
694 } // if
[b87a5ed]695 newnode->type->forall = maybeClone( type->forall );
696 newnode->storageClasses = storageClasses;
697 newnode->name = assign_strptr( newName );
698 return newnode;
[3848e0e]699}
700
701DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
[b87a5ed]702 if ( o ) {
703 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
704 if ( type ) {
705 TypeData *srcType = type;
[a32b204]706 while ( srcType->base ) {
[b87a5ed]707 srcType = srcType->base;
[68cd1ce]708 } // while
[b87a5ed]709 TypeData *newType = srcType->clone();
710 if ( newType->kind == TypeData::AggregateInst ) {
711 // don't duplicate members
712 if ( newType->aggInst->aggregate->kind == TypeData::Enum ) {
713 delete newType->aggInst->aggregate->enumeration->constants;
714 newType->aggInst->aggregate->enumeration->constants = 0;
715 } else {
716 assert( newType->aggInst->aggregate->kind == TypeData::Aggregate );
[721f17a]717 delete newType->aggInst->aggregate->aggregate->fields;
718 newType->aggInst->aggregate->aggregate->fields = 0;
[68cd1ce]719 } // if
720 } // if
[b87a5ed]721 newType->forall = maybeClone( type->forall );
722 if ( ! o->type ) {
723 o->type = newType;
724 } else {
725 addTypeToType( newType, o->type );
726 delete newType;
[68cd1ce]727 } // if
728 } // if
729 } // if
[b87a5ed]730 return o;
[3848e0e]731}
732
733DeclarationNode *DeclarationNode::cloneType( string *newName ) {
[b87a5ed]734 DeclarationNode *newnode = new DeclarationNode;
735 newnode->type = maybeClone( type );
736 newnode->storageClasses = storageClasses;
737 newnode->name = assign_strptr( newName );
738 return newnode;
[3848e0e]739}
740
741DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
[b87a5ed]742 if ( o ) {
743 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
744 if ( type ) {
745 TypeData *newType = type->clone();
746 if ( ! o->type ) {
747 o->type = newType;
748 } else {
749 addTypeToType( newType, o->type );
750 delete newType;
[68cd1ce]751 } // if
752 } // if
753 } // if
[b87a5ed]754 return o;
[51b73452]755}
756
[3848e0e]757DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
[b87a5ed]758 if ( node != 0 ) {
759 set_link( node );
[68cd1ce]760 } // if
[b87a5ed]761 return this;
[51b73452]762}
763
[3848e0e]764DeclarationNode *DeclarationNode::extractAggregate() const {
[b87a5ed]765 if ( type ) {
766 TypeData *ret = type->extractAggregate();
767 if ( ret ) {
768 DeclarationNode *newnode = new DeclarationNode;
769 newnode->type = ret;
770 return newnode;
[843054c2]771 } // if
772 } // if
773 return 0;
[3848e0e]774}
775
[a61fea9a]776void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
[b87a5ed]777 SemanticError errors;
[59db689]778 std::back_insert_iterator< std::list< Declaration *> > out( outputList );
[b87a5ed]779 const DeclarationNode *cur = firstNode;
[a32b204]780 while ( cur ) {
[b87a5ed]781 try {
782 if ( DeclarationNode *extr = cur->extractAggregate() ) {
783 // handle the case where a structure declaration is contained within an object or type declaration
784 Declaration *decl = extr->build();
785 if ( decl ) {
786 *out++ = decl;
[843054c2]787 } // if
788 } // if
[b87a5ed]789 Declaration *decl = cur->build();
790 if ( decl ) {
791 *out++ = decl;
[843054c2]792 } // if
[b87a5ed]793 } catch( SemanticError &e ) {
794 errors.append( e );
[843054c2]795 } // try
[59db689]796 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
[843054c2]797 } // while
[b87a5ed]798 if ( ! errors.isEmpty() ) {
799 throw errors;
[843054c2]800 } // if
[3848e0e]801}
802
[59db689]803void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
[b87a5ed]804 SemanticError errors;
[59db689]805 std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
[b87a5ed]806 const DeclarationNode *cur = firstNode;
[a32b204]807 while ( cur ) {
[b87a5ed]808 try {
[3848e0e]809/// if ( DeclarationNode *extr = cur->extractAggregate() ) {
[51b73452]810/// // handle the case where a structure declaration is contained within an object or type
811/// // declaration
812/// Declaration *decl = extr->build();
[3848e0e]813/// if ( decl ) {
814/// *out++ = decl;
[51b73452]815/// }
816/// }
[b87a5ed]817 Declaration *decl = cur->build();
818 if ( decl ) {
[59db689]819 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
[b87a5ed]820 *out++ = dwt;
[59db689]821 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
[b87a5ed]822 StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
[68cd1ce]823 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
[b87a5ed]824 delete agg;
[59db689]825 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
[b87a5ed]826 UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
[68cd1ce]827 *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
[843054c2]828 } // if
829 } // if
[b87a5ed]830 } catch( SemanticError &e ) {
831 errors.append( e );
[843054c2]832 } // try
[59db689]833 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
[843054c2]834 } // while
[b87a5ed]835 if ( ! errors.isEmpty() ) {
836 throw errors;
[843054c2]837 } // if
[3848e0e]838}
839
[59db689]840void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
[b87a5ed]841 SemanticError errors;
[59db689]842 std::back_insert_iterator< std::list< Type *> > out( outputList );
[b87a5ed]843 const DeclarationNode *cur = firstNode;
[a32b204]844 while ( cur ) {
[b87a5ed]845 try {
846 *out++ = cur->buildType();
847 } catch( SemanticError &e ) {
848 errors.append( e );
[843054c2]849 } // try
[59db689]850 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
[843054c2]851 } // while
[b87a5ed]852 if ( ! errors.isEmpty() ) {
853 throw errors;
[843054c2]854 } // if
[51b73452]855}
856
[3848e0e]857Declaration *DeclarationNode::build() const {
[843054c2]858 if ( type ) {
[de62360d]859 Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) );
[b87a5ed]860 return newDecl;
[843054c2]861 } // if
[de62360d]862 if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
[843054c2]863 return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
864 } // if
[de62360d]865 throw SemanticError( "invalid function specifier in declaration of ", this );
[51b73452]866}
867
[3848e0e]868Type *DeclarationNode::buildType() const {
[b87a5ed]869 assert( type );
[51b73452]870
[b87a5ed]871 switch ( type->kind ) {
872 case TypeData::Enum:
873 return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
874 case TypeData::Aggregate: {
875 ReferenceToType *ret;
876 switch ( type->aggregate->kind ) {
877 case DeclarationNode::Struct:
878 ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
879 break;
880 case DeclarationNode::Union:
881 ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
882 break;
883 case DeclarationNode::Context:
884 ret = new ContextInstType( type->buildQualifiers(), type->aggregate->name );
885 break;
886 default:
887 assert( false );
888 } // switch
889 buildList( type->aggregate->actuals, ret->get_parameters() );
890 return ret;
891 }
892 case TypeData::Symbolic: {
893 TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
894 buildList( type->symbolic->actuals, ret->get_parameters() );
895 return ret;
896 }
897 default:
898 return type->build();
899 } // switch
[3848e0e]900}
901
[68cd1ce]902DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
903 DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
904 for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
905 if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
906 if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ?
[b87a5ed]907 throw SemanticError( "invalid combination of storage classes in declaration of ", this );
[68cd1ce]908 } // if
909 ret = *i;
910 } // for
[b87a5ed]911 return ret;
[51b73452]912}
913
[de62360d]914bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
915 std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
916 if ( first == storageClasses.end() ) return false; // not found
917 first = std::find( ++first, storageClasses.end(), key ); // found
918 if ( first == storageClasses.end() ) return true; // not found again
919 throw SemanticError( "duplicate function specifier in declaration of ", this );
[51b73452]920}
[b87a5ed]921
922// Local Variables: //
923// tab-width: 4 //
924// mode: c++ //
925// compile-command: "make install" //
926// End: //
Note: See TracBrowser for help on using the repository browser.