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