2 // Copyright (c) 2012-2013 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.
7 #include "compiler/translator/DirectiveHandler.h"
11 #include "compiler/translator/compilerdebug.h"
12 #include "compiler/translator/Diagnostics.h"
14 static TBehavior getBehavior(const std::string& str)
16 const char kRequire[] = "require";
17 const char kEnable[] = "enable";
18 const char kDisable[] = "disable";
19 const char kWarn[] = "warn";
21 if (str == kRequire) return EBhRequire;
22 else if (str == kEnable) return EBhEnable;
23 else if (str == kDisable) return EBhDisable;
24 else if (str == kWarn) return EBhWarn;
28 TDirectiveHandler::TDirectiveHandler(TExtensionBehavior& extBehavior,
29 TDiagnostics& diagnostics,
31 : mExtensionBehavior(extBehavior),
32 mDiagnostics(diagnostics),
33 mShaderVersion(shaderVersion)
37 TDirectiveHandler::~TDirectiveHandler()
41 void TDirectiveHandler::handleError(const pp::SourceLocation& loc,
42 const std::string& msg)
44 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc, msg, "", "");
47 void TDirectiveHandler::handlePragma(const pp::SourceLocation& loc,
48 const std::string& name,
49 const std::string& value,
54 const char kInvariant[] = "invariant";
55 const char kAll[] = "all";
57 if (name == kInvariant && value == kAll)
58 mPragma.stdgl.invariantAll = true;
59 // The STDGL pragma is used to reserve pragmas for use by future
60 // revisions of GLSL. Do not generate an error on unexpected
66 const char kOptimize[] = "optimize";
67 const char kDebug[] = "debug";
68 const char kOn[] = "on";
69 const char kOff[] = "off";
71 bool invalidValue = false;
72 if (name == kOptimize)
74 if (value == kOn) mPragma.optimize = true;
75 else if (value == kOff) mPragma.optimize = false;
76 else invalidValue = true;
78 else if (name == kDebug)
80 if (value == kOn) mPragma.debug = true;
81 else if (value == kOff) mPragma.debug = false;
82 else invalidValue = true;
86 mDiagnostics.report(pp::Diagnostics::PP_UNRECOGNIZED_PRAGMA, loc, name);
92 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
93 "invalid pragma value", value,
94 "'on' or 'off' expected");
99 void TDirectiveHandler::handleExtension(const pp::SourceLocation& loc,
100 const std::string& name,
101 const std::string& behavior)
103 const char kExtAll[] = "all";
105 TBehavior behaviorVal = getBehavior(behavior);
106 if (behaviorVal == EBhUndefined)
108 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
109 "behavior", name, "invalid");
115 if (behaviorVal == EBhRequire)
117 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
119 "cannot have 'require' behavior");
121 else if (behaviorVal == EBhEnable)
123 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
125 "cannot have 'enable' behavior");
129 for (TExtensionBehavior::iterator iter = mExtensionBehavior.begin();
130 iter != mExtensionBehavior.end(); ++iter)
131 iter->second = behaviorVal;
136 TExtensionBehavior::iterator iter = mExtensionBehavior.find(name);
137 if (iter != mExtensionBehavior.end())
139 iter->second = behaviorVal;
143 pp::Diagnostics::Severity severity = pp::Diagnostics::PP_ERROR;
144 switch (behaviorVal) {
146 severity = pp::Diagnostics::PP_ERROR;
151 severity = pp::Diagnostics::PP_WARNING;
157 mDiagnostics.writeInfo(severity, loc,
158 "extension", name, "is not supported");
161 void TDirectiveHandler::handleVersion(const pp::SourceLocation& loc,
164 if (version == 100 ||
167 mShaderVersion = version;
171 std::stringstream stream;
173 std::string str = stream.str();
174 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
175 "version number", str, "not supported");