source: src/Parser/DeclarationNode.cc@ 7eabc25

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 7eabc25 was a5a71d0, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'fix-memory-error' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Makefile.in
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy
src/ResolvExpr/Resolver.cc
src/SymTab/Validate.cc
src/SynTree/Declaration.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/prelude.cf

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