-
Notifications
You must be signed in to change notification settings - Fork 10
/
Parser.h
152 lines (117 loc) · 4.66 KB
/
Parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef CCONS_PARSER_H
#define CCONS_PARSER_H
//
// Parser is used to invoke the clang libraries to perform actual parsing of
// the input received in the Console.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include <string>
#include <vector>
#include <llvm/ADT/OwningPtr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/LangOptions.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Lex/HeaderSearchOptions.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/ModuleLoader.h>
namespace clang {
class ASTConsumer;
class ASTContext;
class DiagnosticsEngine;
class FileSystemOptions;
class FunctionDecl;
class Preprocessor;
class PPCallbacks;
class SourceManager;
class TargetInfo;
class Token;
} // namespace clang
namespace ccons {
//
// ParseOperation
//
class ParseOperation : public clang::ModuleLoader {
public:
ParseOperation(const clang::LangOptions& options,
clang::TargetOptions* targetOptions,
clang::DiagnosticsEngine *engine,
clang::PPCallbacks *callbacks = 0);
virtual ~ParseOperation();
clang::ASTContext * getASTContext() const;
clang::Preprocessor * getPreprocessor() const;
clang::SourceManager * getSourceManager() const;
clang::TargetInfo * getTargetInfo() const;
virtual clang::ModuleLoadResult loadModule(clang::SourceLocation ImportLoc,
clang::ModuleIdPath Path,
clang::Module::NameVisibilityKind Visibility,
bool IsInclusionDirective);
virtual void makeModuleVisible(clang::Module *Mod,
clang::Module::NameVisibilityKind Visibility,
clang::SourceLocation ImportLoc,
bool Complain);
private:
clang::LangOptions _langOpts;
llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> _hsOptions;
llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> _ppOptions;
llvm::OwningPtr<clang::FileSystemOptions> _fsOpts;
llvm::OwningPtr<clang::FileManager> _fm;
llvm::OwningPtr<clang::SourceManager> _sm;
llvm::OwningPtr<clang::HeaderSearch> _hs;
llvm::OwningPtr<clang::Preprocessor> _pp;
llvm::OwningPtr<clang::ASTContext> _ast;
llvm::OwningPtr<clang::TargetInfo> _target;
};
//
// Parser
//
class Parser {
public:
Parser(const clang::LangOptions& options, clang::TargetOptions* targetOptions);
~Parser();
enum InputType { Incomplete, TopLevel, Stmt };
// Analyze the specified input to determine whether its complete or not.
InputType analyzeInput(const std::string& contextSource,
const std::string& buffer,
int& indentLevel,
std::vector<clang::FunctionDecl*> *fds);
// Create a new ParseOperation that the caller should take ownership of
// and the lifetime of which must be shorter than of the Parser.
ParseOperation * createParseOperation(clang::DiagnosticsEngine *engine,
clang::PPCallbacks *callbacks = 0);
// Parse the specified source code with the specified parse operation
// and consumer. Upon parsing, ownership of parseOp is transferred to
// the Parser permanently.
void parse(const std::string& src,
ParseOperation *parseOp,
clang::ASTConsumer *consumer);
// Parse by implicitely creating a ParseOperation. Equivalent to
// parse(src, createParseOperation(diag), consumer).
void parse(const std::string& src,
clang::DiagnosticsEngine *engine,
clang::ASTConsumer *consumer);
// Returns the last parse operation or NULL if there isn't one.
ParseOperation * getLastParseOperation() const;
// Release any accumulated parse operations (including their resulting
// ASTs and other clang data structures).
void releaseAccumulatedParseOperations();
private:
const clang::LangOptions& _options;
clang::TargetOptions* _targetOptions;
std::vector<ParseOperation*> _ops;
int analyzeTokens(clang::Preprocessor& PP,
const llvm::MemoryBuffer *MemBuf,
clang::Token& LastTok,
int& IndentLevel,
bool& TokWasDo);
static llvm::MemoryBuffer * createMemoryBuffer(const std::string& src,
const char *name,
clang::SourceManager *sm);
};
} // namespace ccons
#endif // CCONS_PARSER_H