2020-07-04 16:47:23 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package shader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2020-07-12 16:07:30 +02:00
|
|
|
gconstant "go/constant"
|
2020-07-04 16:47:23 +02:00
|
|
|
"go/token"
|
2020-07-04 20:23:56 +02:00
|
|
|
"strings"
|
2020-07-04 16:47:23 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2020-07-04 16:47:23 +02:00
|
|
|
)
|
|
|
|
|
2020-09-02 19:45:22 +02:00
|
|
|
func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool {
|
2020-09-02 19:10:52 +02:00
|
|
|
if !canTruncateToInteger(expr.Const) {
|
2020-09-02 19:45:22 +02:00
|
|
|
cs.addError(node.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String()))
|
2020-09-02 19:10:52 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-03-09 17:54:11 +01:00
|
|
|
expr.Const = gconstant.ToInt(expr.Const)
|
2020-09-02 19:10:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-10 08:45:28 +02:00
|
|
|
func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inParams, outParams []variable, returnType shaderir.Type) ([]shaderir.Stmt, bool) {
|
2020-07-04 17:03:21 +02:00
|
|
|
var stmts []shaderir.Stmt
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
switch stmt := stmt.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.DEFINE:
|
|
|
|
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 21:59:04 +02:00
|
|
|
ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, true)
|
2020-07-04 17:03:21 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
case token.ASSIGN:
|
|
|
|
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-09-12 21:59:04 +02:00
|
|
|
ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, false)
|
2020-07-04 17:03:21 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2023-09-11 20:00:34 +02:00
|
|
|
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN, token.AND_NOT_ASSIGN:
|
2022-08-17 16:13:21 +02:00
|
|
|
rhs, rts, ss, ok := cs.parseExpr(block, fname, stmt.Rhs[0], true)
|
2022-03-13 11:17:44 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2022-08-17 16:13:21 +02:00
|
|
|
lhs, lts, ss, ok := cs.parseExpr(block, fname, stmt.Lhs[0], true)
|
2022-03-13 11:17:44 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2023-05-08 18:38:39 +02:00
|
|
|
if lhs[0].Type == shaderir.UniformVariable {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(stmt.Pos(), "a uniform variable cannot be assigned")
|
2023-05-08 18:38:39 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
var op shaderir.Op
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.ADD_ASSIGN:
|
|
|
|
op = shaderir.Add
|
|
|
|
case token.SUB_ASSIGN:
|
|
|
|
op = shaderir.Sub
|
|
|
|
case token.MUL_ASSIGN:
|
2022-03-13 11:17:44 +01:00
|
|
|
if lts[0].IsMatrix() || rts[0].IsMatrix() {
|
|
|
|
op = shaderir.MatrixMul
|
|
|
|
} else {
|
|
|
|
op = shaderir.ComponentWiseMul
|
|
|
|
}
|
2020-07-04 16:47:23 +02:00
|
|
|
case token.QUO_ASSIGN:
|
|
|
|
op = shaderir.Div
|
|
|
|
case token.REM_ASSIGN:
|
|
|
|
op = shaderir.ModOp
|
2023-09-11 20:00:34 +02:00
|
|
|
case token.AND_ASSIGN:
|
|
|
|
op = shaderir.And
|
|
|
|
case token.OR_ASSIGN:
|
|
|
|
op = shaderir.Or
|
|
|
|
case token.XOR_ASSIGN:
|
|
|
|
op = shaderir.Xor
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
|
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
|
2022-01-20 19:37:31 +01:00
|
|
|
if lts[0].Main == rts[0].Main {
|
2022-07-09 15:29:42 +02:00
|
|
|
if op == shaderir.Div && rts[0].IsMatrix() {
|
2022-01-20 19:37:31 +01:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-09-12 21:17:43 +02:00
|
|
|
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
|
|
|
|
if lts[0].Main != shaderir.Int && !lts[0].IsIntVector() {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
|
|
|
}
|
|
|
|
if rts[0].Main != shaderir.Int && !rts[0].IsIntVector() {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, rts[0].String()))
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-07-09 15:29:42 +02:00
|
|
|
if lts[0].Main == shaderir.Int && rhs[0].Const != nil {
|
|
|
|
if !cs.forceToInt(stmt, &rhs[0]) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 19:37:31 +01:00
|
|
|
} else {
|
2022-01-11 16:23:55 +01:00
|
|
|
switch lts[0].Main {
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Int, shaderir.IVec2, shaderir.IVec3, shaderir.IVec4:
|
|
|
|
if rts[0].Main != shaderir.Int {
|
|
|
|
if !rts[0].Equal(&shaderir.Type{}) {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if !cs.forceToInt(stmt, &rhs[0]) {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-01-11 16:23:55 +01:00
|
|
|
}
|
|
|
|
case shaderir.Float:
|
2023-09-12 21:17:43 +02:00
|
|
|
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
|
|
|
} else if rhs[0].Const != nil &&
|
2023-07-29 07:18:29 +02:00
|
|
|
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
2022-07-09 15:29:42 +02:00
|
|
|
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown {
|
2022-01-11 16:23:55 +01:00
|
|
|
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
|
|
|
} else {
|
2022-01-17 12:52:41 +01:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
2023-09-12 21:17:43 +02:00
|
|
|
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
|
|
|
} else if (op == shaderir.MatrixMul || op == shaderir.Div) &&
|
2022-11-20 09:18:24 +01:00
|
|
|
(rts[0].Main == shaderir.Float ||
|
|
|
|
(rhs[0].Const != nil &&
|
2023-07-29 07:18:29 +02:00
|
|
|
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
2022-11-20 09:18:24 +01:00
|
|
|
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
|
|
|
|
if rhs[0].Const != nil {
|
|
|
|
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
|
|
|
}
|
2022-03-13 11:17:44 +01:00
|
|
|
} else if op == shaderir.MatrixMul && ((lts[0].Main == shaderir.Vec2 && rts[0].Main == shaderir.Mat2) ||
|
2022-01-20 17:59:54 +01:00
|
|
|
(lts[0].Main == shaderir.Vec3 && rts[0].Main == shaderir.Mat3) ||
|
2022-01-20 18:03:46 +01:00
|
|
|
(lts[0].Main == shaderir.Vec4 && rts[0].Main == shaderir.Mat4)) {
|
2022-01-20 17:59:54 +01:00
|
|
|
// OK
|
2023-07-27 17:13:40 +02:00
|
|
|
} else if (op == shaderir.MatrixMul || op == shaderir.ComponentWiseMul || lts[0].IsFloatVector()) &&
|
2022-11-20 09:18:24 +01:00
|
|
|
(rts[0].Main == shaderir.Float ||
|
|
|
|
(rhs[0].Const != nil &&
|
2023-07-29 07:18:29 +02:00
|
|
|
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
2022-11-20 09:18:24 +01:00
|
|
|
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
|
|
|
|
if rhs[0].Const != nil {
|
|
|
|
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
|
|
|
}
|
2022-01-17 12:52:41 +01:00
|
|
|
} else {
|
2022-01-11 16:23:55 +01:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
2020-09-02 19:10:52 +02:00
|
|
|
return nil, false
|
2020-08-12 06:33:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 07:36:07 +01:00
|
|
|
if op == shaderir.ModOp && lts[0].Main != shaderir.Int && lts[0].Main != shaderir.IVec2 && lts[0].Main != shaderir.IVec3 && lts[0].Main != shaderir.IVec4 {
|
2022-01-11 16:23:55 +01:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %% not defined on %s", lts[0].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
lhs[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: op,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
lhs[0],
|
|
|
|
rhs[0],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
|
|
|
|
}
|
|
|
|
case *ast.BlockStmt:
|
2022-07-10 08:45:28 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, stmt.List, inParams, outParams, returnType, true)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.BlockStmt,
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks: []*shaderir.Block{
|
2020-07-04 16:47:23 +02:00
|
|
|
b.ir,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
case *ast.DeclStmt:
|
2022-08-17 16:13:21 +02:00
|
|
|
ss, ok := cs.parseDecl(block, fname, stmt.Decl)
|
2020-07-12 08:51:06 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-12 08:51:06 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-11 14:08:19 +02:00
|
|
|
|
|
|
|
case *ast.ForStmt:
|
2023-09-03 10:15:23 +02:00
|
|
|
ss, ok := cs.parseFor(block, fname, stmt, inParams, outParams, returnType, true)
|
2020-07-11 14:08:19 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-09-03 10:15:23 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-04 15:49:29 +02:00
|
|
|
case *ast.IfStmt:
|
2020-07-04 20:29:34 +02:00
|
|
|
if stmt.Init != nil {
|
|
|
|
init := stmt.Init
|
|
|
|
stmt.Init = nil
|
2022-07-10 08:45:28 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, []ast.Stmt{init, stmt}, inParams, outParams, returnType, true)
|
2020-07-04 20:29:34 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-11 14:08:19 +02:00
|
|
|
Type: shaderir.BlockStmt,
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks: []*shaderir.Block{b.ir},
|
2020-07-04 20:29:34 +02:00
|
|
|
})
|
|
|
|
return stmts, true
|
|
|
|
}
|
2020-07-04 15:49:29 +02:00
|
|
|
|
2022-08-17 16:13:21 +02:00
|
|
|
exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.Cond, true)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
2020-07-04 20:23:56 +02:00
|
|
|
var tss []string
|
|
|
|
for _, t := range ts {
|
|
|
|
tss = append(tss, t.String())
|
|
|
|
}
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", strings.Join(tss, ", ")))
|
2020-07-04 15:49:29 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2020-08-09 08:55:59 +02:00
|
|
|
var bs []*shaderir.Block
|
2022-07-10 08:45:28 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, stmt.Body.List, inParams, outParams, returnType, true)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
|
|
|
|
if stmt.Else != nil {
|
|
|
|
switch s := stmt.Else.(type) {
|
|
|
|
case *ast.BlockStmt:
|
2022-07-10 08:45:28 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, s.List, inParams, outParams, returnType, true)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
default:
|
2022-07-10 08:45:28 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, []ast.Stmt{s}, inParams, outParams, returnType, true)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.If,
|
|
|
|
Exprs: exprs,
|
|
|
|
Blocks: bs,
|
|
|
|
})
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-12 16:07:30 +02:00
|
|
|
case *ast.IncDecStmt:
|
2022-08-17 16:13:21 +02:00
|
|
|
exprs, _, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
|
2020-07-12 16:07:30 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
var op shaderir.Op
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.INC:
|
|
|
|
op = shaderir.Add
|
|
|
|
case token.DEC:
|
|
|
|
op = shaderir.Sub
|
|
|
|
}
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
exprs[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: op,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
exprs[0],
|
|
|
|
{
|
2023-07-29 07:10:31 +02:00
|
|
|
Type: shaderir.NumberExpr,
|
|
|
|
Const: gconstant.MakeInt64(1),
|
2020-07-12 16:07:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
case *ast.ReturnStmt:
|
2020-09-12 10:44:04 +02:00
|
|
|
if len(stmt.Results) != len(outParams) && len(stmt.Results) != 1 {
|
2020-09-06 17:13:46 +02:00
|
|
|
if !(len(stmt.Results) == 0 && len(outParams) > 0 && outParams[0].name != "") {
|
2020-09-12 10:44:04 +02:00
|
|
|
// TODO: Check variable shadowings.
|
2022-11-20 17:46:50 +01:00
|
|
|
// https://go.dev/ref/spec#Return_statements
|
2020-09-06 17:13:46 +02:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-09-06 15:08:57 +02:00
|
|
|
}
|
|
|
|
|
2022-07-10 09:02:50 +02:00
|
|
|
var exprs []shaderir.Expr
|
|
|
|
var types []shaderir.Type
|
|
|
|
for _, r := range stmt.Results {
|
2022-08-17 16:13:21 +02:00
|
|
|
es, ts, ss, ok := cs.parseExpr(block, fname, r, true)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-09-12 11:54:36 +02:00
|
|
|
|
2022-07-10 09:02:50 +02:00
|
|
|
if len(es) > 1 && (len(stmt.Results) > 1 || len(outParams) == 1) {
|
|
|
|
cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
|
|
|
|
return nil, false
|
2020-09-12 11:36:22 +02:00
|
|
|
}
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
if len(outParams) > 1 && len(stmt.Results) == 1 {
|
2022-07-10 09:02:50 +02:00
|
|
|
if len(es) == 1 {
|
2020-09-12 10:44:04 +02:00
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
2020-09-12 11:36:22 +02:00
|
|
|
return nil, false
|
2020-09-12 10:44:04 +02:00
|
|
|
}
|
2022-07-10 09:02:50 +02:00
|
|
|
if len(es) > 1 && len(es) != len(outParams) {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(es)))
|
2020-09-12 11:54:36 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-09-02 19:10:52 +02:00
|
|
|
|
2022-07-10 09:02:50 +02:00
|
|
|
exprs = append(exprs, es...)
|
|
|
|
types = append(types, ts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range types {
|
|
|
|
expr := exprs[i]
|
2022-07-10 08:45:28 +02:00
|
|
|
var outT shaderir.Type
|
|
|
|
if len(outParams) == 0 {
|
|
|
|
outT = returnType
|
|
|
|
} else {
|
|
|
|
outT = outParams[i].typ
|
|
|
|
}
|
2022-08-19 08:24:00 +02:00
|
|
|
if expr.Const != nil {
|
2022-07-10 08:45:28 +02:00
|
|
|
switch outT.Main {
|
2023-07-24 19:41:41 +02:00
|
|
|
case shaderir.Bool:
|
|
|
|
if expr.Const.Kind() != gconstant.Bool {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
t = shaderir.Type{Main: shaderir.Bool}
|
2022-07-10 09:02:50 +02:00
|
|
|
case shaderir.Int:
|
2023-07-27 16:00:55 +02:00
|
|
|
if gconstant.ToInt(expr.Const).Kind() == gconstant.Unknown {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
2022-07-10 09:02:50 +02:00
|
|
|
return nil, false
|
2020-09-06 15:08:57 +02:00
|
|
|
}
|
2023-07-27 16:00:55 +02:00
|
|
|
expr.Const = gconstant.ToInt(expr.Const)
|
2022-07-10 09:02:50 +02:00
|
|
|
t = shaderir.Type{Main: shaderir.Int}
|
|
|
|
case shaderir.Float:
|
2023-07-24 19:41:41 +02:00
|
|
|
if gconstant.ToFloat(expr.Const).Kind() == gconstant.Unknown {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-07-27 16:00:55 +02:00
|
|
|
expr.Const = gconstant.ToFloat(expr.Const)
|
2022-07-10 09:02:50 +02:00
|
|
|
t = shaderir.Type{Main: shaderir.Float}
|
2020-09-02 19:10:52 +02:00
|
|
|
}
|
2022-07-10 09:02:50 +02:00
|
|
|
}
|
2020-09-02 19:10:52 +02:00
|
|
|
|
2022-07-10 08:45:28 +02:00
|
|
|
if !t.Equal(&outT) {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
2022-07-10 09:02:50 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
2020-09-06 15:08:57 +02:00
|
|
|
|
2022-07-10 08:45:28 +02:00
|
|
|
if len(outParams) > 0 {
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: len(inParams) + i,
|
|
|
|
},
|
|
|
|
expr,
|
2020-07-04 16:47:23 +02:00
|
|
|
},
|
2022-07-10 08:45:28 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Return,
|
|
|
|
Exprs: []shaderir.Expr{expr},
|
|
|
|
})
|
|
|
|
// When a return type is specified, there should be only one expr here.
|
|
|
|
break
|
|
|
|
}
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2022-07-10 09:02:50 +02:00
|
|
|
|
2022-07-10 08:45:28 +02:00
|
|
|
if len(outParams) > 0 {
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Return,
|
|
|
|
})
|
|
|
|
}
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-12 19:59:11 +02:00
|
|
|
case *ast.BranchStmt:
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.BREAK:
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Break,
|
|
|
|
})
|
|
|
|
case token.CONTINUE:
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Continue,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid token: %s", stmt.Tok))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
case *ast.ExprStmt:
|
2021-12-27 15:14:05 +01:00
|
|
|
if _, ok := stmt.X.(*ast.CallExpr); !ok {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(stmt.Pos(), "the statement is evaluated but not used")
|
2021-12-27 15:14:05 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:13:21 +02:00
|
|
|
exprs, _, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
for _, expr := range exprs {
|
2021-12-27 15:14:05 +01:00
|
|
|
// There can be a non-call expr like LocalVariable expressions.
|
|
|
|
// These are necessary to be used as arguments for an outside function callers.
|
2020-07-04 16:47:23 +02:00
|
|
|
if expr.Type != shaderir.Call {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-27 14:05:55 +01:00
|
|
|
if expr.Exprs[0].Type == shaderir.BuiltinFuncExpr {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(stmt.Pos(), "the statement is evaluated but not used")
|
2021-12-27 14:05:55 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.ExprStmt,
|
|
|
|
Exprs: []shaderir.Expr{expr},
|
|
|
|
})
|
|
|
|
}
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected statement: %#v", stmt))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
return stmts, true
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 21:59:04 +02:00
|
|
|
func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, rhs []ast.Expr, inParams []variable, define bool) ([]shaderir.Stmt, bool) {
|
2020-07-04 17:03:21 +02:00
|
|
|
var stmts []shaderir.Stmt
|
2020-07-04 16:47:23 +02:00
|
|
|
var rhsExprs []shaderir.Expr
|
|
|
|
var rhsTypes []shaderir.Type
|
2020-09-02 18:15:37 +02:00
|
|
|
allblank := true
|
2020-07-04 16:47:23 +02:00
|
|
|
|
|
|
|
for i, e := range lhs {
|
|
|
|
if len(lhs) == len(rhs) {
|
|
|
|
// Prase RHS first for the order of the statements.
|
2022-08-17 16:13:21 +02:00
|
|
|
r, rts, ss, ok := cs.parseExpr(block, fname, rhs[i], true)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
|
|
|
if define {
|
2020-08-30 14:11:27 +02:00
|
|
|
name := e.(*ast.Ident).Name
|
2020-09-02 18:27:28 +02:00
|
|
|
if name != "_" {
|
|
|
|
for _, v := range block.vars {
|
|
|
|
if v.name == name {
|
|
|
|
cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-08-30 14:11:27 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-04 16:47:23 +02:00
|
|
|
ts, ok := cs.functionReturnTypes(block, rhs[i])
|
|
|
|
if !ok {
|
2022-04-08 15:44:37 +02:00
|
|
|
ts = rts
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
if len(ts) > 1 {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2021-04-08 17:32:15 +02:00
|
|
|
t := ts[0]
|
|
|
|
if t.Main == shaderir.None {
|
|
|
|
t = toDefaultType(r[0].Const)
|
|
|
|
}
|
|
|
|
block.addNamedLocalVariable(name, t, e.Pos())
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(r) > 1 {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:13:21 +02:00
|
|
|
l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
2023-04-26 15:07:40 +02:00
|
|
|
if len(l) != len(r) {
|
|
|
|
if len(r) == 0 {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "right-hand side (no value) used as value")
|
2023-04-26 15:07:40 +02:00
|
|
|
} else {
|
|
|
|
cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but the right-hand side has %d values", len(l), len(r)))
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:15:37 +02:00
|
|
|
if l[0].Type == shaderir.Blank {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
var isAssignmentForbidden func(e *shaderir.Expr) bool
|
|
|
|
isAssignmentForbidden = func(e *shaderir.Expr) bool {
|
|
|
|
switch e.Type {
|
|
|
|
case shaderir.UniformVariable:
|
|
|
|
return true
|
|
|
|
case shaderir.LocalVariable:
|
|
|
|
if fname == cs.vertexEntry || fname == cs.fragmentEntry {
|
|
|
|
return e.Index < len(inParams)
|
|
|
|
}
|
|
|
|
case shaderir.FieldSelector:
|
|
|
|
return isAssignmentForbidden(&e.Exprs[0])
|
|
|
|
case shaderir.Index:
|
|
|
|
return isAssignmentForbidden(&e.Exprs[0])
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isAssignmentForbidden(&l[0]) {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "a uniform variable cannot be assigned")
|
2020-09-12 21:59:04 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
2020-09-02 18:15:37 +02:00
|
|
|
allblank = false
|
|
|
|
|
2022-04-08 15:44:37 +02:00
|
|
|
for i := range lts {
|
2022-11-20 07:36:07 +01:00
|
|
|
if !canAssign(<s[i], &rts[i], r[i].Const) {
|
2022-04-08 15:44:37 +02:00
|
|
|
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String()))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:40:43 +02:00
|
|
|
if len(lhs) == 1 {
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{l[0], r[0]},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// For variable swapping, use temporary variables.
|
2022-04-08 15:44:37 +02:00
|
|
|
t := rts[0]
|
2021-04-08 17:32:15 +02:00
|
|
|
if t.Main == shaderir.None {
|
|
|
|
t = toDefaultType(r[0].Const)
|
|
|
|
}
|
2020-08-16 11:40:43 +02:00
|
|
|
block.vars = append(block.vars, variable{
|
2021-04-08 17:32:15 +02:00
|
|
|
typ: t,
|
2020-08-16 11:40:43 +02:00
|
|
|
})
|
2023-09-03 18:23:01 +02:00
|
|
|
idx := block.totalLocalVariableCount() - 1
|
2020-08-16 11:40:43 +02:00
|
|
|
stmts = append(stmts,
|
|
|
|
shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: idx,
|
|
|
|
},
|
|
|
|
r[0],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
l[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: idx,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-07-04 16:47:23 +02:00
|
|
|
} else {
|
|
|
|
if i == 0 {
|
2020-07-04 17:03:21 +02:00
|
|
|
var ss []shaderir.Stmt
|
2020-07-04 16:47:23 +02:00
|
|
|
var ok bool
|
2022-08-17 16:13:21 +02:00
|
|
|
rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, fname, rhs[0], true)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
if len(rhsExprs) != len(lhs) {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if define {
|
2020-08-30 14:11:27 +02:00
|
|
|
name := e.(*ast.Ident).Name
|
2020-09-02 18:27:28 +02:00
|
|
|
if name != "_" {
|
|
|
|
for _, v := range block.vars {
|
|
|
|
if v.name == name {
|
|
|
|
cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-08-30 14:11:27 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 17:32:15 +02:00
|
|
|
t := rhsTypes[i]
|
|
|
|
if t.Main == shaderir.None {
|
2021-04-08 18:25:34 +02:00
|
|
|
// TODO: This is to determine a type when the rhs values are constants (not literals),
|
2021-04-08 17:32:15 +02:00
|
|
|
// but there are no actual cases when len(lhs) != len(rhs). Is this correct?
|
|
|
|
t = toDefaultType(rhsExprs[i].Const)
|
|
|
|
}
|
|
|
|
block.addNamedLocalVariable(name, t, e.Pos())
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:13:21 +02:00
|
|
|
l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
2023-09-03 19:21:16 +02:00
|
|
|
if len(l) != 1 {
|
|
|
|
cs.addError(pos, fmt.Sprintf("unexpected count of types in lhs: %d", len(l)))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(lts) != 1 {
|
|
|
|
cs.addError(pos, fmt.Sprintf("unexpected count of expressions in lhs: %d", len(l)))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:15:37 +02:00
|
|
|
if l[0].Type == shaderir.Blank {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
allblank = false
|
|
|
|
|
2023-09-03 19:21:16 +02:00
|
|
|
if !canAssign(<s[0], &rhsTypes[i], rhsExprs[i].Const) {
|
|
|
|
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rhsTypes[i].String(), lts[0].String()))
|
|
|
|
return nil, false
|
2022-04-08 15:44:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{l[0], rhsExprs[i]},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
if define && allblank {
|
2023-07-05 09:36:29 +02:00
|
|
|
cs.addError(pos, "no new variables on left side of :=")
|
2020-09-02 18:15:37 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
return stmts, true
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2021-04-08 17:32:15 +02:00
|
|
|
|
|
|
|
func toDefaultType(v gconstant.Value) shaderir.Type {
|
|
|
|
switch v.Kind() {
|
|
|
|
case gconstant.Bool:
|
|
|
|
return shaderir.Type{Main: shaderir.Bool}
|
|
|
|
case gconstant.Int:
|
|
|
|
return shaderir.Type{Main: shaderir.Int}
|
|
|
|
case gconstant.Float:
|
|
|
|
return shaderir.Type{Main: shaderir.Float}
|
|
|
|
}
|
|
|
|
// TODO: Should this be an error?
|
|
|
|
return shaderir.Type{}
|
|
|
|
}
|
2022-04-08 15:44:37 +02:00
|
|
|
|
2022-11-20 07:36:07 +01:00
|
|
|
func canAssign(lt *shaderir.Type, rt *shaderir.Type, rc gconstant.Value) bool {
|
2022-04-08 15:44:37 +02:00
|
|
|
if lt.Equal(rt) {
|
|
|
|
return true
|
|
|
|
}
|
2022-11-20 07:36:07 +01:00
|
|
|
|
|
|
|
if rc == nil {
|
|
|
|
return false
|
2022-04-08 15:44:37 +02:00
|
|
|
}
|
2022-11-20 07:36:07 +01:00
|
|
|
|
2022-11-20 09:18:24 +01:00
|
|
|
if !rt.Equal(&shaderir.Type{}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-20 07:36:07 +01:00
|
|
|
switch lt.Main {
|
|
|
|
case shaderir.Bool:
|
|
|
|
return rc.Kind() == gconstant.Bool
|
|
|
|
case shaderir.Int:
|
2023-07-27 16:00:55 +02:00
|
|
|
return gconstant.ToInt(rc).Kind() != gconstant.Unknown
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Float:
|
|
|
|
return gconstant.ToFloat(rc).Kind() != gconstant.Unknown
|
|
|
|
}
|
|
|
|
|
2022-04-08 15:44:37 +02:00
|
|
|
return false
|
|
|
|
}
|
2023-09-03 10:15:23 +02:00
|
|
|
|
|
|
|
func (cs *compileState) parseFor(block *block, fname string, stmt *ast.ForStmt, inParams, outParams []variable, returnType shaderir.Type, checkLocalVariableUsage bool) ([]shaderir.Stmt, bool) {
|
|
|
|
msg := "for-statement must follow this format: for (varname) := (constant); (varname) (op) (constant); (varname) (op) (constant) { ..."
|
|
|
|
if stmt.Init == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if stmt.Cond == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if stmt.Post == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new pseudo block for the initial statement, so that the counter variable belongs to the
|
|
|
|
// new pseudo block for each for-loop. Without this, the same-named counter variables in different
|
|
|
|
// for-loops confuses the parser.
|
|
|
|
pseudoBlock, ok := cs.parseBlock(block, fname, []ast.Stmt{stmt.Init}, inParams, outParams, returnType, false)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
ss := pseudoBlock.ir.Stmts
|
|
|
|
|
|
|
|
if len(ss) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ss[0].Type != shaderir.Assign {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ss[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
varidx := ss[0].Exprs[0].Index
|
|
|
|
if ss[0].Exprs[1].Const == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pseudoBlock.vars) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
vartype := pseudoBlock.vars[0].typ
|
|
|
|
init := ss[0].Exprs[1].Const
|
|
|
|
|
|
|
|
exprs, ts, ss, ok := cs.parseExpr(pseudoBlock, fname, stmt.Cond, true)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(exprs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's condition must be bool")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ss) != 0 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Type != shaderir.Binary {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
op := exprs[0].Op
|
|
|
|
if op != shaderir.LessThanOp && op != shaderir.LessThanEqualOp && op != shaderir.GreaterThanOp && op != shaderir.GreaterThanEqualOp && op != shaderir.EqualOp && op != shaderir.NotEqualOp {
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's condition must have one of these operators: <, <=, >, >=, ==, !=")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[1].Const == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
end := exprs[0].Exprs[1].Const
|
|
|
|
|
|
|
|
postSs, ok := cs.parseStmt(pseudoBlock, fname, stmt.Post, inParams, outParams, returnType)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(postSs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Type != shaderir.Assign {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Type != shaderir.Binary {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[1].Const == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
delta := postSs[0].Exprs[1].Exprs[1].Const
|
|
|
|
switch postSs[0].Exprs[1].Op {
|
|
|
|
case shaderir.Add:
|
|
|
|
case shaderir.Sub:
|
|
|
|
delta = gconstant.UnaryOp(token.SUB, delta, 0)
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's post statement must have one of these operators: +=, -=, ++, --")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok := cs.parseBlock(pseudoBlock, fname, []ast.Stmt{stmt.Body}, inParams, outParams, returnType, true)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bodyir := b.ir
|
|
|
|
for len(bodyir.Stmts) == 1 && bodyir.Stmts[0].Type == shaderir.BlockStmt {
|
|
|
|
bodyir = bodyir.Stmts[0].Blocks[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the pseudo block is not actually used, copy the variable part to the actual block.
|
|
|
|
// This must be done after parsing the for-loop is done, or the duplicated variables confuses the
|
|
|
|
// parsing.
|
|
|
|
v := pseudoBlock.vars[0]
|
|
|
|
v.forLoopCounter = true
|
|
|
|
block.vars = append(block.vars, v)
|
|
|
|
|
|
|
|
return []shaderir.Stmt{
|
|
|
|
{
|
|
|
|
Type: shaderir.For,
|
|
|
|
Blocks: []*shaderir.Block{bodyir},
|
|
|
|
ForVarType: vartype,
|
|
|
|
ForVarIndex: varidx,
|
|
|
|
ForInit: init,
|
|
|
|
ForEnd: end,
|
|
|
|
ForOp: op,
|
|
|
|
ForDelta: delta,
|
|
|
|
},
|
|
|
|
}, true
|
|
|
|
}
|