1 //===-- Parser.h - Parser for LLVM IR text assembly files -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // These classes are implemented by the lib/AsmParser library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_ASMPARSER_PARSER_H 14 #define LLVM_ASMPARSER_PARSER_H 15 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/StringRef.h" 18 #include <memory> 19 20 namespace llvm { 21 22 class Constant; 23 class LLVMContext; 24 class MemoryBufferRef; 25 class Module; 26 class ModuleSummaryIndex; 27 struct SlotMapping; 28 class SMDiagnostic; 29 class Type; 30 31 typedef llvm::function_ref<Optional<std::string>(StringRef)> 32 DataLayoutCallbackTy; 33 34 /// This function is a main interface to the LLVM Assembly Parser. It parses 35 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a 36 /// Module (intermediate representation) with the corresponding features. Note 37 /// that this does not verify that the generated Module is valid, so you should 38 /// run the verifier after parsing the file to check that it is okay. 39 /// Parse LLVM Assembly from a file 40 /// \param Filename The name of the file to parse 41 /// \param Err Error result info. 42 /// \param Context Context in which to allocate globals info. 43 /// \param Slots The optional slot mapping that will be initialized during 44 /// parsing. 45 std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, 46 LLVMContext &Context, 47 SlotMapping *Slots = nullptr); 48 49 /// The function is a secondary interface to the LLVM Assembly Parser. It parses 50 /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a 51 /// Module (intermediate representation) with the corresponding features. Note 52 /// that this does not verify that the generated Module is valid, so you should 53 /// run the verifier after parsing the file to check that it is okay. 54 /// Parse LLVM Assembly from a string 55 /// \param AsmString The string containing assembly 56 /// \param Err Error result info. 57 /// \param Context Context in which to allocate globals info. 58 /// \param Slots The optional slot mapping that will be initialized during 59 /// parsing. 60 std::unique_ptr<Module> parseAssemblyString(StringRef AsmString, 61 SMDiagnostic &Err, 62 LLVMContext &Context, 63 SlotMapping *Slots = nullptr); 64 65 /// Holds the Module and ModuleSummaryIndex returned by the interfaces 66 /// that parse both. 67 struct ParsedModuleAndIndex { 68 std::unique_ptr<Module> Mod; 69 std::unique_ptr<ModuleSummaryIndex> Index; 70 }; 71 72 /// This function is a main interface to the LLVM Assembly Parser. It parses 73 /// an ASCII file that (presumably) contains LLVM Assembly code, including 74 /// a module summary. It returns a Module (intermediate representation) and 75 /// a ModuleSummaryIndex with the corresponding features. Note that this does 76 /// not verify that the generated Module or Index are valid, so you should 77 /// run the verifier after parsing the file to check that they are okay. 78 /// Parse LLVM Assembly from a file 79 /// \param Filename The name of the file to parse 80 /// \param Err Error result info. 81 /// \param Context Context in which to allocate globals info. 82 /// \param Slots The optional slot mapping that will be initialized during 83 /// parsing. 84 /// \param DataLayoutCallback Override datalayout in the llvm assembly. 85 ParsedModuleAndIndex parseAssemblyFileWithIndex( 86 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, 87 SlotMapping *Slots = nullptr, 88 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); 89 90 /// Only for use in llvm-as for testing; this does not produce a valid module. 91 ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo( 92 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, 93 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback); 94 95 /// This function is a main interface to the LLVM Assembly Parser. It parses 96 /// an ASCII file that (presumably) contains LLVM Assembly code for a module 97 /// summary. It returns a a ModuleSummaryIndex with the corresponding features. 98 /// Note that this does not verify that the generated Index is valid, so you 99 /// should run the verifier after parsing the file to check that it is okay. 100 /// Parse LLVM Assembly Index from a file 101 /// \param Filename The name of the file to parse 102 /// \param Err Error result info. 103 std::unique_ptr<ModuleSummaryIndex> 104 parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err); 105 106 /// parseAssemblyFile and parseAssemblyString are wrappers around this function. 107 /// Parse LLVM Assembly from a MemoryBuffer. 108 /// \param F The MemoryBuffer containing assembly 109 /// \param Err Error result info. 110 /// \param Slots The optional slot mapping that will be initialized during 111 /// parsing. 112 /// \param DataLayoutCallback Override datalayout in the llvm assembly. 113 std::unique_ptr<Module> parseAssembly( 114 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, 115 SlotMapping *Slots = nullptr, 116 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); 117 118 /// Parse LLVM Assembly including the summary index from a MemoryBuffer. 119 /// 120 /// \param F The MemoryBuffer containing assembly with summary 121 /// \param Err Error result info. 122 /// \param Slots The optional slot mapping that will be initialized during 123 /// parsing. 124 /// 125 /// parseAssemblyFileWithIndex is a wrapper around this function. 126 ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, 127 SMDiagnostic &Err, 128 LLVMContext &Context, 129 SlotMapping *Slots = nullptr); 130 131 /// Parse LLVM Assembly for summary index from a MemoryBuffer. 132 /// 133 /// \param F The MemoryBuffer containing assembly with summary 134 /// \param Err Error result info. 135 /// 136 /// parseSummaryIndexAssemblyFile is a wrapper around this function. 137 std::unique_ptr<ModuleSummaryIndex> 138 parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err); 139 140 /// This function is the low-level interface to the LLVM Assembly Parser. 141 /// This is kept as an independent function instead of being inlined into 142 /// parseAssembly for the convenience of interactive users that want to add 143 /// recently parsed bits to an existing module. 144 /// 145 /// \param F The MemoryBuffer containing assembly 146 /// \param M The module to add data to. 147 /// \param Index The index to add data to. 148 /// \param Err Error result info. 149 /// \param Slots The optional slot mapping that will be initialized during 150 /// parsing. 151 /// \return true on error. 152 /// \param DataLayoutCallback Override datalayout in the llvm assembly. 153 bool parseAssemblyInto( 154 MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, 155 SlotMapping *Slots = nullptr, 156 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); 157 158 /// Parse a type and a constant value in the given string. 159 /// 160 /// The constant value can be any LLVM constant, including a constant 161 /// expression. 162 /// 163 /// \param Slots The optional slot mapping that will restore the parsing state 164 /// of the module. 165 /// \return null on error. 166 Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M, 167 const SlotMapping *Slots = nullptr); 168 169 /// Parse a type in the given string. 170 /// 171 /// \param Slots The optional slot mapping that will restore the parsing state 172 /// of the module. 173 /// \return null on error. 174 Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, 175 const SlotMapping *Slots = nullptr); 176 177 /// Parse a string \p Asm that starts with a type. 178 /// \p Read[out] gives the number of characters that have been read to parse 179 /// the type in \p Asm. 180 /// 181 /// \param Slots The optional slot mapping that will restore the parsing state 182 /// of the module. 183 /// \return null on error. 184 Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, 185 const Module &M, const SlotMapping *Slots = nullptr); 186 187 } // End llvm namespace 188 189 #endif 190