source: src/Parser/DeclarationNode.cc@ c3acf0aa

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 c3acf0aa was f196351, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Scrubbing out more traces of TreeStruct.

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