LLVM 20.0.0git
LLLexer.h
Go to the documentation of this file.
1//===- LLLexer.h - Lexer for LLVM 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// This class represents the Lexer for .ll files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ASMPARSER_LLLEXER_H
14#define LLVM_ASMPARSER_LLLEXER_H
15
16#include "LLToken.h"
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APSInt.h"
19#include "llvm/Support/SMLoc.h"
20#include <string>
21
22namespace llvm {
23 class Type;
24 class SMDiagnostic;
25 class SourceMgr;
26 class LLVMContext;
27
28 class LLLexer {
29 const char *CurPtr;
30 StringRef CurBuf;
31
32 enum class ErrorPriority {
33 None, // No error message present.
34 Parser, // Errors issued by parser.
35 Lexer, // Errors issued by lexer.
36 };
37
38 struct ErrorInfo {
39 ErrorPriority Priority = ErrorPriority::None;
41
42 explicit ErrorInfo(SMDiagnostic &Error) : Error(Error) {}
43 } ErrorInfo;
44
45 SourceMgr &SM;
46 LLVMContext &Context;
47
48 // Information about the current token.
49 const char *TokStart;
50 lltok::Kind CurKind;
51 std::string StrVal;
52 unsigned UIntVal = 0;
53 Type *TyVal = nullptr;
54 APFloat APFloatVal{0.0};
55 APSInt APSIntVal{0};
56
57 // When false (default), an identifier ending in ':' is a label token.
58 // When true, the ':' is treated as a separate token.
59 bool IgnoreColonInIdentifiers = false;
60
61 public:
62 explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
63 LLVMContext &C);
64
66 return CurKind = LexToken();
67 }
68
69 typedef SMLoc LocTy;
70 LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
71 lltok::Kind getKind() const { return CurKind; }
72 const std::string &getStrVal() const { return StrVal; }
73 Type *getTyVal() const { return TyVal; }
74 unsigned getUIntVal() const { return UIntVal; }
75 const APSInt &getAPSIntVal() const { return APSIntVal; }
76 const APFloat &getAPFloatVal() const { return APFloatVal; }
77
79 IgnoreColonInIdentifiers = val;
80 }
81
82 // This returns true as a convenience for the parser functions that return
83 // true on error.
84 bool ParseError(LocTy ErrorLoc, const Twine &Msg) {
85 Error(ErrorLoc, Msg, ErrorPriority::Parser);
86 return true;
87 }
88 bool ParseError(const Twine &Msg) { return ParseError(getLoc(), Msg); }
89
90 void Warning(LocTy WarningLoc, const Twine &Msg) const;
91 void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); }
92
93 private:
94 lltok::Kind LexToken();
95
96 int getNextChar();
97 void SkipLineComment();
98 bool SkipCComment();
99 lltok::Kind ReadString(lltok::Kind kind);
100 bool ReadVarName();
101
102 lltok::Kind LexIdentifier();
103 lltok::Kind LexDigitOrNegative();
104 lltok::Kind LexPositive();
105 lltok::Kind LexAt();
106 lltok::Kind LexDollar();
107 lltok::Kind LexExclaim();
108 lltok::Kind LexPercent();
109 lltok::Kind LexUIntID(lltok::Kind Token);
111 lltok::Kind LexQuote();
112 lltok::Kind Lex0x();
113 lltok::Kind LexHash();
114 lltok::Kind LexCaret();
115
116 uint64_t atoull(const char *Buffer, const char *End);
117 uint64_t HexIntToVal(const char *Buffer, const char *End);
118 void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
119 void FP80HexToIntPair(const char *Buffer, const char *End,
120 uint64_t Pair[2]);
121
122 void Error(LocTy ErrorLoc, const Twine &Msg, ErrorPriority Origin);
123
124 void LexError(LocTy ErrorLoc, const Twine &Msg) {
125 Error(ErrorLoc, Msg, ErrorPriority::Lexer);
126 }
127 void LexError(const Twine &Msg) { LexError(getLoc(), Msg); }
128 };
129} // end namespace llvm
130
131#endif
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
RelocType Type
Definition: COFFYAML.cpp:410
bool End
Definition: ELF_riscv.cpp:480
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
bool ParseError(LocTy ErrorLoc, const Twine &Msg)
Definition: LLLexer.h:84
lltok::Kind Lex()
Definition: LLLexer.h:65
unsigned getUIntVal() const
Definition: LLLexer.h:74
lltok::Kind getKind() const
Definition: LLLexer.h:71
const std::string & getStrVal() const
Definition: LLLexer.h:72
void Warning(const Twine &Msg) const
Definition: LLLexer.h:91
Type * getTyVal() const
Definition: LLLexer.h:73
LocTy getLoc() const
Definition: LLLexer.h:70
SMLoc LocTy
Definition: LLLexer.h:69
const APSInt & getAPSIntVal() const
Definition: LLLexer.h:75
void setIgnoreColonInIdentifiers(bool val)
Definition: LLLexer.h:78
const APFloat & getAPFloatVal() const
Definition: LLLexer.h:76
bool ParseError(const Twine &Msg)
Definition: LLLexer.h:88
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
Represents a location in source code.
Definition: SMLoc.h:23
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ None
Definition: CodeGenData.h:106
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.