source: src/SymTab/Mangler.cc@ bcb14b5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since bcb14b5 was 0e761e40, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Change type variable name mangling to use combination of tyvar position/encoding prefix rather than type variable name

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