source: src/SymTab/Mangler.cc @ ad51cc2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ad51cc2 was e35f30a, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Fix generic type name mangling, add mangleGenericParams mode to name mangler [fixes #41]

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