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 "SynTree/Declaration.h" // for TypeDecl, DeclarationWithType |
---|
30 | #include "SynTree/Expression.h" // for TypeExpr, Expression, operator<< |
---|
31 | #include "SynTree/Type.h" // for Type, ReferenceToType, Type::Fora... |
---|
32 | |
---|
33 | namespace SymTab { |
---|
34 | namespace Mangler { |
---|
35 | namespace { |
---|
36 | /// Mangles names to a unique C identifier |
---|
37 | struct Mangler : public WithShortCircuiting, public WithVisitorRef<Mangler>, public WithGuards { |
---|
38 | Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); |
---|
39 | Mangler( const Mangler & ) = delete; |
---|
40 | |
---|
41 | void previsit( BaseSyntaxNode * ) { visit_children = false; } |
---|
42 | |
---|
43 | void postvisit( ObjectDecl * declaration ); |
---|
44 | void postvisit( FunctionDecl * declaration ); |
---|
45 | void postvisit( TypeDecl * declaration ); |
---|
46 | |
---|
47 | void postvisit( VoidType * voidType ); |
---|
48 | void postvisit( BasicType * basicType ); |
---|
49 | void postvisit( PointerType * pointerType ); |
---|
50 | void postvisit( ArrayType * arrayType ); |
---|
51 | void postvisit( ReferenceType * refType ); |
---|
52 | void postvisit( FunctionType * functionType ); |
---|
53 | void postvisit( StructInstType * aggregateUseType ); |
---|
54 | void postvisit( UnionInstType * aggregateUseType ); |
---|
55 | void postvisit( EnumInstType * aggregateUseType ); |
---|
56 | void postvisit( TypeInstType * aggregateUseType ); |
---|
57 | void postvisit( TraitInstType * inst ); |
---|
58 | void postvisit( TupleType * tupleType ); |
---|
59 | void postvisit( VarArgsType * varArgsType ); |
---|
60 | void postvisit( ZeroType * zeroType ); |
---|
61 | void postvisit( OneType * oneType ); |
---|
62 | void postvisit( QualifiedType * qualType ); |
---|
63 | |
---|
64 | std::string get_mangleName() { return mangleName.str(); } |
---|
65 | private: |
---|
66 | std::ostringstream mangleName; ///< Mangled name being constructed |
---|
67 | typedef std::map< std::string, std::pair< int, int > > VarMapType; |
---|
68 | VarMapType varNums; ///< Map of type variables to indices |
---|
69 | int nextVarNum; ///< Next type variable index |
---|
70 | bool isTopLevel; ///< Is the Mangler at the top level |
---|
71 | bool mangleOverridable; ///< Specially mangle overridable built-in methods |
---|
72 | bool typeMode; ///< Produce a unique mangled name for a type |
---|
73 | bool mangleGenericParams; ///< Include generic parameters in name mangling if true |
---|
74 | bool inFunctionType = false; ///< Include type qualifiers if false. |
---|
75 | |
---|
76 | void mangleDecl( DeclarationWithType *declaration ); |
---|
77 | void mangleRef( ReferenceToType *refType, std::string prefix ); |
---|
78 | |
---|
79 | void printQualifiers( Type *type ); |
---|
80 | }; // Mangler |
---|
81 | } // namespace |
---|
82 | |
---|
83 | std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) { |
---|
84 | PassVisitor<Mangler> mangler( mangleOverridable, typeMode, mangleGenericParams ); |
---|
85 | maybeAccept( decl, mangler ); |
---|
86 | return mangler.pass.get_mangleName(); |
---|
87 | } |
---|
88 | |
---|
89 | std::string mangleType( Type * ty ) { |
---|
90 | PassVisitor<Mangler> mangler( false, true, true ); |
---|
91 | maybeAccept( ty, mangler ); |
---|
92 | return mangler.pass.get_mangleName(); |
---|
93 | } |
---|
94 | |
---|
95 | std::string mangleConcrete( Type * ty ) { |
---|
96 | PassVisitor<Mangler> mangler( false, false, false ); |
---|
97 | maybeAccept( ty, mangler ); |
---|
98 | return mangler.pass.get_mangleName(); |
---|
99 | } |
---|
100 | |
---|
101 | namespace { |
---|
102 | Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) |
---|
103 | : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ), mangleGenericParams( mangleGenericParams ) {} |
---|
104 | |
---|
105 | void Mangler::mangleDecl( DeclarationWithType * declaration ) { |
---|
106 | bool wasTopLevel = isTopLevel; |
---|
107 | if ( isTopLevel ) { |
---|
108 | varNums.clear(); |
---|
109 | nextVarNum = 0; |
---|
110 | isTopLevel = false; |
---|
111 | } // if |
---|
112 | mangleName << "__"; |
---|
113 | CodeGen::OperatorInfo opInfo; |
---|
114 | if ( operatorLookup( declaration->get_name(), opInfo ) ) { |
---|
115 | mangleName << opInfo.outputName; |
---|
116 | } else { |
---|
117 | mangleName << declaration->get_name(); |
---|
118 | } // if |
---|
119 | mangleName << "__"; |
---|
120 | maybeAccept( declaration->get_type(), *visitor ); |
---|
121 | if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { |
---|
122 | // want to be able to override autogenerated and intrinsic routines, |
---|
123 | // so they need a different name mangling |
---|
124 | if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { |
---|
125 | mangleName << "autogen__"; |
---|
126 | } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
127 | mangleName << "intrinsic__"; |
---|
128 | } else { |
---|
129 | // if we add another kind of overridable function, this has to change |
---|
130 | assert( false && "unknown overrideable linkage" ); |
---|
131 | } // if |
---|
132 | } |
---|
133 | isTopLevel = wasTopLevel; |
---|
134 | } |
---|
135 | |
---|
136 | void Mangler::postvisit( ObjectDecl * declaration ) { |
---|
137 | mangleDecl( declaration ); |
---|
138 | } |
---|
139 | |
---|
140 | void Mangler::postvisit( FunctionDecl * declaration ) { |
---|
141 | mangleDecl( declaration ); |
---|
142 | } |
---|
143 | |
---|
144 | void Mangler::postvisit( VoidType * voidType ) { |
---|
145 | printQualifiers( voidType ); |
---|
146 | mangleName << "v"; |
---|
147 | } |
---|
148 | |
---|
149 | void Mangler::postvisit( BasicType * basicType ) { |
---|
150 | static const char *btLetter[] = { |
---|
151 | "b", // Bool |
---|
152 | "c", // Char |
---|
153 | "Sc", // SignedChar |
---|
154 | "Uc", // UnsignedChar |
---|
155 | "s", // ShortSignedInt |
---|
156 | "Us", // ShortUnsignedInt |
---|
157 | "i", // SignedInt |
---|
158 | "Ui", // UnsignedInt |
---|
159 | "l", // LongSignedInt |
---|
160 | "Ul", // LongUnsignedInt |
---|
161 | "q", // LongLongSignedInt |
---|
162 | "Uq", // LongLongUnsignedInt |
---|
163 | "f", // Float |
---|
164 | "d", // Double |
---|
165 | "r", // LongDouble |
---|
166 | "Xf", // FloatComplex |
---|
167 | "Xd", // DoubleComplex |
---|
168 | "Xr", // LongDoubleComplex |
---|
169 | "If", // FloatImaginary |
---|
170 | "Id", // DoubleImaginary |
---|
171 | "Ir", // LongDoubleImaginary |
---|
172 | "w", // SignedInt128 |
---|
173 | "Uw", // UnsignedInt128 |
---|
174 | "x", // Float80 |
---|
175 | "y", // Float128 |
---|
176 | }; |
---|
177 | static_assert( |
---|
178 | sizeof(btLetter)/sizeof(btLetter[0]) == BasicType::NUMBER_OF_BASIC_TYPES, |
---|
179 | "Each basic type kind should have a corresponding mangler letter" |
---|
180 | ); |
---|
181 | |
---|
182 | printQualifiers( basicType ); |
---|
183 | assert( basicType->get_kind() < sizeof(btLetter)/sizeof(btLetter[0]) ); |
---|
184 | mangleName << btLetter[ basicType->get_kind() ]; |
---|
185 | } |
---|
186 | |
---|
187 | void Mangler::postvisit( PointerType * pointerType ) { |
---|
188 | printQualifiers( pointerType ); |
---|
189 | // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers |
---|
190 | if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName << "P"; |
---|
191 | maybeAccept( pointerType->base, *visitor ); |
---|
192 | } |
---|
193 | |
---|
194 | void Mangler::postvisit( ArrayType * arrayType ) { |
---|
195 | // TODO: encode dimension |
---|
196 | printQualifiers( arrayType ); |
---|
197 | mangleName << "A0"; |
---|
198 | maybeAccept( arrayType->base, *visitor ); |
---|
199 | } |
---|
200 | |
---|
201 | void Mangler::postvisit( ReferenceType * refType ) { |
---|
202 | // don't print prefix (e.g. 'R') for reference types so that references and non-references do not overload. |
---|
203 | // Further, do not print the qualifiers for a reference type (but do run printQualifers because of TypeDecls, etc.), |
---|
204 | // by pretending every reference type is a function parameter. |
---|
205 | GuardValue( inFunctionType ); |
---|
206 | inFunctionType = true; |
---|
207 | printQualifiers( refType ); |
---|
208 | maybeAccept( refType->base, *visitor ); |
---|
209 | } |
---|
210 | |
---|
211 | namespace { |
---|
212 | inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { |
---|
213 | std::list< Type* > ret; |
---|
214 | std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), |
---|
215 | std::mem_fun( &DeclarationWithType::get_type ) ); |
---|
216 | return ret; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | void Mangler::postvisit( FunctionType * functionType ) { |
---|
221 | printQualifiers( functionType ); |
---|
222 | mangleName << "F"; |
---|
223 | // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters, |
---|
224 | // since qualifiers on outermost parameter type do not differentiate function types, e.g., |
---|
225 | // void (*)(const int) and void (*)(int) are the same type, but void (*)(const int *) and void (*)(int *) are different |
---|
226 | GuardValue( inFunctionType ); |
---|
227 | inFunctionType = true; |
---|
228 | std::list< Type* > returnTypes = getTypes( functionType->returnVals ); |
---|
229 | acceptAll( returnTypes, *visitor ); |
---|
230 | mangleName << "_"; |
---|
231 | std::list< Type* > paramTypes = getTypes( functionType->parameters ); |
---|
232 | acceptAll( paramTypes, *visitor ); |
---|
233 | mangleName << "_"; |
---|
234 | } |
---|
235 | |
---|
236 | void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { |
---|
237 | printQualifiers( refType ); |
---|
238 | |
---|
239 | mangleName << ( refType->name.length() + prefix.length() ) << prefix << refType->name; |
---|
240 | |
---|
241 | if ( mangleGenericParams ) { |
---|
242 | std::list< Expression* >& params = refType->parameters; |
---|
243 | if ( ! params.empty() ) { |
---|
244 | mangleName << "_"; |
---|
245 | for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { |
---|
246 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
247 | assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString(*param)); |
---|
248 | maybeAccept( paramType->type, *visitor ); |
---|
249 | } |
---|
250 | mangleName << "_"; |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | void Mangler::postvisit( StructInstType * aggregateUseType ) { |
---|
256 | mangleRef( aggregateUseType, "s" ); |
---|
257 | } |
---|
258 | |
---|
259 | void Mangler::postvisit( UnionInstType * aggregateUseType ) { |
---|
260 | mangleRef( aggregateUseType, "u" ); |
---|
261 | } |
---|
262 | |
---|
263 | void Mangler::postvisit( EnumInstType * aggregateUseType ) { |
---|
264 | mangleRef( aggregateUseType, "e" ); |
---|
265 | } |
---|
266 | |
---|
267 | void Mangler::postvisit( TypeInstType * typeInst ) { |
---|
268 | VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); |
---|
269 | if ( varNum == varNums.end() ) { |
---|
270 | mangleRef( typeInst, "t" ); |
---|
271 | } else { |
---|
272 | printQualifiers( typeInst ); |
---|
273 | std::ostringstream numStream; |
---|
274 | numStream << varNum->second.first; |
---|
275 | switch ( (TypeDecl::Kind )varNum->second.second ) { |
---|
276 | case TypeDecl::Dtype: |
---|
277 | mangleName << "d"; |
---|
278 | break; |
---|
279 | case TypeDecl::Ftype: |
---|
280 | mangleName << "f"; |
---|
281 | break; |
---|
282 | case TypeDecl::Ttype: |
---|
283 | mangleName << "tVARGS"; |
---|
284 | break; |
---|
285 | default: |
---|
286 | assert( false ); |
---|
287 | } // switch |
---|
288 | mangleName << numStream.str(); |
---|
289 | } // if |
---|
290 | } |
---|
291 | |
---|
292 | void Mangler::postvisit( TraitInstType * inst ) { |
---|
293 | printQualifiers( inst ); |
---|
294 | mangleName << "_Y" << inst->name << "_"; |
---|
295 | } |
---|
296 | |
---|
297 | void Mangler::postvisit( TupleType * tupleType ) { |
---|
298 | printQualifiers( tupleType ); |
---|
299 | mangleName << "T"; |
---|
300 | acceptAll( tupleType->types, *visitor ); |
---|
301 | mangleName << "_"; |
---|
302 | } |
---|
303 | |
---|
304 | void Mangler::postvisit( VarArgsType * varArgsType ) { |
---|
305 | printQualifiers( varArgsType ); |
---|
306 | mangleName << "VARGS"; |
---|
307 | } |
---|
308 | |
---|
309 | void Mangler::postvisit( ZeroType * ) { |
---|
310 | mangleName << "Z"; |
---|
311 | } |
---|
312 | |
---|
313 | void Mangler::postvisit( OneType * ) { |
---|
314 | mangleName << "O"; |
---|
315 | } |
---|
316 | |
---|
317 | void Mangler::postvisit( QualifiedType * qualType ) { |
---|
318 | maybeAccept( qualType->parent, *visitor ); |
---|
319 | mangleName << "__"; |
---|
320 | maybeAccept( qualType->child, *visitor ); |
---|
321 | } |
---|
322 | |
---|
323 | void Mangler::postvisit( TypeDecl * decl ) { |
---|
324 | static const char *typePrefix[] = { "BT", "BD", "BF" }; |
---|
325 | mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name; |
---|
326 | } |
---|
327 | |
---|
328 | __attribute__((unused)) void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { |
---|
329 | for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { |
---|
330 | os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; |
---|
331 | } // for |
---|
332 | } |
---|
333 | |
---|
334 | void Mangler::printQualifiers( Type * type ) { |
---|
335 | // skip if not including qualifiers |
---|
336 | if ( typeMode ) return; |
---|
337 | if ( ! type->get_forall().empty() ) { |
---|
338 | std::list< std::string > assertionNames; |
---|
339 | int tcount = 0, dcount = 0, fcount = 0, vcount = 0; |
---|
340 | mangleName << "A"; |
---|
341 | for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { |
---|
342 | switch ( (*i)->get_kind() ) { |
---|
343 | case TypeDecl::Dtype: |
---|
344 | dcount++; |
---|
345 | break; |
---|
346 | case TypeDecl::Ftype: |
---|
347 | fcount++; |
---|
348 | break; |
---|
349 | case TypeDecl::Ttype: |
---|
350 | vcount++; |
---|
351 | break; |
---|
352 | default: |
---|
353 | assert( false ); |
---|
354 | } // switch |
---|
355 | varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); |
---|
356 | for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { |
---|
357 | PassVisitor<Mangler> sub_mangler( mangleOverridable, typeMode, mangleGenericParams ); |
---|
358 | sub_mangler.pass.nextVarNum = nextVarNum; |
---|
359 | sub_mangler.pass.isTopLevel = false; |
---|
360 | sub_mangler.pass.varNums = varNums; |
---|
361 | (*assert)->accept( sub_mangler ); |
---|
362 | assertionNames.push_back( sub_mangler.pass.mangleName.str() ); |
---|
363 | } // for |
---|
364 | } // for |
---|
365 | mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; |
---|
366 | std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); |
---|
367 | mangleName << "_"; |
---|
368 | } // if |
---|
369 | if ( ! inFunctionType ) { |
---|
370 | // these qualifiers do not distinguish the outermost type of a function parameter |
---|
371 | if ( type->get_const() ) { |
---|
372 | mangleName << "C"; |
---|
373 | } // if |
---|
374 | if ( type->get_volatile() ) { |
---|
375 | mangleName << "V"; |
---|
376 | } // if |
---|
377 | // Removed due to restrict not affecting function compatibility in GCC |
---|
378 | // if ( type->get_isRestrict() ) { |
---|
379 | // mangleName << "E"; |
---|
380 | // } // if |
---|
381 | if ( type->get_atomic() ) { |
---|
382 | mangleName << "A"; |
---|
383 | } // if |
---|
384 | } |
---|
385 | if ( type->get_mutex() ) { |
---|
386 | mangleName << "M"; |
---|
387 | } // if |
---|
388 | if ( type->get_lvalue() ) { |
---|
389 | // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues |
---|
390 | mangleName << "L"; |
---|
391 | } |
---|
392 | |
---|
393 | if ( inFunctionType ) { |
---|
394 | // turn off inFunctionType so that types can be differentiated for nested qualifiers |
---|
395 | GuardValue( inFunctionType ); |
---|
396 | inFunctionType = false; |
---|
397 | } |
---|
398 | } |
---|
399 | } // namespace |
---|
400 | } // namespace Mangler |
---|
401 | } // namespace SymTab |
---|
402 | |
---|
403 | // Local Variables: // |
---|
404 | // tab-width: 4 // |
---|
405 | // mode: c++ // |
---|
406 | // compile-command: "make install" // |
---|
407 | // End: // |
---|