source: src/SymTab/Mangler.cc@ 3eab0ef6

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 3eab0ef6 was c3acf0aa, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

More header cleaning

  • Property mode set to 100644
File size: 9.8 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// Mangler.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 21:40:29 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jun 28 15:31:00 2017
13// Update Count : 21
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/SemanticError.h" // for SemanticError
26#include "Common/utility.h" // for toString
27#include "Parser/LinkageSpec.h" // for Spec, isOverridable, AutoGen, Int...
28#include "SynTree/Declaration.h" // for TypeDecl, DeclarationWithType
29#include "SynTree/Expression.h" // for TypeExpr, Expression, operator<<
30#include "SynTree/Type.h" // for Type, ReferenceToType, Type::Fora...
31
32namespace SymTab {
33 std::string Mangler::mangleType( Type *ty ) {
34 Mangler mangler( false, true );
35 maybeAccept( ty, mangler );
36 return mangler.get_mangleName();
37 }
38
39 Mangler::Mangler( bool mangleOverridable, bool typeMode )
40 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ) {}
41
42 Mangler::Mangler( const Mangler &rhs ) : mangleName() {
43 varNums = rhs.varNums;
44 nextVarNum = rhs.nextVarNum;
45 isTopLevel = rhs.isTopLevel;
46 mangleOverridable = rhs.mangleOverridable;
47 typeMode = rhs.typeMode;
48 }
49
50 void Mangler::mangleDecl( DeclarationWithType *declaration ) {
51 bool wasTopLevel = isTopLevel;
52 if ( isTopLevel ) {
53 varNums.clear();
54 nextVarNum = 0;
55 isTopLevel = false;
56 } // if
57 mangleName << "__";
58 CodeGen::OperatorInfo opInfo;
59 if ( operatorLookup( declaration->get_name(), opInfo ) ) {
60 mangleName << opInfo.outputName;
61 } else {
62 mangleName << declaration->get_name();
63 } // if
64 mangleName << "__";
65 maybeAccept( declaration->get_type(), *this );
66 if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) {
67 // want to be able to override autogenerated and intrinsic routines,
68 // so they need a different name mangling
69 if ( declaration->get_linkage() == LinkageSpec::AutoGen ) {
70 mangleName << "autogen__";
71 } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) {
72 mangleName << "intrinsic__";
73 } else {
74 // if we add another kind of overridable function, this has to change
75 assert( false && "unknown overrideable linkage" );
76 } // if
77 }
78 isTopLevel = wasTopLevel;
79 }
80
81 void Mangler::visit( ObjectDecl *declaration ) {
82 mangleDecl( declaration );
83 }
84
85 void Mangler::visit( FunctionDecl *declaration ) {
86 mangleDecl( declaration );
87 }
88
89 void Mangler::visit( VoidType *voidType ) {
90 printQualifiers( voidType );
91 mangleName << "v";
92 }
93
94 void Mangler::visit( BasicType *basicType ) {
95 static const char *btLetter[] = {
96 "b", // Bool
97 "c", // Char
98 "Sc", // SignedChar
99 "Uc", // UnsignedChar
100 "s", // ShortSignedInt
101 "Us", // ShortUnsignedInt
102 "i", // SignedInt
103 "Ui", // UnsignedInt
104 "l", // LongSignedInt
105 "Ul", // LongUnsignedInt
106 "q", // LongLongSignedInt
107 "Uq", // LongLongUnsignedInt
108 "f", // Float
109 "d", // Double
110 "r", // LongDouble
111 "Xf", // FloatComplex
112 "Xd", // DoubleComplex
113 "Xr", // LongDoubleComplex
114 "If", // FloatImaginary
115 "Id", // DoubleImaginary
116 "Ir", // LongDoubleImaginary
117 };
118
119 printQualifiers( basicType );
120 mangleName << btLetter[ basicType->get_kind() ];
121 }
122
123 void Mangler::visit( PointerType *pointerType ) {
124 printQualifiers( pointerType );
125 mangleName << "P";
126 maybeAccept( pointerType->get_base(), *this );
127 }
128
129 void Mangler::visit( ArrayType *arrayType ) {
130 // TODO: encode dimension
131 printQualifiers( arrayType );
132 mangleName << "A0";
133 maybeAccept( arrayType->get_base(), *this );
134 }
135
136 namespace {
137 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) {
138 std::list< Type* > ret;
139 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ),
140 std::mem_fun( &DeclarationWithType::get_type ) );
141 return ret;
142 }
143 }
144
145 void Mangler::visit( FunctionType *functionType ) {
146 printQualifiers( functionType );
147 mangleName << "F";
148 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() );
149 acceptAll( returnTypes, *this );
150 mangleName << "_";
151 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() );
152 acceptAll( paramTypes, *this );
153 mangleName << "_";
154 }
155
156 void Mangler::mangleRef( ReferenceToType *refType, std::string prefix ) {
157 printQualifiers( refType );
158
159 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
160 }
161
162 void Mangler::mangleGenericRef( ReferenceToType *refType, std::string prefix ) {
163 printQualifiers( refType );
164
165 std::ostringstream oldName( mangleName.str() );
166 mangleName.clear();
167
168 mangleName << prefix << refType->get_name();
169
170 std::list< Expression* >& params = refType->get_parameters();
171 if ( ! params.empty() ) {
172 mangleName << "_";
173 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) {
174 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
175 assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str());
176 maybeAccept( paramType->get_type(), *this );
177 }
178 mangleName << "_";
179 }
180
181 oldName << mangleName.str().length() << mangleName.str();
182 mangleName.str( oldName.str() );
183 }
184
185 void Mangler::visit( StructInstType *aggregateUseType ) {
186 if ( typeMode ) mangleGenericRef( aggregateUseType, "s" );
187 else mangleRef( aggregateUseType, "s" );
188 }
189
190 void Mangler::visit( UnionInstType *aggregateUseType ) {
191 if ( typeMode ) mangleGenericRef( aggregateUseType, "u" );
192 else mangleRef( aggregateUseType, "u" );
193 }
194
195 void Mangler::visit( EnumInstType *aggregateUseType ) {
196 mangleRef( aggregateUseType, "e" );
197 }
198
199 void Mangler::visit( TypeInstType *typeInst ) {
200 VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
201 if ( varNum == varNums.end() ) {
202 mangleRef( typeInst, "t" );
203 } else {
204 printQualifiers( typeInst );
205 std::ostringstream numStream;
206 numStream << varNum->second.first;
207 switch ( (TypeDecl::Kind )varNum->second.second ) {
208 case TypeDecl::Any:
209 mangleName << "t";
210 break;
211 case TypeDecl::Dtype:
212 mangleName << "d";
213 break;
214 case TypeDecl::Ftype:
215 mangleName << "f";
216 break;
217 case TypeDecl::Ttype:
218 mangleName << "tVARGS";
219 break;
220 default:
221 assert( false );
222 } // switch
223 mangleName << numStream.str();
224 } // if
225 }
226
227 void Mangler::visit( TupleType *tupleType ) {
228 printQualifiers( tupleType );
229 mangleName << "T";
230 acceptAll( tupleType->get_types(), *this );
231 mangleName << "_";
232 }
233
234 void Mangler::visit( VarArgsType *varArgsType ) {
235 printQualifiers( varArgsType );
236 mangleName << "VARGS";
237 }
238
239 void Mangler::visit( __attribute__((unused)) ZeroType *zeroType ) {
240 mangleName << "Z";
241 }
242
243 void Mangler::visit( __attribute__((unused)) OneType *oneType ) {
244 mangleName << "O";
245 }
246
247 void Mangler::visit( TypeDecl *decl ) {
248 static const char *typePrefix[] = { "BT", "BD", "BF" };
249 mangleName << typePrefix[ decl->get_kind() ] << ( decl->get_name().length() + 1 ) << decl->get_name();
250 }
251
252 void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) {
253 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) {
254 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl;
255 } // for
256 }
257
258 void Mangler::printQualifiers( Type *type ) {
259 // skip if not including qualifiers
260 if ( typeMode ) return;
261
262 if ( ! type->get_forall().empty() ) {
263 std::list< std::string > assertionNames;
264 int tcount = 0, dcount = 0, fcount = 0, vcount = 0;
265 mangleName << "A";
266 for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
267 switch ( (*i)->get_kind() ) {
268 case TypeDecl::Any:
269 tcount++;
270 break;
271 case TypeDecl::Dtype:
272 dcount++;
273 break;
274 case TypeDecl::Ftype:
275 fcount++;
276 break;
277 case TypeDecl::Ttype:
278 vcount++;
279 break;
280 default:
281 assert( false );
282 } // switch
283 varNums[ (*i )->get_name() ] = std::pair< int, int >( nextVarNum++, (int )(*i )->get_kind() );
284 for ( std::list< DeclarationWithType* >::iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
285 Mangler sub_mangler( mangleOverridable, typeMode );
286 sub_mangler.nextVarNum = nextVarNum;
287 sub_mangler.isTopLevel = false;
288 sub_mangler.varNums = varNums;
289 (*assert)->accept( sub_mangler );
290 assertionNames.push_back( sub_mangler.mangleName.str() );
291 } // for
292 } // for
293 mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_";
294 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
295 mangleName << "_";
296 } // if
297 if ( type->get_const() ) {
298 mangleName << "C";
299 } // if
300 if ( type->get_volatile() ) {
301 mangleName << "V";
302 } // if
303 // Removed due to restrict not affecting function compatibility in GCC
304// if ( type->get_isRestrict() ) {
305// mangleName << "R";
306// } // if
307 if ( type->get_lvalue() ) {
308 mangleName << "L";
309 } // if
310 if ( type->get_atomic() ) {
311 mangleName << "A";
312 } // if
313 }
314} // namespace SymTab
315
316// Local Variables: //
317// tab-width: 4 //
318// mode: c++ //
319// compile-command: "make install" //
320// End: //
Note: See TracBrowser for help on using the repository browser.