blob: 21deb6e08910bda8e99fbead656615ae06e0edcc [file] [log] [blame]
Chris Lattner660c6b92007-11-18 08:46:26 +00001//===- LLLexer.h - Lexer for LLVM Assembly Files ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner660c6b92007-11-18 08:46:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_ASMPARSER_LLLEXER_H
15#define LLVM_LIB_ASMPARSER_LLLEXER_H
Chris Lattner660c6b92007-11-18 08:46:26 +000016
Chris Lattnerac161bf2009-01-02 07:01:27 +000017#include "LLToken.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000018#include "llvm/ADT/APFloat.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000019#include "llvm/ADT/APSInt.h"
Chris Lattner200e0752009-07-02 23:08:13 +000020#include "llvm/Support/SourceMgr.h"
Misha Brukmanfcef9092009-01-02 22:49:28 +000021#include <string>
Chris Lattner660c6b92007-11-18 08:46:26 +000022
23namespace llvm {
24 class MemoryBuffer;
Chris Lattnerac161bf2009-01-02 07:01:27 +000025 class Type;
Chris Lattnera76611a2009-07-02 22:46:18 +000026 class SMDiagnostic;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000027 class LLVMContext;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000028
Benjamin Kramer079b96e2013-09-11 18:05:11 +000029 class LLLexer {
Chris Lattner660c6b92007-11-18 08:46:26 +000030 const char *CurPtr;
Rafael Espindola43d22b82014-08-18 22:28:28 +000031 StringRef CurBuf;
Chris Lattnera76611a2009-07-02 22:46:18 +000032 SMDiagnostic &ErrorInfo;
Chris Lattner200e0752009-07-02 23:08:13 +000033 SourceMgr &SM;
Owen Anderson32bc1e12009-07-07 18:44:11 +000034 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000035
36 // Information about the current token.
Chris Lattner660c6b92007-11-18 08:46:26 +000037 const char *TokStart;
Chris Lattnerac161bf2009-01-02 07:01:27 +000038 lltok::Kind CurKind;
39 std::string StrVal;
40 unsigned UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000041 Type *TyVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +000042 APFloat APFloatVal;
43 APSInt APSIntVal;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000044
Teresa Johnson63ee0e72018-06-26 13:56:49 +000045 // When false (default), an identifier ending in ':' is a label token.
46 // When true, the ':' is treated as a separate token.
47 bool IgnoreColonInIdentifiers;
48
Chris Lattner660c6b92007-11-18 08:46:26 +000049 public:
Rafael Espindola43d22b82014-08-18 22:28:28 +000050 explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
Owen Anderson32bc1e12009-07-07 18:44:11 +000051 LLVMContext &C);
Chris Lattner660c6b92007-11-18 08:46:26 +000052
Chris Lattnerac161bf2009-01-02 07:01:27 +000053 lltok::Kind Lex() {
54 return CurKind = LexToken();
55 }
56
Chris Lattner200e0752009-07-02 23:08:13 +000057 typedef SMLoc LocTy;
58 LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
Chris Lattnerac161bf2009-01-02 07:01:27 +000059 lltok::Kind getKind() const { return CurKind; }
Chris Lattnerdba43462010-04-01 04:53:22 +000060 const std::string &getStrVal() const { return StrVal; }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000061 Type *getTyVal() const { return TyVal; }
Chris Lattnerac161bf2009-01-02 07:01:27 +000062 unsigned getUIntVal() const { return UIntVal; }
63 const APSInt &getAPSIntVal() const { return APSIntVal; }
64 const APFloat &getAPFloatVal() const { return APFloatVal; }
65
Teresa Johnson63ee0e72018-06-26 13:56:49 +000066 void setIgnoreColonInIdentifiers(bool val) {
67 IgnoreColonInIdentifiers = val;
68 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +000069
Fangrui Song28f69c72018-07-12 02:03:53 +000070 bool Error(LocTy ErrorLoc, const Twine &Msg) const;
Benjamin Kramerc7583112010-09-27 17:42:11 +000071 bool Error(const Twine &Msg) const { return Error(getLoc(), Msg); }
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +000072
73 void Warning(LocTy WarningLoc, const Twine &Msg) const;
74 void Warning(const Twine &Msg) const { return Warning(getLoc(), Msg); }
75
Chris Lattner660c6b92007-11-18 08:46:26 +000076 private:
Chris Lattnerac161bf2009-01-02 07:01:27 +000077 lltok::Kind LexToken();
78
Chris Lattner660c6b92007-11-18 08:46:26 +000079 int getNextChar();
80 void SkipLineComment();
Nick Lewycky2176cdc2011-06-04 18:16:26 +000081 lltok::Kind ReadString(lltok::Kind kind);
82 bool ReadVarName();
83
Chris Lattnerac161bf2009-01-02 07:01:27 +000084 lltok::Kind LexIdentifier();
85 lltok::Kind LexDigitOrNegative();
86 lltok::Kind LexPositive();
87 lltok::Kind LexAt();
David Majnemerdad0a642014-06-27 18:19:56 +000088 lltok::Kind LexDollar();
Chris Lattner1eed2d62009-12-30 04:56:59 +000089 lltok::Kind LexExclaim();
Chris Lattnerac161bf2009-01-02 07:01:27 +000090 lltok::Kind LexPercent();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000091 lltok::Kind LexUIntID(lltok::Kind Token);
David Majnemer1d681aa2014-12-10 00:43:17 +000092 lltok::Kind LexVar(lltok::Kind Var, lltok::Kind VarID);
Chris Lattnerac161bf2009-01-02 07:01:27 +000093 lltok::Kind LexQuote();
94 lltok::Kind Lex0x();
Bill Wendling63b88192013-02-06 06:52:58 +000095 lltok::Kind LexHash();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000096 lltok::Kind LexCaret();
Misha Brukman1d9a93d2009-01-02 22:46:48 +000097
Chris Lattnerac161bf2009-01-02 07:01:27 +000098 uint64_t atoull(const char *Buffer, const char *End);
99 uint64_t HexIntToVal(const char *Buffer, const char *End);
100 void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
Fangrui Song28f69c72018-07-12 02:03:53 +0000101 void FP80HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
Chris Lattner660c6b92007-11-18 08:46:26 +0000102 };
103} // end namespace llvm
104
105#endif