source: src/Parser/TypeData.cc@ 2dc6621

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 no_list persistent-indexer pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 2dc6621 was 3d7e53b, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add anon flag to TypeData and remove anonymous members for named aggregates

  • Property mode set to 100644
File size: 34.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jun 6 17:40:33 2018
13// Update Count : 604
14//
15
16#include <cassert> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case Reference:
38 case EnumConstant:
39 case GlobalScope:
40 // nothing else to initialize
41 break;
42 case Basic:
43 // basic = new Basic_t;
44 break;
45 case Array:
46 // array = new Array_t;
47 array.dimension = nullptr;
48 array.isVarLen = false;
49 array.isStatic = false;
50 break;
51 case Function:
52 // function = new Function_t;
53 function.params = nullptr;
54 function.idList = nullptr;
55 function.oldDeclList = nullptr;
56 function.body = nullptr;
57 function.withExprs = nullptr;
58 break;
59 // Enum is an Aggregate, so both structures are initialized together.
60 case Enum:
61 // enumeration = new Enumeration_t;
62 enumeration.name = nullptr;
63 enumeration.constants = nullptr;
64 enumeration.body = false;
65 enumeration.anon = false;
66 break;
67 case Aggregate:
68 // aggregate = new Aggregate_t;
69 aggregate.kind = DeclarationNode::NoAggregate;
70 aggregate.name = nullptr;
71 aggregate.params = nullptr;
72 aggregate.actuals = nullptr;
73 aggregate.fields = nullptr;
74 aggregate.body = false;
75 aggregate.tagged = false;
76 aggregate.parent = nullptr;
77 aggregate.anon = false;
78 break;
79 case AggregateInst:
80 // aggInst = new AggInst_t;
81 aggInst.aggregate = nullptr;
82 aggInst.params = nullptr;
83 aggInst.hoistType = false;;
84 break;
85 case Symbolic:
86 case SymbolicInst:
87 // symbolic = new Symbolic_t;
88 symbolic.name = nullptr;
89 symbolic.params = nullptr;
90 symbolic.actuals = nullptr;
91 symbolic.assertions = nullptr;
92 break;
93 case Tuple:
94 // tuple = new Tuple_t;
95 tuple = nullptr;
96 break;
97 case Typeof:
98 // typeexpr = new Typeof_t;
99 typeexpr = nullptr;
100 break;
101 case Builtin:
102 // builtin = new Builtin_t;
103 case Qualified:
104 qualified.parent = nullptr;
105 qualified.child = nullptr;
106 break;
107 } // switch
108} // TypeData::TypeData
109
110
111TypeData::~TypeData() {
112 delete base;
113 delete forall;
114
115 switch ( kind ) {
116 case Unknown:
117 case Pointer:
118 case Reference:
119 case EnumConstant:
120 case GlobalScope:
121 // nothing to destroy
122 break;
123 case Basic:
124 // delete basic;
125 break;
126 case Array:
127 delete array.dimension;
128 // delete array;
129 break;
130 case Function:
131 delete function.params;
132 delete function.idList;
133 delete function.oldDeclList;
134 delete function.body;
135 delete function.withExprs;
136 // delete function;
137 break;
138 case Aggregate:
139 delete aggregate.name;
140 delete aggregate.params;
141 delete aggregate.actuals;
142 delete aggregate.fields;
143 // delete aggregate;
144 break;
145 case AggregateInst:
146 delete aggInst.aggregate;
147 delete aggInst.params;
148 // delete aggInst;
149 break;
150 case Enum:
151 delete enumeration.name;
152 delete enumeration.constants;
153 // delete enumeration;
154 break;
155 case Symbolic:
156 case SymbolicInst:
157 delete symbolic.name;
158 delete symbolic.params;
159 delete symbolic.actuals;
160 delete symbolic.assertions;
161 // delete symbolic;
162 break;
163 case Tuple:
164 // delete tuple->members;
165 delete tuple;
166 break;
167 case Typeof:
168 // delete typeexpr->expr;
169 delete typeexpr;
170 break;
171 case Builtin:
172 // delete builtin;
173 break;
174 case Qualified:
175 delete qualified.parent;
176 delete qualified.child;
177 } // switch
178} // TypeData::~TypeData
179
180
181TypeData * TypeData::clone() const {
182 TypeData * newtype = new TypeData( kind );
183 newtype->qualifiers = qualifiers;
184 newtype->base = maybeClone( base );
185 newtype->forall = maybeClone( forall );
186
187 switch ( kind ) {
188 case Unknown:
189 case EnumConstant:
190 case Pointer:
191 case Reference:
192 case GlobalScope:
193 // nothing else to copy
194 break;
195 case Basic:
196 newtype->basictype = basictype;
197 newtype->complextype = complextype;
198 newtype->signedness = signedness;
199 newtype->length = length;
200 break;
201 case Array:
202 newtype->array.dimension = maybeClone( array.dimension );
203 newtype->array.isVarLen = array.isVarLen;
204 newtype->array.isStatic = array.isStatic;
205 break;
206 case Function:
207 newtype->function.params = maybeClone( function.params );
208 newtype->function.idList = maybeClone( function.idList );
209 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
210 newtype->function.body = maybeClone( function.body );
211 newtype->function.withExprs = maybeClone( function.withExprs );
212 break;
213 case Aggregate:
214 newtype->aggregate.kind = aggregate.kind;
215 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
216 newtype->aggregate.params = maybeClone( aggregate.params );
217 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
218 newtype->aggregate.fields = maybeClone( aggregate.fields );
219 newtype->aggregate.body = aggregate.body;
220 newtype->aggregate.anon = aggregate.anon;
221 newtype->aggregate.tagged = aggregate.tagged;
222 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
223 break;
224 case AggregateInst:
225 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
226 newtype->aggInst.params = maybeClone( aggInst.params );
227 newtype->aggInst.hoistType = aggInst.hoistType;
228 break;
229 case Enum:
230 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
231 newtype->enumeration.constants = maybeClone( enumeration.constants );
232 newtype->enumeration.body = enumeration.body;
233 newtype->enumeration.anon = enumeration.anon;
234 break;
235 case Symbolic:
236 case SymbolicInst:
237 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
238 newtype->symbolic.params = maybeClone( symbolic.params );
239 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
240 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
241 newtype->symbolic.isTypedef = symbolic.isTypedef;
242 break;
243 case Tuple:
244 newtype->tuple = maybeClone( tuple );
245 break;
246 case Typeof:
247 newtype->typeexpr = maybeClone( typeexpr );
248 break;
249 case Builtin:
250 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
251 newtype->builtintype = builtintype;
252 break;
253 case Qualified:
254 newtype->qualified.parent = maybeClone( qualified.parent );
255 newtype->qualified.child = maybeClone( qualified.child );
256 break;
257 } // switch
258 return newtype;
259} // TypeData::clone
260
261
262void TypeData::print( ostream &os, int indent ) const {
263 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
264 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
265 } // for
266
267 if ( forall ) {
268 os << "forall " << endl;
269 forall->printList( os, indent + 4 );
270 } // if
271
272 switch ( kind ) {
273 case Unknown:
274 os << "entity of unknown type ";
275 break;
276 case Pointer:
277 os << "pointer ";
278 if ( base ) {
279 os << "to ";
280 base->print( os, indent );
281 } // if
282 break;
283 case EnumConstant:
284 os << "enumeration constant ";
285 break;
286 case Basic:
287 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
288 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
289 assert( basictype != DeclarationNode::NoBasicType );
290 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
291 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
292 break;
293 case Array:
294 if ( array.isStatic ) {
295 os << "static ";
296 } // if
297 if ( array.dimension ) {
298 os << "array of ";
299 array.dimension->printOneLine( os, indent );
300 } else if ( array.isVarLen ) {
301 os << "variable-length array of ";
302 } else {
303 os << "open array of ";
304 } // if
305 if ( base ) {
306 base->print( os, indent );
307 } // if
308 break;
309 case Function:
310 os << "function" << endl;
311 if ( function.params ) {
312 os << string( indent + 2, ' ' ) << "with parameters " << endl;
313 function.params->printList( os, indent + 4 );
314 } else {
315 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
316 } // if
317 if ( function.idList ) {
318 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
319 function.idList->printList( os, indent + 4 );
320 } // if
321 if ( function.oldDeclList ) {
322 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
323 function.oldDeclList->printList( os, indent + 4 );
324 } // if
325 os << string( indent + 2, ' ' ) << "returning ";
326 if ( base ) {
327 base->print( os, indent + 4 );
328 } else {
329 os << "nothing ";
330 } // if
331 os << endl;
332 if ( function.body ) {
333 os << string( indent + 2, ' ' ) << "with body " << endl;
334 function.body->printList( os, indent + 2 );
335 } // if
336 break;
337 case Aggregate:
338 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
339 if ( aggregate.params ) {
340 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
341 aggregate.params->printList( os, indent + 4 );
342 } // if
343 if ( aggregate.actuals ) {
344 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
345 aggregate.actuals->printList( os, indent + 4 );
346 } // if
347 if ( aggregate.fields ) {
348 os << string( indent + 2, ' ' ) << "with members " << endl;
349 aggregate.fields->printList( os, indent + 4 );
350 } // if
351 if ( aggregate.body ) {
352 os << string( indent + 2, ' ' ) << " with body " << endl;
353 } // if
354 break;
355 case AggregateInst:
356 if ( aggInst.aggregate ) {
357 os << "instance of " ;
358 aggInst.aggregate->print( os, indent );
359 } else {
360 os << "instance of an unspecified aggregate ";
361 } // if
362 if ( aggInst.params ) {
363 os << string( indent + 2, ' ' ) << "with parameters " << endl;
364 aggInst.params->printList( os, indent + 2 );
365 } // if
366 break;
367 case Enum:
368 os << "enumeration ";
369 if ( enumeration.constants ) {
370 os << "with constants" << endl;
371 enumeration.constants->printList( os, indent + 2 );
372 } // if
373 if ( enumeration.body ) {
374 os << string( indent + 2, ' ' ) << " with body " << endl;
375 } // if
376 break;
377 case SymbolicInst:
378 os << "instance of type " << *symbolic.name;
379 if ( symbolic.actuals ) {
380 os << " with parameters" << endl;
381 symbolic.actuals->printList( os, indent + 2 );
382 } // if
383 break;
384 case Symbolic:
385 if ( symbolic.isTypedef ) {
386 os << "typedef definition ";
387 } else {
388 os << "type definition ";
389 } // if
390 if ( symbolic.params ) {
391 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
392 symbolic.params->printList( os, indent + 2 );
393 } // if
394 if ( symbolic.assertions ) {
395 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
396 symbolic.assertions->printList( os, indent + 4 );
397 os << string( indent + 2, ' ' );
398 } // if
399 if ( base ) {
400 os << "for ";
401 base->print( os, indent + 2 );
402 } // if
403 break;
404 case Tuple:
405 os << "tuple ";
406 if ( tuple ) {
407 os << "with members " << endl;
408 tuple->printList( os, indent + 2 );
409 } // if
410 break;
411 case Typeof:
412 os << "type-of expression ";
413 if ( typeexpr ) {
414 typeexpr->print( os, indent + 2 );
415 } // if
416 break;
417 case Builtin:
418 os << DeclarationNode::builtinTypeNames[builtintype];
419 break;
420 default:
421 os << "internal error: TypeData::print " << kind << endl;
422 assert( false );
423 } // switch
424} // TypeData::print
425
426
427template< typename ForallList >
428void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
429 buildList( firstNode, outputList );
430 auto n = firstNode;
431 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
432 TypeDecl * td = static_cast<TypeDecl *>(*i);
433 if ( n->variable.tyClass == DeclarationNode::Otype ) {
434 // add assertion parameters to `type' tyvars in reverse order
435 // add dtor: void ^?{}(T *)
436 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
437 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
438 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
439
440 // add copy ctor: void ?{}(T *, T)
441 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
442 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
443 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
444 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
445
446 // add default ctor: void ?{}(T *)
447 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
448 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
449 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
450
451 // add assignment operator: T * ?=?(T *, T)
452 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
453 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
454 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
455 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
456 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
457 } // if
458 } // for
459} // buildForall
460
461
462Type * typebuild( const TypeData * td ) {
463 assert( td );
464 switch ( td->kind ) {
465 case TypeData::Unknown:
466 // fill in implicit int
467 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
468 case TypeData::Basic:
469 return buildBasicType( td );
470 case TypeData::Pointer:
471 return buildPointer( td );
472 case TypeData::Array:
473 return buildArray( td );
474 case TypeData::Reference:
475 return buildReference( td );
476 case TypeData::Function:
477 return buildFunction( td );
478 case TypeData::AggregateInst:
479 return buildAggInst( td );
480 case TypeData::EnumConstant:
481 // the name gets filled in later -- by SymTab::Validate
482 return new EnumInstType( buildQualifiers( td ), "" );
483 case TypeData::SymbolicInst:
484 return buildSymbolicInst( td );
485 case TypeData::Tuple:
486 return buildTuple( td );
487 case TypeData::Typeof:
488 return buildTypeof( td );
489 case TypeData::Builtin:
490 if(td->builtintype == DeclarationNode::Zero) {
491 return new ZeroType( noQualifiers );
492 }
493 else if(td->builtintype == DeclarationNode::One) {
494 return new OneType( noQualifiers );
495 }
496 else {
497 return new VarArgsType( buildQualifiers( td ) );
498 }
499 case TypeData::GlobalScope:
500 return new GlobalScopeType();
501 case TypeData::Qualified:
502 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
503 case TypeData::Symbolic:
504 case TypeData::Enum:
505 case TypeData::Aggregate:
506 assert( false );
507 } // switch
508
509 return nullptr;
510} // typebuild
511
512
513TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
514 TypeData * ret = nullptr;
515
516 switch ( td->kind ) {
517 case TypeData::Aggregate:
518 if ( ! toplevel && td->aggregate.body ) {
519 ret = td->clone();
520 } // if
521 break;
522 case TypeData::Enum:
523 if ( ! toplevel && td->enumeration.body ) {
524 ret = td->clone();
525 } // if
526 break;
527 case TypeData::AggregateInst:
528 if ( td->aggInst.aggregate ) {
529 ret = typeextractAggregate( td->aggInst.aggregate, false );
530 } // if
531 break;
532 default:
533 if ( td->base ) {
534 ret = typeextractAggregate( td->base, false );
535 } // if
536 } // switch
537 return ret;
538} // typeextractAggregate
539
540
541Type::Qualifiers buildQualifiers( const TypeData * td ) {
542 return td->qualifiers;
543} // buildQualifiers
544
545
546static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
547 SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
548} // genTSError
549
550Type * buildBasicType( const TypeData * td ) {
551 BasicType::Kind ret;
552
553 switch ( td->basictype ) {
554 case DeclarationNode::Void:
555 if ( td->signedness != DeclarationNode::NoSignedness ) {
556 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
557 } // if
558 if ( td->length != DeclarationNode::NoLength ) {
559 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
560 } // if
561 return new VoidType( buildQualifiers( td ) );
562 break;
563
564 case DeclarationNode::Bool:
565 if ( td->signedness != DeclarationNode::NoSignedness ) {
566 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
567 } // if
568 if ( td->length != DeclarationNode::NoLength ) {
569 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
570 } // if
571
572 ret = BasicType::Bool;
573 break;
574
575 case DeclarationNode::Char:
576 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
577 // character types. The implementation shall define char to have the same range, representation, and behavior as
578 // either signed char or unsigned char.
579 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
580
581 if ( td->length != DeclarationNode::NoLength ) {
582 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
583 } // if
584
585 ret = chartype[ td->signedness ];
586 break;
587
588 case DeclarationNode::Int:
589 static BasicType::Kind inttype[2][4] = {
590 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
591 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
592 };
593
594 Integral: ;
595 if ( td->signedness == DeclarationNode::NoSignedness ) {
596 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
597 } // if
598 ret = inttype[ td->signedness ][ td->length ];
599 break;
600
601 case DeclarationNode::Int128:
602 ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
603 if ( td->length != DeclarationNode::NoLength ) {
604 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
605 } // if
606 break;
607
608 case DeclarationNode::Float:
609 case DeclarationNode::Float80:
610 case DeclarationNode::Float128:
611 case DeclarationNode::Double:
612 case DeclarationNode::LongDouble: // not set until below
613 static BasicType::Kind floattype[3][3] = {
614 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
615 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
616 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
617 };
618
619 FloatingPoint: ;
620 if ( td->signedness != DeclarationNode::NoSignedness ) {
621 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
622 } // if
623 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
624 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
625 } // if
626 if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
627 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
628 } // if
629 if ( td->length == DeclarationNode::Long ) {
630 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
631 } // if
632
633 if ( td->basictype == DeclarationNode::Float80 || td->basictype == DeclarationNode::Float128 ) {
634 // if ( td->complextype != DeclarationNode::NoComplexType ) {
635 // genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
636 // }
637 if ( td->basictype == DeclarationNode::Float80 ) ret = BasicType::Float80;
638 else ret = BasicType::Float128;
639 break;
640 }
641
642 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
643 break;
644
645 case DeclarationNode::NoBasicType:
646 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
647 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
648 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
649 goto FloatingPoint;
650 } // if
651
652 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
653 goto Integral;
654 default:
655 assertf( false, "unknown basic type" );
656 return nullptr;
657 } // switch
658
659 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
660 buildForall( td->forall, bt->get_forall() );
661 return bt;
662} // buildBasicType
663
664
665PointerType * buildPointer( const TypeData * td ) {
666 PointerType * pt;
667 if ( td->base ) {
668 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
669 } else {
670 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
671 } // if
672 buildForall( td->forall, pt->get_forall() );
673 return pt;
674} // buildPointer
675
676
677ArrayType * buildArray( const TypeData * td ) {
678 ArrayType * at;
679 if ( td->base ) {
680 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
681 td->array.isVarLen, td->array.isStatic );
682 } else {
683 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
684 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
685 } // if
686 buildForall( td->forall, at->get_forall() );
687 return at;
688} // buildArray
689
690
691ReferenceType * buildReference( const TypeData * td ) {
692 ReferenceType * rt;
693 if ( td->base ) {
694 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
695 } else {
696 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
697 } // if
698 buildForall( td->forall, rt->get_forall() );
699 return rt;
700} // buildReference
701
702
703AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
704 assert( td->kind == TypeData::Aggregate );
705 AggregateDecl * at;
706 switch ( td->aggregate.kind ) {
707 case DeclarationNode::Struct:
708 case DeclarationNode::Coroutine:
709 case DeclarationNode::Monitor:
710 case DeclarationNode::Thread:
711 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
712 buildForall( td->aggregate.params, at->get_parameters() );
713 break;
714 case DeclarationNode::Union:
715 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
716 buildForall( td->aggregate.params, at->get_parameters() );
717 break;
718 case DeclarationNode::Trait:
719 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
720 buildList( td->aggregate.params, at->get_parameters() );
721 break;
722 default:
723 assert( false );
724 } // switch
725
726 buildList( td->aggregate.fields, at->get_members() );
727 at->set_body( td->aggregate.body );
728
729 return at;
730} // buildAggregate
731
732
733ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
734 switch ( type->kind ) {
735 case TypeData::Enum: {
736 if ( type->enumeration.body ) {
737 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
738 return new EnumInstType( buildQualifiers( type ), typedecl );
739 } else {
740 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
741 } // if
742 }
743 case TypeData::Aggregate: {
744 ReferenceToType * ret;
745 if ( type->aggregate.body ) {
746 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
747 switch ( type->aggregate.kind ) {
748 case DeclarationNode::Struct:
749 case DeclarationNode::Coroutine:
750 case DeclarationNode::Monitor:
751 case DeclarationNode::Thread:
752 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
753 break;
754 case DeclarationNode::Union:
755 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
756 break;
757 case DeclarationNode::Trait:
758 assert( false );
759 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
760 break;
761 default:
762 assert( false );
763 } // switch
764 } else {
765 switch ( type->aggregate.kind ) {
766 case DeclarationNode::Struct:
767 case DeclarationNode::Coroutine:
768 case DeclarationNode::Monitor:
769 case DeclarationNode::Thread:
770 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
771 break;
772 case DeclarationNode::Union:
773 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
774 break;
775 case DeclarationNode::Trait:
776 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
777 break;
778 default:
779 assert( false );
780 } // switch
781 } // if
782 return ret;
783 }
784 default:
785 assert( false );
786 } // switch
787} // buildAggInst
788
789
790ReferenceToType * buildAggInst( const TypeData * td ) {
791 assert( td->kind == TypeData::AggregateInst );
792
793 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
794 ReferenceToType * ret = nullptr;
795 TypeData * type = td->aggInst.aggregate;
796 switch ( type->kind ) {
797 case TypeData::Enum: {
798 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
799 }
800 case TypeData::Aggregate: {
801 switch ( type->aggregate.kind ) {
802 case DeclarationNode::Struct:
803 case DeclarationNode::Coroutine:
804 case DeclarationNode::Monitor:
805 case DeclarationNode::Thread:
806 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
807 break;
808 case DeclarationNode::Union:
809 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
810 break;
811 case DeclarationNode::Trait:
812 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
813 break;
814 default:
815 assert( false );
816 } // switch
817 }
818 break;
819 default:
820 assert( false );
821 } // switch
822
823 ret->set_hoistType( td->aggInst.hoistType );
824 buildList( td->aggInst.params, ret->get_parameters() );
825 buildForall( td->forall, ret->get_forall() );
826 return ret;
827} // buildAggInst
828
829
830NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
831 assert( td->kind == TypeData::Symbolic );
832 NamedTypeDecl * ret;
833 assert( td->base );
834 if ( td->symbolic.isTypedef ) {
835 ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
836 } else {
837 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
838 } // if
839 buildList( td->symbolic.params, ret->get_parameters() );
840 buildList( td->symbolic.assertions, ret->get_assertions() );
841 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
842 return ret;
843} // buildSymbolic
844
845
846EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
847 assert( td->kind == TypeData::Enum );
848 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
849 buildList( td->enumeration.constants, ret->get_members() );
850 list< Declaration * >::iterator members = ret->get_members().begin();
851 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
852 if ( cur->has_enumeratorValue() ) {
853 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
854 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
855 } // if
856 } // for
857 ret->set_body( td->enumeration.body );
858 return ret;
859} // buildEnum
860
861
862TypeInstType * buildSymbolicInst( const TypeData * td ) {
863 assert( td->kind == TypeData::SymbolicInst );
864 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
865 buildList( td->symbolic.actuals, ret->get_parameters() );
866 buildForall( td->forall, ret->get_forall() );
867 return ret;
868} // buildSymbolicInst
869
870
871TupleType * buildTuple( const TypeData * td ) {
872 assert( td->kind == TypeData::Tuple );
873 std::list< Type * > types;
874 buildTypeList( td->tuple, types );
875 TupleType * ret = new TupleType( buildQualifiers( td ), types );
876 buildForall( td->forall, ret->get_forall() );
877 return ret;
878} // buildTuple
879
880
881TypeofType * buildTypeof( const TypeData * td ) {
882 assert( td->kind == TypeData::Typeof );
883 assert( td->typeexpr );
884 // assert( td->typeexpr->expr );
885 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
886} // buildTypeof
887
888
889Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
890 if ( td->kind == TypeData::Function ) {
891 if ( td->function.idList ) { // KR function ?
892 buildKRFunction( td->function ); // transform into C11 function
893 } // if
894
895 FunctionDecl * decl;
896 Statement * stmt = maybeBuild<Statement>( td->function.body );
897 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
898 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
899 buildList( td->function.withExprs, decl->withExprs );
900 return decl->set_asmName( asmName );
901 } else if ( td->kind == TypeData::Aggregate ) {
902 return buildAggregate( td, attributes, linkage );
903 } else if ( td->kind == TypeData::Enum ) {
904 return buildEnum( td, attributes, linkage );
905 } else if ( td->kind == TypeData::Symbolic ) {
906 return buildSymbolic( td, attributes, name, scs, linkage );
907 } else {
908 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
909 } // if
910 return nullptr;
911} // buildDecl
912
913
914FunctionType * buildFunction( const TypeData * td ) {
915 assert( td->kind == TypeData::Function );
916 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
917 buildList( td->function.params, ft->parameters );
918 buildForall( td->forall, ft->forall );
919 if ( td->base ) {
920 switch ( td->base->kind ) {
921 case TypeData::Tuple:
922 buildList( td->base->tuple, ft->returnVals );
923 break;
924 default:
925 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
926 } // switch
927 } else {
928 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
929 } // if
930 return ft;
931} // buildFunction
932
933
934// Transform KR routine declarations into C99 routine declarations:
935//
936// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
937//
938// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
939// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
940// extra names are disallowed.
941//
942// Note, there is no KR routine-prototype syntax:
943//
944// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
945// rtn(); // valid KR prototype
946
947void buildKRFunction( const TypeData::Function_t & function ) {
948 assert( ! function.params );
949 // loop over declaration first as it is easier to spot errors
950 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
951 // scan ALL parameter names for each declaration name to check for duplicates
952 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
953 if ( *decl->name == *param->name ) {
954 // type set => parameter name already transformed by a declaration names so there is a duplicate
955 // declaration name attempting a second transformation
956 if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
957 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
958 // parameter name attempting a second transformation
959 if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
960 param->type = decl->type; // set copy declaration type to parameter type
961 decl->type = nullptr; // reset declaration type
962 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
963 } // if
964 } // for
965 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
966 if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
967 } // for
968
969 // Parameter names without a declaration default to type int:
970 //
971 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
972
973 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
974 if ( ! param->type ) { // generate type int for empty parameter type
975 param->type = new TypeData( TypeData::Basic );
976 param->type->basictype = DeclarationNode::Int;
977 } // if
978 } // for
979
980 function.params = function.idList; // newly modified idList becomes parameters
981 function.idList = nullptr; // idList now empty
982 delete function.oldDeclList; // deletes entire list
983 function.oldDeclList = nullptr; // reset
984} // buildKRFunction
985
986// Local Variables: //
987// tab-width: 4 //
988// mode: c++ //
989// compile-command: "make install" //
990// End: //
Note: See TracBrowser for help on using the repository browser.