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.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 21:44:03 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Wed Aug 19 15:48:46 2015 |
---|
13 | // Update Count : 14 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef MANGLER_H |
---|
17 | #define MANGLER_H |
---|
18 | |
---|
19 | #include <map> // for map, map<>::value_compare |
---|
20 | #include <sstream> // for ostringstream |
---|
21 | #include <string> // for string |
---|
22 | #include <utility> // for pair |
---|
23 | |
---|
24 | #include "SynTree/SynTree.h" // for Types |
---|
25 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
26 | |
---|
27 | namespace SymTab { |
---|
28 | /// Mangles names to a unique C identifier |
---|
29 | class Mangler : public Visitor { |
---|
30 | public: |
---|
31 | /// Mangle syntax tree object; primary interface to clients |
---|
32 | template< typename SynTreeClass > |
---|
33 | static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool typeMode = false ); |
---|
34 | /// Mangle a type name; secondary interface |
---|
35 | static std::string mangleType( Type* ty ); |
---|
36 | |
---|
37 | virtual void visit( ObjectDecl *declaration ); |
---|
38 | virtual void visit( FunctionDecl *declaration ); |
---|
39 | virtual void visit( TypeDecl *declaration ); |
---|
40 | |
---|
41 | virtual void visit( VoidType *voidType ); |
---|
42 | virtual void visit( BasicType *basicType ); |
---|
43 | virtual void visit( PointerType *pointerType ); |
---|
44 | virtual void visit( ArrayType *arrayType ); |
---|
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 ); |
---|
51 | virtual void visit( VarArgsType *varArgsType ); |
---|
52 | virtual void visit( ZeroType *zeroType ); |
---|
53 | virtual void visit( OneType *oneType ); |
---|
54 | |
---|
55 | std::string get_mangleName() { return mangleName.str(); } |
---|
56 | private: |
---|
57 | std::ostringstream mangleName; ///< Mangled name being constructed |
---|
58 | typedef std::map< std::string, std::pair< int, int > > VarMapType; |
---|
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 |
---|
63 | bool typeMode; ///< Produce a unique mangled name for a type |
---|
64 | |
---|
65 | Mangler( bool mangleOverridable, bool typeMode ); |
---|
66 | Mangler( const Mangler & ); |
---|
67 | |
---|
68 | void mangleDecl( DeclarationWithType *declaration ); |
---|
69 | void mangleRef( ReferenceToType *refType, std::string prefix ); |
---|
70 | void mangleGenericRef( ReferenceToType *refType, std::string prefix ); |
---|
71 | |
---|
72 | void printQualifiers( Type *type ); |
---|
73 | }; // Mangler |
---|
74 | |
---|
75 | template< typename SynTreeClass > |
---|
76 | std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool typeMode ) { |
---|
77 | Mangler mangler( mangleOverridable, typeMode ); |
---|
78 | maybeAccept( decl, mangler ); |
---|
79 | return mangler.get_mangleName(); |
---|
80 | } |
---|
81 | } // SymTab |
---|
82 | |
---|
83 | #endif // MANGLER_H |
---|
84 | |
---|
85 | // Local Variables: // |
---|
86 | // tab-width: 4 // |
---|
87 | // mode: c++ // |
---|
88 | // compile-command: "make install" // |
---|
89 | // End: // |
---|