ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_IRGENERATOR |
| 9 | #define SKSL_IRGENERATOR |
| 10 | |
John Stiles | ddefaee | 2020-08-11 15:13:26 -0400 | [diff] [blame] | 11 | #include <unordered_map> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 12 | #include <unordered_set> |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 13 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLASTFile.h" |
| 15 | #include "src/sksl/SkSLASTNode.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLErrorReporter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLBlock.h" |
| 18 | #include "src/sksl/ir/SkSLExpression.h" |
| 19 | #include "src/sksl/ir/SkSLExtension.h" |
| 20 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 21 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 22 | #include "src/sksl/ir/SkSLModifiers.h" |
| 23 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 24 | #include "src/sksl/ir/SkSLProgram.h" |
| 25 | #include "src/sksl/ir/SkSLSection.h" |
| 26 | #include "src/sksl/ir/SkSLStatement.h" |
| 27 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 28 | #include "src/sksl/ir/SkSLType.h" |
| 29 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 30 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 31 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 32 | |
| 33 | namespace SkSL { |
| 34 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 35 | namespace dsl { |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 36 | class DSLCore; |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 37 | class DSLFunction; |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 38 | class DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 39 | class DSLWriter; |
| 40 | } |
| 41 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 42 | class ExternalFunction; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 43 | class ExternalValue; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 44 | class FunctionCall; |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 45 | class StructDefinition; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 46 | struct ParsedModule; |
Ethan Nicholas | cb0f409 | 2019-04-19 11:26:50 -0400 | [diff] [blame] | 47 | struct Swizzle; |
| 48 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | /** |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 50 | * Intrinsics are passed between the Compiler and the IRGenerator using IRIntrinsicMaps. |
| 51 | */ |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 52 | class IRIntrinsicMap { |
| 53 | public: |
| 54 | IRIntrinsicMap(IRIntrinsicMap* parent) : fParent(parent) {} |
| 55 | |
| 56 | void insertOrDie(String key, std::unique_ptr<ProgramElement> element) { |
| 57 | SkASSERT(fIntrinsics.find(key) == fIntrinsics.end()); |
| 58 | fIntrinsics[key] = Intrinsic{std::move(element), false}; |
| 59 | } |
| 60 | |
Brian Osman | afa18ee | 2020-10-07 17:47:45 -0400 | [diff] [blame] | 61 | const ProgramElement* find(const String& key) { |
| 62 | auto iter = fIntrinsics.find(key); |
| 63 | if (iter == fIntrinsics.end()) { |
| 64 | return fParent ? fParent->find(key) : nullptr; |
| 65 | } |
| 66 | return iter->second.fIntrinsic.get(); |
| 67 | } |
| 68 | |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 69 | // Only returns an intrinsic that isn't already marked as included, and then marks it. |
Brian Osman | 00a8b5b | 2020-10-02 09:06:04 -0400 | [diff] [blame] | 70 | const ProgramElement* findAndInclude(const String& key) { |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 71 | auto iter = fIntrinsics.find(key); |
| 72 | if (iter == fIntrinsics.end()) { |
| 73 | return fParent ? fParent->findAndInclude(key) : nullptr; |
| 74 | } |
| 75 | if (iter->second.fAlreadyIncluded) { |
| 76 | return nullptr; |
| 77 | } |
| 78 | iter->second.fAlreadyIncluded = true; |
| 79 | return iter->second.fIntrinsic.get(); |
| 80 | } |
| 81 | |
| 82 | void resetAlreadyIncluded() { |
| 83 | for (auto& pair : fIntrinsics) { |
| 84 | pair.second.fAlreadyIncluded = false; |
| 85 | } |
| 86 | if (fParent) { |
| 87 | fParent->resetAlreadyIncluded(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | struct Intrinsic { |
| 93 | std::unique_ptr<ProgramElement> fIntrinsic; |
| 94 | bool fAlreadyIncluded = false; |
| 95 | }; |
| 96 | |
| 97 | std::unordered_map<String, Intrinsic> fIntrinsics; |
| 98 | IRIntrinsicMap* fParent = nullptr; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 99 | }; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 100 | |
| 101 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 102 | * Performs semantic analysis on an abstract syntax tree (AST) and produces the corresponding |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 103 | * (unoptimized) intermediate representation (IR). |
| 104 | */ |
| 105 | class IRGenerator { |
| 106 | public: |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 107 | IRGenerator(const Context* context, |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 108 | const ShaderCapsClass* caps); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 109 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 110 | struct IRBundle { |
| 111 | std::vector<std::unique_ptr<ProgramElement>> fElements; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 112 | std::vector<const ProgramElement*> fSharedElements; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 113 | std::unique_ptr<ModifiersPool> fModifiers; |
| 114 | std::shared_ptr<SymbolTable> fSymbolTable; |
| 115 | Program::Inputs fInputs; |
| 116 | }; |
| 117 | |
| 118 | /** |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 119 | * If externalFuncs is supplied, those values are registered in the symbol table of the |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 120 | * Program, but ownership is *not* transferred. It is up to the caller to keep them alive. |
| 121 | */ |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 122 | IRBundle convertProgram( |
| 123 | Program::Kind kind, |
| 124 | const Program::Settings* settings, |
| 125 | const ParsedModule& base, |
| 126 | bool isBuiltinCode, |
| 127 | const char* text, |
| 128 | size_t length, |
| 129 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 131 | // both of these functions return null and report an error if the setting does not exist |
| 132 | const Type* typeForSetting(int offset, String name) const; |
| 133 | std::unique_ptr<Expression> valueForSetting(int offset, String name) const; |
| 134 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 135 | const Program::Settings* settings() const { return fSettings; } |
| 136 | |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 137 | ErrorReporter& errorReporter() const { return fContext.fErrors; } |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 138 | |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 139 | std::shared_ptr<SymbolTable>& symbolTable() { |
| 140 | return fSymbolTable; |
| 141 | } |
| 142 | |
| 143 | void setSymbolTable(std::shared_ptr<SymbolTable>& symbolTable) { |
| 144 | fSymbolTable = symbolTable; |
| 145 | } |
| 146 | |
| 147 | void pushSymbolTable(); |
| 148 | void popSymbolTable(); |
| 149 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 150 | const Context& fContext; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 151 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | private: |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 153 | /** |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 154 | * Relinquishes ownership of the Modifiers that have been collected so far and returns them. |
| 155 | */ |
| 156 | std::unique_ptr<ModifiersPool> releaseModifiers(); |
| 157 | |
Ethan Nicholas | 63d7ee3 | 2020-08-17 10:57:12 -0400 | [diff] [blame] | 158 | void checkModifiers(int offset, const Modifiers& modifiers, int permitted); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 159 | void checkVarDeclaration(int offset, const Modifiers& modifiers, const Type* baseType, |
Ethan Nicholas | 489e552 | 2021-01-20 10:53:11 -0500 | [diff] [blame] | 160 | Variable::Storage storage); |
| 161 | std::unique_ptr<Statement> convertVarDeclaration(int offset, const Modifiers& modifiers, |
| 162 | const Type* baseType, StringFragment name, |
| 163 | bool isArray, |
| 164 | std::unique_ptr<Expression> arraySize, |
| 165 | std::unique_ptr<Expression> value, |
| 166 | Variable::Storage storage); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 167 | StatementArray convertVarDeclarations(const ASTNode& decl, Variable::Storage storage); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 168 | void convertFunction(const ASTNode& f); |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 169 | std::unique_ptr<Statement> convertSingleStatement(const ASTNode& statement); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 170 | std::unique_ptr<Statement> convertStatement(const ASTNode& statement); |
| 171 | std::unique_ptr<Expression> convertExpression(const ASTNode& expression); |
| 172 | std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(const ASTNode& m); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 173 | |
Brian Osman | d807039 | 2020-09-09 15:50:02 -0400 | [diff] [blame] | 174 | const Type* convertType(const ASTNode& type, bool allowVoid = false); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 175 | std::unique_ptr<Expression> call(int offset, |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 176 | std::unique_ptr<Expression> function, |
| 177 | ExpressionArray arguments); |
| 178 | std::unique_ptr<Expression> call(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 179 | const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 180 | ExpressionArray arguments); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 181 | CoercionCost callCost(const FunctionDeclaration& function, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 182 | const ExpressionArray& arguments); |
Ethan Nicholas | dcd2f86 | 2020-12-17 23:24:25 +0000 | [diff] [blame] | 183 | std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 184 | CoercionCost coercionCost(const Expression& expr, const Type& type); |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame^] | 185 | int convertArraySize(const Type& type, int offset, const ASTNode& s); |
| 186 | int convertArraySize(const Type& type, std::unique_ptr<Expression> s); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 187 | bool containsConstantZero(Expression& expr); |
| 188 | bool dividesByZero(Token::Kind op, Expression& right); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 189 | std::unique_ptr<Expression> convertBinaryExpression(std::unique_ptr<Expression> left, |
| 190 | Token::Kind op, |
| 191 | std::unique_ptr<Expression> right); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 192 | std::unique_ptr<Block> convertBlock(const ASTNode& block); |
| 193 | std::unique_ptr<Statement> convertBreak(const ASTNode& b); |
John Stiles | 248f57b | 2021-02-03 15:11:18 -0500 | [diff] [blame] | 194 | std::unique_ptr<Expression> convertArrayConstructor(int offset, |
| 195 | const Type& type, |
| 196 | ExpressionArray args); |
John Stiles | 53f0ddf | 2021-01-05 18:47:09 -0500 | [diff] [blame] | 197 | std::unique_ptr<Expression> convertScalarConstructor(int offset, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 198 | const Type& type, |
| 199 | ExpressionArray params); |
| 200 | std::unique_ptr<Expression> convertCompoundConstructor(int offset, |
| 201 | const Type& type, |
| 202 | ExpressionArray params); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 203 | std::unique_ptr<Expression> convertConstructor(int offset, |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 204 | const Type& type, |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 205 | ExpressionArray params); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 206 | std::unique_ptr<Statement> convertContinue(const ASTNode& c); |
| 207 | std::unique_ptr<Statement> convertDiscard(const ASTNode& d); |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 208 | std::unique_ptr<Statement> convertDo(std::unique_ptr<Statement> stmt, |
| 209 | std::unique_ptr<Expression> test); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 210 | std::unique_ptr<Statement> convertDo(const ASTNode& d); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 211 | std::unique_ptr<Statement> convertSwitch(int offset, |
| 212 | bool isStatic, |
| 213 | std::unique_ptr<Expression> value, |
| 214 | ExpressionArray caseValues, |
| 215 | SkTArray<StatementArray> caseStatements, |
| 216 | std::shared_ptr<SymbolTable> symbolTable); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 217 | std::unique_ptr<Statement> convertSwitch(const ASTNode& s); |
| 218 | std::unique_ptr<Expression> convertBinaryExpression(const ASTNode& expression); |
| 219 | std::unique_ptr<Extension> convertExtension(int offset, StringFragment name); |
| 220 | std::unique_ptr<Statement> convertExpressionStatement(const ASTNode& s); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 221 | std::unique_ptr<Expression> convertField(std::unique_ptr<Expression> base, |
| 222 | StringFragment field); |
Ethan Nicholas | 2ed0d94 | 2021-01-20 07:51:23 -0500 | [diff] [blame] | 223 | std::unique_ptr<Statement> convertFor(int offset, |
| 224 | std::unique_ptr<Statement> initializer, |
| 225 | std::unique_ptr<Expression> test, |
| 226 | std::unique_ptr<Expression> next, |
| 227 | std::unique_ptr<Statement> statement); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 228 | std::unique_ptr<Statement> convertFor(const ASTNode& f); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 229 | std::unique_ptr<Expression> convertIdentifier(int offset, StringFragment identifier); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 230 | std::unique_ptr<Expression> convertIdentifier(const ASTNode& identifier); |
| 231 | std::unique_ptr<Statement> convertIf(const ASTNode& s); |
John Stiles | 5ede6e3 | 2021-01-11 13:00:49 -0500 | [diff] [blame] | 232 | std::unique_ptr<Statement> convertIf(int offset, bool isStatic, |
| 233 | std::unique_ptr<Expression> test, |
| 234 | std::unique_ptr<Statement> ifTrue, |
| 235 | std::unique_ptr<Statement> ifFalse); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 236 | std::unique_ptr<InterfaceBlock> convertInterfaceBlock(const ASTNode& s); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 237 | Modifiers convertModifiers(const Modifiers& m); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 238 | std::unique_ptr<Expression> convertPrefixExpression(const ASTNode& expression); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 239 | std::unique_ptr<Statement> convertReturn(int offset, std::unique_ptr<Expression> result); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 240 | std::unique_ptr<Statement> convertReturn(const ASTNode& r); |
| 241 | std::unique_ptr<Section> convertSection(const ASTNode& e); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 242 | std::unique_ptr<Expression> convertCallExpression(const ASTNode& expression); |
| 243 | std::unique_ptr<Expression> convertFieldExpression(const ASTNode& expression); |
| 244 | std::unique_ptr<Expression> convertIndexExpression(const ASTNode& expression); |
John Stiles | 1b27c3d | 2020-12-07 12:14:55 -0500 | [diff] [blame] | 245 | std::unique_ptr<Expression> convertIndex(std::unique_ptr<Expression> base, |
Ethan Nicholas | 4d2bbbb | 2021-01-12 11:56:23 -0500 | [diff] [blame] | 246 | std::unique_ptr<Expression> index); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 247 | std::unique_ptr<Expression> convertPostfixExpression(std::unique_ptr<Expression> base, |
| 248 | Token::Kind op); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 249 | std::unique_ptr<Expression> convertPostfixExpression(const ASTNode& expression); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 250 | std::unique_ptr<Expression> convertPrefixExpression(Token::Kind op, |
| 251 | std::unique_ptr<Expression> base); |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 252 | std::unique_ptr<Expression> convertScopeExpression(const ASTNode& expression); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 253 | std::unique_ptr<StructDefinition> convertStructDefinition(const ASTNode& expression); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 254 | std::unique_ptr<Expression> convertTypeField(int offset, const Type& type, |
| 255 | StringFragment field); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 256 | std::unique_ptr<Expression> convertSwizzle(std::unique_ptr<Expression> base, String fields); |
| 257 | std::unique_ptr<Expression> convertTernaryExpression(std::unique_ptr<Expression> test, |
| 258 | std::unique_ptr<Expression> ifTrue, |
| 259 | std::unique_ptr<Expression> ifFalse); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 260 | std::unique_ptr<Expression> convertTernaryExpression(const ASTNode& expression); |
| 261 | std::unique_ptr<Statement> convertVarDeclarationStatement(const ASTNode& s); |
Ethan Nicholas | 9ead3df | 2021-01-06 12:10:48 -0500 | [diff] [blame] | 262 | std::unique_ptr<Statement> convertWhile(int offset, std::unique_ptr<Expression> test, |
| 263 | std::unique_ptr<Statement> statement); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 264 | std::unique_ptr<Statement> convertWhile(const ASTNode& w); |
John Stiles | 7bd7033 | 2020-11-30 17:04:09 -0500 | [diff] [blame] | 265 | void convertGlobalVarDeclarations(const ASTNode& decl); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 266 | void convertEnum(const ASTNode& e); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 267 | std::unique_ptr<Block> applyInvocationIDWorkaround(std::unique_ptr<Block> main); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 268 | // returns a statement which converts sk_Position from device to normalized coordinates |
| 269 | std::unique_ptr<Statement> getNormalizeSkPositionCode(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 270 | |
| 271 | void checkValid(const Expression& expr); |
John Stiles | b4b627e | 2020-11-13 15:55:27 -0500 | [diff] [blame] | 272 | bool typeContainsPrivateFields(const Type& type); |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 273 | bool setRefKind(Expression& expr, VariableReference::RefKind kind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 274 | void copyIntrinsicIfNeeded(const FunctionDeclaration& function); |
Brian Osman | 9496fe5 | 2020-11-18 14:48:19 -0500 | [diff] [blame] | 275 | void findAndDeclareBuiltinVariables(); |
John Stiles | e3a91cf | 2021-01-26 10:13:58 -0500 | [diff] [blame] | 276 | bool detectVarDeclarationWithoutScope(const Statement& stmt); |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 277 | // Coerces returns to correct type and detects invalid break / continue placement |
| 278 | void finalizeFunction(FunctionDefinition& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 279 | |
Brian Osman | 818fd6d | 2020-12-30 15:06:22 -0500 | [diff] [blame] | 280 | // Runtime effects (and the interpreter, which uses the same CPU runtime) require adherence to |
| 281 | // the strict rules from The OpenGL ES Shading Language Version 1.00. (Including Appendix A). |
| 282 | bool strictES2Mode() const { |
| 283 | return fKind == Program::kRuntimeEffect_Kind || fKind == Program::kGeneric_Kind; |
| 284 | } |
| 285 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 286 | Program::Inputs fInputs; |
| 287 | const Program::Settings* fSettings = nullptr; |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 288 | const ShaderCapsClass* fCaps = nullptr; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 289 | Program::Kind fKind; |
| 290 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 291 | std::unique_ptr<ASTFile> fFile; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 292 | std::unordered_map<String, Program::Settings::Value> fCapsMap; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 293 | std::shared_ptr<SymbolTable> fSymbolTable = nullptr; |
Ethan Nicholas | 70728ef | 2020-05-28 07:09:00 -0400 | [diff] [blame] | 294 | // additional statements that need to be inserted before the one that convertStatement is |
| 295 | // currently working on |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 296 | StatementArray fExtraStatements; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 297 | // Symbols which have definitions in the include files. |
| 298 | IRIntrinsicMap* fIntrinsics = nullptr; |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 299 | std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics; |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 300 | int fInvocations; |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame] | 301 | std::unordered_set<const Type*> fDefinedStructs; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 302 | std::vector<std::unique_ptr<ProgramElement>>* fProgramElements = nullptr; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 303 | std::vector<const ProgramElement*>* fSharedElements = nullptr; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 304 | const Variable* fRTAdjust = nullptr; |
| 305 | const Variable* fRTAdjustInterfaceBlock = nullptr; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 306 | int fRTAdjustFieldIndex; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 307 | bool fCanInline = true; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 308 | // true if we are currently processing one of the built-in SkSL include files |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 309 | bool fIsBuiltinCode = false; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 310 | std::unique_ptr<ModifiersPool> fModifiers; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 311 | |
| 312 | friend class AutoSymbolTable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 313 | friend class AutoLoopLevel; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 314 | friend class AutoSwitchLevel; |
John Stiles | d1c4dac | 2020-08-11 18:50:50 -0400 | [diff] [blame] | 315 | friend class AutoDisableInline; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 316 | friend class Compiler; |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 317 | friend class dsl::DSLCore; |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 318 | friend class dsl::DSLFunction; |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 319 | friend class dsl::DSLVar; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 320 | friend class dsl::DSLWriter; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 323 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 324 | |
| 325 | #endif |