source: src/Parser/TypeData.cc@ 47498bd

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 47498bd was 47498bd, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add nodes for global scope type

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