source: src/Parser/DeclarationNode.cc@ 905eca1

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 905eca1 was 738e304, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

merge qualifier types and use the one in Type

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