source: src/Parser/TypeData.cc @ 90c3b1c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 90c3b1c was 90c3b1c, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

compile CFA with C++11, further update refrat with lstlisting macros, support varags, enumeration initialization, add implicit separators to output streams, update example programs that print

  • Property mode set to 100644
File size: 27.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TypeData.cc --
8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Mar  2 16:34:22 2016
13// Update Count     : 47
14//
15
16#include <cassert>
17#include <algorithm>
18#include <iterator>
19#include "Common/utility.h"
20#include "TypeData.h"
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Statement.h"
25#include "SynTree/Initializer.h"
26
27TypeData::TypeData( Kind k ) : kind( k ), base( 0 ), forall( 0 ) {
28        switch ( kind ) {
29          case Unknown:
30          case Pointer:
31          case EnumConstant:
32                // nothing else to initialize
33                break;
34          case Basic:
35                basic = new Basic_t;
36                break;
37          case Array:
38                array = new Array_t;
39                array->dimension = 0;
40                array->isVarLen = false;
41                array->isStatic = false;
42                break;
43          case Function:
44                function = new Function_t;
45                function->params = 0;
46                function->idList = 0;
47                function->oldDeclList = 0;
48                function->body = 0;
49                function->hasBody = false;
50                function->newStyle = false;
51                break;
52          case Aggregate:
53                aggregate = new Aggregate_t;
54                aggregate->params = 0;
55                aggregate->actuals = 0;
56                aggregate->fields = 0;
57                break;
58          case AggregateInst:
59                aggInst = new AggInst_t;
60                aggInst->aggregate = 0;
61                aggInst->params = 0;
62                break;
63          case Enum:
64                enumeration = new Enumeration_t;
65                enumeration->constants = 0;
66                break;
67          case Symbolic:
68          case SymbolicInst:
69                symbolic = new Symbolic_t;
70                symbolic->params = 0;
71                symbolic->actuals = 0;
72                symbolic->assertions = 0;
73                break;
74          case Variable:
75                variable = new Variable_t;
76                variable->tyClass = DeclarationNode::Type;
77                variable->assertions = 0;
78                break;
79          case Tuple:
80                tuple = new Tuple_t;
81                tuple->members = 0;
82                break;
83          case Typeof:
84                typeexpr = new Typeof_t;
85                typeexpr->expr = 0;
86                break;
87          case Builtin:
88                builtin = new Builtin_t;
89                break;
90          case Attr:
91                attr = new Attr_t;
92                attr->expr = 0;
93                attr->type = 0;
94                break;
95        } // switch
96}
97
98TypeData::~TypeData() {
99        delete base;
100        delete forall;
101
102        switch ( kind ) {
103          case Unknown:
104          case Pointer:
105          case EnumConstant:
106                // nothing to destroy
107                break;
108          case Basic:
109                delete basic;
110                break;
111          case Array:
112                delete array->dimension;
113                delete array;
114                break;
115          case Function:
116                delete function->params;
117                delete function->idList;
118                delete function->oldDeclList;
119                delete function->body;
120                delete function;
121                break;
122          case Aggregate:
123                delete aggregate->params;
124                delete aggregate->actuals;
125                delete aggregate->fields;
126                delete aggregate;
127                break;
128          case AggregateInst:
129                delete aggInst->aggregate;
130                delete aggInst->params;
131                delete aggInst;
132                break;
133          case Enum:
134                delete enumeration->constants;
135                delete enumeration;
136                break;
137          case Symbolic:
138          case SymbolicInst:
139                delete symbolic->params;
140                delete symbolic->actuals;
141                delete symbolic->assertions;
142                delete symbolic;
143                break;
144          case Variable:
145                delete variable->assertions;
146                delete variable;
147                break;
148          case Tuple:
149                delete tuple->members;
150                delete tuple;
151                break;
152          case Typeof:
153                delete typeexpr->expr;
154                delete typeexpr;
155                break;
156          case Builtin:
157                delete builtin;
158                break;
159          case Attr:
160                delete attr->expr;
161                delete attr->type;
162                delete attr;
163                break;
164        } // switch
165}
166
167TypeData *TypeData::clone() const {
168        TypeData *newtype = new TypeData( kind );
169        newtype->qualifiers = qualifiers;
170        newtype->base = maybeClone( base );
171        newtype->forall = maybeClone( forall );
172
173        switch ( kind ) {
174          case Unknown:
175          case EnumConstant:
176          case Pointer:
177                // nothing else to copy
178                break;
179          case Basic:
180                newtype->basic->typeSpec = basic->typeSpec;
181                newtype->basic->modifiers = basic->modifiers;
182                break;
183          case Array:
184                newtype->array->dimension = maybeClone( array->dimension );
185                newtype->array->isVarLen = array->isVarLen;
186                newtype->array->isStatic = array->isStatic;
187                break;
188          case Function:
189                newtype->function->params = maybeClone( function->params );
190                newtype->function->idList = maybeClone( function->idList );
191                newtype->function->oldDeclList = maybeClone( function->oldDeclList );
192                newtype->function->body = maybeClone( function->body );
193                newtype->function->hasBody = function->hasBody;
194                newtype->function->newStyle = function->newStyle;
195                break;
196          case Aggregate:
197                newtype->aggregate->params = maybeClone( aggregate->params );
198                newtype->aggregate->actuals = maybeClone( aggregate->actuals );
199                newtype->aggregate->fields = maybeClone( aggregate->fields );
200                newtype->aggregate->name = aggregate->name;
201                newtype->aggregate->kind = aggregate->kind;
202                break;
203          case AggregateInst:
204                newtype->aggInst->aggregate = maybeClone( aggInst->aggregate );
205                newtype->aggInst->params = maybeClone( aggInst->params );
206                break;
207          case Enum:
208                newtype->enumeration->name = enumeration->name;
209                newtype->enumeration->constants = maybeClone( enumeration->constants );
210                break;
211          case Symbolic:
212          case SymbolicInst:
213                newtype->symbolic->params = maybeClone( symbolic->params );
214                newtype->symbolic->actuals = maybeClone( symbolic->actuals );
215                newtype->symbolic->assertions = maybeClone( symbolic->assertions );
216                newtype->symbolic->isTypedef = symbolic->isTypedef;
217                newtype->symbolic->name = symbolic->name;
218                break;
219          case Variable:
220                newtype->variable->assertions = maybeClone( variable->assertions );
221                newtype->variable->name = variable->name;
222                newtype->variable->tyClass = variable->tyClass;
223                break;
224          case Tuple:
225                newtype->tuple->members = maybeClone( tuple->members );
226                break;
227          case Typeof:
228                newtype->typeexpr->expr = maybeClone( typeexpr->expr );
229                break;
230          case Builtin:
231                newtype->builtin->type = builtin->type;
232                break;
233          case Attr:
234                newtype->attr->expr = maybeClone( attr->expr );
235                newtype->attr->type = maybeClone( attr->type );
236                break;
237        } // switch
238        return newtype;
239}
240
241void TypeData::print( std::ostream &os, int indent ) const {
242        using std::endl;
243        using std::string;
244
245        printEnums( qualifiers.begin(), qualifiers.end(), DeclarationNode::qualifierName, os );
246
247        if ( forall ) {
248                os << "forall " << endl;
249                forall->printList( os, indent + 4 );
250        } // if
251
252        switch ( kind ) {
253          case Unknown:
254                os << "entity of unknown type ";
255                break;
256          case Pointer:
257                os << "pointer ";
258                if ( base ) {
259                        os << "to ";
260                        base->print( os, indent );
261                } // if
262                break;
263          case EnumConstant:
264                os << "enumeration constant ";
265                break;
266          case Basic:
267                printEnums( basic->modifiers.begin(), basic->modifiers.end(), DeclarationNode::modifierName, os );
268                printEnums( basic->typeSpec.begin(), basic->typeSpec.end(), DeclarationNode::basicTypeName, os );
269                break;
270          case Array:
271                if ( array->isStatic ) {
272                        os << "static ";
273                } // if
274                if ( array->dimension ) {
275                        os << "array of ";
276                        array->dimension->printOneLine( os, indent );
277                } else if ( array->isVarLen ) {
278                        os << "variable-length array of ";
279                } else {
280                        os << "open array of ";
281                } // if
282                if ( base ) {
283                        base->print( os, indent );
284                } // if
285                break;
286          case Function:
287                os << "function" << endl;
288                if ( function->params ) {
289                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
290                        function->params->printList( os, indent + 4 );
291                } else {
292                        os << string( indent + 2, ' ' ) << "with no parameters " << endl;
293                } // if
294                if ( function->idList ) {
295                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
296                        function->idList->printList( os, indent + 4 );
297                } // if
298                if ( function->oldDeclList ) {
299                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
300                        function->oldDeclList->printList( os, indent + 4 );
301                } // if
302                os << string( indent + 2, ' ' ) << "returning ";
303                if ( base ) {
304                        base->print( os, indent + 4 );
305                } else {
306                        os << "nothing ";
307                } // if
308                os << endl;
309                if ( function->hasBody ) {
310                        os << string( indent + 2, ' ' ) << "with body " << endl;
311                } // if
312                if ( function->body ) {
313                        function->body->printList( os, indent + 2 );
314                } // if
315                break;
316          case Aggregate:
317                os << DeclarationNode::aggregateName[ aggregate->kind ] << ' ' << aggregate->name << endl;
318                if ( aggregate->params ) {
319                        os << string( indent + 2, ' ' ) << "with type parameters " << endl;
320                        aggregate->params->printList( os, indent + 4 );
321                } // if
322                if ( aggregate->actuals ) {
323                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
324                        aggregate->actuals->printList( os, indent + 4 );
325                } // if
326                if ( aggregate->fields ) {
327                        os << string( indent + 2, ' ' ) << "with members " << endl;
328                        aggregate->fields->printList( os, indent + 4 );
329///     } else {
330///       os << string( indent + 2, ' ' ) << "with no members " << endl;
331                } // if
332                break;
333          case AggregateInst:
334                if ( aggInst->aggregate ) {
335                        os << "instance of " ;
336                        aggInst->aggregate->print( os, indent );
337                } else {
338                        os << "instance of an unspecified aggregate ";
339                } // if
340                if ( aggInst->params ) {
341                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
342                        aggInst->params->printList( os, indent + 2 );
343                } // if
344                break;
345          case Enum:
346                os << "enumeration ";
347                if ( enumeration->constants ) {
348                        os << "with constants" << endl;
349                        enumeration->constants->printList( os, indent + 2 );
350                } // if
351                break;
352          case SymbolicInst:
353                os << "instance of type " << symbolic->name;
354                if ( symbolic->actuals ) {
355                        os << " with parameters" << endl;
356                        symbolic->actuals->printList( os, indent + 2 );
357                } // if
358                break;
359          case Symbolic:
360                if ( symbolic->isTypedef ) {
361                        os << "typedef definition ";
362                } else {
363                        os << "type definition ";
364                } // if
365                if ( symbolic->params ) {
366                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
367                        symbolic->params->printList( os, indent + 2 );
368                } // if
369                if ( symbolic->assertions ) {
370                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
371                        symbolic->assertions->printList( os, indent + 4 );
372                        os << string( indent + 2, ' ' );
373                } // if
374                if ( base ) {
375                        os << "for ";
376                        base->print( os, indent + 2 );
377                } // if
378                break;
379          case Variable:
380                os << DeclarationNode::typeClassName[ variable->tyClass ] << " variable ";
381                if ( variable->assertions ) {
382                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
383                        variable->assertions->printList( os, indent + 4 );
384                        os << string( indent + 2, ' ' );
385                } // if
386                break;
387          case Tuple:
388                os << "tuple ";
389                if ( tuple->members ) {
390                        os << "with members " << endl;
391                        tuple->members->printList( os, indent + 2 );
392                } // if
393                break;
394          case Typeof:
395                os << "type-of expression ";
396                if ( typeexpr->expr ) {
397                        typeexpr->expr->print( os, indent + 2 );
398                } // if
399                break;
400          case Attr:
401                os << "attribute type decl " << attr->name << " applied to ";
402                if ( attr->expr ) {
403                        attr->expr->print( os, indent + 2 );
404                } // if
405                if ( attr->type ) {
406                        attr->type->print( os, indent + 2 );
407                } // if
408                break;
409          case Builtin:
410                os << "gcc builtin type";
411                break;
412          default:
413                os << "internal error: TypeData::print " << kind  << endl;
414                assert( false );
415        } // switch
416}
417
418TypeData *TypeData::extractAggregate( bool toplevel ) const {
419        TypeData *ret = 0;
420
421        switch ( kind ) {
422          case Aggregate:
423                if ( ! toplevel && aggregate->fields ) {
424                        ret = clone();
425                        ret->qualifiers.clear();
426                } // if
427                break;
428          case Enum:
429                if ( ! toplevel && enumeration->constants ) {
430                        ret = clone();
431                        ret->qualifiers.clear();
432                } // if
433                break;
434          case AggregateInst:
435                if ( aggInst->aggregate ) {
436                        ret = aggInst->aggregate->extractAggregate( false );
437                } // if
438                break;
439          default:
440                if ( base ) {
441                        ret = base->extractAggregate( false );
442                } // if
443        } // switch
444        return ret;
445}
446
447void buildForall( const DeclarationNode *firstNode, std::list< TypeDecl* > &outputList ) {
448        buildList( firstNode, outputList );
449        for ( std::list< TypeDecl* >::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
450                if ( (*i)->get_kind() == TypeDecl::Any ) {
451                        FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
452                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
453                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
454                        assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
455                        (*i)->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false, false ) );
456                } // if
457        } // for
458}
459
460Declaration *TypeData::buildDecl( std::string name, DeclarationNode::StorageClass sc, Expression *bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Type linkage, Initializer *init ) const {
461        if ( kind == TypeData::Function ) {
462                FunctionDecl *decl;
463                if ( function->hasBody ) {
464                        if ( function->body ) {
465                                Statement *stmt = function->body->build();
466                                CompoundStmt *body = dynamic_cast< CompoundStmt* >( stmt );
467                                assert( body );
468                                decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline, isNoreturn );
469                        } else {
470                                // std::list<Label> ls;
471                                decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list<Label>() ), isInline, isNoreturn );
472                        } // if
473                } else {
474                        decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline, isNoreturn );
475                } // if
476                for ( DeclarationNode *cur = function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_link() ) ) {
477                        if ( cur->get_name() != "" ) {
478                                decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() );
479                        } // if
480                } // for
481                buildList( function->oldDeclList, decl->get_oldDecls() );
482                return decl;
483        } else if ( kind == TypeData::Aggregate ) {
484                return buildAggregate();
485        } else if ( kind == TypeData::Enum ) {
486                return buildEnum();
487        } else if ( kind == TypeData::Symbolic ) {
488                return buildSymbolic( name, sc );
489        } else if ( kind == TypeData::Variable ) {
490                return buildVariable();
491        } else {
492                return new ObjectDecl( name, sc, linkage, bitfieldWidth, build(), init, isInline, isNoreturn );
493        } // if
494        return 0;
495}
496
497Type *TypeData::build() const {
498        switch ( kind ) {
499          case Unknown:
500                // fill in implicit int
501                return new BasicType( buildQualifiers(), BasicType::SignedInt );
502          case Basic:
503                return buildBasicType();
504          case Pointer:
505                return buildPointer();
506          case Array:
507                return buildArray();
508          case Function:
509                return buildFunction();
510          case AggregateInst:
511                return buildAggInst();
512          case EnumConstant:
513                // the name gets filled in later -- by SymTab::Validate
514                return new EnumInstType( buildQualifiers(), "" );
515          case SymbolicInst:
516                return buildSymbolicInst();;
517          case Tuple:
518                return buildTuple();
519          case Typeof:
520                return buildTypeof();
521          case Builtin:
522                return new VarArgsType();
523          case Attr:
524                return buildAttr();
525          case Symbolic:
526          case Enum:
527          case Aggregate:
528          case Variable:
529                assert( false );
530        } // switch
531        return 0;
532}
533
534Type::Qualifiers TypeData::buildQualifiers() const {
535        Type::Qualifiers q;
536        for ( std::list< DeclarationNode::Qualifier >::const_iterator i = qualifiers.begin(); i != qualifiers.end(); ++i ) {
537                switch ( *i ) {
538                  case DeclarationNode::Const:
539                        q.isConst = true;
540                        break;
541                  case DeclarationNode::Volatile:
542                        q.isVolatile = true;
543                        break;
544                  case DeclarationNode::Restrict:
545                        q.isRestrict = true;
546                        break;
547                  case DeclarationNode::Lvalue:
548                        q.isLvalue = true;
549                        break;
550                  case DeclarationNode::Atomic:
551                        q.isAtomic = true;
552                        break;
553                } // switch
554        } // for
555        return q;
556}
557
558Type *TypeData::buildBasicType() const {
559        static const BasicType::Kind kindMap[] = { BasicType::Char, BasicType::SignedInt, BasicType::Float, BasicType::Double,
560                                                                                           BasicType::Char /* void */, BasicType::Bool, BasicType::DoubleComplex,
561                                                                                           BasicType::DoubleImaginary };
562        bool init = false;
563        bool sawDouble = false;
564        bool sawSigned = false;
565        BasicType::Kind ret;
566
567        for ( std::list< DeclarationNode::BasicType >::const_iterator i = basic->typeSpec.begin(); i != basic->typeSpec.end(); ++i ) {
568                if ( ! init ) {
569                        init = true;
570                        if ( *i == DeclarationNode::Void ) {
571                                if ( basic->typeSpec.size() != 1 || ! basic->modifiers.empty() ) {
572                                        throw SemanticError( "invalid type specifier \"void\" in type: ", this );
573                                } else {
574                                        return new VoidType( buildQualifiers() );
575                                } // if
576                        } else {
577                                ret = kindMap[ *i ];
578                        } // if
579                } else {
580                        switch ( *i ) {
581                          case DeclarationNode::Float:
582                                if ( sawDouble ) {
583                                        throw SemanticError( "invalid type specifier \"float\" in type: ", this );
584                                } else {
585                                        switch ( ret ) {
586                                          case BasicType::DoubleComplex:
587                                                ret = BasicType::FloatComplex;
588                                                break;
589                                          case BasicType::DoubleImaginary:
590                                                ret = BasicType::FloatImaginary;
591                                                break;
592                                          default:
593                                                throw SemanticError( "invalid type specifier \"float\" in type: ", this );
594                                        } // switch
595                                } // if
596                                break;
597                          case DeclarationNode::Double:
598                                if ( sawDouble ) {
599                                        throw SemanticError( "duplicate type specifier \"double\" in type: ", this );
600                                } else {
601                                        switch ( ret ) {
602                                          case BasicType::DoubleComplex:
603                                          case BasicType::DoubleImaginary:
604                                                break;
605                                          default:
606                                                throw SemanticError( "invalid type specifier \"double\" in type: ", this );
607                                        } // switch
608                                } // if
609                                break;
610                          case DeclarationNode::Complex:
611                                switch ( ret ) {
612                                  case BasicType::Float:
613                                        ret = BasicType::FloatComplex;
614                                        break;
615                                  case BasicType::Double:
616                                        ret = BasicType::DoubleComplex;
617                                        break;
618                                  default:
619                                        throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this );
620                                } // switch
621                                break;
622                          case DeclarationNode::Imaginary:
623                                switch ( ret ) {
624                                  case BasicType::Float:
625                                        ret = BasicType::FloatImaginary;
626                                        break;
627                                  case BasicType::Double:
628                                        ret = BasicType::DoubleImaginary;
629                                        break;
630                                  default:
631                                        throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this );
632                                } // switch
633                                break;
634                          default:
635                                throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this );
636                        } // switch
637                } // if
638                if ( *i == DeclarationNode::Double ) {
639                        sawDouble = true;
640                } // if
641        } // for
642
643        for ( std::list< DeclarationNode::Modifier >::const_iterator i = basic->modifiers.begin(); i != basic->modifiers.end(); ++i ) {
644                switch ( *i ) {
645                  case DeclarationNode::Long:
646                        if ( ! init ) {
647                                init = true;
648                                ret = BasicType::LongSignedInt;
649                        } else {
650                                switch ( ret ) {
651                                  case BasicType::SignedInt:
652                                        ret = BasicType::LongSignedInt;
653                                        break;
654                                  case BasicType::UnsignedInt:
655                                        ret = BasicType::LongUnsignedInt;
656                                        break;
657                                  case BasicType::LongSignedInt:
658                                        ret = BasicType::LongLongSignedInt;
659                                        break;
660                                  case BasicType::LongUnsignedInt:
661                                        ret = BasicType::LongLongUnsignedInt;
662                                        break;
663                                  case BasicType::Double:
664                                        ret = BasicType::LongDouble;
665                                        break;
666                                  case BasicType::DoubleComplex:
667                                        ret = BasicType::LongDoubleComplex;
668                                        break;
669                                  case BasicType::DoubleImaginary:
670                                        ret = BasicType::LongDoubleImaginary;
671                                        break;
672                                  default:
673                                        throw SemanticError( "invalid type modifier \"long\" in type: ", this );
674                                } // switch
675                        } // if
676                        break;
677                  case DeclarationNode::Short:
678                        if ( ! init ) {
679                                init = true;
680                                ret = BasicType::ShortSignedInt;
681                        } else {
682                                switch ( ret ) {
683                                  case BasicType::SignedInt:
684                                        ret = BasicType::ShortSignedInt;
685                                        break;
686                                  case BasicType::UnsignedInt:
687                                        ret = BasicType::ShortUnsignedInt;
688                                        break;
689                                  default:
690                                        throw SemanticError( "invalid type modifier \"short\" in type: ", this );
691                                } // switch
692                        } // if
693                        break;
694                  case DeclarationNode::Signed:
695                        if ( ! init ) {
696                                init = true;
697                                ret = BasicType::SignedInt;
698                        } else if ( sawSigned ) {
699                                throw SemanticError( "duplicate type modifer \"signed\" in type: ", this );
700                        } else {
701                                switch ( ret ) {
702                                  case BasicType::LongLongSignedInt:
703                                        ret = BasicType::LongLongUnsignedInt;
704                                        break;
705                                  case BasicType::LongSignedInt:
706                                        ret = BasicType::LongUnsignedInt;
707                                        break;
708                                  case BasicType::SignedInt:
709                                  case BasicType::ShortSignedInt:
710                                        break;
711                                  case BasicType::Char:
712                                        ret = BasicType::SignedChar;
713                                        break;
714                                  default:
715                                        throw SemanticError( "invalid type modifer \"signed\" in type: ", this );
716                                } // switch
717                        } // if
718                        break;
719                  case DeclarationNode::Unsigned:
720                        if ( ! init ) {
721                                init = true;
722                                ret = BasicType::UnsignedInt;
723                        } else if ( sawSigned ) {
724                                throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
725                        } else {
726                                switch ( ret ) {
727                                  case BasicType::LongLongSignedInt:
728                                        ret = BasicType::LongLongUnsignedInt;
729                                        break;
730                                  case BasicType::LongSignedInt:
731                                        ret = BasicType::LongUnsignedInt;
732                                        break;
733                                  case BasicType::SignedInt:
734                                        ret = BasicType::UnsignedInt;
735                                        break;
736                                  case BasicType::ShortSignedInt:
737                                        ret = BasicType::ShortUnsignedInt;
738                                        break;
739                                  case BasicType::Char:
740                                        ret = BasicType::UnsignedChar;
741                                        break;
742                                  default:
743                                        throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
744                                } // switch
745                        } // if
746                        break;
747                } // switch
748
749                if ( *i == DeclarationNode::Signed ) {
750                        sawSigned = true;
751                } // if
752        } // for
753
754        BasicType *bt;
755        if ( ! init ) {
756                bt = new BasicType( buildQualifiers(), BasicType::SignedInt );
757        } else {
758                bt = new BasicType( buildQualifiers(), ret );
759        } // if
760        buildForall( forall, bt->get_forall() );
761        return bt;
762}
763
764
765PointerType *TypeData::buildPointer() const {
766        PointerType *pt;
767        if ( base ) {
768                pt = new PointerType( buildQualifiers(), base->build() );
769        } else {
770                pt = new PointerType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
771        } // if
772        buildForall( forall, pt->get_forall() );
773        return pt;
774}
775
776ArrayType *TypeData::buildArray() const {
777        ArrayType *at;
778        if ( base ) {
779                at = new ArrayType( buildQualifiers(), base->build(), maybeBuild< Expression >( array->dimension ),
780                                                        array->isVarLen, array->isStatic );
781        } else {
782                at = new ArrayType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
783                                                        maybeBuild< Expression >( array->dimension ), array->isVarLen, array->isStatic );
784        } // if
785        buildForall( forall, at->get_forall() );
786        return at;
787}
788
789FunctionType *TypeData::buildFunction() const {
790        assert( kind == Function );
791        bool hasEllipsis = function->params ? function->params->get_hasEllipsis() : true;
792        if ( ! function->params ) hasEllipsis = ! function->newStyle;
793        FunctionType *ft = new FunctionType( buildQualifiers(), hasEllipsis );
794        buildList( function->params, ft->get_parameters() );
795        buildForall( forall, ft->get_forall() );
796        if ( base ) {
797                switch ( base->kind ) {
798                  case Tuple:
799                        buildList( base->tuple->members, ft->get_returnVals() );
800                        break;
801                  default:
802                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) );
803                } // switch
804        } else {
805                ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) );
806        } // if
807        return ft;
808}
809
810AggregateDecl *TypeData::buildAggregate() const {
811        assert( kind == Aggregate );
812        AggregateDecl *at;
813        switch ( aggregate->kind ) {
814          case DeclarationNode::Struct:
815                at = new StructDecl( aggregate->name );
816                buildForall( aggregate->params, at->get_parameters() );
817                break;
818          case DeclarationNode::Union:
819                at = new UnionDecl( aggregate->name );
820                buildForall( aggregate->params, at->get_parameters() );
821                break;
822          case DeclarationNode::Context:
823                at = new ContextDecl( aggregate->name );
824                buildList( aggregate->params, at->get_parameters() );
825                break;
826          default:
827                assert( false );
828        } // switch
829//      buildList( aggregate->params, at->get_parameters() );
830        buildList( aggregate->fields, at->get_members() );
831
832        return at;
833}
834
835/// namespace {
836/// Type*
837/// makeType( Declaration* decl )
838/// {
839///   if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
840///     return dwt->get_type()->clone();
841///   } else {
842///     return 0;
843///   }
844/// }
845/// }
846
847ReferenceToType *TypeData::buildAggInst() const {
848        assert( kind == AggregateInst );
849
850        ReferenceToType *ret;
851        if ( aggInst->aggregate->kind == Enum ) {
852                ret = new EnumInstType( buildQualifiers(), aggInst->aggregate->enumeration->name );
853        } else {
854                assert( aggInst->aggregate->kind == Aggregate );
855                switch ( aggInst->aggregate->aggregate->kind ) {
856                  case DeclarationNode::Struct:
857                        ret = new StructInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
858                        break;
859                  case DeclarationNode::Union:
860                        ret = new UnionInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
861                        break;
862                  case DeclarationNode::Context:
863                        ret = new ContextInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
864                        break;
865                  default:
866                        assert( false );
867                } // switch
868        } // if
869        buildList( aggInst->params, ret->get_parameters() );
870        buildForall( forall, ret->get_forall() );
871        return ret;
872}
873
874NamedTypeDecl *TypeData::buildSymbolic( const std::string &name, DeclarationNode::StorageClass sc ) const {
875        assert( kind == Symbolic );
876        NamedTypeDecl *ret;
877        if ( symbolic->isTypedef ) {
878                ret = new TypedefDecl( name, sc, maybeBuild< Type >( base ) );
879        } else {
880                ret = new TypeDecl( name, sc, maybeBuild< Type >( base ), TypeDecl::Any );
881        } // if
882        buildList( symbolic->params, ret->get_parameters() );
883        buildList( symbolic->assertions, ret->get_assertions() );
884        return ret;
885}
886
887TypeDecl *TypeData::buildVariable() const {
888        assert( kind == Variable );
889        static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
890
891        TypeDecl *ret = new TypeDecl( variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ variable->tyClass ] );
892        buildList( variable->assertions, ret->get_assertions() );
893        return ret;
894}
895
896EnumDecl *TypeData::buildEnum() const {
897        assert( kind == Enum );
898        EnumDecl *ret = new EnumDecl( enumeration->name );
899        buildList( enumeration->constants, ret->get_members() );
900        std::list< Declaration * >::iterator members = ret->get_members().begin();
901        for ( const DeclarationNode *cur = enumeration->constants; cur != NULL; cur = dynamic_cast<DeclarationNode *>( cur->get_link() ), ++members ) {
902                if ( cur->get_enumeratorValue() != NULL ) {
903                        ObjectDecl *member = dynamic_cast<ObjectDecl *>(*members);
904                        member->set_init( new SingleInit( maybeBuild< Expression >( cur->get_enumeratorValue() ) ) );
905                } // if
906        } // for
907        return ret;
908}
909
910TypeInstType *TypeData::buildSymbolicInst() const {
911        assert( kind == SymbolicInst );
912        TypeInstType *ret = new TypeInstType( buildQualifiers(), symbolic->name, false );
913        buildList( symbolic->actuals, ret->get_parameters() );
914        buildForall( forall, ret->get_forall() );
915        return ret;
916}
917
918TupleType *TypeData::buildTuple() const {
919        assert( kind == Tuple );
920        TupleType *ret = new TupleType( buildQualifiers() );
921        buildTypeList( tuple->members, ret->get_types() );
922        buildForall( forall, ret->get_forall() );
923        return ret;
924}
925
926TypeofType *TypeData::buildTypeof() const {
927        assert( kind == Typeof );
928        assert( typeexpr );
929        assert( typeexpr->expr );
930        TypeofType *ret = new TypeofType( buildQualifiers(), typeexpr->expr->build() );
931        return ret;
932}
933
934AttrType *TypeData::buildAttr() const {
935        assert( kind == Attr );
936        assert( attr );
937        AttrType *ret;
938        if ( attr->expr ) {
939                ret = new AttrType( buildQualifiers(), attr->name, attr->expr->build() );
940        } else {
941                assert( attr->type );
942                ret = new AttrType( buildQualifiers(), attr->name, attr->type->buildType() );
943        } // if
944        return ret;
945}
946
947// Local Variables: //
948// tab-width: 4 //
949// mode: c++ //
950// compile-command: "make install" //
951// End: //
Note: See TracBrowser for help on using the repository browser.