source: src/Parser/DeclarationNode.cc@ f37147b

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 f37147b was 43c89a7, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add hoistType flag (currently unused)

  • Property mode set to 100644
File size: 33.7 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 22:21:06 2017
13// Update Count : 775
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.hoistType = o->type->aggregate.body;
610 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
611 } else {
612 type->aggInst.hoistType = o->type->enumeration.body;
613 } // if
614 type->qualifiers |= o->type->qualifiers;
615 } else {
616 type = o->type;
617 } // if
618 o->type = nullptr;
619 } else {
620 addTypeToType( o->type, type );
621 } // if
622 } // if
623 if ( o->bitfieldWidth ) {
624 bitfieldWidth = o->bitfieldWidth;
625 } // if
626
627 // there may be typedefs chained onto the type
628 if ( o->get_next() ) {
629 set_last( o->get_next()->clone() );
630 } // if
631 } // if
632 delete o;
633 return this;
634}
635
636DeclarationNode * DeclarationNode::addTypedef() {
637 TypeData * newtype = new TypeData( TypeData::Symbolic );
638 newtype->symbolic.params = nullptr;
639 newtype->symbolic.isTypedef = true;
640 newtype->symbolic.name = name ? new string( *name ) : nullptr;
641 newtype->base = type;
642 type = newtype;
643 return this;
644}
645
646DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
647 if ( variable.tyClass != NoTypeClass ) {
648 if ( variable.assertions ) {
649 variable.assertions->appendList( assertions );
650 } else {
651 variable.assertions = assertions;
652 } // if
653 return this;
654 } // if
655
656 assert( type );
657 switch ( type->kind ) {
658 case TypeData::Symbolic:
659 if ( type->symbolic.assertions ) {
660 type->symbolic.assertions->appendList( assertions );
661 } else {
662 type->symbolic.assertions = assertions;
663 } // if
664 break;
665 default:
666 assert( false );
667 } // switch
668
669 return this;
670}
671
672DeclarationNode * DeclarationNode::addName( string * newname ) {
673 assert( ! name );
674 name = newname;
675 return this;
676}
677
678DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
679 assert( ! asmName );
680 asmName = newname ? newname->asmName : nullptr;
681 return this->addQualifiers( newname );
682}
683
684DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
685 bitfieldWidth = size;
686 return this;
687}
688
689DeclarationNode * DeclarationNode::addVarArgs() {
690 assert( type );
691 hasEllipsis = true;
692 return this;
693}
694
695DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
696 assert( type );
697 assert( type->kind == TypeData::Function );
698 assert( ! type->function.body );
699 type->function.body = body;
700 return this;
701}
702
703DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
704 assert( type );
705 assert( type->kind == TypeData::Function );
706 assert( ! type->function.oldDeclList );
707 type->function.oldDeclList = list;
708 return this;
709}
710
711DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
712 if ( type ) {
713 TypeData * prevBase = type;
714 TypeData * curBase = type->base;
715 while ( curBase != nullptr ) {
716 prevBase = curBase;
717 curBase = curBase->base;
718 } // while
719 prevBase->base = newType;
720 } else {
721 type = newType;
722 } // if
723 return this;
724}
725
726DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
727 if ( a ) {
728 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
729 attributes.push_front( attr );
730 } // for
731 a->attributes.clear();
732 } // if
733 return this;
734} // copyAttribute
735
736DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
737 if ( p ) {
738 assert( p->type->kind == TypeData::Pointer );
739 setBase( p->type );
740 p->type = nullptr;
741 copyAttribute( p );
742 delete p;
743 } // if
744 return this;
745}
746
747DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
748 if ( a ) {
749 assert( a->type->kind == TypeData::Array );
750 setBase( a->type );
751 a->type = nullptr;
752 copyAttribute( a );
753 delete a;
754 } // if
755 return this;
756}
757
758DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
759 if ( p ) {
760 assert( p->type->kind == TypeData::Pointer );
761 if ( type ) {
762 switch ( type->kind ) {
763 case TypeData::Aggregate:
764 case TypeData::Enum:
765 p->type->base = new TypeData( TypeData::AggregateInst );
766 p->type->base->aggInst.aggregate = type;
767 if ( type->kind == TypeData::Aggregate ) {
768 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
769 } // if
770 p->type->base->qualifiers |= type->qualifiers;
771 break;
772
773 default:
774 p->type->base = type;
775 } // switch
776 type = nullptr;
777 } // if
778 delete this;
779 return p;
780 } else {
781 return this;
782 } // if
783}
784
785static TypeData * findLast( TypeData * a ) {
786 assert( a );
787 TypeData * cur = a;
788 while ( cur->base ) {
789 cur = cur->base;
790 } // while
791 return cur;
792}
793
794DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
795 if ( a ) {
796 assert( a->type->kind == TypeData::Array );
797 TypeData * lastArray = findLast( a->type );
798 if ( type ) {
799 switch ( type->kind ) {
800 case TypeData::Aggregate:
801 case TypeData::Enum:
802 lastArray->base = new TypeData( TypeData::AggregateInst );
803 lastArray->base->aggInst.aggregate = type;
804 if ( type->kind == TypeData::Aggregate ) {
805 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
806 } // if
807 lastArray->base->qualifiers |= type->qualifiers;
808 break;
809 default:
810 lastArray->base = type;
811 } // switch
812 type = nullptr;
813 } // if
814 delete this;
815 return a;
816 } else {
817 return this;
818 } // if
819}
820
821DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
822 TypeData * ftype = new TypeData( TypeData::Function );
823 ftype->function.params = params;
824 setBase( ftype );
825 return this;
826}
827
828static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
829 if ( type ) {
830 if ( type->kind != TypeData::Function ) {
831 type->base = addIdListToType( type->base, ids );
832 } else {
833 type->function.idList = ids;
834 } // if
835 return type;
836 } else {
837 TypeData * newtype = new TypeData( TypeData::Function );
838 newtype->function.idList = ids;
839 return newtype;
840 } // if
841} // addIdListToType
842
843DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
844 type = addIdListToType( type, ids );
845 return this;
846}
847
848DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
849 initializer = init;
850 return this;
851}
852
853DeclarationNode * DeclarationNode::cloneType( string * newName ) {
854 DeclarationNode * newnode = new DeclarationNode;
855 newnode->type = maybeClone( type );
856 assert( storageClass == NoStorageClass );
857 newnode->copyStorageClasses( this );
858 assert( newName );
859 newnode->name = newName;
860 return newnode;
861}
862
863DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
864 if ( ! o ) return nullptr;
865
866 o->copyStorageClasses( this );
867 if ( type ) {
868 TypeData * srcType = type;
869
870 // search for the base type by scanning off pointers and array designators
871 while ( srcType->base ) {
872 srcType = srcType->base;
873 } // while
874
875 TypeData * newType = srcType->clone();
876 if ( newType->kind == TypeData::AggregateInst ) {
877 // don't duplicate members
878 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
879 delete newType->aggInst.aggregate->enumeration.constants;
880 newType->aggInst.aggregate->enumeration.constants = nullptr;
881 } else {
882 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
883 delete newType->aggInst.aggregate->aggregate.fields;
884 newType->aggInst.aggregate->aggregate.fields = nullptr;
885 } // if
886 // don't hoist twice
887 newType->aggInst.hoistType = false;
888 } // if
889
890 newType->forall = maybeClone( type->forall );
891 if ( ! o->type ) {
892 o->type = newType;
893 } else {
894 addTypeToType( newType, o->type );
895 delete newType;
896 } // if
897 } // if
898 return o;
899}
900
901DeclarationNode * DeclarationNode::extractAggregate() const {
902 if ( type ) {
903 TypeData * ret = typeextractAggregate( type );
904 if ( ret ) {
905 DeclarationNode * newnode = new DeclarationNode;
906 newnode->type = ret;
907 return newnode;
908 } // if
909 } // if
910 return nullptr;
911}
912
913void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
914 SemanticError errors;
915 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
916
917 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
918 try {
919 if ( DeclarationNode * extr = cur->extractAggregate() ) {
920 // handle the case where a structure declaration is contained within an object or type declaration
921 Declaration * decl = extr->build();
922 if ( decl ) {
923 decl->location = cur->location;
924 * out++ = decl;
925 } // if
926 delete extr;
927 } // if
928
929 Declaration * decl = cur->build();
930 if ( decl ) {
931 decl->location = cur->location;
932 * out++ = decl;
933 } // if
934 } catch( SemanticError &e ) {
935 e.set_location( cur->location );
936 errors.append( e );
937 } // try
938 } // while
939
940 if ( ! errors.isEmpty() ) {
941 throw errors;
942 } // if
943} // buildList
944
945void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
946 SemanticError errors;
947 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
948
949 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
950 try {
951 Declaration * decl = cur->build();
952 if ( decl ) {
953 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
954 dwt->location = cur->location;
955 * out++ = dwt;
956 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
957 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
958 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
959 obj->location = cur->location;
960 * out++ = obj;
961 delete agg;
962 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
963 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
964 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
965 obj->location = cur->location;
966 * out++ = obj;
967 } // if
968 } // if
969 } catch( SemanticError &e ) {
970 e.set_location( cur->location );
971 errors.append( e );
972 } // try
973 } // for
974
975 if ( ! errors.isEmpty() ) {
976 throw errors;
977 } // if
978} // buildList
979
980void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
981 SemanticError errors;
982 std::back_insert_iterator< std::list< Type * > > out( outputList );
983 const DeclarationNode * cur = firstNode;
984
985 while ( cur ) {
986 try {
987 * out++ = cur->buildType();
988 } catch( SemanticError &e ) {
989 e.set_location( cur->location );
990 errors.append( e );
991 } // try
992 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
993 } // while
994
995 if ( ! errors.isEmpty() ) {
996 throw errors;
997 } // if
998} // buildTypeList
999
1000Declaration * DeclarationNode::build() const {
1001 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
1002
1003 if ( asmStmt ) {
1004 return new AsmDecl( safe_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1005 } // if
1006
1007// if ( variable.name ) {
1008 if ( variable.tyClass != NoTypeClass ) {
1009 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1010 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
1011 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1012// TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
1013 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
1014 buildList( variable.assertions, ret->get_assertions() );
1015 return ret;
1016 } // if
1017
1018 if ( type ) {
1019 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1020 } // if
1021
1022 if ( ! isInline && ! isNoreturn ) {
1023 assertf( name, "ObjectDecl are assumed to have names\n" );
1024 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1025 } // if
1026
1027 throw SemanticError( "invalid function specifier ", this );
1028}
1029
1030Type * DeclarationNode::buildType() const {
1031 assert( type );
1032
1033 if ( attr.expr ) {
1034// return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
1035 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1036 } else if ( attr.type ) {
1037// return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
1038 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1039 } // if
1040
1041 switch ( type->kind ) {
1042 case TypeData::Enum:
1043 case TypeData::Aggregate: {
1044 ReferenceToType * ret = buildComAggInst( type, attributes );
1045 buildList( type->aggregate.actuals, ret->get_parameters() );
1046 return ret;
1047 }
1048 case TypeData::Symbolic: {
1049 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1050 buildList( type->symbolic.actuals, ret->get_parameters() );
1051 return ret;
1052 }
1053 default:
1054 Type * simpletypes = typebuild( type );
1055 simpletypes->get_attributes() = attributes; // copy because member is const
1056 return simpletypes;
1057 } // switch
1058}
1059
1060// Local Variables: //
1061// tab-width: 4 //
1062// mode: c++ //
1063// compile-command: "make install" //
1064// End: //
Note: See TracBrowser for help on using the repository browser.