source: src/Parser/DeclarationNode.cc@ 255b294

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

fixed missing body in enumeration, removed hashBody function flag, only generate sue prototype if body

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