source: src/GenPoly/GenPoly.cc@ 933667d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 933667d was aadc9a4, checked in by Aaron Moss <a3moss@…>, 10 years ago

Refactor isPolyRet to include generic return types

  • Property mode set to 100644
File size: 6.0 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 FunctionType * getFunctionType( Type *ty ) {
130 PointerType *ptrType;
131 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
132 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
133 } else {
134 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
135 }
136 }
137
138 void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
139 for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
140 assert( *tyVar );
141 tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
142 }
143 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
144 makeTyVarMap( pointer->get_base(), tyVarMap );
145 }
146 }
147
148 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
149 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
150 os << i->first << " (" << i->second << ") ";
151 } // for
152 os << std::endl;
153 }
154
155 std::string sizeofName( Type *ty ) {
156 return std::string( "_sizeof_" ) + SymTab::Mangler::mangleType( ty );
157 }
158
159 std::string alignofName( Type *ty ) {
160 return std::string( "_alignof_" ) + SymTab::Mangler::mangleType( ty );
161 }
162} // namespace GenPoly
163
164// Local Variables: //
165// tab-width: 4 //
166// mode: c++ //
167// compile-command: "make install" //
168// End: //
Note: See TracBrowser for help on using the repository browser.