source: src/Parser/DeclarationNode.cc@ 908cc83

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 908cc83 was 7d05e7e, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fix error messages for declarations

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