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 | // Mangler.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 21:40:29 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Sep 25 15:49:26 2017 |
---|
13 | // Update Count : 23 |
---|
14 | // |
---|
15 | #include "Mangler.h" |
---|
16 | |
---|
17 | #include <algorithm> // for copy, transform |
---|
18 | #include <cassert> // for assert, assertf |
---|
19 | #include <functional> // for const_mem_fun_t, mem_fun |
---|
20 | #include <iterator> // for ostream_iterator, back_insert_ite... |
---|
21 | #include <list> // for _List_iterator, list, _List_const... |
---|
22 | #include <string> // for string, char_traits, operator<< |
---|
23 | |
---|
24 | #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
25 | #include "Common/PassVisitor.h" |
---|
26 | #include "Common/SemanticError.h" // for SemanticError |
---|
27 | #include "Common/utility.h" // for toString |
---|
28 | #include "Parser/LinkageSpec.h" // for Spec, isOverridable, AutoGen, Int... |
---|
29 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
30 | #include "SynTree/Declaration.h" // for TypeDecl, DeclarationWithType |
---|
31 | #include "SynTree/Expression.h" // for TypeExpr, Expression, operator<< |
---|
32 | #include "SynTree/Type.h" // for Type, ReferenceToType, Type::Fora... |
---|
33 | |
---|
34 | namespace SymTab { |
---|
35 | namespace Mangler { |
---|
36 | namespace { |
---|
37 | /// Mangles names to a unique C identifier |
---|
38 | struct Mangler : public WithShortCircuiting, public WithVisitorRef<Mangler>, public WithGuards { |
---|
39 | Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); |
---|
40 | Mangler( const Mangler & ) = delete; |
---|
41 | |
---|
42 | void previsit( BaseSyntaxNode * ) { visit_children = false; } |
---|
43 | |
---|
44 | void postvisit( ObjectDecl * declaration ); |
---|
45 | void postvisit( FunctionDecl * declaration ); |
---|
46 | void postvisit( TypeDecl * declaration ); |
---|
47 | |
---|
48 | void postvisit( VoidType * voidType ); |
---|
49 | void postvisit( BasicType * basicType ); |
---|
50 | void postvisit( PointerType * pointerType ); |
---|
51 | void postvisit( ArrayType * arrayType ); |
---|
52 | void postvisit( ReferenceType * refType ); |
---|
53 | void postvisit( FunctionType * functionType ); |
---|
54 | void postvisit( StructInstType * aggregateUseType ); |
---|
55 | void postvisit( UnionInstType * aggregateUseType ); |
---|
56 | void postvisit( EnumInstType * aggregateUseType ); |
---|
57 | void postvisit( TypeInstType * aggregateUseType ); |
---|
58 | void postvisit( TraitInstType * inst ); |
---|
59 | void postvisit( TupleType * tupleType ); |
---|
60 | void postvisit( VarArgsType * varArgsType ); |
---|
61 | void postvisit( ZeroType * zeroType ); |
---|
62 | void postvisit( OneType * oneType ); |
---|
63 | void postvisit( QualifiedType * qualType ); |
---|
64 | |
---|
65 | std::string get_mangleName() { return mangleName.str(); } |
---|
66 | private: |
---|
67 | std::ostringstream mangleName; ///< Mangled name being constructed |
---|
68 | typedef std::map< std::string, std::pair< int, int > > VarMapType; |
---|
69 | VarMapType varNums; ///< Map of type variables to indices |
---|
70 | int nextVarNum; ///< Next type variable index |
---|
71 | bool isTopLevel; ///< Is the Mangler at the top level |
---|
72 | bool mangleOverridable; ///< Specially mangle overridable built-in methods |
---|
73 | bool typeMode; ///< Produce a unique mangled name for a type |
---|
74 | bool mangleGenericParams; ///< Include generic parameters in name mangling if true |
---|
75 | bool inFunctionType = false; ///< Include type qualifiers if false. |
---|
76 | bool inQualifiedType = false; ///< Add start/end delimiters around qualified type |
---|
77 | |
---|
78 | public: |
---|
79 | Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams, |
---|
80 | int nextVarNum, const VarMapType& varNums ); |
---|
81 | |
---|
82 | private: |
---|
83 | void mangleDecl( DeclarationWithType *declaration ); |
---|
84 | void mangleRef( ReferenceToType *refType, std::string prefix ); |
---|
85 | |
---|
86 | void printQualifiers( Type *type ); |
---|
87 | }; // Mangler |
---|
88 | } // namespace |
---|
89 | |
---|
90 | std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) { |
---|
91 | PassVisitor<Mangler> mangler( mangleOverridable, typeMode, mangleGenericParams ); |
---|
92 | maybeAccept( decl, mangler ); |
---|
93 | return mangler.pass.get_mangleName(); |
---|
94 | } |
---|
95 | |
---|
96 | std::string mangleType( Type * ty ) { |
---|
97 | PassVisitor<Mangler> mangler( false, true, true ); |
---|
98 | maybeAccept( ty, mangler ); |
---|
99 | return mangler.pass.get_mangleName(); |
---|
100 | } |
---|
101 | |
---|
102 | std::string mangleConcrete( Type * ty ) { |
---|
103 | PassVisitor<Mangler> mangler( false, false, false ); |
---|
104 | maybeAccept( ty, mangler ); |
---|
105 | return mangler.pass.get_mangleName(); |
---|
106 | } |
---|
107 | |
---|
108 | namespace { |
---|
109 | Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) |
---|
110 | : nextVarNum( 0 ), isTopLevel( true ), |
---|
111 | mangleOverridable( mangleOverridable ), typeMode( typeMode ), |
---|
112 | mangleGenericParams( mangleGenericParams ) {} |
---|
113 | |
---|
114 | Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams, |
---|
115 | int nextVarNum, const VarMapType& varNums ) |
---|
116 | : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ), |
---|
117 | mangleOverridable( mangleOverridable ), typeMode( typeMode ), |
---|
118 | mangleGenericParams( mangleGenericParams ) {} |
---|
119 | |
---|
120 | void Mangler::mangleDecl( DeclarationWithType * declaration ) { |
---|
121 | bool wasTopLevel = isTopLevel; |
---|
122 | if ( isTopLevel ) { |
---|
123 | varNums.clear(); |
---|
124 | nextVarNum = 0; |
---|
125 | isTopLevel = false; |
---|
126 | } // if |
---|
127 | mangleName << Encoding::manglePrefix; |
---|
128 | CodeGen::OperatorInfo opInfo; |
---|
129 | if ( operatorLookup( declaration->get_name(), opInfo ) ) { |
---|
130 | mangleName << opInfo.outputName.size() << opInfo.outputName; |
---|
131 | } else { |
---|
132 | mangleName << declaration->name.size() << declaration->name; |
---|
133 | } // if |
---|
134 | maybeAccept( declaration->get_type(), *visitor ); |
---|
135 | if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { |
---|
136 | // want to be able to override autogenerated and intrinsic routines, |
---|
137 | // so they need a different name mangling |
---|
138 | if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { |
---|
139 | mangleName << Encoding::autogen; |
---|
140 | } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
141 | mangleName << Encoding::intrinsic; |
---|
142 | } else { |
---|
143 | // if we add another kind of overridable function, this has to change |
---|
144 | assert( false && "unknown overrideable linkage" ); |
---|
145 | } // if |
---|
146 | } |
---|
147 | isTopLevel = wasTopLevel; |
---|
148 | } |
---|
149 | |
---|
150 | void Mangler::postvisit( ObjectDecl * declaration ) { |
---|
151 | mangleDecl( declaration ); |
---|
152 | } |
---|
153 | |
---|
154 | void Mangler::postvisit( FunctionDecl * declaration ) { |
---|
155 | mangleDecl( declaration ); |
---|
156 | } |
---|
157 | |
---|
158 | void Mangler::postvisit( VoidType * voidType ) { |
---|
159 | printQualifiers( voidType ); |
---|
160 | mangleName << Encoding::void_t; |
---|
161 | } |
---|
162 | |
---|
163 | void Mangler::postvisit( BasicType * basicType ) { |
---|
164 | printQualifiers( basicType ); |
---|
165 | assertf( basicType->get_kind() < BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->get_kind() ); |
---|
166 | mangleName << Encoding::basicTypes[ basicType->get_kind() ]; |
---|
167 | } |
---|
168 | |
---|
169 | void Mangler::postvisit( PointerType * pointerType ) { |
---|
170 | printQualifiers( pointerType ); |
---|
171 | // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers |
---|
172 | if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName << Encoding::pointer; |
---|
173 | maybeAccept( pointerType->base, *visitor ); |
---|
174 | } |
---|
175 | |
---|
176 | void Mangler::postvisit( ArrayType * arrayType ) { |
---|
177 | // TODO: encode dimension |
---|
178 | printQualifiers( arrayType ); |
---|
179 | mangleName << Encoding::array << "0"; |
---|
180 | maybeAccept( arrayType->base, *visitor ); |
---|
181 | } |
---|
182 | |
---|
183 | void Mangler::postvisit( ReferenceType * refType ) { |
---|
184 | // don't print prefix (e.g. 'R') for reference types so that references and non-references do not overload. |
---|
185 | // Further, do not print the qualifiers for a reference type (but do run printQualifers because of TypeDecls, etc.), |
---|
186 | // by pretending every reference type is a function parameter. |
---|
187 | GuardValue( inFunctionType ); |
---|
188 | inFunctionType = true; |
---|
189 | printQualifiers( refType ); |
---|
190 | maybeAccept( refType->base, *visitor ); |
---|
191 | } |
---|
192 | |
---|
193 | namespace { |
---|
194 | inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { |
---|
195 | std::list< Type* > ret; |
---|
196 | std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), |
---|
197 | std::mem_fun( &DeclarationWithType::get_type ) ); |
---|
198 | return ret; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | void Mangler::postvisit( FunctionType * functionType ) { |
---|
203 | printQualifiers( functionType ); |
---|
204 | mangleName << Encoding::function; |
---|
205 | // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters, |
---|
206 | // since qualifiers on outermost parameter type do not differentiate function types, e.g., |
---|
207 | // void (*)(const int) and void (*)(int) are the same type, but void (*)(const int *) and void (*)(int *) are different |
---|
208 | GuardValue( inFunctionType ); |
---|
209 | inFunctionType = true; |
---|
210 | std::list< Type* > returnTypes = getTypes( functionType->returnVals ); |
---|
211 | if (returnTypes.empty()) mangleName << Encoding::void_t; |
---|
212 | else acceptAll( returnTypes, *visitor ); |
---|
213 | mangleName << "_"; |
---|
214 | std::list< Type* > paramTypes = getTypes( functionType->parameters ); |
---|
215 | acceptAll( paramTypes, *visitor ); |
---|
216 | mangleName << "_"; |
---|
217 | } |
---|
218 | |
---|
219 | void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { |
---|
220 | printQualifiers( refType ); |
---|
221 | |
---|
222 | mangleName << prefix << refType->name.length() << refType->name; |
---|
223 | |
---|
224 | if ( mangleGenericParams ) { |
---|
225 | std::list< Expression* >& params = refType->parameters; |
---|
226 | if ( ! params.empty() ) { |
---|
227 | mangleName << "_"; |
---|
228 | for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { |
---|
229 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
230 | assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString(*param)); |
---|
231 | maybeAccept( paramType->type, *visitor ); |
---|
232 | } |
---|
233 | mangleName << "_"; |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | void Mangler::postvisit( StructInstType * aggregateUseType ) { |
---|
239 | mangleRef( aggregateUseType, Encoding::struct_t ); |
---|
240 | } |
---|
241 | |
---|
242 | void Mangler::postvisit( UnionInstType * aggregateUseType ) { |
---|
243 | mangleRef( aggregateUseType, Encoding::union_t ); |
---|
244 | } |
---|
245 | |
---|
246 | void Mangler::postvisit( EnumInstType * aggregateUseType ) { |
---|
247 | mangleRef( aggregateUseType, Encoding::enum_t ); |
---|
248 | } |
---|
249 | |
---|
250 | void Mangler::postvisit( TypeInstType * typeInst ) { |
---|
251 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
252 | if ( varNum == varNums.end() ) { |
---|
253 | mangleRef( typeInst, Encoding::type ); |
---|
254 | } else { |
---|
255 | printQualifiers( typeInst ); |
---|
256 | // Note: Can't use name here, since type variable names do not actually disambiguate a function, e.g. |
---|
257 | // forall(dtype T) void f(T); |
---|
258 | // forall(dtype S) void f(S); |
---|
259 | // are equivalent and should mangle the same way. This is accomplished by numbering the type variables when they |
---|
260 | // are first found and prefixing with the appropriate encoding for the type class. |
---|
261 | assertf( varNum->second.second < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", varNum->second.second ); |
---|
262 | mangleName << Encoding::typeVariables[varNum->second.second] << varNum->second.first; |
---|
263 | } // if |
---|
264 | } |
---|
265 | |
---|
266 | void Mangler::postvisit( TraitInstType * inst ) { |
---|
267 | printQualifiers( inst ); |
---|
268 | mangleName << inst->name.size() << inst->name; |
---|
269 | } |
---|
270 | |
---|
271 | void Mangler::postvisit( TupleType * tupleType ) { |
---|
272 | printQualifiers( tupleType ); |
---|
273 | mangleName << Encoding::tuple << tupleType->types.size(); |
---|
274 | acceptAll( tupleType->types, *visitor ); |
---|
275 | } |
---|
276 | |
---|
277 | void Mangler::postvisit( VarArgsType * varArgsType ) { |
---|
278 | printQualifiers( varArgsType ); |
---|
279 | static const std::string vargs = "__builtin_va_list"; |
---|
280 | mangleName << Encoding::type << vargs.size() << vargs; |
---|
281 | } |
---|
282 | |
---|
283 | void Mangler::postvisit( ZeroType * ) { |
---|
284 | mangleName << Encoding::zero; |
---|
285 | } |
---|
286 | |
---|
287 | void Mangler::postvisit( OneType * ) { |
---|
288 | mangleName << Encoding::one; |
---|
289 | } |
---|
290 | |
---|
291 | void Mangler::postvisit( QualifiedType * qualType ) { |
---|
292 | bool inqual = inQualifiedType; |
---|
293 | if (! inqual ) { |
---|
294 | // N marks the start of a qualified type |
---|
295 | inQualifiedType = true; |
---|
296 | mangleName << Encoding::qualifiedTypeStart; |
---|
297 | } |
---|
298 | maybeAccept( qualType->parent, *visitor ); |
---|
299 | maybeAccept( qualType->child, *visitor ); |
---|
300 | if ( ! inqual ) { |
---|
301 | // E marks the end of a qualified type |
---|
302 | inQualifiedType = false; |
---|
303 | mangleName << Encoding::qualifiedTypeEnd; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | void Mangler::postvisit( TypeDecl * decl ) { |
---|
308 | // TODO: is there any case where mangling a TypeDecl makes sense? If so, this code needs to be |
---|
309 | // fixed to ensure that two TypeDecls mangle to the same name when they are the same type and vice versa. |
---|
310 | // Note: The current scheme may already work correctly for this case, I have not thought about this deeply |
---|
311 | // and the case has not yet come up in practice. Alternatively, if not then this code can be removed |
---|
312 | // aside from the assert false. |
---|
313 | assertf(false, "Mangler should not visit typedecl: %s", toCString(decl)); |
---|
314 | assertf( decl->get_kind() < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->get_kind() ); |
---|
315 | mangleName << Encoding::typeVariables[ decl->get_kind() ] << ( decl->name.length() ) << decl->name; |
---|
316 | } |
---|
317 | |
---|
318 | __attribute__((unused)) void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
319 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
320 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
321 | } // for |
---|
322 | } |
---|
323 | |
---|
324 | void Mangler::printQualifiers( Type * type ) { |
---|
325 | // skip if not including qualifiers |
---|
326 | if ( typeMode ) return; |
---|
327 | if ( ! type->get_forall().empty() ) { |
---|
328 | std::list< std::string > assertionNames; |
---|
329 | int dcount = 0, fcount = 0, vcount = 0, acount = 0; |
---|
330 | mangleName << Encoding::forall; |
---|
331 | for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { |
---|
332 | switch ( (*i)->get_kind() ) { |
---|
333 | case TypeDecl::Dtype: |
---|
334 | dcount++; |
---|
335 | break; |
---|
336 | case TypeDecl::Ftype: |
---|
337 | fcount++; |
---|
338 | break; |
---|
339 | case TypeDecl::Ttype: |
---|
340 | vcount++; |
---|
341 | break; |
---|
342 | default: |
---|
343 | assert( false ); |
---|
344 | } // switch |
---|
345 | varNums[ (*i)->name ] = std::make_pair( nextVarNum, (int)(*i)->get_kind() ); |
---|
346 | for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { |
---|
347 | PassVisitor<Mangler> sub_mangler( |
---|
348 | mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums ); |
---|
349 | (*assert)->accept( sub_mangler ); |
---|
350 | assertionNames.push_back( sub_mangler.pass.get_mangleName() ); |
---|
351 | acount++; |
---|
352 | } // for |
---|
353 | } // for |
---|
354 | mangleName << dcount << "_" << fcount << "_" << vcount << "_" << acount << "_"; |
---|
355 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
356 | mangleName << "_"; |
---|
357 | } // if |
---|
358 | if ( ! inFunctionType ) { |
---|
359 | // these qualifiers do not distinguish the outermost type of a function parameter |
---|
360 | if ( type->get_const() ) { |
---|
361 | mangleName << Encoding::qualifiers.at(Type::Const); |
---|
362 | } // if |
---|
363 | if ( type->get_volatile() ) { |
---|
364 | mangleName << Encoding::qualifiers.at(Type::Volatile); |
---|
365 | } // if |
---|
366 | // Removed due to restrict not affecting function compatibility in GCC |
---|
367 | // if ( type->get_isRestrict() ) { |
---|
368 | // mangleName << "E"; |
---|
369 | // } // if |
---|
370 | if ( type->get_atomic() ) { |
---|
371 | mangleName << Encoding::qualifiers.at(Type::Atomic); |
---|
372 | } // if |
---|
373 | } |
---|
374 | if ( type->get_mutex() ) { |
---|
375 | mangleName << Encoding::qualifiers.at(Type::Mutex); |
---|
376 | } // if |
---|
377 | if ( type->get_lvalue() ) { |
---|
378 | // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues |
---|
379 | mangleName << Encoding::qualifiers.at(Type::Lvalue); |
---|
380 | } |
---|
381 | |
---|
382 | if ( inFunctionType ) { |
---|
383 | // turn off inFunctionType so that types can be differentiated for nested qualifiers |
---|
384 | GuardValue( inFunctionType ); |
---|
385 | inFunctionType = false; |
---|
386 | } |
---|
387 | } |
---|
388 | } // namespace |
---|
389 | } // namespace Mangler |
---|
390 | } // namespace SymTab |
---|
391 | |
---|
392 | // Local Variables: // |
---|
393 | // tab-width: 4 // |
---|
394 | // mode: c++ // |
---|
395 | // compile-command: "make install" // |
---|
396 | // End: // |
---|