source: src/Parser/DeclarationNode.cc@ 37466ba0

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 37466ba0 was 409433da, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

support coroutine, monitor, thread as kind of structure

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