source: src/InitTweak/InitTweak.cc @ 29e8bf5

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

unused thunks are no longer generated for intrinsic function calls

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include "InitTweak.h"
2#include "SynTree/Visitor.h"
3#include "SynTree/Statement.h"
4#include "SynTree/Initializer.h"
5#include "SynTree/Expression.h"
6#include "GenPoly/GenPoly.h"
7
8namespace InitTweak {
9        namespace {
10                class HasDesignations : public Visitor {
11                public:
12                        bool hasDesignations = false;
13                        template<typename Init>
14                        void handleInit( Init * init ) {
15                                if ( ! init->get_designators().empty() ) hasDesignations = true;
16                                else Visitor::visit( init );
17                        }
18                        virtual void visit( SingleInit * singleInit ) { handleInit( singleInit); }
19                        virtual void visit( ListInit * listInit ) { handleInit( listInit); }
20                };
21
22                class InitExpander : public Visitor {
23                        public:
24                        InitExpander() {}
25                        virtual void visit( SingleInit * singleInit );
26                        virtual void visit( ListInit * listInit );
27                        std::list< Expression * > argList;
28                };
29
30                void InitExpander::visit( SingleInit * singleInit ) {
31                        argList.push_back( singleInit->get_value()->clone() );
32                }
33
34                void InitExpander::visit( ListInit * listInit ) {
35                        // xxx - for now, assume no nested list inits
36                        std::list<Initializer*>::iterator it = listInit->begin_initializers();
37                        for ( ; it != listInit->end_initializers(); ++it ) {
38                                (*it)->accept( *this );
39                        }
40                }
41        }
42
43        std::list< Expression * > makeInitList( Initializer * init ) {
44                InitExpander expander;
45                maybeAccept( init, expander );
46                return expander.argList;
47        }
48
49        bool isDesignated( Initializer * init ) {
50                HasDesignations finder;
51                maybeAccept( init, finder );
52                return finder.hasDesignations;
53        }
54
55        bool tryConstruct( ObjectDecl * objDecl ) {
56                return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
57                        (objDecl->get_init() == NULL ||
58                                ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) &&
59                        ! isDesignated( objDecl->get_init() )
60                        && objDecl->get_storageClass() != DeclarationNode::Extern;
61        }
62
63        Expression * getCtorDtorCall( Statement * stmt ) {
64                if ( stmt == NULL ) return NULL;
65                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
66                        return exprStmt->get_expr();
67                } else if ( CompoundStmt * compoundStmt = dynamic_cast< CompoundStmt * >( stmt ) ) {
68                        // could also be a compound statement with a loop, in the case of an array
69                        if( compoundStmt->get_kids().size() == 2 ) {
70                                // loop variable and loop
71                                ForStmt * forStmt = dynamic_cast< ForStmt * >( compoundStmt->get_kids().back() );
72                                assert( forStmt && forStmt->get_body() );
73                                return getCtorDtorCall( forStmt->get_body() );
74                        } else if ( compoundStmt->get_kids().size() == 1 ) {
75                                // should be the call statement, but in any case there's only one option
76                                return getCtorDtorCall( compoundStmt->get_kids().front() );
77                        } else {
78                                assert( false && "too many statements in compoundStmt for getCtorDtorCall" );
79                        }
80                } if ( ImplicitCtorDtorStmt * impCtorDtorStmt = dynamic_cast< ImplicitCtorDtorStmt * > ( stmt ) ) {
81                        return getCtorDtorCall( impCtorDtorStmt->get_callStmt() );
82                } else {
83                        // should never get here
84                        assert( false && "encountered unknown call statement" );
85                }
86        }
87        namespace {
88                VariableExpr * getCalledFunction( ApplicationExpr * appExpr ) {
89                        assert( appExpr );
90                        return dynamic_cast< VariableExpr * >( appExpr->get_function() );
91                }
92        }
93
94        ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
95                ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
96                if ( ! appExpr ) return NULL;
97                VariableExpr * function = getCalledFunction( appExpr );
98                assert( function );
99                // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
100                // will call all member dtors, and some members may have a user defined dtor.
101                return function->get_var()->get_linkage() == LinkageSpec::Intrinsic ? appExpr : NULL;
102        }
103
104        bool isInstrinsicSingleArgCallStmt( Statement * stmt ) {
105                Expression * callExpr = getCtorDtorCall( stmt );
106                if ( ! callExpr ) return false;
107                if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) {
108                        assert( ! appExpr->get_function()->get_results().empty() );
109                        FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_results().front() );
110                        assert( funcType );
111                        return funcType->get_parameters().size() == 1;
112                }
113                return false;
114        }
115
116        namespace {
117                template<typename CallExpr>
118                Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
119                        if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" );
120                        for ( Expression *& arg : callExpr->get_args() ) {
121                                if ( pos == 0 ) return arg;
122                                pos--;
123                        }
124                        assert( false );
125                }
126        }
127
128        Expression *& getCallArg( Expression * callExpr, unsigned int pos ) {
129                if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
130                        return callArg( appExpr, pos );
131                } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
132                        return callArg( untypedExpr, pos );
133                } else {
134                        assert( false && "Unexpected expression type passed to getCallArg" );
135                }
136        }
137
138        namespace {
139                std::string funcName( Expression * func ) {
140                        if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
141                                return nameExpr->get_name();
142                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
143                                return varExpr->get_var()->get_name();
144                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
145                                return funcName( castExpr->get_arg() );
146                        } else {
147                                assert( false && "Unexpected expression type being called as a function in call expression" );
148                        }
149                }
150        }
151
152        std::string getFunctionName( Expression * expr ) {
153                if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
154                        return funcName( appExpr->get_function() );
155                } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
156                        return funcName( untypedExpr->get_function() );
157                } else {
158                        std::cerr << expr << std::endl;
159                        assert( false && "Unexpected expression type passed to getFunctionName" );
160                }
161        }
162
163        Type * getPointerBase( Type * type ) {
164                if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
165                        return ptrType->get_base();
166                } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
167                        return arrayType->get_base();
168                } else {
169                        return NULL;
170                }
171        }
172
173        Type * isPointerType( Type * type ) {
174                if ( getPointerBase( type ) ) return type;
175                else return NULL;
176        }
177
178        class ConstExprChecker : public Visitor {
179        public:
180                ConstExprChecker() : isConstExpr( true ) {}
181
182                virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; }
183                virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; }
184                virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; }
185                virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
186                virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; }
187                virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; }
188                virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; }
189                virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; }
190                virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ }
191                // these might be okay?
192                // virtual void visit( SizeofExpr *sizeofExpr );
193                // virtual void visit( AlignofExpr *alignofExpr );
194                // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
195                // virtual void visit( OffsetofExpr *offsetofExpr );
196                // virtual void visit( OffsetPackExpr *offsetPackExpr );
197                // virtual void visit( AttrExpr *attrExpr );
198                // virtual void visit( CommaExpr *commaExpr );
199                // virtual void visit( LogicalExpr *logicalExpr );
200                // virtual void visit( ConditionalExpr *conditionalExpr );
201                virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; }
202                virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; }
203                virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; }
204                virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; }
205                virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; }
206                virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; }
207
208                bool isConstExpr;
209        };
210
211        bool isConstExpr( Expression * expr ) {
212                if ( expr ) {
213                        ConstExprChecker checker;
214                        expr->accept( checker );
215                        return checker.isConstExpr;
216                }
217                return true;
218        }
219
220        bool isConstExpr( Initializer * init ) {
221                if ( init ) {
222                        ConstExprChecker checker;
223                        init->accept( checker );
224                        return checker.isConstExpr;
225                } // if
226                // for all intents and purposes, no initializer means const expr
227                return true;
228        }
229
230}
Note: See TracBrowser for help on using the repository browser.