source: src/Parser/DeclarationNode.cc@ 2298f728

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

more refactoring of parser code

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