Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 1 | //===- LLLexer.h - Lexer for LLVM Assembly Files ----------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This class represents the Lexer for .ll files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 13 | #ifndef LLVM_LIB_ASMPARSER_LLLEXER_H |
| 14 | #define LLVM_LIB_ASMPARSER_LLLEXER_H |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 15 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 16 | #include "LLToken.h" |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APFloat.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/APSInt.h" |
Chris Lattner | 200e075 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/SourceMgr.h" |
Misha Brukman | fcef909 | 2009-01-02 22:49:28 +0000 | [diff] [blame] | 20 | #include <string> |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | class MemoryBuffer; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 24 | class Type; |
Chris Lattner | a76611a | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 25 | class SMDiagnostic; |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 26 | class LLVMContext; |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 27 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 28 | class LLLexer { |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 29 | const char *CurPtr; |
Rafael Espindola | 43d22b8 | 2014-08-18 22:28:28 +0000 | [diff] [blame] | 30 | StringRef CurBuf; |
Chris Lattner | a76611a | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 31 | SMDiagnostic &ErrorInfo; |
Chris Lattner | 200e075 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 32 | SourceMgr &SM; |
Owen Anderson | 32bc1e1 | 2009-07-07 18:44:11 +0000 | [diff] [blame] | 33 | LLVMContext &Context; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 34 | |
| 35 | // Information about the current token. |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 36 | const char *TokStart; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 37 | lltok::Kind CurKind; |
| 38 | std::string StrVal; |
| 39 | unsigned UIntVal; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 40 | Type *TyVal; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 41 | APFloat APFloatVal; |
| 42 | APSInt APSIntVal; |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 43 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 44 | // When false (default), an identifier ending in ':' is a label token. |
| 45 | // When true, the ':' is treated as a separate token. |
| 46 | bool IgnoreColonInIdentifiers; |
| 47 | |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 48 | public: |
Rafael Espindola | 43d22b8 | 2014-08-18 22:28:28 +0000 | [diff] [blame] | 49 | explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &, |
Owen Anderson | 32bc1e1 | 2009-07-07 18:44:11 +0000 | [diff] [blame] | 50 | LLVMContext &C); |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 51 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 52 | lltok::Kind Lex() { |
| 53 | return CurKind = LexToken(); |
| 54 | } |
| 55 | |
Chris Lattner | 200e075 | 2009-07-02 23:08:13 +0000 | [diff] [blame] | 56 | typedef SMLoc LocTy; |
| 57 | LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 58 | lltok::Kind getKind() const { return CurKind; } |
Chris Lattner | dba4346 | 2010-04-01 04:53:22 +0000 | [diff] [blame] | 59 | const std::string &getStrVal() const { return StrVal; } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 60 | Type *getTyVal() const { return TyVal; } |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 61 | unsigned getUIntVal() const { return UIntVal; } |
| 62 | const APSInt &getAPSIntVal() const { return APSIntVal; } |
| 63 | const APFloat &getAPFloatVal() const { return APFloatVal; } |
| 64 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 65 | void setIgnoreColonInIdentifiers(bool val) { |
| 66 | IgnoreColonInIdentifiers = val; |
| 67 | } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 68 | |
Fangrui Song | 28f69c7 | 2018-07-12 02:03:53 +0000 | [diff] [blame] | 69 | bool Error(LocTy ErrorLoc, const Twine &Msg) const; |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 70 | bool Error(const Twine &Msg) const { return Error(getLoc(), Msg); } |
Saleem Abdulrasool | efa31a9 | 2014-04-05 22:42:53 +0000 | [diff] [blame] | 71 | |
| 72 | void Warning(LocTy WarningLoc, const Twine &Msg) const; |
| 73 | void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); } |
| 74 | |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 75 | private: |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 76 | lltok::Kind LexToken(); |
| 77 | |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 78 | int getNextChar(); |
| 79 | void SkipLineComment(); |
Nick Lewycky | 2176cdc | 2011-06-04 18:16:26 +0000 | [diff] [blame] | 80 | lltok::Kind ReadString(lltok::Kind kind); |
| 81 | bool ReadVarName(); |
| 82 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 83 | lltok::Kind LexIdentifier(); |
| 84 | lltok::Kind LexDigitOrNegative(); |
| 85 | lltok::Kind LexPositive(); |
| 86 | lltok::Kind LexAt(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 87 | lltok::Kind LexDollar(); |
Chris Lattner | 1eed2d6 | 2009-12-30 04:56:59 +0000 | [diff] [blame] | 88 | lltok::Kind LexExclaim(); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 89 | lltok::Kind LexPercent(); |
Teresa Johnson | 08d5b4e | 2018-05-26 02:34:13 +0000 | [diff] [blame] | 90 | lltok::Kind LexUIntID(lltok::Kind Token); |
David Majnemer | 1d681aa | 2014-12-10 00:43:17 +0000 | [diff] [blame] | 91 | lltok::Kind LexVar(lltok::Kind Var, lltok::Kind VarID); |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 92 | lltok::Kind LexQuote(); |
| 93 | lltok::Kind Lex0x(); |
Bill Wendling | 63b8819 | 2013-02-06 06:52:58 +0000 | [diff] [blame] | 94 | lltok::Kind LexHash(); |
Teresa Johnson | 08d5b4e | 2018-05-26 02:34:13 +0000 | [diff] [blame] | 95 | lltok::Kind LexCaret(); |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 96 | |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 97 | uint64_t atoull(const char *Buffer, const char *End); |
| 98 | uint64_t HexIntToVal(const char *Buffer, const char *End); |
| 99 | void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]); |
Fangrui Song | 28f69c7 | 2018-07-12 02:03:53 +0000 | [diff] [blame] | 100 | void FP80HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]); |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 101 | }; |
| 102 | } // end namespace llvm |
| 103 | |
| 104 | #endif |