source: src/Parser/DeclarationNode.cc@ 5509ff4

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 5509ff4 was ba01b14, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update constant parsing add _FloatNN

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