Path: blob/main_old/src/compiler/preprocessor/Preprocessor.cpp
1693 views
//1// Copyright 2011 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "compiler/preprocessor/Preprocessor.h"78#include "common/debug.h"9#include "compiler/preprocessor/DiagnosticsBase.h"10#include "compiler/preprocessor/DirectiveParser.h"11#include "compiler/preprocessor/Macro.h"12#include "compiler/preprocessor/MacroExpander.h"13#include "compiler/preprocessor/Token.h"14#include "compiler/preprocessor/Tokenizer.h"1516namespace angle17{1819namespace pp20{2122struct PreprocessorImpl23{24Diagnostics *diagnostics;25MacroSet macroSet;26Tokenizer tokenizer;27DirectiveParser directiveParser;28MacroExpander macroExpander;2930PreprocessorImpl(Diagnostics *diag,31DirectiveHandler *directiveHandler,32const PreprocessorSettings &settings)33: diagnostics(diag),34tokenizer(diag),35directiveParser(&tokenizer, ¯oSet, diag, directiveHandler, settings),36macroExpander(&directiveParser, ¯oSet, diag, settings, false)37{}38};3940Preprocessor::Preprocessor(Diagnostics *diagnostics,41DirectiveHandler *directiveHandler,42const PreprocessorSettings &settings)43{44mImpl = new PreprocessorImpl(diagnostics, directiveHandler, settings);45}4647Preprocessor::~Preprocessor()48{49delete mImpl;50}5152bool Preprocessor::init(size_t count, const char *const string[], const int length[])53{54static const int kDefaultGLSLVersion = 100;5556// Add standard pre-defined macros.57predefineMacro("__LINE__", 0);58predefineMacro("__FILE__", 0);59predefineMacro("__VERSION__", kDefaultGLSLVersion);60predefineMacro("GL_ES", 1);6162return mImpl->tokenizer.init(count, string, length);63}6465void Preprocessor::predefineMacro(const char *name, int value)66{67PredefineMacro(&mImpl->macroSet, name, value);68}6970void Preprocessor::lex(Token *token)71{72bool validToken = false;73while (!validToken)74{75mImpl->macroExpander.lex(token);76switch (token->type)77{78// We should not be returning internal preprocessing tokens.79// Convert preprocessing tokens to compiler tokens or report80// diagnostics.81case Token::PP_HASH:82UNREACHABLE();83break;84case Token::PP_NUMBER:85mImpl->diagnostics->report(Diagnostics::PP_INVALID_NUMBER, token->location,86token->text);87break;88case Token::PP_OTHER:89mImpl->diagnostics->report(Diagnostics::PP_INVALID_CHARACTER, token->location,90token->text);91break;92default:93validToken = true;94break;95}96}97}9899void Preprocessor::setMaxTokenSize(size_t maxTokenSize)100{101mImpl->tokenizer.setMaxTokenSize(maxTokenSize);102}103104} // namespace pp105106} // namespace angle107108109