source: src/Parser/TypeData.cc @ 9d55ff6

ADTast-experimentalpthread-emulation
Last change on this file since 9d55ff6 was 66406f3, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

update debug printing

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