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