Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:20

0001 //===-- Parser.h - Parser for LLVM IR text assembly files -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  These classes are implemented by the lib/AsmParser library.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_ASMPARSER_PARSER_H
0014 #define LLVM_ASMPARSER_PARSER_H
0015 
0016 #include "llvm/ADT/STLFunctionalExtras.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include <memory>
0019 #include <optional>
0020 
0021 namespace llvm {
0022 
0023 class Constant;
0024 class DIExpression;
0025 class LLVMContext;
0026 class MemoryBufferRef;
0027 class Module;
0028 class ModuleSummaryIndex;
0029 struct SlotMapping;
0030 class SMDiagnostic;
0031 class Type;
0032 
0033 typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
0034     DataLayoutCallbackTy;
0035 
0036 /// This function is a main interface to the LLVM Assembly Parser. It parses
0037 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
0038 /// Module (intermediate representation) with the corresponding features. Note
0039 /// that this does not verify that the generated Module is valid, so you should
0040 /// run the verifier after parsing the file to check that it is okay.
0041 /// Parse LLVM Assembly from a file
0042 /// \param Filename The name of the file to parse
0043 /// \param Err Error result info.
0044 /// \param Context Context in which to allocate globals info.
0045 /// \param Slots The optional slot mapping that will be initialized during
0046 ///              parsing.
0047 std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
0048                                           LLVMContext &Context,
0049                                           SlotMapping *Slots = nullptr);
0050 
0051 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
0052 /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
0053 /// Module (intermediate representation) with the corresponding features. Note
0054 /// that this does not verify that the generated Module is valid, so you should
0055 /// run the verifier after parsing the file to check that it is okay.
0056 /// Parse LLVM Assembly from a string
0057 /// \param AsmString The string containing assembly
0058 /// \param Err Error result info.
0059 /// \param Context Context in which to allocate globals info.
0060 /// \param Slots The optional slot mapping that will be initialized during
0061 ///              parsing.
0062 std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
0063                                             SMDiagnostic &Err,
0064                                             LLVMContext &Context,
0065                                             SlotMapping *Slots = nullptr);
0066 
0067 /// Holds the Module and ModuleSummaryIndex returned by the interfaces
0068 /// that parse both.
0069 struct ParsedModuleAndIndex {
0070   std::unique_ptr<Module> Mod;
0071   std::unique_ptr<ModuleSummaryIndex> Index;
0072 };
0073 
0074 /// This function is a main interface to the LLVM Assembly Parser. It parses
0075 /// an ASCII file that (presumably) contains LLVM Assembly code, including
0076 /// a module summary. It returns a Module (intermediate representation) and
0077 /// a ModuleSummaryIndex with the corresponding features. Note that this does
0078 /// not verify that the generated Module or Index are valid, so you should
0079 /// run the verifier after parsing the file to check that they are okay.
0080 /// Parse LLVM Assembly from a file
0081 /// \param Filename The name of the file to parse
0082 /// \param Err Error result info.
0083 /// \param Context Context in which to allocate globals info.
0084 /// \param Slots The optional slot mapping that will be initialized during
0085 ///              parsing.
0086 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
0087 ParsedModuleAndIndex parseAssemblyFileWithIndex(
0088     StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
0089     SlotMapping *Slots = nullptr,
0090     DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
0091       return std::nullopt;
0092     });
0093 
0094 /// Only for use in llvm-as for testing; this does not produce a valid module.
0095 ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(
0096     StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
0097     SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
0098 
0099 /// This function is a main interface to the LLVM Assembly Parser. It parses
0100 /// an ASCII file that (presumably) contains LLVM Assembly code for a module
0101 /// summary. It returns a ModuleSummaryIndex with the corresponding features.
0102 /// Note that this does not verify that the generated Index is valid, so you
0103 /// should run the verifier after parsing the file to check that it is okay.
0104 /// Parse LLVM Assembly Index from a file
0105 /// \param Filename The name of the file to parse
0106 /// \param Err Error result info.
0107 std::unique_ptr<ModuleSummaryIndex>
0108 parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
0109 
0110 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
0111 /// an ASCII string that (presumably) contains LLVM Assembly code for a module
0112 /// summary. It returns a a ModuleSummaryIndex with the corresponding features.
0113 /// Note that this does not verify that the generated Index is valid, so you
0114 /// should run the verifier after parsing the file to check that it is okay.
0115 /// Parse LLVM Assembly from a string
0116 /// \param AsmString The string containing assembly
0117 /// \param Err Error result info.
0118 std::unique_ptr<ModuleSummaryIndex>
0119 parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
0120 
0121 /// parseAssemblyFile and parseAssemblyString are wrappers around this function.
0122 /// Parse LLVM Assembly from a MemoryBuffer.
0123 /// \param F The MemoryBuffer containing assembly
0124 /// \param Err Error result info.
0125 /// \param Slots The optional slot mapping that will be initialized during
0126 ///              parsing.
0127 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
0128 std::unique_ptr<Module> parseAssembly(
0129     MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
0130     SlotMapping *Slots = nullptr,
0131     DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
0132       return std::nullopt;
0133     });
0134 
0135 /// Parse LLVM Assembly including the summary index from a MemoryBuffer.
0136 ///
0137 /// \param F The MemoryBuffer containing assembly with summary
0138 /// \param Err Error result info.
0139 /// \param Slots The optional slot mapping that will be initialized during
0140 ///              parsing.
0141 ///
0142 /// parseAssemblyFileWithIndex is a wrapper around this function.
0143 ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F,
0144                                             SMDiagnostic &Err,
0145                                             LLVMContext &Context,
0146                                             SlotMapping *Slots = nullptr);
0147 
0148 /// Parse LLVM Assembly for summary index from a MemoryBuffer.
0149 ///
0150 /// \param F The MemoryBuffer containing assembly with summary
0151 /// \param Err Error result info.
0152 ///
0153 /// parseSummaryIndexAssemblyFile is a wrapper around this function.
0154 std::unique_ptr<ModuleSummaryIndex>
0155 parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
0156 
0157 /// This function is the low-level interface to the LLVM Assembly Parser.
0158 /// This is kept as an independent function instead of being inlined into
0159 /// parseAssembly for the convenience of interactive users that want to add
0160 /// recently parsed bits to an existing module.
0161 ///
0162 /// \param F The MemoryBuffer containing assembly
0163 /// \param M The module to add data to.
0164 /// \param Index The index to add data to.
0165 /// \param Err Error result info.
0166 /// \param Slots The optional slot mapping that will be initialized during
0167 ///              parsing.
0168 /// \return true on error.
0169 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
0170 bool parseAssemblyInto(
0171     MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
0172     SlotMapping *Slots = nullptr,
0173     DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
0174       return std::nullopt;
0175     });
0176 
0177 /// Parse a type and a constant value in the given string.
0178 ///
0179 /// The constant value can be any LLVM constant, including a constant
0180 /// expression.
0181 ///
0182 /// \param Slots The optional slot mapping that will restore the parsing state
0183 /// of the module.
0184 /// \return null on error.
0185 Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M,
0186                              const SlotMapping *Slots = nullptr);
0187 
0188 /// Parse a type in the given string.
0189 ///
0190 /// \param Slots The optional slot mapping that will restore the parsing state
0191 /// of the module.
0192 /// \return null on error.
0193 Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
0194                 const SlotMapping *Slots = nullptr);
0195 
0196 /// Parse a string \p Asm that starts with a type.
0197 /// \p Read[out] gives the number of characters that have been read to parse
0198 /// the type in \p Asm.
0199 ///
0200 /// \param Slots The optional slot mapping that will restore the parsing state
0201 /// of the module.
0202 /// \return null on error.
0203 Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
0204                            const Module &M, const SlotMapping *Slots = nullptr);
0205 
0206 DIExpression *parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read,
0207                                                SMDiagnostic &Err,
0208                                                const Module &M,
0209                                                const SlotMapping *Slots);
0210 
0211 } // End llvm namespace
0212 
0213 #endif