source: src/SymTab/Mangler.cc @ a935892

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a935892 was d76c588, checked in by Aaron Moss <a3moss@…>, 5 years ago

Stubs for new resolver, implementation of new indexer, type environment

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