source: src/Parser/DeclarationNode.cc@ c8dfcd3

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor 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 c8dfcd3 was 413ad05, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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