00001 //===-- codegen/ExportMap.h ----------------------------------- -*- C++ -*-===// 00002 // 00003 // This file is distributed under the MIT license. See LICENSE.txt for details. 00004 // 00005 // Copyright (C) 2009, Stephen Wilson 00006 // 00007 //===----------------------------------------------------------------------===// 00008 00009 #ifndef COMMA_CODEGEN_EXPORTMAP_HDR_GUARD 00010 #define COMMA_CODEGEN_EXPORTMAP_HDR_GUARD 00011 00012 #include "comma/ast/AstBase.h" 00013 00014 #include <map> 00015 #include <vector> 00016 00017 namespace comma { 00018 00019 class ExportMap { 00020 00021 private: 00022 typedef std::pair<const SubroutineDecl *, unsigned> ExportPair; 00023 00024 struct SignatureEntry { 00025 SignatureEntry() : sigoid(0) { } 00026 const Sigoid *sigoid; 00027 std::vector<unsigned> offsets; 00028 std::vector<ExportPair> exports; 00029 unsigned totalExports; 00030 }; 00031 00032 public: 00033 typedef const SignatureEntry &SignatureKey; 00034 00035 const SignatureKey addSignature(const Sigoid *sig) { 00036 SignatureEntry &entry = signatureTable[sig]; 00037 00038 // If the returned value is default constructed, there is no entry yet 00039 // in our table. Initialize. 00040 if (entry.sigoid == 0) 00041 initializeUsingSignature(entry, sig); 00042 00043 return entry; 00044 } 00045 00046 unsigned getIndex(const SubroutineDecl *decl); 00047 00048 const SignatureKey lookupSignature(const Sigoid *sig) { 00049 return addSignature(sig); 00050 } 00051 00052 static const Sigoid *getSigoid(const SignatureKey entry) { 00053 return entry.sigoid; 00054 } 00055 00056 static unsigned getSignatureOffset(const SubroutineDecl *decl); 00057 00058 unsigned getLocalIndex(const SubroutineDecl *decl); 00059 00060 typedef std::vector<unsigned>::const_iterator offset_iterator; 00061 00062 static offset_iterator begin_offsets(const SignatureKey entry) { 00063 return entry.offsets.begin(); 00064 } 00065 00066 static offset_iterator end_offsets(const SignatureKey entry) { 00067 return entry.offsets.end(); 00068 } 00069 00070 static unsigned numberOfExports(SignatureKey key) { 00071 return key.totalExports; 00072 } 00073 00075 void dump(const SignatureKey entry); 00076 00077 private: 00078 typedef std::map<const Sigoid *, SignatureEntry> SignatureMap; 00079 SignatureMap signatureTable; 00080 00081 void initializeUsingSignature(SignatureEntry &entry, const Sigoid *sig); 00082 00083 static const Sigoid *resolveSignatureContext(const SubroutineDecl *decl); 00084 00085 static const Sigoid *resolveSignatureOrigin(const SubroutineDecl *decl); 00086 }; 00087 00088 00089 } // end comma namespace. 00090 00091 #endif