blob: ef61d47b2aa5bd084eee51b40f681512cefdbc3d [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman66b8ab22014-05-06 15:57:45 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman66b8ab22014-05-06 15:57:45 -040014
Nicolas Capenscc863da2015-01-21 15:50:55 -050015#include "DirectiveHandler.h"
John Bauman66b8ab22014-05-06 15:57:45 -040016
17#include <sstream>
18
Nicolas Capenscc863da2015-01-21 15:50:55 -050019#include "debug.h"
20#include "Diagnostics.h"
John Bauman66b8ab22014-05-06 15:57:45 -040021
22static TBehavior getBehavior(const std::string& str)
23{
Nicolas Capens0bac2852016-05-07 06:09:58 -040024 static const std::string kRequire("require");
25 static const std::string kEnable("enable");
26 static const std::string kDisable("disable");
27 static const std::string kWarn("warn");
John Bauman66b8ab22014-05-06 15:57:45 -040028
Nicolas Capens0bac2852016-05-07 06:09:58 -040029 if (str == kRequire) return EBhRequire;
30 else if (str == kEnable) return EBhEnable;
31 else if (str == kDisable) return EBhDisable;
32 else if (str == kWarn) return EBhWarn;
33 return EBhUndefined;
John Bauman66b8ab22014-05-06 15:57:45 -040034}
35
36TDirectiveHandler::TDirectiveHandler(TExtensionBehavior& extBehavior,
Nicolas Capensc6841852015-02-15 14:25:37 -050037 TDiagnostics& diagnostics,
38 int& shaderVersion)
Nicolas Capens0bac2852016-05-07 06:09:58 -040039 : mExtensionBehavior(extBehavior),
40 mDiagnostics(diagnostics),
41 mShaderVersion(shaderVersion)
John Bauman66b8ab22014-05-06 15:57:45 -040042{
43}
44
45TDirectiveHandler::~TDirectiveHandler()
46{
47}
48
49void TDirectiveHandler::handleError(const pp::SourceLocation& loc,
50 const std::string& msg)
51{
Nicolas Capens0bac2852016-05-07 06:09:58 -040052 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc, msg, "", "");
John Bauman66b8ab22014-05-06 15:57:45 -040053}
54
55void TDirectiveHandler::handlePragma(const pp::SourceLocation& loc,
56 const std::string& name,
Alexis Hetue13238e2017-12-15 18:01:07 -050057 const std::string& value,
58 bool stdgl)
John Bauman66b8ab22014-05-06 15:57:45 -040059{
Nicolas Capens0bac2852016-05-07 06:09:58 -040060 static const std::string kSTDGL("STDGL");
61 static const std::string kOptimize("optimize");
62 static const std::string kDebug("debug");
63 static const std::string kOn("on");
64 static const std::string kOff("off");
John Bauman66b8ab22014-05-06 15:57:45 -040065
Nicolas Capens0bac2852016-05-07 06:09:58 -040066 bool invalidValue = false;
Alexis Hetue13238e2017-12-15 18:01:07 -050067 if (stdgl || (name == kSTDGL))
Nicolas Capens0bac2852016-05-07 06:09:58 -040068 {
69 // The STDGL pragma is used to reserve pragmas for use by future
70 // revisions of GLSL. Ignore it.
71 return;
72 }
73 else if (name == kOptimize)
74 {
75 if (value == kOn) mPragma.optimize = true;
76 else if (value == kOff) mPragma.optimize = false;
77 else invalidValue = true;
78 }
79 else if (name == kDebug)
80 {
81 if (value == kOn) mPragma.debug = true;
82 else if (value == kOff) mPragma.debug = false;
83 else invalidValue = true;
84 }
85 else
86 {
Alexis Hetue13238e2017-12-15 18:01:07 -050087 mDiagnostics.report(pp::Diagnostics::PP_UNRECOGNIZED_PRAGMA, loc, name);
Nicolas Capens0bac2852016-05-07 06:09:58 -040088 return;
89 }
John Bauman66b8ab22014-05-06 15:57:45 -040090
Nicolas Capens0bac2852016-05-07 06:09:58 -040091 if (invalidValue)
92 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
93 "invalid pragma value", value,
94 "'on' or 'off' expected");
John Bauman66b8ab22014-05-06 15:57:45 -040095}
96
97void TDirectiveHandler::handleExtension(const pp::SourceLocation& loc,
98 const std::string& name,
99 const std::string& behavior)
100{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400101 static const std::string kExtAll("all");
John Bauman66b8ab22014-05-06 15:57:45 -0400102
Nicolas Capens0bac2852016-05-07 06:09:58 -0400103 TBehavior behaviorVal = getBehavior(behavior);
104 if (behaviorVal == EBhUndefined)
105 {
106 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
107 "behavior", name, "invalid");
108 return;
109 }
John Bauman66b8ab22014-05-06 15:57:45 -0400110
Nicolas Capens0bac2852016-05-07 06:09:58 -0400111 if (name == kExtAll)
112 {
113 if (behaviorVal == EBhRequire)
114 {
115 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
116 "extension", name,
117 "cannot have 'require' behavior");
118 }
119 else if (behaviorVal == EBhEnable)
120 {
121 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
122 "extension", name,
123 "cannot have 'enable' behavior");
124 }
125 else
126 {
127 for (TExtensionBehavior::iterator iter = mExtensionBehavior.begin();
128 iter != mExtensionBehavior.end(); ++iter)
129 iter->second = behaviorVal;
130 }
131 return;
132 }
John Bauman66b8ab22014-05-06 15:57:45 -0400133
Nicolas Capens0bac2852016-05-07 06:09:58 -0400134 TExtensionBehavior::iterator iter = mExtensionBehavior.find(name);
135 if (iter != mExtensionBehavior.end())
136 {
137 iter->second = behaviorVal;
138 return;
139 }
John Bauman66b8ab22014-05-06 15:57:45 -0400140
Nicolas Capens0bac2852016-05-07 06:09:58 -0400141 pp::Diagnostics::Severity severity = pp::Diagnostics::PP_ERROR;
142 switch (behaviorVal) {
143 case EBhRequire:
144 severity = pp::Diagnostics::PP_ERROR;
145 break;
146 case EBhEnable:
147 case EBhWarn:
148 case EBhDisable:
149 severity = pp::Diagnostics::PP_WARNING;
150 break;
151 default:
152 UNREACHABLE(behaviorVal);
153 break;
154 }
155 mDiagnostics.writeInfo(severity, loc,
156 "extension", name, "is not supported");
John Bauman66b8ab22014-05-06 15:57:45 -0400157}
158
159void TDirectiveHandler::handleVersion(const pp::SourceLocation& loc,
160 int version)
161{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400162 if (version == 100 ||
163 version == 300)
164 {
165 mShaderVersion = version;
166 }
167 else
168 {
169 std::stringstream stream;
170 stream << version;
171 std::string str = stream.str();
172 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, loc,
173 "version number", str, "not supported");
174 }
John Bauman66b8ab22014-05-06 15:57:45 -0400175}