source: src/SymTab/Validate.cc @ 620cb95

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 620cb95 was 620cb95, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

separate Autogen from Validate, call default ctor/dtors on array elements

  • Property mode set to 100644
File size: 24.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Validate.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 21:50:04 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Mon Feb 22 12:26:37 2016
13// Update Count     : 297
14//
15
16// The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
17// as regular in structure as possible.  Some assumptions can be made regarding the state of the tree after this pass is
18// complete, including:
19//
20// - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
21//   union.
22//
23// - All enumeration constants have type EnumInstType.
24//
25// - The type "void" never occurs in lists of function parameter or return types; neither do tuple types.  A function
26//   taking no arguments has no argument types, and tuples are flattened.
27//
28// - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
29//   by the particular set of type arguments.
30//
31// - Every declaration is assigned a unique id.
32//
33// - No typedef declarations or instances exist; the actual type is substituted for each instance.
34//
35// - Each type, struct, and union definition is followed by an appropriate assignment operator.
36//
37// - Each use of a struct or union is connected to a complete definition of that struct or union, even if that
38//   definition occurs later in the input.
39
40#include <list>
41#include <iterator>
42#include "Validate.h"
43#include "SynTree/Visitor.h"
44#include "SynTree/Mutator.h"
45#include "SynTree/Type.h"
46#include "SynTree/Statement.h"
47#include "SynTree/TypeSubstitution.h"
48#include "Indexer.h"
49#include "FixFunction.h"
50// #include "ImplementationType.h"
51#include "Common/utility.h"
52#include "Common/UniqueName.h"
53#include "AddVisit.h"
54#include "MakeLibCfa.h"
55#include "TypeEquality.h"
56#include "Autogen.h"
57#include "ResolvExpr/typeops.h"
58
59#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
60
61namespace SymTab {
62        class HoistStruct : public Visitor {
63          public:
64                /// Flattens nested struct types
65                static void hoistStruct( std::list< Declaration * > &translationUnit );
66
67                std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
68
69                virtual void visit( StructDecl *aggregateDecl );
70                virtual void visit( UnionDecl *aggregateDecl );
71
72                virtual void visit( CompoundStmt *compoundStmt );
73                virtual void visit( IfStmt *ifStmt );
74                virtual void visit( WhileStmt *whileStmt );
75                virtual void visit( ForStmt *forStmt );
76                virtual void visit( SwitchStmt *switchStmt );
77                virtual void visit( ChooseStmt *chooseStmt );
78                virtual void visit( CaseStmt *caseStmt );
79                virtual void visit( CatchStmt *catchStmt );
80          private:
81                HoistStruct();
82
83                template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
84
85                std::list< Declaration * > declsToAdd;
86                bool inStruct;
87        };
88
89        /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
90        class Pass1 : public Visitor {
91                typedef Visitor Parent;
92                virtual void visit( EnumDecl *aggregateDecl );
93                virtual void visit( FunctionType *func );
94        };
95
96        /// Associates forward declarations of aggregates with their definitions
97        class Pass2 : public Indexer {
98                typedef Indexer Parent;
99          public:
100                Pass2( bool doDebug, const Indexer *indexer );
101          private:
102                virtual void visit( StructInstType *structInst );
103                virtual void visit( UnionInstType *unionInst );
104                virtual void visit( ContextInstType *contextInst );
105                virtual void visit( StructDecl *structDecl );
106                virtual void visit( UnionDecl *unionDecl );
107                virtual void visit( TypeInstType *typeInst );
108
109                const Indexer *indexer;
110
111                typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
112                typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
113                ForwardStructsType forwardStructs;
114                ForwardUnionsType forwardUnions;
115        };
116
117        /// Replaces array and function types in forall lists by appropriate pointer type
118        class Pass3 : public Indexer {
119                typedef Indexer Parent;
120          public:
121                Pass3( const Indexer *indexer );
122          private:
123                virtual void visit( ObjectDecl *object );
124                virtual void visit( FunctionDecl *func );
125
126                const Indexer *indexer;
127        };
128
129        class ReturnChecker : public Visitor {
130          public:
131                /// Checks that return statements return nothing if their return type is void
132                /// and return something if the return type is non-void.
133                static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
134
135          private:
136                virtual void visit( FunctionDecl * functionDecl );
137
138                virtual void visit( ReturnStmt * returnStmt );
139
140                std::list< DeclarationWithType * > returnVals;
141        };
142
143        class EliminateTypedef : public Mutator {
144          public:
145                EliminateTypedef() : scopeLevel( 0 ) {}
146                /// Replaces typedefs by forward declarations
147                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
148          private:
149                virtual Declaration *mutate( TypedefDecl *typeDecl );
150                virtual TypeDecl *mutate( TypeDecl *typeDecl );
151                virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
152                virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
153                virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
154                virtual Type *mutate( TypeInstType *aggregateUseType );
155                virtual Expression *mutate( CastExpr *castExpr );
156
157                virtual Declaration *mutate( StructDecl * structDecl );
158                virtual Declaration *mutate( UnionDecl * unionDecl );
159                virtual Declaration *mutate( EnumDecl * enumDecl );
160                virtual Declaration *mutate( ContextDecl * contextDecl );
161
162                template<typename AggDecl>
163                AggDecl *handleAggregate( AggDecl * aggDecl );
164
165                typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
166                TypedefMap typedefNames;
167                int scopeLevel;
168        };
169
170        class VerifyCtorDtor : public Visitor {
171        public:
172                /// ensure that constructors and destructors have at least one
173                /// parameter, the first of which must be a pointer, and no
174                /// return values.
175                static void verify( std::list< Declaration * > &translationUnit );
176
177                // VerifyCtorDtor() {}
178
179                virtual void visit( FunctionDecl *funcDecl );
180        private:
181        };
182
183        void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
184                Pass1 pass1;
185                Pass2 pass2( doDebug, 0 );
186                Pass3 pass3( 0 );
187                EliminateTypedef::eliminateTypedef( translationUnit );
188                HoistStruct::hoistStruct( translationUnit );
189                acceptAll( translationUnit, pass1 );
190                acceptAll( translationUnit, pass2 );
191                ReturnChecker::checkFunctionReturns( translationUnit );
192                autogenerateRoutines( translationUnit );
193                acceptAll( translationUnit, pass3 );
194                VerifyCtorDtor::verify( translationUnit );
195        }
196
197        void validateType( Type *type, const Indexer *indexer ) {
198                Pass1 pass1;
199                Pass2 pass2( false, indexer );
200                Pass3 pass3( indexer );
201                type->accept( pass1 );
202                type->accept( pass2 );
203                type->accept( pass3 );
204        }
205
206        void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
207                HoistStruct hoister;
208                acceptAndAdd( translationUnit, hoister, true );
209        }
210
211        HoistStruct::HoistStruct() : inStruct( false ) {
212        }
213
214        void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
215                std::list< Declaration * >::iterator i = declList.begin();
216                while ( i != declList.end() ) {
217                        std::list< Declaration * >::iterator next = i;
218                        ++next;
219                        if ( pred( *i ) ) {
220                                if ( doDelete ) {
221                                        delete *i;
222                                } // if
223                                declList.erase( i );
224                        } // if
225                        i = next;
226                } // while
227        }
228
229        bool isStructOrUnion( Declaration *decl ) {
230                return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
231        }
232
233        template< typename AggDecl >
234        void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
235                if ( inStruct ) {
236                        // Add elements in stack order corresponding to nesting structure.
237                        declsToAdd.push_front( aggregateDecl );
238                        Visitor::visit( aggregateDecl );
239                } else {
240                        inStruct = true;
241                        Visitor::visit( aggregateDecl );
242                        inStruct = false;
243                } // if
244                // Always remove the hoisted aggregate from the inner structure.
245                filter( aggregateDecl->get_members(), isStructOrUnion, false );
246        }
247
248        void HoistStruct::visit( StructDecl *aggregateDecl ) {
249                handleAggregate( aggregateDecl );
250        }
251
252        void HoistStruct::visit( UnionDecl *aggregateDecl ) {
253                handleAggregate( aggregateDecl );
254        }
255
256        void HoistStruct::visit( CompoundStmt *compoundStmt ) {
257                addVisit( compoundStmt, *this );
258        }
259
260        void HoistStruct::visit( IfStmt *ifStmt ) {
261                addVisit( ifStmt, *this );
262        }
263
264        void HoistStruct::visit( WhileStmt *whileStmt ) {
265                addVisit( whileStmt, *this );
266        }
267
268        void HoistStruct::visit( ForStmt *forStmt ) {
269                addVisit( forStmt, *this );
270        }
271
272        void HoistStruct::visit( SwitchStmt *switchStmt ) {
273                addVisit( switchStmt, *this );
274        }
275
276        void HoistStruct::visit( ChooseStmt *switchStmt ) {
277                addVisit( switchStmt, *this );
278        }
279
280        void HoistStruct::visit( CaseStmt *caseStmt ) {
281                addVisit( caseStmt, *this );
282        }
283
284        void HoistStruct::visit( CatchStmt *cathStmt ) {
285                addVisit( cathStmt, *this );
286        }
287
288        void Pass1::visit( EnumDecl *enumDecl ) {
289                // Set the type of each member of the enumeration to be EnumConstant
290
291                for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
292                        ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
293                        assert( obj );
294                        // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
295                        BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
296                        obj->set_type( enumType ) ;
297                } // for
298                Parent::visit( enumDecl );
299        }
300
301        namespace {
302                template< typename DWTIterator >
303                void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
304                        // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
305                        // entirely other fix ups are handled by the FixFunction class
306                        if ( begin == end ) return;
307                        FixFunction fixer;
308                        DWTIterator i = begin;
309                        *i = (*i )->acceptMutator( fixer );
310                        if ( fixer.get_isVoid() ) {
311                                DWTIterator j = i;
312                                ++i;
313                                func->get_parameters().erase( j );
314                                if ( i != end ) {
315                                        throw SemanticError( "invalid type void in function type ", func );
316                                } // if
317                        } else {
318                                ++i;
319                                for ( ; i != end; ++i ) {
320                                        FixFunction fixer;
321                                        *i = (*i )->acceptMutator( fixer );
322                                        if ( fixer.get_isVoid() ) {
323                                                throw SemanticError( "invalid type void in function type ", func );
324                                        } // if
325                                } // for
326                        } // if
327                }
328        }
329
330        void Pass1::visit( FunctionType *func ) {
331                // Fix up parameters and return types
332                fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
333                fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
334                Visitor::visit( func );
335        }
336
337        Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
338                if ( other_indexer ) {
339                        indexer = other_indexer;
340                } else {
341                        indexer = this;
342                } // if
343        }
344
345        void Pass2::visit( StructInstType *structInst ) {
346                Parent::visit( structInst );
347                StructDecl *st = indexer->lookupStruct( structInst->get_name() );
348                // it's not a semantic error if the struct is not found, just an implicit forward declaration
349                if ( st ) {
350                        //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
351                        structInst->set_baseStruct( st );
352                } // if
353                if ( ! st || st->get_members().empty() ) {
354                        // use of forward declaration
355                        forwardStructs[ structInst->get_name() ].push_back( structInst );
356                } // if
357        }
358
359        void Pass2::visit( UnionInstType *unionInst ) {
360                Parent::visit( unionInst );
361                UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
362                // it's not a semantic error if the union is not found, just an implicit forward declaration
363                if ( un ) {
364                        unionInst->set_baseUnion( un );
365                } // if
366                if ( ! un || un->get_members().empty() ) {
367                        // use of forward declaration
368                        forwardUnions[ unionInst->get_name() ].push_back( unionInst );
369                } // if
370        }
371
372        void Pass2::visit( ContextInstType *contextInst ) {
373                Parent::visit( contextInst );
374                ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
375                if ( ! ctx ) {
376                        throw SemanticError( "use of undeclared context " + contextInst->get_name() );
377                } // if
378                for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
379                        for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
380                                if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
381                                        cloneAll( otherCtx->get_members(), contextInst->get_members() );
382                                } else {
383                                        contextInst->get_members().push_back( (*assert )->clone() );
384                                } // if
385                        } // for
386                } // for
387
388                if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
389                        throw SemanticError( "incorrect number of context parameters: ", contextInst );
390                } // if
391
392                applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) );
393        }
394
395        void Pass2::visit( StructDecl *structDecl ) {
396                if ( ! structDecl->get_members().empty() ) {
397                        ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
398                        if ( fwds != forwardStructs.end() ) {
399                                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
400                                        (*inst )->set_baseStruct( structDecl );
401                                } // for
402                                forwardStructs.erase( fwds );
403                        } // if
404                } // if
405                Indexer::visit( structDecl );
406        }
407
408        void Pass2::visit( UnionDecl *unionDecl ) {
409                if ( ! unionDecl->get_members().empty() ) {
410                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
411                        if ( fwds != forwardUnions.end() ) {
412                                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
413                                        (*inst )->set_baseUnion( unionDecl );
414                                } // for
415                                forwardUnions.erase( fwds );
416                        } // if
417                } // if
418                Indexer::visit( unionDecl );
419        }
420
421        void Pass2::visit( TypeInstType *typeInst ) {
422                if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
423                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
424                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
425                        } // if
426                } // if
427        }
428
429        Pass3::Pass3( const Indexer *other_indexer ) :  Indexer( false ) {
430                if ( other_indexer ) {
431                        indexer = other_indexer;
432                } else {
433                        indexer = this;
434                } // if
435        }
436
437        /// Fix up assertions
438        void forallFixer( Type *func ) {
439                for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
440                        std::list< DeclarationWithType * > toBeDone, nextRound;
441                        toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
442                        while ( ! toBeDone.empty() ) {
443                                for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
444                                        if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
445                                                for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
446                                                        DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
447                                                        assert( dwt );
448                                                        nextRound.push_back( dwt->clone() );
449                                                }
450                                                delete ctx;
451                                        } else {
452                                                FixFunction fixer;
453                                                *assertion = (*assertion )->acceptMutator( fixer );
454                                                if ( fixer.get_isVoid() ) {
455                                                        throw SemanticError( "invalid type void in assertion of function ", func );
456                                                }
457                                                (*type )->get_assertions().push_back( *assertion );
458                                        } // if
459                                } // for
460                                toBeDone.clear();
461                                toBeDone.splice( toBeDone.end(), nextRound );
462                        } // while
463                } // for
464        }
465
466        void Pass3::visit( ObjectDecl *object ) {
467                forallFixer( object->get_type() );
468                if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
469                        forallFixer( pointer->get_base() );
470                } // if
471                Parent::visit( object );
472                object->fixUniqueId();
473        }
474
475        void Pass3::visit( FunctionDecl *func ) {
476                forallFixer( func->get_type() );
477                Parent::visit( func );
478                func->fixUniqueId();
479        }
480
481        void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
482                ReturnChecker checker;
483                acceptAll( translationUnit, checker );
484        }
485
486        void ReturnChecker::visit( FunctionDecl * functionDecl ) {
487                std::list< DeclarationWithType * > oldReturnVals = returnVals;
488                returnVals = functionDecl->get_functionType()->get_returnVals();
489                Visitor::visit( functionDecl );
490                returnVals = oldReturnVals;
491        }
492
493        void ReturnChecker::visit( ReturnStmt * returnStmt ) {
494                if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
495                        throw SemanticError( "Non-void function returns no values: " , returnStmt );
496                } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) {
497                        throw SemanticError( "void function returns values: " , returnStmt );
498                }
499        }
500
501
502        bool isTypedef( Declaration *decl ) {
503                return dynamic_cast< TypedefDecl * >( decl );
504        }
505
506        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
507                EliminateTypedef eliminator;
508                mutateAll( translationUnit, eliminator );
509                filter( translationUnit, isTypedef, true );
510        }
511
512        Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
513                // instances of typedef types will come here. If it is an instance
514                // of a typdef type, link the instance to its actual type.
515                TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
516                if ( def != typedefNames.end() ) {
517                        Type *ret = def->second.first->get_base()->clone();
518                        ret->get_qualifiers() += typeInst->get_qualifiers();
519                        // place instance parameters on the typedef'd type
520                        if ( ! typeInst->get_parameters().empty() ) {
521                                ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
522                                if ( ! rtt ) {
523                                        throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
524                                }
525                                rtt->get_parameters().clear();
526                                cloneAll(typeInst->get_parameters(), rtt->get_parameters());
527                        } // if
528                        delete typeInst;
529                        return ret;
530                } // if
531                return typeInst;
532        }
533
534        Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
535                Declaration *ret = Mutator::mutate( tyDecl );
536                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
537                        // typedef to the same name from the same scope
538                        // must be from the same type
539
540                        Type * t1 = tyDecl->get_base();
541                        Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
542                        if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
543                                throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
544                        }
545                } else {
546                        typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
547                } // if
548
549                // When a typedef is a forward declaration:
550                //    typedef struct screen SCREEN;
551                // the declaration portion must be retained:
552                //    struct screen;
553                // because the expansion of the typedef is:
554                //    void rtn( SCREEN *p ) => void rtn( struct screen *p )
555                // hence the type-name "screen" must be defined.
556                // Note, qualifiers on the typedef are superfluous for the forward declaration.
557                if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
558                        return new StructDecl( aggDecl->get_name() );
559                } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
560                        return new UnionDecl( aggDecl->get_name() );
561                } else {
562                        return ret;
563                } // if
564        }
565
566        TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
567                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
568                if ( i != typedefNames.end() ) {
569                        typedefNames.erase( i ) ;
570                } // if
571                return typeDecl;
572        }
573
574        DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
575                TypedefMap oldNames = typedefNames;
576                DeclarationWithType *ret = Mutator::mutate( funcDecl );
577                typedefNames = oldNames;
578                return ret;
579        }
580
581        DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
582                TypedefMap oldNames = typedefNames;
583                DeclarationWithType *ret = Mutator::mutate( objDecl );
584                typedefNames = oldNames;
585                // is the type a function?
586                if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
587                        // replace the current object declaration with a function declaration
588                        return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
589                } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
590                        throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
591                } // if
592                return ret;
593        }
594
595        Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
596                TypedefMap oldNames = typedefNames;
597                Expression *ret = Mutator::mutate( castExpr );
598                typedefNames = oldNames;
599                return ret;
600        }
601
602        CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
603                TypedefMap oldNames = typedefNames;
604                scopeLevel += 1;
605                CompoundStmt *ret = Mutator::mutate( compoundStmt );
606                scopeLevel -= 1;
607                std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
608                while ( i != compoundStmt->get_kids().end() ) {
609                        std::list< Statement * >::iterator next = i+1;
610                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
611                                if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
612                                        delete *i;
613                                        compoundStmt->get_kids().erase( i );
614                                } // if
615                        } // if
616                        i = next;
617                } // while
618                typedefNames = oldNames;
619                return ret;
620        }
621
622        // there may be typedefs nested within aggregates
623        // in order for everything to work properly, these
624        // should be removed as well
625        template<typename AggDecl>
626        AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
627                std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
628                for ( ; it != aggDecl->get_members().end(); ) {
629                        std::list< Declaration * >::iterator next = it+1;
630                        if ( dynamic_cast< TypedefDecl * >( *it ) ) {
631                                delete *it;
632                                aggDecl->get_members().erase( it );
633                        } // if
634                        it = next;
635                }
636                return aggDecl;
637        }
638
639        Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
640                Mutator::mutate( structDecl );
641                return handleAggregate( structDecl );
642        }
643
644        Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
645                Mutator::mutate( unionDecl );
646                return handleAggregate( unionDecl );
647        }
648
649        Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
650                Mutator::mutate( enumDecl );
651                return handleAggregate( enumDecl );
652        }
653
654                Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
655                Mutator::mutate( contextDecl );
656                return handleAggregate( contextDecl );
657        }
658
659        void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) {
660                VerifyCtorDtor verifier;
661                acceptAll( translationUnit, verifier );
662        }
663
664        void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) {
665                FunctionType * funcType = funcDecl->get_functionType();
666                std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
667                std::list< DeclarationWithType * > &params = funcType->get_parameters();
668
669                if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) {
670                        if ( params.size() == 0 ) {
671                                throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl );
672                        }
673                        if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) {
674                                throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl );
675                        }
676                        if ( returnVals.size() != 0 ) {
677                                throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
678                        }
679                }
680
681                Visitor::visit( funcDecl );
682                // original idea: modify signature of ctor/dtors and insert appropriate return statements
683                // to cause desired behaviour
684                // new idea: add comma exprs to every ctor call to produce first parameter.
685                // this requires some memoization of the first parameter, because it can be a
686                // complicated expression with side effects (see: malloc). idea: add temporary variable
687                // that is assigned address of constructed object in ctor argument position and
688                // return the temporary. It should also be done after all implicit ctors are
689                // added, so not in this pass!
690        }
691} // namespace SymTab
692
693// Local Variables: //
694// tab-width: 4 //
695// mode: c++ //
696// compile-command: "make install" //
697// End: //
Note: See TracBrowser for help on using the repository browser.