LLVM API Documentation

Module.h
Go to the documentation of this file.
00001 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 /// @file
00011 /// Module.h This file contains the declarations for the Module class.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_MODULE_H
00016 #define LLVM_IR_MODULE_H
00017 
00018 #include "llvm/ADT/iterator_range.h"
00019 #include "llvm/IR/Comdat.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/Function.h"
00022 #include "llvm/IR/GlobalAlias.h"
00023 #include "llvm/IR/GlobalVariable.h"
00024 #include "llvm/IR/Metadata.h"
00025 #include "llvm/Support/CBindingWrapping.h"
00026 #include "llvm/Support/DataTypes.h"
00027 #include <system_error>
00028 
00029 namespace llvm {
00030 class FunctionType;
00031 class GVMaterializer;
00032 class LLVMContext;
00033 class RandomNumberGenerator;
00034 class StructType;
00035 template<typename T> struct DenseMapInfo;
00036 template<typename KeyT, typename ValueT, typename KeyInfoT> class DenseMap;
00037 
00038 template<> struct ilist_traits<Function>
00039   : public SymbolTableListTraits<Function, Module> {
00040 
00041   // createSentinel is used to get hold of the node that marks the end of the
00042   // list... (same trick used here as in ilist_traits<Instruction>)
00043   Function *createSentinel() const {
00044     return static_cast<Function*>(&Sentinel);
00045   }
00046   static void destroySentinel(Function*) {}
00047 
00048   Function *provideInitialHead() const { return createSentinel(); }
00049   Function *ensureHead(Function*) const { return createSentinel(); }
00050   static void noteHead(Function*, Function*) {}
00051 
00052 private:
00053   mutable ilist_node<Function> Sentinel;
00054 };
00055 
00056 template<> struct ilist_traits<GlobalVariable>
00057   : public SymbolTableListTraits<GlobalVariable, Module> {
00058   // createSentinel is used to create a node that marks the end of the list.
00059   GlobalVariable *createSentinel() const {
00060     return static_cast<GlobalVariable*>(&Sentinel);
00061   }
00062   static void destroySentinel(GlobalVariable*) {}
00063 
00064   GlobalVariable *provideInitialHead() const { return createSentinel(); }
00065   GlobalVariable *ensureHead(GlobalVariable*) const { return createSentinel(); }
00066   static void noteHead(GlobalVariable*, GlobalVariable*) {}
00067 private:
00068   mutable ilist_node<GlobalVariable> Sentinel;
00069 };
00070 
00071 template<> struct ilist_traits<GlobalAlias>
00072   : public SymbolTableListTraits<GlobalAlias, Module> {
00073   // createSentinel is used to create a node that marks the end of the list.
00074   GlobalAlias *createSentinel() const {
00075     return static_cast<GlobalAlias*>(&Sentinel);
00076   }
00077   static void destroySentinel(GlobalAlias*) {}
00078 
00079   GlobalAlias *provideInitialHead() const { return createSentinel(); }
00080   GlobalAlias *ensureHead(GlobalAlias*) const { return createSentinel(); }
00081   static void noteHead(GlobalAlias*, GlobalAlias*) {}
00082 private:
00083   mutable ilist_node<GlobalAlias> Sentinel;
00084 };
00085 
00086 template<> struct ilist_traits<NamedMDNode>
00087   : public ilist_default_traits<NamedMDNode> {
00088   // createSentinel is used to get hold of a node that marks the end of
00089   // the list...
00090   NamedMDNode *createSentinel() const {
00091     return static_cast<NamedMDNode*>(&Sentinel);
00092   }
00093   static void destroySentinel(NamedMDNode*) {}
00094 
00095   NamedMDNode *provideInitialHead() const { return createSentinel(); }
00096   NamedMDNode *ensureHead(NamedMDNode*) const { return createSentinel(); }
00097   static void noteHead(NamedMDNode*, NamedMDNode*) {}
00098   void addNodeToList(NamedMDNode *) {}
00099   void removeNodeFromList(NamedMDNode *) {}
00100 private:
00101   mutable ilist_node<NamedMDNode> Sentinel;
00102 };
00103 
00104 /// A Module instance is used to store all the information related to an
00105 /// LLVM module. Modules are the top level container of all other LLVM
00106 /// Intermediate Representation (IR) objects. Each module directly contains a
00107 /// list of globals variables, a list of functions, a list of libraries (or
00108 /// other modules) this module depends on, a symbol table, and various data
00109 /// about the target's characteristics.
00110 ///
00111 /// A module maintains a GlobalValRefMap object that is used to hold all
00112 /// constant references to global variables in the module.  When a global
00113 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
00114 /// @brief The main container class for the LLVM Intermediate Representation.
00115 class Module {
00116 /// @name Types And Enumerations
00117 /// @{
00118 public:
00119   /// The type for the list of global variables.
00120   typedef iplist<GlobalVariable> GlobalListType;
00121   /// The type for the list of functions.
00122   typedef iplist<Function> FunctionListType;
00123   /// The type for the list of aliases.
00124   typedef iplist<GlobalAlias> AliasListType;
00125   /// The type for the list of named metadata.
00126   typedef ilist<NamedMDNode> NamedMDListType;
00127   /// The type of the comdat "symbol" table.
00128   typedef StringMap<Comdat> ComdatSymTabType;
00129 
00130   /// The Global Variable iterator.
00131   typedef GlobalListType::iterator                      global_iterator;
00132   /// The Global Variable constant iterator.
00133   typedef GlobalListType::const_iterator          const_global_iterator;
00134 
00135   /// The Function iterators.
00136   typedef FunctionListType::iterator                           iterator;
00137   /// The Function constant iterator
00138   typedef FunctionListType::const_iterator               const_iterator;
00139 
00140   /// The Function reverse iterator.
00141   typedef FunctionListType::reverse_iterator             reverse_iterator;
00142   /// The Function constant reverse iterator.
00143   typedef FunctionListType::const_reverse_iterator const_reverse_iterator;
00144 
00145   /// The Global Alias iterators.
00146   typedef AliasListType::iterator                        alias_iterator;
00147   /// The Global Alias constant iterator
00148   typedef AliasListType::const_iterator            const_alias_iterator;
00149 
00150   /// The named metadata iterators.
00151   typedef NamedMDListType::iterator             named_metadata_iterator;
00152   /// The named metadata constant interators.
00153   typedef NamedMDListType::const_iterator const_named_metadata_iterator;
00154 
00155   /// This enumeration defines the supported behaviors of module flags.
00156   enum ModFlagBehavior {
00157     /// Emits an error if two values disagree, otherwise the resulting value is
00158     /// that of the operands.
00159     Error = 1,
00160 
00161     /// Emits a warning if two values disagree. The result value will be the
00162     /// operand for the flag from the first module being linked.
00163     Warning = 2,
00164 
00165     /// Adds a requirement that another module flag be present and have a
00166     /// specified value after linking is performed. The value must be a metadata
00167     /// pair, where the first element of the pair is the ID of the module flag
00168     /// to be restricted, and the second element of the pair is the value the
00169     /// module flag should be restricted to. This behavior can be used to
00170     /// restrict the allowable results (via triggering of an error) of linking
00171     /// IDs with the **Override** behavior.
00172     Require = 3,
00173 
00174     /// Uses the specified value, regardless of the behavior or value of the
00175     /// other module. If both modules specify **Override**, but the values
00176     /// differ, an error will be emitted.
00177     Override = 4,
00178 
00179     /// Appends the two values, which are required to be metadata nodes.
00180     Append = 5,
00181 
00182     /// Appends the two values, which are required to be metadata
00183     /// nodes. However, duplicate entries in the second list are dropped
00184     /// during the append operation.
00185     AppendUnique = 6
00186   };
00187 
00188   struct ModuleFlagEntry {
00189     ModFlagBehavior Behavior;
00190     MDString *Key;
00191     Value *Val;
00192     ModuleFlagEntry(ModFlagBehavior B, MDString *K, Value *V)
00193       : Behavior(B), Key(K), Val(V) {}
00194   };
00195 
00196 /// @}
00197 /// @name Member Variables
00198 /// @{
00199 private:
00200   LLVMContext &Context;           ///< The LLVMContext from which types and
00201                                   ///< constants are allocated.
00202   GlobalListType GlobalList;      ///< The Global Variables in the module
00203   FunctionListType FunctionList;  ///< The Functions in the module
00204   AliasListType AliasList;        ///< The Aliases in the module
00205   NamedMDListType NamedMDList;    ///< The named metadata in the module
00206   std::string GlobalScopeAsm;     ///< Inline Asm at global scope.
00207   ValueSymbolTable *ValSymTab;    ///< Symbol table for values
00208   ComdatSymTabType ComdatSymTab;  ///< Symbol table for COMDATs
00209   std::unique_ptr<GVMaterializer>
00210   Materializer;                   ///< Used to materialize GlobalValues
00211   std::string ModuleID;           ///< Human readable identifier for the module
00212   std::string TargetTriple;       ///< Platform target triple Module compiled on
00213   void *NamedMDSymTab;            ///< NamedMDNode names.
00214   // Allow lazy initialization in const method.
00215   mutable RandomNumberGenerator *RNG; ///< The random number generator for this module.
00216 
00217   // We need to keep the string because the C API expects us to own the string
00218   // representation.
00219   // Since we have it, we also use an empty string to represent a module without
00220   // a DataLayout. If it has a DataLayout, these variables are in sync and the
00221   // string is just a cache of getDataLayout()->getStringRepresentation().
00222   std::string DataLayoutStr;
00223   DataLayout DL;
00224 
00225   friend class Constant;
00226 
00227 /// @}
00228 /// @name Constructors
00229 /// @{
00230 public:
00231   /// The Module constructor. Note that there is no default constructor. You
00232   /// must provide a name for the module upon construction.
00233   explicit Module(StringRef ModuleID, LLVMContext& C);
00234   /// The module destructor. This will dropAllReferences.
00235   ~Module();
00236 
00237 /// @}
00238 /// @name Module Level Accessors
00239 /// @{
00240 
00241   /// Get the module identifier which is, essentially, the name of the module.
00242   /// @returns the module identifier as a string
00243   const std::string &getModuleIdentifier() const { return ModuleID; }
00244 
00245   /// Get the data layout string for the module's target platform. This is
00246   /// equivalent to getDataLayout()->getStringRepresentation().
00247   const std::string &getDataLayoutStr() const { return DataLayoutStr; }
00248 
00249   /// Get the data layout for the module's target platform.
00250   const DataLayout *getDataLayout() const;
00251 
00252   /// Get the target triple which is a string describing the target host.
00253   /// @returns a string containing the target triple.
00254   const std::string &getTargetTriple() const { return TargetTriple; }
00255 
00256   /// Get the global data context.
00257   /// @returns LLVMContext - a container for LLVM's global information
00258   LLVMContext &getContext() const { return Context; }
00259 
00260   /// Get any module-scope inline assembly blocks.
00261   /// @returns a string containing the module-scope inline assembly blocks.
00262   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
00263 
00264   /// Get the RandomNumberGenerator for this module. The RNG can be
00265   /// seeded via -rng-seed=<uint64> and is salted with the ModuleID.
00266   /// The returned RNG should not be shared across threads.
00267   RandomNumberGenerator &getRNG() const;
00268 
00269 /// @}
00270 /// @name Module Level Mutators
00271 /// @{
00272 
00273   /// Set the module identifier.
00274   void setModuleIdentifier(StringRef ID) { ModuleID = ID; }
00275 
00276   /// Set the data layout
00277   void setDataLayout(StringRef Desc);
00278   void setDataLayout(const DataLayout *Other);
00279 
00280   /// Set the target triple.
00281   void setTargetTriple(StringRef T) { TargetTriple = T; }
00282 
00283   /// Set the module-scope inline assembly blocks.
00284   void setModuleInlineAsm(StringRef Asm) {
00285     GlobalScopeAsm = Asm;
00286     if (!GlobalScopeAsm.empty() &&
00287         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00288       GlobalScopeAsm += '\n';
00289   }
00290 
00291   /// Append to the module-scope inline assembly blocks, automatically inserting
00292   /// a separating newline if necessary.
00293   void appendModuleInlineAsm(StringRef Asm) {
00294     GlobalScopeAsm += Asm;
00295     if (!GlobalScopeAsm.empty() &&
00296         GlobalScopeAsm[GlobalScopeAsm.size()-1] != '\n')
00297       GlobalScopeAsm += '\n';
00298   }
00299 
00300 /// @}
00301 /// @name Generic Value Accessors
00302 /// @{
00303 
00304   /// Return the global value in the module with the specified name, of
00305   /// arbitrary type. This method returns null if a global with the specified
00306   /// name is not found.
00307   GlobalValue *getNamedValue(StringRef Name) const;
00308 
00309   /// Return a unique non-zero ID for the specified metadata kind. This ID is
00310   /// uniqued across modules in the current LLVMContext.
00311   unsigned getMDKindID(StringRef Name) const;
00312 
00313   /// Populate client supplied SmallVector with the name for custom metadata IDs
00314   /// registered in this LLVMContext.
00315   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
00316 
00317   /// Return the type with the specified name, or null if there is none by that
00318   /// name.
00319   StructType *getTypeByName(StringRef Name) const;
00320 
00321 /// @}
00322 /// @name Function Accessors
00323 /// @{
00324 
00325   /// Look up the specified function in the module symbol table. Four
00326   /// possibilities:
00327   ///   1. If it does not exist, add a prototype for the function and return it.
00328   ///   2. If it exists, and has a local linkage, the existing function is
00329   ///      renamed and a new one is inserted.
00330   ///   3. Otherwise, if the existing function has the correct prototype, return
00331   ///      the existing function.
00332   ///   4. Finally, the function exists but has the wrong prototype: return the
00333   ///      function with a constantexpr cast to the right prototype.
00334   Constant *getOrInsertFunction(StringRef Name, FunctionType *T,
00335                                 AttributeSet AttributeList);
00336 
00337   Constant *getOrInsertFunction(StringRef Name, FunctionType *T);
00338 
00339   /// Look up the specified function in the module symbol table. If it does not
00340   /// exist, add a prototype for the function and return it. This function
00341   /// guarantees to return a constant of pointer to the specified function type
00342   /// or a ConstantExpr BitCast of that type if the named function has a
00343   /// different type. This version of the method takes a null terminated list of
00344   /// function arguments, which makes it easier for clients to use.
00345   Constant *getOrInsertFunction(StringRef Name,
00346                                 AttributeSet AttributeList,
00347                                 Type *RetTy, ...)  END_WITH_NULL;
00348 
00349   /// Same as above, but without the attributes.
00350   Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
00351     END_WITH_NULL;
00352 
00353   /// Look up the specified function in the module symbol table. If it does not
00354   /// exist, return null.
00355   Function *getFunction(StringRef Name) const;
00356 
00357 /// @}
00358 /// @name Global Variable Accessors
00359 /// @{
00360 
00361   /// Look up the specified global variable in the module symbol table. If it
00362   /// does not exist, return null. If AllowInternal is set to true, this
00363   /// function will return types that have InternalLinkage. By default, these
00364   /// types are not returned.
00365   const GlobalVariable *getGlobalVariable(StringRef Name,
00366                                           bool AllowInternal = false) const {
00367     return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
00368   }
00369 
00370   GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
00371 
00372   /// Return the global variable in the module with the specified name, of
00373   /// arbitrary type. This method returns null if a global with the specified
00374   /// name is not found.
00375   GlobalVariable *getNamedGlobal(StringRef Name) {
00376     return getGlobalVariable(Name, true);
00377   }
00378   const GlobalVariable *getNamedGlobal(StringRef Name) const {
00379     return const_cast<Module *>(this)->getNamedGlobal(Name);
00380   }
00381 
00382   /// Look up the specified global in the module symbol table.
00383   ///   1. If it does not exist, add a declaration of the global and return it.
00384   ///   2. Else, the global exists but has the wrong type: return the function
00385   ///      with a constantexpr cast to the right type.
00386   ///   3. Finally, if the existing global is the correct declaration, return
00387   ///      the existing global.
00388   Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
00389 
00390 /// @}
00391 /// @name Global Alias Accessors
00392 /// @{
00393 
00394   /// Return the global alias in the module with the specified name, of
00395   /// arbitrary type. This method returns null if a global with the specified
00396   /// name is not found.
00397   GlobalAlias *getNamedAlias(StringRef Name) const;
00398 
00399 /// @}
00400 /// @name Named Metadata Accessors
00401 /// @{
00402 
00403   /// Return the first NamedMDNode in the module with the specified name. This
00404   /// method returns null if a NamedMDNode with the specified name is not found.
00405   NamedMDNode *getNamedMetadata(const Twine &Name) const;
00406 
00407   /// Return the named MDNode in the module with the specified name. This method
00408   /// returns a new NamedMDNode if a NamedMDNode with the specified name is not
00409   /// found.
00410   NamedMDNode *getOrInsertNamedMetadata(StringRef Name);
00411 
00412   /// Remove the given NamedMDNode from this module and delete it.
00413   void eraseNamedMetadata(NamedMDNode *NMD);
00414 
00415 /// @}
00416 /// @name Comdat Accessors
00417 /// @{
00418 
00419   /// Return the Comdat in the module with the specified name. It is created
00420   /// if it didn't already exist.
00421   Comdat *getOrInsertComdat(StringRef Name);
00422 
00423 /// @}
00424 /// @name Module Flags Accessors
00425 /// @{
00426 
00427   /// Returns the module flags in the provided vector.
00428   void getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const;
00429 
00430   /// Return the corresponding value if Key appears in module flags, otherwise
00431   /// return null.
00432   Value *getModuleFlag(StringRef Key) const;
00433 
00434   /// Returns the NamedMDNode in the module that represents module-level flags.
00435   /// This method returns null if there are no module-level flags.
00436   NamedMDNode *getModuleFlagsMetadata() const;
00437 
00438   /// Returns the NamedMDNode in the module that represents module-level flags.
00439   /// If module-level flags aren't found, it creates the named metadata that
00440   /// contains them.
00441   NamedMDNode *getOrInsertModuleFlagsMetadata();
00442 
00443   /// Add a module-level flag to the module-level flags metadata. It will create
00444   /// the module-level flags named metadata if it doesn't already exist.
00445   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Value *Val);
00446   void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
00447   void addModuleFlag(MDNode *Node);
00448 
00449 /// @}
00450 /// @name Materialization
00451 /// @{
00452 
00453   /// Sets the GVMaterializer to GVM. This module must not yet have a
00454   /// Materializer. To reset the materializer for a module that already has one,
00455   /// call MaterializeAllPermanently first. Destroying this module will destroy
00456   /// its materializer without materializing any more GlobalValues. Without
00457   /// destroying the Module, there is no way to detach or destroy a materializer
00458   /// without materializing all the GVs it controls, to avoid leaving orphan
00459   /// unmaterialized GVs.
00460   void setMaterializer(GVMaterializer *GVM);
00461   /// Retrieves the GVMaterializer, if any, for this Module.
00462   GVMaterializer *getMaterializer() const { return Materializer.get(); }
00463 
00464   /// True if the definition of GV has yet to be materializedfrom the
00465   /// GVMaterializer.
00466   bool isMaterializable(const GlobalValue *GV) const;
00467   /// Returns true if this GV was loaded from this Module's GVMaterializer and
00468   /// the GVMaterializer knows how to dematerialize the GV.
00469   bool isDematerializable(const GlobalValue *GV) const;
00470 
00471   /// Make sure the GlobalValue is fully read. If the module is corrupt, this
00472   /// returns true and fills in the optional string with information about the
00473   /// problem. If successful, this returns false.
00474   bool Materialize(GlobalValue *GV, std::string *ErrInfo = nullptr);
00475   /// If the GlobalValue is read in, and if the GVMaterializer supports it,
00476   /// release the memory for the function, and set it up to be materialized
00477   /// lazily. If !isDematerializable(), this method is a noop.
00478   void Dematerialize(GlobalValue *GV);
00479 
00480   /// Make sure all GlobalValues in this Module are fully read.
00481   std::error_code materializeAll();
00482 
00483   /// Make sure all GlobalValues in this Module are fully read and clear the
00484   /// Materializer. If the module is corrupt, this DOES NOT clear the old
00485   /// Materializer.
00486   std::error_code materializeAllPermanently(bool ReleaseBuffer = false);
00487 
00488 /// @}
00489 /// @name Direct access to the globals list, functions list, and symbol table
00490 /// @{
00491 
00492   /// Get the Module's list of global variables (constant).
00493   const GlobalListType   &getGlobalList() const       { return GlobalList; }
00494   /// Get the Module's list of global variables.
00495   GlobalListType         &getGlobalList()             { return GlobalList; }
00496   static iplist<GlobalVariable> Module::*getSublistAccess(GlobalVariable*) {
00497     return &Module::GlobalList;
00498   }
00499   /// Get the Module's list of functions (constant).
00500   const FunctionListType &getFunctionList() const     { return FunctionList; }
00501   /// Get the Module's list of functions.
00502   FunctionListType       &getFunctionList()           { return FunctionList; }
00503   static iplist<Function> Module::*getSublistAccess(Function*) {
00504     return &Module::FunctionList;
00505   }
00506   /// Get the Module's list of aliases (constant).
00507   const AliasListType    &getAliasList() const        { return AliasList; }
00508   /// Get the Module's list of aliases.
00509   AliasListType          &getAliasList()              { return AliasList; }
00510   static iplist<GlobalAlias> Module::*getSublistAccess(GlobalAlias*) {
00511     return &Module::AliasList;
00512   }
00513   /// Get the Module's list of named metadata (constant).
00514   const NamedMDListType  &getNamedMDList() const      { return NamedMDList; }
00515   /// Get the Module's list of named metadata.
00516   NamedMDListType        &getNamedMDList()            { return NamedMDList; }
00517   static ilist<NamedMDNode> Module::*getSublistAccess(NamedMDNode*) {
00518     return &Module::NamedMDList;
00519   }
00520   /// Get the symbol table of global variable and function identifiers
00521   const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
00522   /// Get the Module's symbol table of global variable and function identifiers.
00523   ValueSymbolTable       &getValueSymbolTable()       { return *ValSymTab; }
00524   /// Get the Module's symbol table for COMDATs (constant).
00525   const ComdatSymTabType &getComdatSymbolTable() const { return ComdatSymTab; }
00526   /// Get the Module's symbol table for COMDATs.
00527   ComdatSymTabType &getComdatSymbolTable() { return ComdatSymTab; }
00528 
00529 /// @}
00530 /// @name Global Variable Iteration
00531 /// @{
00532 
00533   global_iterator       global_begin()       { return GlobalList.begin(); }
00534   const_global_iterator global_begin() const { return GlobalList.begin(); }
00535   global_iterator       global_end  ()       { return GlobalList.end(); }
00536   const_global_iterator global_end  () const { return GlobalList.end(); }
00537   bool                  global_empty() const { return GlobalList.empty(); }
00538 
00539   iterator_range<global_iterator> globals() {
00540     return iterator_range<global_iterator>(global_begin(), global_end());
00541   }
00542   iterator_range<const_global_iterator> globals() const {
00543     return iterator_range<const_global_iterator>(global_begin(), global_end());
00544   }
00545 
00546 /// @}
00547 /// @name Function Iteration
00548 /// @{
00549 
00550   iterator                begin()       { return FunctionList.begin(); }
00551   const_iterator          begin() const { return FunctionList.begin(); }
00552   iterator                end  ()       { return FunctionList.end();   }
00553   const_iterator          end  () const { return FunctionList.end();   }
00554   reverse_iterator        rbegin()      { return FunctionList.rbegin(); }
00555   const_reverse_iterator  rbegin() const{ return FunctionList.rbegin(); }
00556   reverse_iterator        rend()        { return FunctionList.rend(); }
00557   const_reverse_iterator  rend() const  { return FunctionList.rend(); }
00558   size_t                  size() const  { return FunctionList.size(); }
00559   bool                    empty() const { return FunctionList.empty(); }
00560 
00561 /// @}
00562 /// @name Alias Iteration
00563 /// @{
00564 
00565   alias_iterator       alias_begin()            { return AliasList.begin(); }
00566   const_alias_iterator alias_begin() const      { return AliasList.begin(); }
00567   alias_iterator       alias_end  ()            { return AliasList.end();   }
00568   const_alias_iterator alias_end  () const      { return AliasList.end();   }
00569   size_t               alias_size () const      { return AliasList.size();  }
00570   bool                 alias_empty() const      { return AliasList.empty(); }
00571 
00572   iterator_range<alias_iterator> aliases() {
00573     return iterator_range<alias_iterator>(alias_begin(), alias_end());
00574   }
00575   iterator_range<const_alias_iterator> aliases() const {
00576     return iterator_range<const_alias_iterator>(alias_begin(), alias_end());
00577   }
00578 
00579 /// @}
00580 /// @name Named Metadata Iteration
00581 /// @{
00582 
00583   named_metadata_iterator named_metadata_begin() { return NamedMDList.begin(); }
00584   const_named_metadata_iterator named_metadata_begin() const {
00585     return NamedMDList.begin();
00586   }
00587 
00588   named_metadata_iterator named_metadata_end() { return NamedMDList.end(); }
00589   const_named_metadata_iterator named_metadata_end() const {
00590     return NamedMDList.end();
00591   }
00592 
00593   size_t named_metadata_size() const { return NamedMDList.size();  }
00594   bool named_metadata_empty() const { return NamedMDList.empty(); }
00595 
00596   iterator_range<named_metadata_iterator> named_metadata() {
00597     return iterator_range<named_metadata_iterator>(named_metadata_begin(),
00598                                                    named_metadata_end());
00599   }
00600   iterator_range<const_named_metadata_iterator> named_metadata() const {
00601     return iterator_range<const_named_metadata_iterator>(named_metadata_begin(),
00602                                                          named_metadata_end());
00603   }
00604 
00605 /// @}
00606 /// @name Utility functions for printing and dumping Module objects
00607 /// @{
00608 
00609   /// Print the module to an output stream with an optional
00610   /// AssemblyAnnotationWriter.
00611   void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const;
00612 
00613   /// Dump the module to stderr (for debugging).
00614   void dump() const;
00615   
00616   /// This function causes all the subinstructions to "let go" of all references
00617   /// that they are maintaining.  This allows one to 'delete' a whole class at
00618   /// a time, even though there may be circular references... first all
00619   /// references are dropped, and all use counts go to zero.  Then everything
00620   /// is delete'd for real.  Note that no operations are valid on an object
00621   /// that has "dropped all references", except operator delete.
00622   void dropAllReferences();
00623 
00624 /// @}
00625 /// @name Utility functions for querying Debug information.
00626 /// @{
00627 
00628   /// \brief Returns the Dwarf Version by checking module flags.
00629   unsigned getDwarfVersion() const;
00630 
00631 /// @}
00632 };
00633 
00634 /// An raw_ostream inserter for modules.
00635 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) {
00636   M.print(O, nullptr);
00637   return O;
00638 }
00639 
00640 // Create wrappers for C Binding types (see CBindingWrapping.h).
00641 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef)
00642 
00643 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
00644  * Module.
00645  */
00646 inline Module *unwrap(LLVMModuleProviderRef MP) {
00647   return reinterpret_cast<Module*>(MP);
00648 }
00649   
00650 } // End llvm namespace
00651 
00652 #endif