blob: 4d3a2920e937475ece2c2878a7476ad30647d7c1 [file] [log] [blame]
Chris Lattner660c6b92007-11-18 08:46:26 +00001//===- LLLexer.h - Lexer for LLVM Assembly Files ----------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner660c6b92007-11-18 08:46:26 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class represents the Lexer for .ll files.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_ASMPARSER_LLLEXER_H
14#define LLVM_LIB_ASMPARSER_LLLEXER_H
Chris Lattner660c6b92007-11-18 08:46:26 +000015
Chris Lattnerac161bf2009-01-02 07:01:27 +000016#include "LLToken.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000017#include "llvm/ADT/APFloat.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "llvm/ADT/APSInt.h"
Chris Lattner200e0752009-07-02 23:08:13 +000019#include "llvm/Support/SourceMgr.h"
Misha Brukmanfcef9092009-01-02 22:49:28 +000020#include <string>
Chris Lattner660c6b92007-11-18 08:46:26 +000021
22namespace llvm {
23 class MemoryBuffer;
Chris Lattnerac161bf2009-01-02 07:01:27 +000024 class Type;
Chris Lattnera76611a2009-07-02 22:46:18 +000025 class SMDiagnostic;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000026 class LLVMContext;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000027
Benjamin Kramer079b96e2013-09-11 18:05:11 +000028 class LLLexer {
Chris Lattner660c6b92007-11-18 08:46:26 +000029 const char *CurPtr;
Rafael Espindola43d22b82014-08-18 22:28:28 +000030 StringRef CurBuf;
Chris Lattnera76611a2009-07-02 22:46:18 +000031 SMDiagnostic &ErrorInfo;
Chris Lattner200e0752009-07-02 23:08:13 +000032 SourceMgr &SM;
Owen Anderson32bc1e12009-07-07 18:44:11 +000033 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000034
35 // Information about the current token.
Chris Lattner660c6b92007-11-18 08:46:26 +000036 const char *TokStart;
Chris Lattnerac161bf2009-01-02 07:01:27 +000037 lltok::Kind CurKind;
38 std::string StrVal;
39 unsigned UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000040 Type *TyVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +000041 APFloat APFloatVal;
42 APSInt APSIntVal;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000043
Teresa Johnson63ee0e72018-06-26 13:56:49 +000044 // 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 Lattner660c6b92007-11-18 08:46:26 +000048 public:
Rafael Espindola43d22b82014-08-18 22:28:28 +000049 explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
Owen Anderson32bc1e12009-07-07 18:44:11 +000050 LLVMContext &C);
Chris Lattner660c6b92007-11-18 08:46:26 +000051
Chris Lattnerac161bf2009-01-02 07:01:27 +000052 lltok::Kind Lex() {
53 return CurKind = LexToken();
54 }
55
Chris Lattner200e0752009-07-02 23:08:13 +000056 typedef SMLoc LocTy;
57 LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
Chris Lattnerac161bf2009-01-02 07:01:27 +000058 lltok::Kind getKind() const { return CurKind; }
Chris Lattnerdba43462010-04-01 04:53:22 +000059 const std::string &getStrVal() const { return StrVal; }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000060 Type *getTyVal() const { return TyVal; }
Chris Lattnerac161bf2009-01-02 07:01:27 +000061 unsigned getUIntVal() const { return UIntVal; }
62 const APSInt &getAPSIntVal() const { return APSIntVal; }
63 const APFloat &getAPFloatVal() const { return APFloatVal; }
64
Teresa Johnson63ee0e72018-06-26 13:56:49 +000065 void setIgnoreColonInIdentifiers(bool val) {
66 IgnoreColonInIdentifiers = val;
67 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000068
Fangrui Song28f69c72018-07-12 02:03:53 +000069 bool Error(LocTy ErrorLoc, const Twine &Msg) const;
Benjamin Kramerc7583112010-09-27 17:42:11 +000070 bool Error(const Twine &Msg) const { return Error(getLoc(), Msg); }
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +000071
72 void Warning(LocTy WarningLoc, const Twine &Msg) const;
73 void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); }
74
Chris Lattner660c6b92007-11-18 08:46:26 +000075 private:
Chris Lattnerac161bf2009-01-02 07:01:27 +000076 lltok::Kind LexToken();
77
Chris Lattner660c6b92007-11-18 08:46:26 +000078 int getNextChar();
79 void SkipLineComment();
Nick Lewycky2176cdc2011-06-04 18:16:26 +000080 lltok::Kind ReadString(lltok::Kind kind);
81 bool ReadVarName();
82
Chris Lattnerac161bf2009-01-02 07:01:27 +000083 lltok::Kind LexIdentifier();
84 lltok::Kind LexDigitOrNegative();
85 lltok::Kind LexPositive();
86 lltok::Kind LexAt();
David Majnemerdad0a642014-06-27 18:19:56 +000087 lltok::Kind LexDollar();
Chris Lattner1eed2d62009-12-30 04:56:59 +000088 lltok::Kind LexExclaim();
Chris Lattnerac161bf2009-01-02 07:01:27 +000089 lltok::Kind LexPercent();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000090 lltok::Kind LexUIntID(lltok::Kind Token);
David Majnemer1d681aa2014-12-10 00:43:17 +000091 lltok::Kind LexVar(lltok::Kind Var, lltok::Kind VarID);
Chris Lattnerac161bf2009-01-02 07:01:27 +000092 lltok::Kind LexQuote();
93 lltok::Kind Lex0x();
Bill Wendling63b88192013-02-06 06:52:58 +000094 lltok::Kind LexHash();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000095 lltok::Kind LexCaret();
Misha Brukman1d9a93d2009-01-02 22:46:48 +000096
Chris Lattnerac161bf2009-01-02 07:01:27 +000097 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 Song28f69c72018-07-12 02:03:53 +0000100 void FP80HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
Chris Lattner660c6b92007-11-18 08:46:26 +0000101 };
102} // end namespace llvm
103
104#endif