source: src/GenPoly/GenPoly.cc @ d63eeb0

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 d63eeb0 was 8488c715, checked in by Aaron Moss <a3moss@…>, 8 years ago

Fixed dereferencing in member expressions

  • Property mode set to 100644
File size: 7.9 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// GenPoly.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 15 16:11:18 2015
13// Update Count     : 13
14//
15
16#include "GenPoly.h"
17
18#include "SymTab/Mangler.h"
19#include "SynTree/Expression.h"
20#include "SynTree/Type.h"
21
22#include <iostream>
23using namespace std;
24
25namespace GenPoly {
26        bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
27                if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
28                        return true;
29                } // if
30                for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
31                        if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
32                                return true;
33                        } // if
34                } // for
35                return false;
36        }
37
38        ReferenceToType *isPolyRet( FunctionType *function ) {
39                if ( ! function->get_returnVals().empty() ) {
40                        TyVarMap forallTypes;
41                        makeTyVarMap( function, forallTypes );
42                        return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
43                } // if
44                return 0;
45        }
46
47        namespace {
48                /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
49                bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
50                        for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
51                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
52                                assert(paramType && "Aggregate parameters should be type expressions");
53                                if ( isPolyType( paramType->get_type(), env ) ) return true;
54                        }
55                        return false;
56                }
57
58                /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
59                bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
60                        for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
61                                TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
62                                assert(paramType && "Aggregate parameters should be type expressions");
63                                if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
64                        }
65                        return false;
66                }
67        }
68
69        Type *isPolyType( Type *type, const TypeSubstitution *env ) {
70                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
71                        if ( env ) {
72                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
73                                        return isPolyType( newType, env );
74                                } // if
75                        } // if
76                        return type;
77                } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
78                        if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
79                } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
80                        if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
81                }
82                return 0;
83        }
84       
85        Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
86                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
87                        if ( env ) {
88                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
89                                        return isPolyType( newType, tyVars, env );
90                                } // if
91                        } // if
92                        if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
93                                return type;
94                        }
95                } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
96                        if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
97                } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
98                        if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
99                }
100                return 0;
101        }
102
103        Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
104                if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
105                        return isPolyType( ptr->get_base(), env );
106                } else if ( env ) {
107                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
108                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
109                                        return isPolyPtr( newType, env );
110                                } // if
111                        } // if
112                } // if
113                return 0;
114        }
115       
116        Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
117                if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
118                        return isPolyType( ptr->get_base(), tyVars, env );
119                } else if ( env ) {
120                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
121                                if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
122                                        return isPolyPtr( newType, tyVars, env );
123                                } // if
124                        } // if
125                } // if
126                return 0;
127        }
128
129        Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
130                int dummy;
131                if ( ! levels ) { levels = &dummy; }
132                *levels = 0;
133
134                while ( true ) {
135                        if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
136                                type = ptr->get_base();
137                                ++(*levels);
138                        } else if ( env ) {
139                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
140                                        if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
141                                                type = newType;
142                                        } else break;
143                                } else break;
144                        } else break;
145                }
146
147                return isPolyType( type, env );
148        }
149       
150        Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
151                int dummy;
152                if ( ! levels ) { levels = &dummy; }
153                *levels = 0;
154
155                while ( true ) {
156                        if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
157                                type = ptr->get_base();
158                                ++(*levels);
159                        } else if ( env ) {
160                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
161                                        if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
162                                                type = newType;
163                                        } else break;
164                                } else break;
165                        } else break;
166                }
167
168                return isPolyType( type, tyVars, env );
169        }
170
171        FunctionType * getFunctionType( Type *ty ) {
172                PointerType *ptrType;
173                if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
174                        return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
175                } else {
176                        return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
177                }
178        }
179
180        VariableExpr * getBaseVar( Expression *expr, int *levels ) {
181                int dummy;
182                if ( ! levels ) { levels = &dummy; }
183                *levels = 0;
184
185                while ( true ) {
186                        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
187                                return varExpr;
188                        } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
189                                expr = addressExpr->get_arg();
190                        } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
191                                // look for compiler-inserted dereference operator
192                                NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
193                                if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
194                                expr = *untypedExpr->begin_args();
195                        } else break;
196
197                        ++(*levels);
198                }
199
200                return 0;
201        }
202
203        void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
204                for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
205                        assert( *tyVar );
206                        tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
207                }
208                if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
209                        makeTyVarMap( pointer->get_base(), tyVarMap );
210                }
211        }
212       
213        void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
214                for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
215                        os << i->first << " (" << i->second << ") ";
216                } // for
217                os << std::endl;
218        }
219
220        std::string sizeofName( Type *ty ) {
221                return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
222        }
223
224        std::string alignofName( Type *ty ) {
225                return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
226        }
227
228        std::string offsetofName( Type* ty ) {
229                return std::string( "_offsetof_" ) + SymTab::Mangler::mangleType( ty );
230        }
231
232} // namespace GenPoly
233
234// Local Variables: //
235// tab-width: 4 //
236// mode: c++ //
237// compile-command: "make install" //
238// End: //
Note: See TracBrowser for help on using the repository browser.