source: src/SymTab/Mangler.cc @ a02842f

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

Remove reference type mangling to prevent reference and non-reference overloading

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