xref: /llvm-project/llvm/include/llvm/AsmParser/LLLexer.h (revision b8ac87f34a6f4405bf8d91339a10f188db30aa3b)
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 
22 namespace 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;
40       SMDiagnostic &Error;
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 
65     lltok::Kind Lex() {
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 
78     void setIgnoreColonInIdentifiers(bool val) {
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);
110     lltok::Kind LexVar(lltok::Kind Var, lltok::Kind VarID);
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
132