LLVM 22.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 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 lltok::Kind getKind() const { return CurKind; }
73 const std::string &getStrVal() const { return StrVal; }
74 Type *getTyVal() const { return TyVal; }
75 unsigned getUIntVal() const { return UIntVal; }
76 const APSInt &getAPSIntVal() const { return APSIntVal; }
77 const APFloat &getAPFloatVal() const { return APFloatVal; }
78
80 IgnoreColonInIdentifiers = val;
81 }
82
83 /// Get the line, column position of the start of the current token,
84 /// zero-indexed
85 std::pair<unsigned, unsigned> getTokLineColumnPos() {
86 auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(TokStart));
87 return {LC.first - 1, LC.second - 1};
88 }
89 /// Get the line, column position of the end of the previous token,
90 /// zero-indexed exclusive
91 std::pair<unsigned, unsigned> getPrevTokEndLineColumnPos() {
92 auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(PrevTokEnd));
93 return {LC.first - 1, LC.second - 1};
94 }
95
96 // This returns true as a convenience for the parser functions that return
97 // true on error.
98 bool ParseError(LocTy ErrorLoc, const Twine &Msg) {
99 Error(ErrorLoc, Msg, ErrorPriority::Parser);
100 return true;
101 }
102 bool ParseError(const Twine &Msg) { return ParseError(getLoc(), Msg); }
103
104 void Warning(LocTy WarningLoc, const Twine &Msg) const;
105 void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); }
106
107 private:
108 lltok::Kind LexToken();
109
110 int getNextChar();
111 void SkipLineComment();
112 bool SkipCComment();
113 lltok::Kind ReadString(lltok::Kind kind);
114 bool ReadVarName();
115
116 lltok::Kind LexIdentifier();
117 lltok::Kind LexDigitOrNegative();
118 lltok::Kind LexPositive();
119 lltok::Kind LexAt();
120 lltok::Kind LexDollar();
121 lltok::Kind LexExclaim();
122 lltok::Kind LexPercent();
123 lltok::Kind LexUIntID(lltok::Kind Token);
125 lltok::Kind LexQuote();
126 lltok::Kind Lex0x();
127 lltok::Kind LexHash();
128 lltok::Kind LexCaret();
129
130 uint64_t atoull(const char *Buffer, const char *End);
131 uint64_t HexIntToVal(const char *Buffer, const char *End);
132 void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
133 void FP80HexToIntPair(const char *Buffer, const char *End,
134 uint64_t Pair[2]);
135
136 void Error(LocTy ErrorLoc, const Twine &Msg, ErrorPriority Origin);
137
138 void LexError(LocTy ErrorLoc, const Twine &Msg) {
139 Error(ErrorLoc, Msg, ErrorPriority::Lexer);
140 }
141 void LexError(const Twine &Msg) { LexError(getLoc(), Msg); }
142 };
143} // end namespace llvm
144
145#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...
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
bool ParseError(LocTy ErrorLoc, const Twine &Msg)
Definition LLLexer.h:98
lltok::Kind Lex()
Definition LLLexer.h:68
unsigned getUIntVal() const
Definition LLLexer.h:75
lltok::Kind getKind() const
Definition LLLexer.h:72
std::pair< unsigned, unsigned > getPrevTokEndLineColumnPos()
Get the line, column position of the end of the previous token, zero-indexed exclusive.
Definition LLLexer.h:91
const std::string & getStrVal() const
Definition LLLexer.h:73
LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &, LLVMContext &C)
Definition LLLexer.cpp:171
void Warning(const Twine &Msg) const
Definition LLLexer.h:105
Type * getTyVal() const
Definition LLLexer.h:74
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:85
const APSInt & getAPSIntVal() const
Definition LLLexer.h:76
void setIgnoreColonInIdentifiers(bool val)
Definition LLLexer.h:79
const APFloat & getAPFloatVal() const
Definition LLLexer.h:77
bool ParseError(const Twine &Msg)
Definition LLLexer.h:102
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:297
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:45
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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.