blob: 21deb6e08910bda8e99fbead656615ae06e0edcc [file] [log] [blame]
Chris Lattner8e3a8e02007-11-18 08:46:26 +00001//===- LLLexer.h - Lexer for LLVM Assembly Files ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner8e3a8e02007-11-18 08:46:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_ASMPARSER_LLLEXER_H
15#define LLVM_LIB_ASMPARSER_LLLEXER_H
Chris Lattner8e3a8e02007-11-18 08:46:26 +000016
Chris Lattnerdf986172009-01-02 07:01:27 +000017#include "LLToken.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000018#include "llvm/ADT/APFloat.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000019#include "llvm/ADT/APSInt.h"
Chris Lattnereeb4a842009-07-02 23:08:13 +000020#include "llvm/Support/SourceMgr.h"
Misha Brukman5679d182009-01-02 22:49:28 +000021#include <string>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000022
23namespace llvm {
24 class MemoryBuffer;
Chris Lattnerdf986172009-01-02 07:01:27 +000025 class Type;
Chris Lattner92bcb422009-07-02 22:46:18 +000026 class SMDiagnostic;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000027 class LLVMContext;
Misha Brukman9ea40342009-01-02 22:46:48 +000028
Benjamin Kramer55c06ae2013-09-11 18:05:11 +000029 class LLLexer {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000030 const char *CurPtr;
Rafael Espindola02822392014-08-18 22:28:28 +000031 StringRef CurBuf;
Chris Lattner92bcb422009-07-02 22:46:18 +000032 SMDiagnostic &ErrorInfo;
Chris Lattnereeb4a842009-07-02 23:08:13 +000033 SourceMgr &SM;
Owen Andersonff6c91e2009-07-07 18:44:11 +000034 LLVMContext &Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000035
36 // Information about the current token.
Chris Lattner8e3a8e02007-11-18 08:46:26 +000037 const char *TokStart;
Chris Lattnerdf986172009-01-02 07:01:27 +000038 lltok::Kind CurKind;
39 std::string StrVal;
40 unsigned UIntVal;
Chris Lattner1afcace2011-07-09 17:41:24 +000041 Type *TyVal;
Chris Lattnerdf986172009-01-02 07:01:27 +000042 APFloat APFloatVal;
43 APSInt APSIntVal;
Misha Brukman9ea40342009-01-02 22:46:48 +000044
Teresa Johnsonc6dda902018-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 Lattner8e3a8e02007-11-18 08:46:26 +000049 public:
Rafael Espindola02822392014-08-18 22:28:28 +000050 explicit LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &,
Owen Andersonff6c91e2009-07-07 18:44:11 +000051 LLVMContext &C);
Chris Lattner8e3a8e02007-11-18 08:46:26 +000052
Chris Lattnerdf986172009-01-02 07:01:27 +000053 lltok::Kind Lex() {
54 return CurKind = LexToken();
55 }
56
Chris Lattnereeb4a842009-07-02 23:08:13 +000057 typedef SMLoc LocTy;
58 LocTy getLoc() const { return SMLoc::getFromPointer(TokStart); }
Chris Lattnerdf986172009-01-02 07:01:27 +000059 lltok::Kind getKind() const { return CurKind; }
Chris Lattner73180c52010-04-01 04:53:22 +000060 const std::string &getStrVal() const { return StrVal; }
Chris Lattner1afcace2011-07-09 17:41:24 +000061 Type *getTyVal() const { return TyVal; }
Chris Lattnerdf986172009-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 Johnsonc6dda902018-06-26 13:56:49 +000066 void setIgnoreColonInIdentifiers(bool val) {
67 IgnoreColonInIdentifiers = val;
68 }
Misha Brukman9ea40342009-01-02 22:46:48 +000069
Fangrui Songf831f822018-07-12 02:03:53 +000070 bool Error(LocTy ErrorLoc, const Twine &Msg) const;
Benjamin Kramerd1e17032010-09-27 17:42:11 +000071 bool Error(const Twine &Msg) const { return Error(getLoc(), Msg); }
Saleem Abdulrasoolfa254cb2014-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 Lattner8e3a8e02007-11-18 08:46:26 +000076 private:
Chris Lattnerdf986172009-01-02 07:01:27 +000077 lltok::Kind LexToken();
78
Chris Lattner8e3a8e02007-11-18 08:46:26 +000079 int getNextChar();
80 void SkipLineComment();
Nick Lewycky9fa89332011-06-04 18:16:26 +000081 lltok::Kind ReadString(lltok::Kind kind);
82 bool ReadVarName();
83
Chris Lattnerdf986172009-01-02 07:01:27 +000084 lltok::Kind LexIdentifier();
85 lltok::Kind LexDigitOrNegative();
86 lltok::Kind LexPositive();
87 lltok::Kind LexAt();
David Majnemerc8a11692014-06-27 18:19:56 +000088 lltok::Kind LexDollar();
Chris Lattnere434d272009-12-30 04:56:59 +000089 lltok::Kind LexExclaim();
Chris Lattnerdf986172009-01-02 07:01:27 +000090 lltok::Kind LexPercent();
Teresa Johnsona9a21472018-05-26 02:34:13 +000091 lltok::Kind LexUIntID(lltok::Kind Token);
David Majnemereca84262014-12-10 00:43:17 +000092 lltok::Kind LexVar(lltok::Kind Var, lltok::Kind VarID);
Chris Lattnerdf986172009-01-02 07:01:27 +000093 lltok::Kind LexQuote();
94 lltok::Kind Lex0x();
Bill Wendling95ce4c22013-02-06 06:52:58 +000095 lltok::Kind LexHash();
Teresa Johnsona9a21472018-05-26 02:34:13 +000096 lltok::Kind LexCaret();
Misha Brukman9ea40342009-01-02 22:46:48 +000097
Chris Lattnerdf986172009-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 Songf831f822018-07-12 02:03:53 +0000101 void FP80HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000102 };
103} // end namespace llvm
104
105#endif