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