source: src/Parser/TypeData.cc @ c29c342

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since c29c342 was f7e4db27, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

improve error messages for useless declarations

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