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 | */ |
| 7 | |
| 8 | #include "SkSLIRGenerator.h" |
| 9 | |
| 10 | #include "limits.h" |
| 11 | |
| 12 | #include "ast/SkSLASTBoolLiteral.h" |
| 13 | #include "ast/SkSLASTFieldSuffix.h" |
| 14 | #include "ast/SkSLASTFloatLiteral.h" |
| 15 | #include "ast/SkSLASTIndexSuffix.h" |
| 16 | #include "ast/SkSLASTIntLiteral.h" |
| 17 | #include "ir/SkSLBinaryExpression.h" |
| 18 | #include "ir/SkSLBoolLiteral.h" |
| 19 | #include "ir/SkSLBreakStatement.h" |
| 20 | #include "ir/SkSLConstructor.h" |
| 21 | #include "ir/SkSLContinueStatement.h" |
| 22 | #include "ir/SkSLDiscardStatement.h" |
| 23 | #include "ir/SkSLDoStatement.h" |
| 24 | #include "ir/SkSLExpressionStatement.h" |
| 25 | #include "ir/SkSLField.h" |
| 26 | #include "ir/SkSLFieldAccess.h" |
| 27 | #include "ir/SkSLFloatLiteral.h" |
| 28 | #include "ir/SkSLForStatement.h" |
| 29 | #include "ir/SkSLFunctionCall.h" |
| 30 | #include "ir/SkSLFunctionDeclaration.h" |
| 31 | #include "ir/SkSLFunctionDefinition.h" |
| 32 | #include "ir/SkSLFunctionReference.h" |
| 33 | #include "ir/SkSLIfStatement.h" |
| 34 | #include "ir/SkSLIndexExpression.h" |
| 35 | #include "ir/SkSLInterfaceBlock.h" |
| 36 | #include "ir/SkSLIntLiteral.h" |
| 37 | #include "ir/SkSLLayout.h" |
| 38 | #include "ir/SkSLPostfixExpression.h" |
| 39 | #include "ir/SkSLPrefixExpression.h" |
| 40 | #include "ir/SkSLReturnStatement.h" |
| 41 | #include "ir/SkSLSwizzle.h" |
| 42 | #include "ir/SkSLTernaryExpression.h" |
| 43 | #include "ir/SkSLUnresolvedFunction.h" |
| 44 | #include "ir/SkSLVariable.h" |
| 45 | #include "ir/SkSLVarDeclaration.h" |
| 46 | #include "ir/SkSLVarDeclarationStatement.h" |
| 47 | #include "ir/SkSLVariableReference.h" |
| 48 | #include "ir/SkSLWhileStatement.h" |
| 49 | |
| 50 | namespace SkSL { |
| 51 | |
| 52 | class AutoSymbolTable { |
| 53 | public: |
| 54 | AutoSymbolTable(IRGenerator* ir) |
| 55 | : fIR(ir) |
| 56 | , fPrevious(fIR->fSymbolTable) { |
| 57 | fIR->pushSymbolTable(); |
| 58 | } |
| 59 | |
| 60 | ~AutoSymbolTable() { |
| 61 | fIR->popSymbolTable(); |
| 62 | ASSERT(fPrevious == fIR->fSymbolTable); |
| 63 | } |
| 64 | |
| 65 | IRGenerator* fIR; |
| 66 | std::shared_ptr<SymbolTable> fPrevious; |
| 67 | }; |
| 68 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 69 | IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 70 | ErrorReporter& errorReporter) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 71 | : fContext(*context) |
| 72 | , fCurrentFunction(nullptr) |
| 73 | , fSymbolTable(std::move(symbolTable)) |
| 74 | , fErrors(errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | |
| 76 | void IRGenerator::pushSymbolTable() { |
| 77 | fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors)); |
| 78 | } |
| 79 | |
| 80 | void IRGenerator::popSymbolTable() { |
| 81 | fSymbolTable = fSymbolTable->fParent; |
| 82 | } |
| 83 | |
| 84 | std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) { |
| 85 | return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName)); |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) { |
| 89 | switch (statement.fKind) { |
| 90 | case ASTStatement::kBlock_Kind: |
| 91 | return this->convertBlock((ASTBlock&) statement); |
| 92 | case ASTStatement::kVarDeclaration_Kind: |
| 93 | return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement); |
| 94 | case ASTStatement::kExpression_Kind: |
| 95 | return this->convertExpressionStatement((ASTExpressionStatement&) statement); |
| 96 | case ASTStatement::kIf_Kind: |
| 97 | return this->convertIf((ASTIfStatement&) statement); |
| 98 | case ASTStatement::kFor_Kind: |
| 99 | return this->convertFor((ASTForStatement&) statement); |
| 100 | case ASTStatement::kWhile_Kind: |
| 101 | return this->convertWhile((ASTWhileStatement&) statement); |
| 102 | case ASTStatement::kDo_Kind: |
| 103 | return this->convertDo((ASTDoStatement&) statement); |
| 104 | case ASTStatement::kReturn_Kind: |
| 105 | return this->convertReturn((ASTReturnStatement&) statement); |
| 106 | case ASTStatement::kBreak_Kind: |
| 107 | return this->convertBreak((ASTBreakStatement&) statement); |
| 108 | case ASTStatement::kContinue_Kind: |
| 109 | return this->convertContinue((ASTContinueStatement&) statement); |
| 110 | case ASTStatement::kDiscard_Kind: |
| 111 | return this->convertDiscard((ASTDiscardStatement&) statement); |
| 112 | default: |
| 113 | ABORT("unsupported statement type: %d\n", statement.fKind); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) { |
| 118 | AutoSymbolTable table(this); |
| 119 | std::vector<std::unique_ptr<Statement>> statements; |
| 120 | for (size_t i = 0; i < block.fStatements.size(); i++) { |
| 121 | std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]); |
| 122 | if (!statement) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | statements.push_back(std::move(statement)); |
| 126 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 127 | return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( |
| 131 | const ASTVarDeclarationStatement& s) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 132 | auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 133 | if (!decl) { |
| 134 | return nullptr; |
| 135 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 136 | return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { |
| 140 | return Modifiers(modifiers); |
| 141 | } |
| 142 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 143 | std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl, |
| 144 | Variable::Storage storage) { |
| 145 | std::vector<VarDeclaration> variables; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 146 | const Type* baseType = this->convertType(*decl.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 147 | if (!baseType) { |
| 148 | return nullptr; |
| 149 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 150 | for (const auto& varDecl : decl.fVars) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | Modifiers modifiers = this->convertModifiers(decl.fModifiers); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 152 | const Type* type = baseType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 153 | ASSERT(type->kind() != Type::kArray_Kind); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 154 | std::vector<std::unique_ptr<Expression>> sizes; |
| 155 | for (const auto& rawSize : varDecl.fSizes) { |
| 156 | if (rawSize) { |
| 157 | auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 158 | if (!size) { |
| 159 | return nullptr; |
| 160 | } |
| 161 | std::string name = type->fName; |
| 162 | uint64_t count; |
| 163 | if (size->fKind == Expression::kIntLiteral_Kind) { |
| 164 | count = ((IntLiteral&) *size).fValue; |
| 165 | if (count <= 0) { |
| 166 | fErrors.error(size->fPosition, "array size must be positive"); |
| 167 | } |
| 168 | name += "[" + to_string(count) + "]"; |
| 169 | } else { |
| 170 | count = -1; |
| 171 | name += "[]"; |
| 172 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 173 | type = new Type(name, Type::kArray_Kind, *type, (int) count); |
| 174 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 175 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 177 | type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1); |
| 178 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 179 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 182 | auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, modifiers, varDecl.fName, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 183 | *type, storage)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | std::unique_ptr<Expression> value; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 185 | if (varDecl.fValue) { |
| 186 | value = this->convertExpression(*varDecl.fValue); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 187 | if (!value) { |
| 188 | return nullptr; |
| 189 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 190 | value = this->coerce(std::move(value), *type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 191 | } |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 192 | if ("sk_FragColor" == varDecl.fName && (*fSymbolTable)[varDecl.fName]) { |
| 193 | // already defined, ignore |
| 194 | } else if ((*fSymbolTable)[varDecl.fName] && |
| 195 | (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind && |
| 196 | ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 197 | // already defined, just update the modifiers |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 198 | Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName]; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 199 | old->fModifiers = var->fModifiers; |
| 200 | } else { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 201 | variables.emplace_back(var.get(), std::move(sizes), std::move(value)); |
| 202 | fSymbolTable->add(varDecl.fName, std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 203 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 205 | return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition, |
| 206 | baseType, |
| 207 | std::move(variables))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | } |
| 209 | |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 210 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration( |
| 211 | const ASTModifiersDeclaration& m) { |
| 212 | Modifiers modifiers = this->convertModifiers(m.fModifiers); |
| 213 | return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers)); |
| 214 | } |
| 215 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 216 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 217 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest), |
| 218 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 219 | if (!test) { |
| 220 | return nullptr; |
| 221 | } |
| 222 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); |
| 223 | if (!ifTrue) { |
| 224 | return nullptr; |
| 225 | } |
| 226 | std::unique_ptr<Statement> ifFalse; |
| 227 | if (s.fIfFalse) { |
| 228 | ifFalse = this->convertStatement(*s.fIfFalse); |
| 229 | if (!ifFalse) { |
| 230 | return nullptr; |
| 231 | } |
| 232 | } |
| 233 | return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test), |
| 234 | std::move(ifTrue), std::move(ifFalse))); |
| 235 | } |
| 236 | |
| 237 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) { |
| 238 | AutoSymbolTable table(this); |
| 239 | std::unique_ptr<Statement> initializer = this->convertStatement(*f.fInitializer); |
| 240 | if (!initializer) { |
| 241 | return nullptr; |
| 242 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 243 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*f.fTest), |
| 244 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 245 | if (!test) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | std::unique_ptr<Expression> next = this->convertExpression(*f.fNext); |
| 249 | if (!next) { |
| 250 | return nullptr; |
| 251 | } |
| 252 | this->checkValid(*next); |
| 253 | std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement); |
| 254 | if (!statement) { |
| 255 | return nullptr; |
| 256 | } |
| 257 | return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer), |
| 258 | std::move(test), std::move(next), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 259 | std::move(statement), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 263 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest), |
| 264 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 265 | if (!test) { |
| 266 | return nullptr; |
| 267 | } |
| 268 | std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement); |
| 269 | if (!statement) { |
| 270 | return nullptr; |
| 271 | } |
| 272 | return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test), |
| 273 | std::move(statement))); |
| 274 | } |
| 275 | |
| 276 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 277 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest), |
| 278 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 279 | if (!test) { |
| 280 | return nullptr; |
| 281 | } |
| 282 | std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement); |
| 283 | if (!statement) { |
| 284 | return nullptr; |
| 285 | } |
| 286 | return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement), |
| 287 | std::move(test))); |
| 288 | } |
| 289 | |
| 290 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement( |
| 291 | const ASTExpressionStatement& s) { |
| 292 | std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression); |
| 293 | if (!e) { |
| 294 | return nullptr; |
| 295 | } |
| 296 | this->checkValid(*e); |
| 297 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 298 | } |
| 299 | |
| 300 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) { |
| 301 | ASSERT(fCurrentFunction); |
| 302 | if (r.fExpression) { |
| 303 | std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression); |
| 304 | if (!result) { |
| 305 | return nullptr; |
| 306 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 307 | if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | fErrors.error(result->fPosition, "may not return a value from a void function"); |
| 309 | } else { |
| 310 | result = this->coerce(std::move(result), fCurrentFunction->fReturnType); |
| 311 | if (!result) { |
| 312 | return nullptr; |
| 313 | } |
| 314 | } |
| 315 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 316 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 317 | if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 318 | fErrors.error(r.fPosition, "expected function to return '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 319 | fCurrentFunction->fReturnType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 320 | } |
| 321 | return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { |
| 326 | return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); |
| 327 | } |
| 328 | |
| 329 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) { |
| 330 | return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); |
| 331 | } |
| 332 | |
| 333 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) { |
| 334 | return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); |
| 335 | } |
| 336 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 337 | static const Type& expand_generics(const Type& type, int i) { |
| 338 | if (type.kind() == Type::kGeneric_Kind) { |
| 339 | return *type.coercibleTypes()[i]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 340 | } |
| 341 | return type; |
| 342 | } |
| 343 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 344 | static void expand_generics(const FunctionDeclaration& decl, |
| 345 | std::shared_ptr<SymbolTable> symbolTable) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 346 | for (int i = 0; i < 4; i++) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 347 | const Type& returnType = expand_generics(decl.fReturnType, i); |
| 348 | std::vector<const Variable*> parameters; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 349 | for (const auto& p : decl.fParameters) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 350 | Variable* var = new Variable(p->fPosition, Modifiers(p->fModifiers), p->fName, |
| 351 | expand_generics(p->fType, i), |
| 352 | Variable::kParameter_Storage); |
| 353 | symbolTable->takeOwnership(var); |
| 354 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 355 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 356 | symbolTable->add(decl.fName, std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration( |
| 357 | decl.fPosition, |
| 358 | decl.fName, |
| 359 | std::move(parameters), |
| 360 | std::move(returnType)))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 365 | bool isGeneric; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 366 | const Type* returnType = this->convertType(*f.fReturnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 367 | if (!returnType) { |
| 368 | return nullptr; |
| 369 | } |
| 370 | isGeneric = returnType->kind() == Type::kGeneric_Kind; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 371 | std::vector<const Variable*> parameters; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 372 | for (const auto& param : f.fParameters) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 373 | const Type* type = this->convertType(*param->fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 374 | if (!type) { |
| 375 | return nullptr; |
| 376 | } |
| 377 | for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { |
| 378 | int size = param->fSizes[j]; |
| 379 | std::string name = type->name() + "[" + to_string(size) + "]"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 380 | Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size); |
| 381 | fSymbolTable->takeOwnership(newType); |
| 382 | type = newType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 383 | } |
| 384 | std::string name = param->fName; |
| 385 | Modifiers modifiers = this->convertModifiers(param->fModifiers); |
| 386 | Position pos = param->fPosition; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 387 | Variable* var = new Variable(pos, modifiers, std::move(name), *type, |
| 388 | Variable::kParameter_Storage); |
| 389 | fSymbolTable->takeOwnership(var); |
| 390 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 391 | isGeneric |= type->kind() == Type::kGeneric_Kind; |
| 392 | } |
| 393 | |
| 394 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 395 | const FunctionDeclaration* decl = nullptr; |
| 396 | auto entry = (*fSymbolTable)[f.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 397 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 398 | std::vector<const FunctionDeclaration*> functions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 399 | switch (entry->fKind) { |
| 400 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 401 | functions = ((UnresolvedFunction*) entry)->fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 402 | break; |
| 403 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 404 | functions.push_back((FunctionDeclaration*) entry); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 405 | break; |
| 406 | default: |
| 407 | fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); |
| 408 | return nullptr; |
| 409 | } |
| 410 | for (const auto& other : functions) { |
| 411 | ASSERT(other->fName == f.fName); |
| 412 | if (parameters.size() == other->fParameters.size()) { |
| 413 | bool match = true; |
| 414 | for (size_t i = 0; i < parameters.size(); i++) { |
| 415 | if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 416 | match = false; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | if (match) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 421 | if (*returnType != other->fReturnType) { |
| 422 | FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 423 | fErrors.error(f.fPosition, "functions '" + newDecl.description() + |
| 424 | "' and '" + other->description() + |
| 425 | "' differ only in return type"); |
| 426 | return nullptr; |
| 427 | } |
| 428 | decl = other; |
| 429 | for (size_t i = 0; i < parameters.size(); i++) { |
| 430 | if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) { |
| 431 | fErrors.error(f.fPosition, "modifiers on parameter " + |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 432 | to_string((uint64_t) i + 1) + |
| 433 | " differ between declaration and " |
| 434 | "definition"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 435 | return nullptr; |
| 436 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 437 | } |
| 438 | if (other->fDefined) { |
| 439 | fErrors.error(f.fPosition, "duplicate definition of " + |
| 440 | other->description()); |
| 441 | } |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | if (!decl) { |
| 448 | // couldn't find an existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 449 | if (isGeneric) { |
| 450 | ASSERT(!f.fBody); |
| 451 | expand_generics(FunctionDeclaration(f.fPosition, f.fName, parameters, *returnType), |
| 452 | fSymbolTable); |
| 453 | } else { |
| 454 | auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration( |
| 455 | f.fPosition, |
| 456 | f.fName, |
| 457 | parameters, |
| 458 | *returnType)); |
| 459 | decl = newDecl.get(); |
| 460 | fSymbolTable->add(decl->fName, std::move(newDecl)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 463 | if (f.fBody) { |
| 464 | ASSERT(!fCurrentFunction); |
| 465 | fCurrentFunction = decl; |
| 466 | decl->fDefined = true; |
| 467 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 468 | AutoSymbolTable table(this); |
| 469 | for (size_t i = 0; i < parameters.size(); i++) { |
| 470 | fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 471 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 472 | std::unique_ptr<Block> body = this->convertBlock(*f.fBody); |
| 473 | fCurrentFunction = nullptr; |
| 474 | if (!body) { |
| 475 | return nullptr; |
| 476 | } |
| 477 | return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl, |
| 478 | std::move(body))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 479 | } |
| 480 | return nullptr; |
| 481 | } |
| 482 | |
| 483 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) { |
| 484 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 485 | AutoSymbolTable table(this); |
| 486 | Modifiers mods = this->convertModifiers(intf.fModifiers); |
| 487 | std::vector<Type::Field> fields; |
| 488 | for (size_t i = 0; i < intf.fDeclarations.size(); i++) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 489 | std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations( |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 490 | *intf.fDeclarations[i], |
| 491 | Variable::kGlobal_Storage); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 492 | for (const auto& var : decl->fVars) { |
| 493 | fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName, |
| 494 | &var.fVar->fType)); |
| 495 | if (var.fValue) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 496 | fErrors.error(decl->fPosition, |
| 497 | "initializers are not permitted on interface block fields"); |
| 498 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 499 | if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 500 | Modifiers::kOut_Flag | |
| 501 | Modifiers::kUniform_Flag | |
| 502 | Modifiers::kConst_Flag)) { |
| 503 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 504 | "interface block fields may not have storage qualifiers"); |
| 505 | } |
| 506 | } |
| 507 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 508 | Type* type = new Type(intf.fInterfaceName, fields); |
| 509 | fSymbolTable->takeOwnership(type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 510 | std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fInterfaceName; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 511 | Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kGlobal_Storage); |
| 512 | fSymbolTable->takeOwnership(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 513 | if (intf.fValueName.length()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 514 | old->addWithoutOwnership(intf.fValueName, var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 515 | } else { |
| 516 | for (size_t i = 0; i < fields.size(); i++) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 517 | old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var, |
| 518 | (int) i))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 521 | return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | } |
| 523 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 524 | const Type* IRGenerator::convertType(const ASTType& type) { |
| 525 | const Symbol* result = (*fSymbolTable)[type.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 526 | if (result && result->fKind == Symbol::kType_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 527 | return (const Type*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 528 | } |
| 529 | fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); |
| 530 | return nullptr; |
| 531 | } |
| 532 | |
| 533 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { |
| 534 | switch (expr.fKind) { |
| 535 | case ASTExpression::kIdentifier_Kind: |
| 536 | return this->convertIdentifier((ASTIdentifier&) expr); |
| 537 | case ASTExpression::kBool_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 538 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 539 | ((ASTBoolLiteral&) expr).fValue)); |
| 540 | case ASTExpression::kInt_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 541 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 542 | ((ASTIntLiteral&) expr).fValue)); |
| 543 | case ASTExpression::kFloat_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 544 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 545 | ((ASTFloatLiteral&) expr).fValue)); |
| 546 | case ASTExpression::kBinary_Kind: |
| 547 | return this->convertBinaryExpression((ASTBinaryExpression&) expr); |
| 548 | case ASTExpression::kPrefix_Kind: |
| 549 | return this->convertPrefixExpression((ASTPrefixExpression&) expr); |
| 550 | case ASTExpression::kSuffix_Kind: |
| 551 | return this->convertSuffixExpression((ASTSuffixExpression&) expr); |
| 552 | case ASTExpression::kTernary_Kind: |
| 553 | return this->convertTernaryExpression((ASTTernaryExpression&) expr); |
| 554 | default: |
| 555 | ABORT("unsupported expression type: %d\n", expr.fKind); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 560 | const Symbol* result = (*fSymbolTable)[identifier.fText]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 561 | if (!result) { |
| 562 | fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'"); |
| 563 | return nullptr; |
| 564 | } |
| 565 | switch (result->fKind) { |
| 566 | case Symbol::kFunctionDeclaration_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 567 | std::vector<const FunctionDeclaration*> f = { |
| 568 | (const FunctionDeclaration*) result |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 569 | }; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 570 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 571 | identifier.fPosition, |
| 572 | f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 573 | } |
| 574 | case Symbol::kUnresolvedFunction_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 575 | const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 576 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 577 | identifier.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 578 | f->fFunctions)); |
| 579 | } |
| 580 | case Symbol::kVariable_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 581 | const Variable* var = (const Variable*) result; |
| 582 | this->markReadFrom(*var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 583 | return std::unique_ptr<VariableReference>(new VariableReference(identifier.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 584 | *var)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | } |
| 586 | case Symbol::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 587 | const Field* field = (const Field*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 588 | VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 589 | return std::unique_ptr<Expression>(new FieldAccess( |
| 590 | std::unique_ptr<Expression>(base), |
| 591 | field->fFieldIndex, |
| 592 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 593 | } |
| 594 | case Symbol::kType_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 595 | const Type* t = (const Type*) result; |
| 596 | return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition, |
| 597 | *t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 598 | } |
| 599 | default: |
| 600 | ABORT("unsupported symbol type %d\n", result->fKind); |
| 601 | } |
| 602 | |
| 603 | } |
| 604 | |
| 605 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 606 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 607 | if (!expr) { |
| 608 | return nullptr; |
| 609 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 610 | if (expr->fType == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 611 | return expr; |
| 612 | } |
| 613 | this->checkValid(*expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 614 | if (expr->fType == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 615 | return nullptr; |
| 616 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 617 | if (!expr->fType.canCoerceTo(type)) { |
| 618 | fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" + |
| 619 | expr->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 620 | return nullptr; |
| 621 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 622 | if (type.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 623 | std::vector<std::unique_ptr<Expression>> args; |
| 624 | args.push_back(std::move(expr)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 625 | ASTIdentifier id(Position(), type.description()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 626 | std::unique_ptr<Expression> ctor = this->convertIdentifier(id); |
| 627 | ASSERT(ctor); |
| 628 | return this->call(Position(), std::move(ctor), std::move(args)); |
| 629 | } |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 630 | std::vector<std::unique_ptr<Expression>> args; |
| 631 | args.push_back(std::move(expr)); |
| 632 | return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 633 | } |
| 634 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 635 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
| 636 | if (left.kind() == Type::kMatrix_Kind) { |
| 637 | return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind; |
| 638 | } |
| 639 | return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind; |
| 640 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 641 | /** |
| 642 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 643 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 644 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 645 | static bool determine_binary_type(const Context& context, |
| 646 | Token::Kind op, |
| 647 | const Type& left, |
| 648 | const Type& right, |
| 649 | const Type** outLeftType, |
| 650 | const Type** outRightType, |
| 651 | const Type** outResultType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 652 | bool tryFlipped) { |
| 653 | bool isLogical; |
| 654 | switch (op) { |
| 655 | case Token::EQEQ: // fall through |
| 656 | case Token::NEQ: // fall through |
| 657 | case Token::LT: // fall through |
| 658 | case Token::GT: // fall through |
| 659 | case Token::LTEQ: // fall through |
| 660 | case Token::GTEQ: |
| 661 | isLogical = true; |
| 662 | break; |
| 663 | case Token::LOGICALOR: // fall through |
| 664 | case Token::LOGICALAND: // fall through |
| 665 | case Token::LOGICALXOR: // fall through |
| 666 | case Token::LOGICALOREQ: // fall through |
| 667 | case Token::LOGICALANDEQ: // fall through |
| 668 | case Token::LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 669 | *outLeftType = context.fBool_Type.get(); |
| 670 | *outRightType = context.fBool_Type.get(); |
| 671 | *outResultType = context.fBool_Type.get(); |
| 672 | return left.canCoerceTo(*context.fBool_Type) && |
| 673 | right.canCoerceTo(*context.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 674 | case Token::STAR: // fall through |
| 675 | case Token::STAREQ: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 676 | if (is_matrix_multiply(left, right)) { |
| 677 | // determine final component type |
| 678 | if (determine_binary_type(context, Token::STAR, left.componentType(), |
| 679 | right.componentType(), outLeftType, outRightType, |
| 680 | outResultType, false)) { |
| 681 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
| 682 | left.rows());; |
| 683 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
| 684 | right.rows());; |
| 685 | int leftColumns = left.columns(); |
| 686 | int leftRows = left.rows(); |
| 687 | int rightColumns; |
| 688 | int rightRows; |
| 689 | if (right.kind() == Type::kVector_Kind) { |
| 690 | // matrix * vector treats the vector as a column vector, so we need to |
| 691 | // transpose it |
| 692 | rightColumns = right.rows(); |
| 693 | rightRows = right.columns(); |
| 694 | ASSERT(rightColumns == 1); |
| 695 | } else { |
| 696 | rightColumns = right.columns(); |
| 697 | rightRows = right.rows(); |
| 698 | } |
| 699 | if (rightColumns > 1) { |
| 700 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 701 | leftRows); |
| 702 | } else { |
| 703 | // result was a column vector, transpose it back to a row |
| 704 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 705 | rightColumns); |
| 706 | } |
| 707 | return leftColumns == rightRows; |
| 708 | } else { |
| 709 | return false; |
| 710 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 711 | } |
| 712 | // fall through |
| 713 | default: |
| 714 | isLogical = false; |
| 715 | } |
| 716 | // FIXME: need to disallow illegal operations like vec3 > vec3. Also do not currently have |
| 717 | // full support for numbers other than float. |
| 718 | if (left == right) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 719 | *outLeftType = &left; |
| 720 | *outRightType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 721 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 722 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 723 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 724 | *outResultType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 725 | } |
| 726 | return true; |
| 727 | } |
| 728 | // FIXME: incorrect for shift operations |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 729 | if (left.canCoerceTo(right)) { |
| 730 | *outLeftType = &right; |
| 731 | *outRightType = &right; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 732 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 733 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 734 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 735 | *outResultType = &right; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 736 | } |
| 737 | return true; |
| 738 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 739 | if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
| 740 | (right.kind() == Type::kScalar_Kind)) { |
| 741 | if (determine_binary_type(context, op, left.componentType(), right, outLeftType, |
| 742 | outRightType, outResultType, false)) { |
| 743 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 744 | if (!isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 745 | *outResultType = &(*outResultType)->toCompound(context, left.columns(), |
| 746 | left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | return false; |
| 751 | } |
| 752 | if (tryFlipped) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 753 | return determine_binary_type(context, op, right, left, outRightType, outLeftType, |
| 754 | outResultType, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 755 | } |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( |
| 760 | const ASTBinaryExpression& expression) { |
| 761 | std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft); |
| 762 | if (!left) { |
| 763 | return nullptr; |
| 764 | } |
| 765 | std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight); |
| 766 | if (!right) { |
| 767 | return nullptr; |
| 768 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 769 | const Type* leftType; |
| 770 | const Type* rightType; |
| 771 | const Type* resultType; |
| 772 | if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 773 | &rightType, &resultType, true)) { |
| 774 | fErrors.error(expression.fPosition, "type mismatch: '" + |
| 775 | Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 776 | "' cannot operate on '" + left->fType.fName + |
| 777 | "', '" + right->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 778 | return nullptr; |
| 779 | } |
| 780 | switch (expression.fOperator) { |
| 781 | case Token::EQ: // fall through |
| 782 | case Token::PLUSEQ: // fall through |
| 783 | case Token::MINUSEQ: // fall through |
| 784 | case Token::STAREQ: // fall through |
| 785 | case Token::SLASHEQ: // fall through |
| 786 | case Token::PERCENTEQ: // fall through |
| 787 | case Token::SHLEQ: // fall through |
| 788 | case Token::SHREQ: // fall through |
| 789 | case Token::BITWISEOREQ: // fall through |
| 790 | case Token::BITWISEXOREQ: // fall through |
| 791 | case Token::BITWISEANDEQ: // fall through |
| 792 | case Token::LOGICALOREQ: // fall through |
| 793 | case Token::LOGICALXOREQ: // fall through |
| 794 | case Token::LOGICALANDEQ: |
| 795 | this->markWrittenTo(*left); |
| 796 | default: |
| 797 | break; |
| 798 | } |
| 799 | return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 800 | this->coerce(std::move(left), |
| 801 | *leftType), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 802 | expression.fOperator, |
| 803 | this->coerce(std::move(right), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 804 | *rightType), |
| 805 | *resultType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( |
| 809 | const ASTTernaryExpression& expression) { |
| 810 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 811 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 812 | if (!test) { |
| 813 | return nullptr; |
| 814 | } |
| 815 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue); |
| 816 | if (!ifTrue) { |
| 817 | return nullptr; |
| 818 | } |
| 819 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse); |
| 820 | if (!ifFalse) { |
| 821 | return nullptr; |
| 822 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 823 | const Type* trueType; |
| 824 | const Type* falseType; |
| 825 | const Type* resultType; |
| 826 | if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 827 | &falseType, &resultType, true)) { |
| 828 | fErrors.error(expression.fPosition, "ternary operator result mismatch: '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 829 | ifTrue->fType.fName + "', '" + |
| 830 | ifFalse->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 831 | return nullptr; |
| 832 | } |
| 833 | ASSERT(trueType == falseType); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 834 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
| 835 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 836 | return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition, |
| 837 | std::move(test), |
| 838 | std::move(ifTrue), |
| 839 | std::move(ifFalse))); |
| 840 | } |
| 841 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 842 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 843 | const FunctionDeclaration& function, |
| 844 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 845 | if (function.fParameters.size() != arguments.size()) { |
| 846 | std::string msg = "call to '" + function.fName + "' expected " + |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 847 | to_string((uint64_t) function.fParameters.size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 848 | " argument"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 849 | if (function.fParameters.size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 850 | msg += "s"; |
| 851 | } |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 852 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 853 | fErrors.error(position, msg); |
| 854 | return nullptr; |
| 855 | } |
| 856 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 857 | arguments[i] = this->coerce(std::move(arguments[i]), function.fParameters[i]->fType); |
| 858 | if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 859 | this->markWrittenTo(*arguments[i]); |
| 860 | } |
| 861 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 862 | return std::unique_ptr<FunctionCall>(new FunctionCall(position, function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 863 | std::move(arguments))); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Determines the cost of coercing the arguments of a function to the required types. Returns true |
| 868 | * if the cost could be computed, false if the call is not valid. Cost has no particular meaning |
| 869 | * other than "lower costs are preferred". |
| 870 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 871 | bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 872 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 873 | int* outCost) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 874 | if (function.fParameters.size() != arguments.size()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 875 | return false; |
| 876 | } |
| 877 | int total = 0; |
| 878 | for (size_t i = 0; i < arguments.size(); i++) { |
| 879 | int cost; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 880 | if (arguments[i]->fType.determineCoercionCost(function.fParameters[i]->fType, &cost)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 881 | total += cost; |
| 882 | } else { |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | *outCost = total; |
| 887 | return true; |
| 888 | } |
| 889 | |
| 890 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 891 | std::unique_ptr<Expression> functionValue, |
| 892 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 893 | if (functionValue->fKind == Expression::kTypeReference_Kind) { |
| 894 | return this->convertConstructor(position, |
| 895 | ((TypeReference&) *functionValue).fValue, |
| 896 | std::move(arguments)); |
| 897 | } |
| 898 | if (functionValue->fKind != Expression::kFunctionReference_Kind) { |
| 899 | fErrors.error(position, "'" + functionValue->description() + "' is not a function"); |
| 900 | return nullptr; |
| 901 | } |
| 902 | FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 903 | int bestCost = INT_MAX; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 904 | const FunctionDeclaration* best = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 905 | if (ref->fFunctions.size() > 1) { |
| 906 | for (const auto& f : ref->fFunctions) { |
| 907 | int cost; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 908 | if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 909 | bestCost = cost; |
| 910 | best = f; |
| 911 | } |
| 912 | } |
| 913 | if (best) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 914 | return this->call(position, *best, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 915 | } |
| 916 | std::string msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 917 | std::string separator = ""; |
| 918 | for (size_t i = 0; i < arguments.size(); i++) { |
| 919 | msg += separator; |
| 920 | separator = ", "; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 921 | msg += arguments[i]->fType.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 922 | } |
| 923 | msg += ")"; |
| 924 | fErrors.error(position, msg); |
| 925 | return nullptr; |
| 926 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 927 | return this->call(position, *ref->fFunctions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | std::unique_ptr<Expression> IRGenerator::convertConstructor( |
| 931 | Position position, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 932 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 933 | std::vector<std::unique_ptr<Expression>> args) { |
| 934 | // FIXME: add support for structs and arrays |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 935 | Type::Kind kind = type.kind(); |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 936 | if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind && |
| 937 | kind != Type::kArray_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 938 | fErrors.error(position, "cannot construct '" + type.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 939 | return nullptr; |
| 940 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 941 | if (type == *fContext.fFloat_Type && args.size() == 1 && |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 942 | args[0]->fKind == Expression::kIntLiteral_Kind) { |
| 943 | int64_t value = ((IntLiteral&) *args[0]).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 944 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 945 | } |
| 946 | if (args.size() == 1 && args[0]->fType == type) { |
| 947 | // argument is already the right type, just return it |
| 948 | return std::move(args[0]); |
| 949 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 950 | if (type.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 951 | if (args.size() != 1) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 952 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 953 | "' constructor, (expected exactly 1 argument, but found " + |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 954 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 955 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 956 | if (args[0]->fType == *fContext.fBool_Type) { |
| 957 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0)); |
| 958 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 959 | return std::unique_ptr<Expression>( |
| 960 | new TernaryExpression(position, std::move(args[0]), |
| 961 | this->coerce(std::move(one), type), |
| 962 | this->coerce(std::move(zero), |
| 963 | type))); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 964 | } else if (!args[0]->fType.isNumber()) { |
| 965 | fErrors.error(position, "invalid argument to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 966 | "' constructor (expected a number or bool, but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 967 | args[0]->fType.description() + "')"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 968 | } |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 969 | if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type || |
| 970 | type == *fContext.fUInt_Type)) { |
| 971 | return std::unique_ptr<Expression>(new IntLiteral(fContext, |
| 972 | position, |
| 973 | ((IntLiteral&) *args[0]).fValue, |
| 974 | &type)); |
| 975 | } |
| 976 | } else if (kind == Type::kArray_Kind) { |
| 977 | const Type& base = type.componentType(); |
| 978 | for (size_t i = 0; i < args.size(); i++) { |
| 979 | args[i] = this->coerce(std::move(args[i]), base); |
| 980 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 981 | } else { |
| 982 | ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); |
| 983 | int actual = 0; |
| 984 | for (size_t i = 0; i < args.size(); i++) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 985 | if (args[i]->fType.kind() == Type::kVector_Kind || |
| 986 | args[i]->fType.kind() == Type::kMatrix_Kind) { |
| 987 | int columns = args[i]->fType.columns(); |
| 988 | int rows = args[i]->fType.rows(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 989 | args[i] = this->coerce(std::move(args[i]), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 990 | type.componentType().toCompound(fContext, columns, rows)); |
| 991 | actual += args[i]->fType.rows() * args[i]->fType.columns(); |
| 992 | } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | actual += 1; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 994 | if (type.kind() != Type::kScalar_Kind) { |
| 995 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 996 | } |
| 997 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 998 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 999 | "parameter to '" + type.description() + "' constructor"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1000 | return nullptr; |
| 1001 | } |
| 1002 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1003 | int min = type.rows() * type.columns(); |
| 1004 | int max = type.columns() > 1 ? INT_MAX : min; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1005 | if ((actual < min || actual > max) && |
| 1006 | !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1007 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1008 | "' constructor (expected " + to_string(min) + " scalar" + |
| 1009 | (min == 1 ? "" : "s") + ", but found " + to_string(actual) + |
| 1010 | ")"); |
| 1011 | return nullptr; |
| 1012 | } |
| 1013 | } |
| 1014 | return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args))); |
| 1015 | } |
| 1016 | |
| 1017 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( |
| 1018 | const ASTPrefixExpression& expression) { |
| 1019 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand); |
| 1020 | if (!base) { |
| 1021 | return nullptr; |
| 1022 | } |
| 1023 | switch (expression.fOperator) { |
| 1024 | case Token::PLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1025 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1026 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1027 | "'+' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1028 | return nullptr; |
| 1029 | } |
| 1030 | return base; |
| 1031 | case Token::MINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1032 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1033 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1034 | "'-' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1035 | return nullptr; |
| 1036 | } |
| 1037 | if (base->fKind == Expression::kIntLiteral_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1038 | return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1039 | -((IntLiteral&) *base).fValue)); |
| 1040 | } |
| 1041 | if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 1042 | double value = -((FloatLiteral&) *base).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1043 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition, |
| 1044 | value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1045 | } |
| 1046 | return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base))); |
| 1047 | case Token::PLUSPLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1048 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1049 | fErrors.error(expression.fPosition, |
| 1050 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1051 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1052 | return nullptr; |
| 1053 | } |
| 1054 | this->markWrittenTo(*base); |
| 1055 | break; |
| 1056 | case Token::MINUSMINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1057 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1058 | fErrors.error(expression.fPosition, |
| 1059 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1060 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1061 | return nullptr; |
| 1062 | } |
| 1063 | this->markWrittenTo(*base); |
| 1064 | break; |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 1065 | case Token::LOGICALNOT: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1066 | if (base->fType != *fContext.fBool_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1067 | fErrors.error(expression.fPosition, |
| 1068 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1069 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1070 | return nullptr; |
| 1071 | } |
| 1072 | break; |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 1073 | case Token::BITWISENOT: |
| 1074 | if (base->fType != *fContext.fInt_Type) { |
| 1075 | fErrors.error(expression.fPosition, |
| 1076 | "'" + Token::OperatorName(expression.fOperator) + |
| 1077 | "' cannot operate on '" + base->fType.description() + "'"); |
| 1078 | return nullptr; |
| 1079 | } |
| 1080 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1081 | default: |
| 1082 | ABORT("unsupported prefix operator\n"); |
| 1083 | } |
| 1084 | return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator, |
| 1085 | std::move(base))); |
| 1086 | } |
| 1087 | |
| 1088 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
| 1089 | const ASTExpression& index) { |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 1090 | if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind && |
| 1091 | base->fType.kind() != Type::kVector_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1092 | fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1093 | "'"); |
| 1094 | return nullptr; |
| 1095 | } |
| 1096 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 1097 | if (!converted) { |
| 1098 | return nullptr; |
| 1099 | } |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 1100 | if (converted->fType != *fContext.fUInt_Type) { |
| 1101 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 1102 | if (!converted) { |
| 1103 | return nullptr; |
| 1104 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1105 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1106 | return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base), |
| 1107 | std::move(converted))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
| 1111 | const std::string& field) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1112 | auto fields = base->fType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1113 | for (size_t i = 0; i < fields.size(); i++) { |
| 1114 | if (fields[i].fName == field) { |
| 1115 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 1116 | } |
| 1117 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1118 | fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a " |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1119 | "field named '" + field + ""); |
| 1120 | return nullptr; |
| 1121 | } |
| 1122 | |
| 1123 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
| 1124 | const std::string& fields) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1125 | if (base->fType.kind() != Type::kVector_Kind) { |
| 1126 | fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1127 | return nullptr; |
| 1128 | } |
| 1129 | std::vector<int> swizzleComponents; |
| 1130 | for (char c : fields) { |
| 1131 | switch (c) { |
| 1132 | case 'x': // fall through |
| 1133 | case 'r': // fall through |
| 1134 | case 's': |
| 1135 | swizzleComponents.push_back(0); |
| 1136 | break; |
| 1137 | case 'y': // fall through |
| 1138 | case 'g': // fall through |
| 1139 | case 't': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1140 | if (base->fType.columns() >= 2) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1141 | swizzleComponents.push_back(1); |
| 1142 | break; |
| 1143 | } |
| 1144 | // fall through |
| 1145 | case 'z': // fall through |
| 1146 | case 'b': // fall through |
| 1147 | case 'p': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1148 | if (base->fType.columns() >= 3) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1149 | swizzleComponents.push_back(2); |
| 1150 | break; |
| 1151 | } |
| 1152 | // fall through |
| 1153 | case 'w': // fall through |
| 1154 | case 'a': // fall through |
| 1155 | case 'q': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1156 | if (base->fType.columns() >= 4) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1157 | swizzleComponents.push_back(3); |
| 1158 | break; |
| 1159 | } |
| 1160 | // fall through |
| 1161 | default: |
| 1162 | fErrors.error(base->fPosition, "invalid swizzle component '" + std::string(1, c) + |
| 1163 | "'"); |
| 1164 | return nullptr; |
| 1165 | } |
| 1166 | } |
| 1167 | ASSERT(swizzleComponents.size() > 0); |
| 1168 | if (swizzleComponents.size() > 4) { |
| 1169 | fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'"); |
| 1170 | return nullptr; |
| 1171 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1172 | return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | std::unique_ptr<Expression> IRGenerator::convertSuffixExpression( |
| 1176 | const ASTSuffixExpression& expression) { |
| 1177 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase); |
| 1178 | if (!base) { |
| 1179 | return nullptr; |
| 1180 | } |
| 1181 | switch (expression.fSuffix->fKind) { |
ethannicholas | dcfe6db | 2016-10-10 10:09:00 -0700 | [diff] [blame^] | 1182 | case ASTSuffix::kIndex_Kind: { |
| 1183 | const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get(); |
| 1184 | if (expr) { |
| 1185 | return this->convertIndex(std::move(base), *expr); |
| 1186 | } else if (base->fKind == Expression::kTypeReference_Kind) { |
| 1187 | const Type& oldType = ((TypeReference&) *base).fValue; |
| 1188 | Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType, |
| 1189 | -1); |
| 1190 | fSymbolTable->takeOwnership(newType); |
| 1191 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition, |
| 1192 | *newType)); |
| 1193 | } else { |
| 1194 | fErrors.error(expression.fPosition, "'[]' must follow a type name"); |
| 1195 | } |
| 1196 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1197 | case ASTSuffix::kCall_Kind: { |
| 1198 | auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments; |
| 1199 | std::vector<std::unique_ptr<Expression>> arguments; |
| 1200 | for (size_t i = 0; i < rawArguments->size(); i++) { |
| 1201 | std::unique_ptr<Expression> converted = |
| 1202 | this->convertExpression(*(*rawArguments)[i]); |
| 1203 | if (!converted) { |
| 1204 | return nullptr; |
| 1205 | } |
| 1206 | arguments.push_back(std::move(converted)); |
| 1207 | } |
| 1208 | return this->call(expression.fPosition, std::move(base), std::move(arguments)); |
| 1209 | } |
| 1210 | case ASTSuffix::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1211 | switch (base->fType.kind()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1212 | case Type::kVector_Kind: |
| 1213 | return this->convertSwizzle(std::move(base), |
| 1214 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1215 | case Type::kStruct_Kind: |
| 1216 | return this->convertField(std::move(base), |
| 1217 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1218 | default: |
| 1219 | fErrors.error(base->fPosition, "cannot swizzle value of type '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1220 | base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1221 | return nullptr; |
| 1222 | } |
| 1223 | } |
| 1224 | case ASTSuffix::kPostIncrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1225 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1226 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1227 | "'++' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1228 | return nullptr; |
| 1229 | } |
| 1230 | this->markWrittenTo(*base); |
| 1231 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
| 1232 | Token::PLUSPLUS)); |
| 1233 | case ASTSuffix::kPostDecrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1234 | if (!base->fType.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1235 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1236 | "'--' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1237 | return nullptr; |
| 1238 | } |
| 1239 | this->markWrittenTo(*base); |
| 1240 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
| 1241 | Token::MINUSMINUS)); |
| 1242 | default: |
| 1243 | ABORT("unsupported suffix operator"); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | void IRGenerator::checkValid(const Expression& expr) { |
| 1248 | switch (expr.fKind) { |
| 1249 | case Expression::kFunctionReference_Kind: |
| 1250 | fErrors.error(expr.fPosition, "expected '(' to begin function call"); |
| 1251 | break; |
| 1252 | case Expression::kTypeReference_Kind: |
| 1253 | fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation"); |
| 1254 | break; |
| 1255 | default: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1256 | ASSERT(expr.fType != *fContext.fInvalid_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | } |
| 1259 | } |
| 1260 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1261 | void IRGenerator::markReadFrom(const Variable& var) { |
| 1262 | var.fIsReadFrom = true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static bool has_duplicates(const Swizzle& swizzle) { |
| 1266 | int bits = 0; |
| 1267 | for (int idx : swizzle.fComponents) { |
| 1268 | ASSERT(idx >= 0 && idx <= 3); |
| 1269 | int bit = 1 << idx; |
| 1270 | if (bits & bit) { |
| 1271 | return true; |
| 1272 | } |
| 1273 | bits |= bit; |
| 1274 | } |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | void IRGenerator::markWrittenTo(const Expression& expr) { |
| 1279 | switch (expr.fKind) { |
| 1280 | case Expression::kVariableReference_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1281 | const Variable& var = ((VariableReference&) expr).fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1282 | if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
| 1283 | fErrors.error(expr.fPosition, |
| 1284 | "cannot modify immutable variable '" + var.fName + "'"); |
| 1285 | } |
| 1286 | var.fIsWrittenTo = true; |
| 1287 | break; |
| 1288 | } |
| 1289 | case Expression::kFieldAccess_Kind: |
| 1290 | this->markWrittenTo(*((FieldAccess&) expr).fBase); |
| 1291 | break; |
| 1292 | case Expression::kSwizzle_Kind: |
| 1293 | if (has_duplicates((Swizzle&) expr)) { |
| 1294 | fErrors.error(expr.fPosition, |
| 1295 | "cannot write to the same swizzle field more than once"); |
| 1296 | } |
| 1297 | this->markWrittenTo(*((Swizzle&) expr).fBase); |
| 1298 | break; |
| 1299 | case Expression::kIndex_Kind: |
| 1300 | this->markWrittenTo(*((IndexExpression&) expr).fBase); |
| 1301 | break; |
| 1302 | default: |
| 1303 | fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'"); |
| 1304 | break; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | } |