source: src/Parser/DeclarationNode.cc@ dac593fd

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

more refactoring of parser code

  • Property mode set to 100644
File size: 29.3 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//
[974906e2]7// DeclarationNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
[8e9cbb2]11// Last Modified By : Peter A. Buhr
[d1625f8]12// Last Modified On : Tue Aug 9 08:39:20 2016
13// Update Count : 169
[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" };
[90c3b1c]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" };
[90c3b1c]40const char *DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
[51b73452]41
42UniqueName DeclarationNode::anonymous( "__anonymous" );
43
[984dce6]44extern LinkageSpec::Type linkage; // defined in parser.yy
[51b73452]45
[3848e0e]46DeclarationNode *DeclarationNode::clone() const {
[b87a5ed]47 DeclarationNode *newnode = new DeclarationNode;
48 newnode->type = maybeClone( type );
49 newnode->name = name;
50 newnode->storageClasses = storageClasses;
[7bf7fb9]51//PAB newnode->bitfieldWidth = maybeClone( bitfieldWidth );
52 newnode->bitfieldWidth = bitfieldWidth;
[b87a5ed]53 newnode->hasEllipsis = hasEllipsis;
54 newnode->initializer = initializer;
55 newnode->next = maybeClone( next );
56 newnode->linkage = linkage;
57 return newnode;
[984dce6]58} // DeclarationNode::clone
[3848e0e]59
60DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
61}
62
63DeclarationNode::~DeclarationNode() {
[b87a5ed]64 delete type;
65 delete bitfieldWidth;
66 delete initializer;
[3848e0e]67}
68
69bool DeclarationNode::get_hasEllipsis() const {
[b87a5ed]70 return hasEllipsis;
[3848e0e]71}
72
73void DeclarationNode::print( std::ostream &os, int indent ) const {
[59db689]74 os << string( indent, ' ' );
[b87a5ed]75 if ( name == "" ) {
76 os << "unnamed: ";
77 } else {
78 os << name << ": ";
[68cd1ce]79 } // if
[51b73452]80
[b87a5ed]81 if ( linkage != LinkageSpec::Cforall ) {
82 os << LinkageSpec::toString( linkage ) << " ";
[68cd1ce]83 } // if
[3848e0e]84
[68cd1ce]85 printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
[b87a5ed]86 if ( type ) {
87 type->print( os, indent );
88 } else {
89 os << "untyped entity ";
[68cd1ce]90 } // if
[3848e0e]91
[b87a5ed]92 if ( bitfieldWidth ) {
[59db689]93 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]94 bitfieldWidth->printOneLine( os );
[68cd1ce]95 } // if
[3848e0e]96
[b87a5ed]97 if ( initializer != 0 ) {
[59db689]98 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]99 initializer->printOneLine( os );
[974906e2]100 os << " maybe constructed? " << initializer->get_maybeConstructed();
101
[68cd1ce]102 } // if
[3848e0e]103
[b87a5ed]104 os << endl;
[51b73452]105}
106
[3848e0e]107void DeclarationNode::printList( std::ostream &os, int indent ) const {
[b87a5ed]108 ParseNode::printList( os, indent );
109 if ( hasEllipsis ) {
110 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]111 } // if
[51b73452]112}
113
[d9a0e76]114DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
[b87a5ed]115 DeclarationNode *newnode = new DeclarationNode;
116 newnode->name = assign_strptr( name );
[3848e0e]117
[b87a5ed]118 newnode->type = new TypeData( TypeData::Function );
119 newnode->type->function->params = param;
120 newnode->type->function->newStyle = newStyle;
121 newnode->type->function->body = body;
[984dce6]122 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
[3848e0e]123
[b87a5ed]124 if ( body ) {
125 newnode->type->function->hasBody = true;
[68cd1ce]126 } // if
[51b73452]127
[b87a5ed]128 if ( ret ) {
129 newnode->type->base = ret->type;
130 ret->type = 0;
131 delete ret;
[68cd1ce]132 } // if
[51b73452]133
[b87a5ed]134 return newnode;
[984dce6]135} // DeclarationNode::newFunction
[3848e0e]136
137DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
[b87a5ed]138 DeclarationNode *newnode = new DeclarationNode;
139 newnode->type = new TypeData();
140 newnode->type->qualifiers.push_back( q );
141 return newnode;
[984dce6]142} // DeclarationNode::newQualifier
[3848e0e]143
[68cd1ce]144DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
[b87a5ed]145 DeclarationNode *newnode = new DeclarationNode;
146 newnode->storageClasses.push_back( sc );
147 return newnode;
[984dce6]148} // DeclarationNode::newStorageClass
[3848e0e]149
150DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
[b87a5ed]151 DeclarationNode *newnode = new DeclarationNode;
152 newnode->type = new TypeData( TypeData::Basic );
153 newnode->type->basic->typeSpec.push_back( bt );
154 return newnode;
[984dce6]155} // DeclarationNode::newBasicType
[3848e0e]156
[90c3b1c]157DeclarationNode *DeclarationNode::newBuiltinType( BuiltinType bt ) {
158 DeclarationNode *newnode = new DeclarationNode;
159 newnode->type = new TypeData( TypeData::Builtin );
160 newnode->type->builtin->type = bt;
161 return newnode;
[984dce6]162} // DeclarationNode::newBuiltinType
[3848e0e]163
164DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
[b87a5ed]165 DeclarationNode *newnode = new DeclarationNode;
166 newnode->type = new TypeData( TypeData::Basic );
167 newnode->type->basic->modifiers.push_back( mod );
168 return newnode;
[984dce6]169} // DeclarationNode::newModifier
[3848e0e]170
[59db689]171DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
[b87a5ed]172 DeclarationNode *newnode = new DeclarationNode;
173 newnode->type = new TypeData( TypeData::Unknown );
174 newnode->type->forall = forall;
175 return newnode;
[984dce6]176} // DeclarationNode::newForall
[3848e0e]177
[59db689]178DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
[b87a5ed]179 DeclarationNode *newnode = new DeclarationNode;
180 newnode->type = new TypeData( TypeData::SymbolicInst );
181 newnode->type->symbolic->name = assign_strptr( name );
182 newnode->type->symbolic->isTypedef = true;
183 newnode->type->symbolic->params = 0;
184 return newnode;
[984dce6]185} // DeclarationNode::newFromTypedef
[3848e0e]186
[5d125e4]187DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
[b87a5ed]188 DeclarationNode *newnode = new DeclarationNode;
189 newnode->type = new TypeData( TypeData::Aggregate );
190 newnode->type->aggregate->kind = kind;
191 newnode->type->aggregate->name = assign_strptr( name );
[2871210]192 if ( newnode->type->aggregate->name == "" ) { // anonymous aggregate ?
[45161b4d]193 newnode->type->aggregate->name = anonymous.newName();
[68cd1ce]194 } // if
[b87a5ed]195 newnode->type->aggregate->actuals = actuals;
[721f17a]196 newnode->type->aggregate->fields = fields;
[5d125e4]197 newnode->type->aggregate->body = body;
[b87a5ed]198 return newnode;
[984dce6]199} // DeclarationNode::newAggregate
[3848e0e]200
201DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
[b87a5ed]202 DeclarationNode *newnode = new DeclarationNode;
203 newnode->name = assign_strptr( name );
204 newnode->type = new TypeData( TypeData::Enum );
205 newnode->type->enumeration->name = newnode->name;
[2871210]206 if ( newnode->type->enumeration->name == "" ) { // anonymous enumeration ?
[b87a5ed]207 newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
[68cd1ce]208 } // if
[b87a5ed]209 newnode->type->enumeration->constants = constants;
210 return newnode;
[984dce6]211} // DeclarationNode::newEnum
[3848e0e]212
[59db689]213DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
[b87a5ed]214 DeclarationNode *newnode = new DeclarationNode;
215 newnode->name = assign_strptr( name );
[90c3b1c]216 newnode->enumeratorValue = constant;
[984dce6]217 typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
[b87a5ed]218 return newnode;
[984dce6]219} // DeclarationNode::newEnumConstant
[3848e0e]220
[59db689]221DeclarationNode *DeclarationNode::newName( std::string *name ) {
[b87a5ed]222 DeclarationNode *newnode = new DeclarationNode;
223 newnode->name = assign_strptr( name );
224 return newnode;
[984dce6]225} // DeclarationNode::newName
[3848e0e]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;
[984dce6]234} // DeclarationNode::newFromTypeGen
[3848e0e]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;
[984dce6]243} // DeclarationNode::newTypeParam
[3848e0e]244
[4040425]245DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
[b87a5ed]246 DeclarationNode *newnode = new DeclarationNode;
247 newnode->type = new TypeData( TypeData::Aggregate );
[4040425]248 newnode->type->aggregate->kind = Trait;
[b87a5ed]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;
[984dce6]253} // DeclarationNode::newTrait
[3848e0e]254
[4040425]255DeclarationNode *DeclarationNode::newTraitUse( 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 );
[4040425]259 newnode->type->aggInst->aggregate->aggregate->kind = Trait;
[b87a5ed]260 newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
261 newnode->type->aggInst->params = params;
262 return newnode;
[984dce6]263} // DeclarationNode::newTraitUse
[3848e0e]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;
[984dce6]273} // DeclarationNode::newTypeDecl
[3848e0e]274
275DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
[b87a5ed]276 DeclarationNode *newnode = new DeclarationNode;
277 newnode->type = new TypeData( TypeData::Pointer );
278 return newnode->addQualifiers( qualifiers );
[984dce6]279} // DeclarationNode::newPointer
[3848e0e]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;
[d1625f8]286 if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantExpr *>( newnode->type->array->dimension->build() ) ) {
[71bd8c6]287 newnode->type->array->isVarLen = false;
288 } else {
289 newnode->type->array->isVarLen = true;
290 } // if
[b87a5ed]291 return newnode->addQualifiers( qualifiers );
[984dce6]292} // DeclarationNode::newArray
[3848e0e]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}
[974906e2]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
[974906e2]510
[b87a5ed]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 );
[974906e2]621 if ( type ) {
[b87a5ed]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}
[974906e2]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
[90c3b1c]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 ) {
[8e9cbb2]859 return type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
[843054c2]860 } // if
[de62360d]861 if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
[8e9cbb2]862 return (new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
[843054c2]863 } // if
[de62360d]864 throw SemanticError( "invalid function specifier in declaration of ", this );
[51b73452]865}
866
[3848e0e]867Type *DeclarationNode::buildType() const {
[b87a5ed]868 assert( type );
[974906e2]869
[b87a5ed]870 switch ( type->kind ) {
871 case TypeData::Enum:
872 return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
873 case TypeData::Aggregate: {
874 ReferenceToType *ret;
875 switch ( type->aggregate->kind ) {
876 case DeclarationNode::Struct:
877 ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
878 break;
879 case DeclarationNode::Union:
880 ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
881 break;
[4040425]882 case DeclarationNode::Trait:
883 ret = new TraitInstType( type->buildQualifiers(), type->aggregate->name );
[b87a5ed]884 break;
885 default:
886 assert( false );
887 } // switch
888 buildList( type->aggregate->actuals, ret->get_parameters() );
889 return ret;
890 }
891 case TypeData::Symbolic: {
892 TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
893 buildList( type->symbolic->actuals, ret->get_parameters() );
894 return ret;
895 }
896 default:
897 return type->build();
898 } // switch
[3848e0e]899}
900
[68cd1ce]901DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
902 DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
903 for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
904 if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
905 if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ?
[b87a5ed]906 throw SemanticError( "invalid combination of storage classes in declaration of ", this );
[68cd1ce]907 } // if
908 ret = *i;
909 } // for
[b87a5ed]910 return ret;
[51b73452]911}
912
[de62360d]913bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
914 std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
915 if ( first == storageClasses.end() ) return false; // not found
916 first = std::find( ++first, storageClasses.end(), key ); // found
917 if ( first == storageClasses.end() ) return true; // not found again
918 throw SemanticError( "duplicate function specifier in declaration of ", this );
[51b73452]919}
[b87a5ed]920
921// Local Variables: //
922// tab-width: 4 //
923// mode: c++ //
924// compile-command: "make install" //
925// End: //
Note: See TracBrowser for help on using the repository browser.