LLVM 24.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 "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APSInt.h"
19#include "llvm/Support/SMLoc.h"
21#include <string>
22
23namespace llvm {
24 class Type;
25 class SMDiagnostic;
26 class LLVMContext;
27
28 class LLLexer {
29 const char *CurPtr;
30 StringRef CurBuf;
31
32 /// The end (exclusive) of the previous token.
33 const char *PrevTokEnd = nullptr;
34
35 enum class ErrorPriority {
36 None, // No error message present.
37 Parser, // Errors issued by parser.
38 Lexer, // Errors issued by lexer.
39 };
40
41 struct ErrorInfo {
42 ErrorPriority Priority = ErrorPriority::None;
43 SMDiagnostic &Error;
44
45 explicit ErrorInfo(SMDiagnostic &Error) : Error(Error) {}
46 } ErrorInfo;
47
48 SourceMgr &SM;
49 LLVMContext &Context;
50
51 // Information about the current token.
52 const char *TokStart;
53 lltok::Kind CurKind;
54 std::string StrVal;
55 unsigned UIntVal = 0;
56 Type *TyVal = nullptr;
57 APFloat APFloatVal{0.0};
58 APSInt APSIntVal{0};
59
60 // When false (default), an identifier ending in ':' is a label token.
61 // When true, the ':' is treated as a separate token.
62 bool IgnoreColonInIdentifiers = false;
63
64 public:
65 LLVM_ABI explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
66 LLVMContext &C);
67
68 lltok::Kind Lex() { return CurKind = LexToken(); }
69
70 typedef SMLoc LocTy;
71 LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
72 LocTy getPrevTokEndLoc() const { return SMLoc::getFromPointer(PrevTokEnd); }
73 lltok::Kind getKind() const { return CurKind; }
74 const std::string &getStrVal() const { return StrVal; }
75 Type *getTyVal() const { return TyVal; }
76 unsigned getUIntVal() const { return UIntVal; }
77 const APSInt &getAPSIntVal() const { return APSIntVal; }
78 const APFloat &getAPFloatVal() const { return APFloatVal; }
79
81 IgnoreColonInIdentifiers = val;
82 }
83
84 /// Get the line, column position of the start of the current token,
85 /// zero-indexed
86 std::pair<unsigned, unsigned> getTokLineColumnPos() {
87 auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(TokStart));
88 return {LC.first - 1, LC.second - 1};
89 }
90 /// Get the line, column position of the end of the previous token,
91 /// zero-indexed exclusive
92 std::pair<unsigned, unsigned> getPrevTokEndLineColumnPos() {
93 auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(PrevTokEnd));
94 return {LC.first - 1, LC.second - 1};
95 }
96
97 // This returns true as a convenience for the parser functions that return
98 // true on error.
99 bool ParseError(LocTy ErrorLoc, const Twine &Msg) {
100 Error(ErrorLoc, Msg, ErrorPriority::Parser);
101 return true;
102 }
103 bool ParseError(const Twine &Msg) { return ParseError(getLoc(), Msg); }
104
105 LLVM_ABI void Warning(LocTy WarningLoc, const Twine &Msg) const;
106 void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); }
107
108 private:
109 LLVM_ABI lltok::Kind LexToken();
110
111 int getNextChar();
112 void SkipLineComment();
113 bool SkipCComment();
114 lltok::Kind ReadString(lltok::Kind kind);
115 bool ReadVarName();
116
117 lltok::Kind LexIdentifier();
118 lltok::Kind LexDigitOrNegative();
119 lltok::Kind LexPositive();
120 lltok::Kind LexAt();
121 lltok::Kind LexDollar();
122 lltok::Kind LexExclaim();
123 lltok::Kind LexPercent();
124 lltok::Kind LexUIntID(lltok::Kind Token);
126 lltok::Kind LexQuote();
127 lltok::Kind Lex0x();
128 lltok::Kind LexHash();
129 lltok::Kind LexCaret();
130 lltok::Kind LexFloatStr();
131
132 uint64_t atoull(const char *Buffer, const char *End);
133 uint64_t HexIntToVal(const char *Buffer, const char *End);
134 void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
135 void FP80HexToIntPair(const char *Buffer, const char *End,
136 uint64_t Pair[2]);
137
138 LLVM_ABI void Error(LocTy ErrorLoc, const Twine &Msg, ErrorPriority Origin);
139
140 void LexError(LocTy ErrorLoc, const Twine &Msg) {
141 Error(ErrorLoc, Msg, ErrorPriority::Lexer);
142 }
143 void LexError(const Twine &Msg) { LexError(getLoc(), Msg); }
144 };
145} // end namespace llvm
146
147#endif
unsigned uint64_t
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...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:215
const char * Msg
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
bool ParseError(LocTy ErrorLoc, const Twine &Msg)
Definition LLLexer.h:99
lltok::Kind Lex()
Definition LLLexer.h:68
unsigned getUIntVal() const
Definition LLLexer.h:76
lltok::Kind getKind() const
Definition LLLexer.h:73
std::pair< unsigned, unsigned > getPrevTokEndLineColumnPos()
Get the line, column position of the end of the previous token, zero-indexed exclusive.
Definition LLLexer.h:92
const std::string & getStrVal() const
Definition LLLexer.h:74
LLVM_ABI LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &, LLVMContext &C)
Definition LLLexer.cpp:171
void Warning(const Twine &Msg) const
Definition LLLexer.h:106
Type * getTyVal() const
Definition LLLexer.h:75
LocTy getLoc() const
Definition LLLexer.h:71
SMLoc LocTy
Definition LLLexer.h:70
std::pair< unsigned, unsigned > getTokLineColumnPos()
Get the line, column position of the start of the current token, zero-indexed.
Definition LLLexer.h:86
const APSInt & getAPSIntVal() const
Definition LLLexer.h:77
void setIgnoreColonInIdentifiers(bool val)
Definition LLLexer.h:80
LocTy getPrevTokEndLoc() const
Definition LLLexer.h:72
const APFloat & getAPFloatVal() const
Definition LLLexer.h:78
bool ParseError(const Twine &Msg)
Definition LLLexer.h:103
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:308
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This is an optimization pass for GlobalISel generic memory operations.
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.