source: src/Parser/DeclarationNode.cc@ 1b772749

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

more refactoring of parser code

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