source: src/Parser/TypeData.cc @ c3b3799

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c3b3799 was 201aeb9, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

first attempt at new basic-type int128, and length suffix with explicit size

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