source: src/SymTab/Mangler.cc @ 779a4a3

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 779a4a3 was f465f0e, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Mangle trait instances to prevent indexer errors before traits are expanded

  • Property mode set to 100644
File size: 13.9 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( 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                                };
174
175                                printQualifiers( basicType );
176                                mangleName << btLetter[ basicType->get_kind() ];
177                        }
178
179                        void Mangler::postvisit( PointerType * pointerType ) {
180                                printQualifiers( pointerType );
181                                mangleName << "P";
182                                maybeAccept( pointerType->base, *visitor );
183                        }
184
185                        void Mangler::postvisit( ArrayType * arrayType ) {
186                                // TODO: encode dimension
187                                printQualifiers( arrayType );
188                                mangleName << "A0";
189                                maybeAccept( arrayType->base, *visitor );
190                        }
191
192                        void Mangler::postvisit( ReferenceType * refType ) {
193                                // don't print prefix (e.g. 'R') for reference types so that references and non-references do not overload.
194                                // Further, do not print the qualifiers for a reference type (but do run printQualifers because of TypeDecls, etc.),
195                                // by pretending every reference type is a function parameter.
196                                GuardValue( inFunctionType );
197                                inFunctionType = true;
198                                printQualifiers( refType );
199                                maybeAccept( refType->base, *visitor );
200                        }
201
202                        namespace {
203                                inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) {
204                                        std::list< Type* > ret;
205                                        std::transform( decls.begin(), decls.end(), std::back_inserter( ret ),
206                                                                        std::mem_fun( &DeclarationWithType::get_type ) );
207                                        return ret;
208                                }
209                        }
210
211                        void Mangler::postvisit( FunctionType * functionType ) {
212                                printQualifiers( functionType );
213                                mangleName << "F";
214                                // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters,
215                                // since qualifiers on outermost parameter type do not differentiate function types, e.g.,
216                                // void (*)(const int) and void (*)(int) are the same type, but void (*)(const int *) and void (*)(int *) are different
217                                GuardValue( inFunctionType );
218                                inFunctionType = true;
219                                std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() );
220                                acceptAll( returnTypes, *visitor );
221                                mangleName << "_";
222                                std::list< Type* > paramTypes = getTypes( functionType->get_parameters() );
223                                acceptAll( paramTypes, *visitor );
224                                mangleName << "_";
225                        }
226
227                        void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) {
228                                printQualifiers( refType );
229
230                                mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name();
231
232                                if ( mangleGenericParams ) {
233                                        std::list< Expression* >& params = refType->get_parameters();
234                                        if ( ! params.empty() ) {
235                                                mangleName << "_";
236                                                for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) {
237                                                        TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
238                                                        assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str());
239                                                        maybeAccept( paramType->get_type(), *visitor );
240                                                }
241                                                mangleName << "_";
242                                        }
243                                }
244                        }
245
246                        void Mangler::postvisit( StructInstType * aggregateUseType ) {
247                                mangleRef( aggregateUseType, "s" );
248                        }
249
250                        void Mangler::postvisit( UnionInstType * aggregateUseType ) {
251                                mangleRef( aggregateUseType, "u" );
252                        }
253
254                        void Mangler::postvisit( EnumInstType * aggregateUseType ) {
255                                mangleRef( aggregateUseType, "e" );
256                        }
257
258                        void Mangler::postvisit( TypeInstType * typeInst ) {
259                                VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
260                                if ( varNum == varNums.end() ) {
261                                        mangleRef( typeInst, "t" );
262                                } else {
263                                        printQualifiers( typeInst );
264                                        std::ostringstream numStream;
265                                        numStream << varNum->second.first;
266                                        switch ( (TypeDecl::Kind )varNum->second.second ) {
267                                          case TypeDecl::Dtype:
268                                                mangleName << "d";
269                                                break;
270                                          case TypeDecl::Ftype:
271                                                mangleName << "f";
272                                                break;
273                                                case TypeDecl::Ttype:
274                                                mangleName << "tVARGS";
275                                                break;
276                                                default:
277                                                assert( false );
278                                        } // switch
279                                        mangleName << numStream.str();
280                                } // if
281                        }
282
283                        void Mangler::postvisit( TraitInstType * inst ) {
284                                printQualifiers( inst );
285                                mangleName << "_Y" << inst->name << "_";
286                        }
287
288                        void Mangler::postvisit( TupleType * tupleType ) {
289                                printQualifiers( tupleType );
290                                mangleName << "T";
291                                acceptAll( tupleType->types, *visitor );
292                                mangleName << "_";
293                        }
294
295                        void Mangler::postvisit( VarArgsType * varArgsType ) {
296                                printQualifiers( varArgsType );
297                                mangleName << "VARGS";
298                        }
299
300                        void Mangler::postvisit( ZeroType * ) {
301                                mangleName << "Z";
302                        }
303
304                        void Mangler::postvisit( OneType * ) {
305                                mangleName << "O";
306                        }
307
308                        void Mangler::postvisit( TypeDecl * decl ) {
309                                static const char *typePrefix[] = { "BT", "BD", "BF" };
310                                mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name;
311                        }
312
313                        __attribute__((unused)) void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) {
314                                for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) {
315                                        os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl;
316                                } // for
317                        }
318
319                        void Mangler::printQualifiers( Type * type ) {
320                                // skip if not including qualifiers
321                                if ( typeMode ) return;
322                                if ( ! type->get_forall().empty() ) {
323                                        std::list< std::string > assertionNames;
324                                        int tcount = 0, dcount = 0, fcount = 0, vcount = 0;
325                                        mangleName << "A";
326                                        for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) {
327                                                switch ( (*i)->get_kind() ) {
328                                                  case TypeDecl::Dtype:
329                                                        dcount++;
330                                                        break;
331                                                  case TypeDecl::Ftype:
332                                                        fcount++;
333                                                        break;
334                                                  case TypeDecl::Ttype:
335                                                        vcount++;
336                                                        break;
337                                                  default:
338                                                        assert( false );
339                                                } // switch
340                                                varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() );
341                                                for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) {
342                                                        PassVisitor<Mangler> sub_mangler( mangleOverridable, typeMode, mangleGenericParams );
343                                                        sub_mangler.pass.nextVarNum = nextVarNum;
344                                                        sub_mangler.pass.isTopLevel = false;
345                                                        sub_mangler.pass.varNums = varNums;
346                                                        (*assert)->accept( sub_mangler );
347                                                        assertionNames.push_back( sub_mangler.pass.mangleName.str() );
348                                                } // for
349                                        } // for
350                                        mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_";
351                                        std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
352                                        mangleName << "_";
353                                } // if
354                                if ( ! inFunctionType ) {
355                                        // these qualifiers do not distinguish the outermost type of a function parameter
356                                        if ( type->get_const() ) {
357                                                mangleName << "C";
358                                        } // if
359                                        if ( type->get_volatile() ) {
360                                                mangleName << "V";
361                                        } // if
362                                        // Removed due to restrict not affecting function compatibility in GCC
363                                        // if ( type->get_isRestrict() ) {
364                                        //      mangleName << "E";
365                                        // } // if
366                                        if ( type->get_atomic() ) {
367                                                mangleName << "A";
368                                        } // if
369                                }
370                                if ( type->get_mutex() ) {
371                                        mangleName << "M";
372                                } // if
373                                if ( type->get_lvalue() ) {
374                                        // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues
375                                        mangleName << "L";
376                                }
377
378                                if ( inFunctionType ) {
379                                        // turn off inFunctionType so that types can be differentiated for nested qualifiers
380                                        GuardValue( inFunctionType );
381                                        inFunctionType = false;
382                                }
383                        }
384                }       // namespace
385        } // namespace Mangler
386} // namespace SymTab
387
388// Local Variables: //
389// tab-width: 4 //
390// mode: c++ //
391// compile-command: "make install" //
392// End: //
Note: See TracBrowser for help on using the repository browser.