1 //
2 // Copyright (c) 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 pp
18 {
19
20 struct PreprocessorImpl
21 {
22 Diagnostics *diagnostics;
23 MacroSet macroSet;
24 Tokenizer tokenizer;
25 DirectiveParser directiveParser;
26 MacroExpander macroExpander;
27
PreprocessorImplpp::PreprocessorImpl28 PreprocessorImpl(Diagnostics *diag,
29 DirectiveHandler *directiveHandler,
30 const PreprocessorSettings &settings)
31 : diagnostics(diag),
32 tokenizer(diag),
33 directiveParser(&tokenizer,
34 ¯oSet,
35 diag,
36 directiveHandler,
37 settings.maxMacroExpansionDepth),
38 macroExpander(&directiveParser, ¯oSet, diag, settings.maxMacroExpansionDepth)
39 {
40 }
41 };
42
Preprocessor(Diagnostics * diagnostics,DirectiveHandler * directiveHandler,const PreprocessorSettings & settings)43 Preprocessor::Preprocessor(Diagnostics *diagnostics,
44 DirectiveHandler *directiveHandler,
45 const PreprocessorSettings &settings)
46 {
47 mImpl = new PreprocessorImpl(diagnostics, directiveHandler, settings);
48 }
49
~Preprocessor()50 Preprocessor::~Preprocessor()
51 {
52 delete mImpl;
53 }
54
init(size_t count,const char * const string[],const int length[])55 bool Preprocessor::init(size_t count, const char *const string[], const int length[])
56 {
57 static const int kDefaultGLSLVersion = 100;
58
59 // Add standard pre-defined macros.
60 predefineMacro("__LINE__", 0);
61 predefineMacro("__FILE__", 0);
62 predefineMacro("__VERSION__", kDefaultGLSLVersion);
63 predefineMacro("GL_ES", 1);
64
65 return mImpl->tokenizer.init(count, string, length);
66 }
67
predefineMacro(const char * name,int value)68 void Preprocessor::predefineMacro(const char *name, int value)
69 {
70 PredefineMacro(&mImpl->macroSet, name, value);
71 }
72
lex(Token * token)73 void Preprocessor::lex(Token *token)
74 {
75 bool validToken = false;
76 while (!validToken)
77 {
78 mImpl->macroExpander.lex(token);
79 switch (token->type)
80 {
81 // We should not be returning internal preprocessing tokens.
82 // Convert preprocessing tokens to compiler tokens or report
83 // diagnostics.
84 case Token::PP_HASH:
85 UNREACHABLE();
86 break;
87 case Token::PP_NUMBER:
88 mImpl->diagnostics->report(Diagnostics::PP_INVALID_NUMBER, token->location,
89 token->text);
90 break;
91 case Token::PP_OTHER:
92 mImpl->diagnostics->report(Diagnostics::PP_INVALID_CHARACTER, token->location,
93 token->text);
94 break;
95 default:
96 validToken = true;
97 break;
98 }
99 }
100 }
101
setMaxTokenSize(size_t maxTokenSize)102 void Preprocessor::setMaxTokenSize(size_t maxTokenSize)
103 {
104 mImpl->tokenizer.setMaxTokenSize(maxTokenSize);
105 }
106
107 } // namespace pp
108