[flang] Fix crash in HLFIR generation (#118399)
[llvm-project.git] / lld / ELF / ScriptLexer.h
blobd689a7e108f93e154e6b4e9148bc6247f2381bf7
1 //===- ScriptLexer.h --------------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_SCRIPT_LEXER_H
10 #define LLD_ELF_SCRIPT_LEXER_H
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/MemoryBufferRef.h"
17 #include <vector>
19 namespace lld::elf {
20 struct Ctx;
22 class ScriptLexer {
23 protected:
24 struct Buffer {
25 // The remaining content to parse and the filename.
26 StringRef s, filename;
27 const char *begin = nullptr;
28 size_t lineNumber = 1;
29 // True if the script is opened as an absolute path under the --sysroot
30 // directory.
31 bool isUnderSysroot = false;
33 Buffer() = default;
34 Buffer(Ctx &ctx, MemoryBufferRef mb);
36 Ctx &ctx;
37 // The current buffer and parent buffers due to INCLUDE.
38 Buffer curBuf;
39 SmallVector<Buffer, 0> buffers;
41 // Used to detect INCLUDE() cycles.
42 llvm::DenseSet<StringRef> activeFilenames;
44 struct Token {
45 StringRef str;
46 explicit operator bool() const { return !str.empty(); }
47 operator StringRef() const { return str; }
50 // The token before the last next().
51 StringRef prevTok;
52 // Rules for what is a token are different when we are in an expression.
53 // curTok holds the cached return value of peek() and is invalid when the
54 // expression state changes.
55 StringRef curTok;
56 size_t prevTokLine = 1;
57 // The inExpr state when curTok is cached.
58 bool curTokState = false;
59 bool eof = false;
61 public:
62 explicit ScriptLexer(Ctx &ctx, MemoryBufferRef mb);
64 void setError(const Twine &msg);
65 void lex();
66 StringRef skipSpace(StringRef s);
67 bool atEOF();
68 StringRef next();
69 StringRef peek();
70 void skip();
71 bool consume(StringRef tok);
72 void expect(StringRef expect);
73 Token till(StringRef tok);
74 std::string getCurrentLocation();
75 MemoryBufferRef getCurrentMB();
77 std::vector<MemoryBufferRef> mbs;
78 bool inExpr = false;
80 private:
81 StringRef getLine();
82 size_t getColumnNumber();
85 } // namespace lld::elf
87 #endif