1 //
2 // Copyright 2011 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "compiler/preprocessor/Preprocessor.h"
8
9 #include "common/debug.h"
10 #include "compiler/preprocessor/DiagnosticsBase.h"
11 #include "compiler/preprocessor/DirectiveParser.h"
12 #include "compiler/preprocessor/Macro.h"
13 #include "compiler/preprocessor/MacroExpander.h"
14 #include "compiler/preprocessor/Token.h"
15 #include "compiler/preprocessor/Tokenizer.h"
16
17 namespace angle
18 {
19
20 namespace pp
21 {
22
23 struct PreprocessorImpl
24 {
25 Diagnostics *diagnostics;
26 MacroSet macroSet;
27 Tokenizer tokenizer;
28 DirectiveParser directiveParser;
29 MacroExpander macroExpander;
30
PreprocessorImplangle::pp::PreprocessorImpl31 PreprocessorImpl(Diagnostics *diag,
32 DirectiveHandler *directiveHandler,
33 const PreprocessorSettings &settings)
34 : diagnostics(diag),
35 tokenizer(diag),
36 directiveParser(&tokenizer, ¯oSet, diag, directiveHandler, settings),
37 macroExpander(&directiveParser, ¯oSet, diag, settings, false)
38 {}
39 };
40
Preprocessor(Diagnostics * diagnostics,DirectiveHandler * directiveHandler,const PreprocessorSettings & settings)41 Preprocessor::Preprocessor(Diagnostics *diagnostics,
42 DirectiveHandler *directiveHandler,
43 const PreprocessorSettings &settings)
44 {
45 mImpl = new PreprocessorImpl(diagnostics, directiveHandler, settings);
46 }
47
~Preprocessor()48 Preprocessor::~Preprocessor()
49 {
50 delete mImpl;
51 }
52
init(size_t count,const char * const string[],const int length[])53 bool Preprocessor::init(size_t count, const char *const string[], const int length[])
54 {
55 // Add standard pre-defined macros.
56 predefineMacro("__LINE__", 0);
57 predefineMacro("__FILE__", 0);
58 predefineMacro("GL_ES", 1);
59
60 return mImpl->tokenizer.init(count, string, length);
61 }
62
predefineMacro(const char * name,int value)63 void Preprocessor::predefineMacro(const char *name, int value)
64 {
65 PredefineMacro(&mImpl->macroSet, name, value);
66 }
67
lex(Token * token)68 void Preprocessor::lex(Token *token)
69 {
70 bool validToken = false;
71 while (!validToken)
72 {
73 mImpl->macroExpander.lex(token);
74 switch (token->type)
75 {
76 // We should not be returning internal preprocessing tokens.
77 // Convert preprocessing tokens to compiler tokens or report
78 // diagnostics.
79 case Token::PP_HASH:
80 UNREACHABLE();
81 break;
82 case Token::PP_NUMBER:
83 mImpl->diagnostics->report(Diagnostics::PP_INVALID_NUMBER, token->location,
84 token->text);
85 break;
86 case Token::PP_OTHER:
87 mImpl->diagnostics->report(Diagnostics::PP_INVALID_CHARACTER, token->location,
88 token->text);
89 break;
90 default:
91 validToken = true;
92 break;
93 }
94 }
95 }
96
setMaxTokenSize(size_t maxTokenSize)97 void Preprocessor::setMaxTokenSize(size_t maxTokenSize)
98 {
99 mImpl->tokenizer.setMaxTokenSize(maxTokenSize);
100 }
101
102 } // namespace pp
103
104 } // namespace angle
105