source: src/Parser/DeclarationNode.cc@ 5f5083e

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

more refactoring of parser code, new tuple syntax

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