[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 | // |
---|
[25bd9074] | 7 | // Mangler.h -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:44:03 2015 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sat Jul 22 09:45:30 2017 |
---|
| 13 | // Update Count : 15 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[30f9072] | 18 | #include <map> // for map, map<>::value_compare |
---|
| 19 | #include <sstream> // for ostringstream |
---|
| 20 | #include <string> // for string |
---|
| 21 | #include <utility> // for pair |
---|
| 22 | |
---|
| 23 | #include "SynTree/SynTree.h" // for Types |
---|
| 24 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
[51b7345] | 25 | |
---|
| 26 | namespace SymTab { |
---|
[78dd0da] | 27 | /// Mangles names to a unique C identifier |
---|
[a08ba92] | 28 | class Mangler : public Visitor { |
---|
| 29 | public: |
---|
[69911c11] | 30 | /// Mangle syntax tree object; primary interface to clients |
---|
[0dd3a2f] | 31 | template< typename SynTreeClass > |
---|
[0270824] | 32 | static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool typeMode = false ); |
---|
[69911c11] | 33 | /// Mangle a type name; secondary interface |
---|
| 34 | static std::string mangleType( Type* ty ); |
---|
[51b7345] | 35 | |
---|
[0dd3a2f] | 36 | virtual void visit( ObjectDecl *declaration ); |
---|
| 37 | virtual void visit( FunctionDecl *declaration ); |
---|
| 38 | virtual void visit( TypeDecl *declaration ); |
---|
| 39 | |
---|
| 40 | virtual void visit( VoidType *voidType ); |
---|
| 41 | virtual void visit( BasicType *basicType ); |
---|
| 42 | virtual void visit( PointerType *pointerType ); |
---|
| 43 | virtual void visit( ArrayType *arrayType ); |
---|
[25bd9074] | 44 | virtual void visit( ReferenceType *refType ); |
---|
[0dd3a2f] | 45 | virtual void visit( FunctionType *functionType ); |
---|
| 46 | virtual void visit( StructInstType *aggregateUseType ); |
---|
| 47 | virtual void visit( UnionInstType *aggregateUseType ); |
---|
| 48 | virtual void visit( EnumInstType *aggregateUseType ); |
---|
| 49 | virtual void visit( TypeInstType *aggregateUseType ); |
---|
| 50 | virtual void visit( TupleType *tupleType ); |
---|
[44b7088] | 51 | virtual void visit( VarArgsType *varArgsType ); |
---|
[89e6ffc] | 52 | virtual void visit( ZeroType *zeroType ); |
---|
| 53 | virtual void visit( OneType *oneType ); |
---|
[25bd9074] | 54 | |
---|
[5f2f2d7] | 55 | std::string get_mangleName() { return mangleName.str(); } |
---|
[a08ba92] | 56 | private: |
---|
[78dd0da] | 57 | std::ostringstream mangleName; ///< Mangled name being constructed |
---|
[0dd3a2f] | 58 | typedef std::map< std::string, std::pair< int, int > > VarMapType; |
---|
[78dd0da] | 59 | VarMapType varNums; ///< Map of type variables to indices |
---|
| 60 | int nextVarNum; ///< Next type variable index |
---|
| 61 | bool isTopLevel; ///< Is the Mangler at the top level |
---|
| 62 | bool mangleOverridable; ///< Specially mangle overridable built-in methods |
---|
[69911c11] | 63 | bool typeMode; ///< Produce a unique mangled name for a type |
---|
[25bd9074] | 64 | |
---|
[69911c11] | 65 | Mangler( bool mangleOverridable, bool typeMode ); |
---|
[0dd3a2f] | 66 | Mangler( const Mangler & ); |
---|
[25bd9074] | 67 | |
---|
[0dd3a2f] | 68 | void mangleDecl( DeclarationWithType *declaration ); |
---|
| 69 | void mangleRef( ReferenceToType *refType, std::string prefix ); |
---|
[8360977] | 70 | void mangleGenericRef( ReferenceToType *refType, std::string prefix ); |
---|
[25bd9074] | 71 | |
---|
[0dd3a2f] | 72 | void printQualifiers( Type *type ); |
---|
[a08ba92] | 73 | }; // Mangler |
---|
[ea3eb06] | 74 | |
---|
[a08ba92] | 75 | template< typename SynTreeClass > |
---|
[0270824] | 76 | std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool typeMode ) { |
---|
| 77 | Mangler mangler( mangleOverridable, typeMode ); |
---|
[0dd3a2f] | 78 | maybeAccept( decl, mangler ); |
---|
| 79 | return mangler.get_mangleName(); |
---|
[a08ba92] | 80 | } |
---|
[ea3eb06] | 81 | } // SymTab |
---|
| 82 | |
---|
[0dd3a2f] | 83 | // Local Variables: // |
---|
| 84 | // tab-width: 4 // |
---|
| 85 | // mode: c++ // |
---|
| 86 | // compile-command: "make install" // |
---|
| 87 | // End: // |
---|