source: src/Parser/DeclarationNode.cc@ e76acbe

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 e76acbe was 28307be, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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