stmt.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // Copyright 2020 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package shader
  15. import (
  16. "fmt"
  17. "go/ast"
  18. gconstant "go/constant"
  19. "go/token"
  20. "strings"
  21. "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
  22. )
  23. func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool {
  24. if !canTruncateToInteger(expr.Const) {
  25. cs.addError(node.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String()))
  26. return false
  27. }
  28. expr.Const = gconstant.ToInt(expr.Const)
  29. return true
  30. }
  31. func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inParams, outParams []variable, returnType shaderir.Type) ([]shaderir.Stmt, bool) {
  32. var stmts []shaderir.Stmt
  33. switch stmt := stmt.(type) {
  34. case *ast.AssignStmt:
  35. switch stmt.Tok {
  36. case token.DEFINE:
  37. if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
  38. cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
  39. return nil, false
  40. }
  41. ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, true)
  42. if !ok {
  43. return nil, false
  44. }
  45. stmts = append(stmts, ss...)
  46. case token.ASSIGN:
  47. if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
  48. cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
  49. return nil, false
  50. }
  51. ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, false)
  52. if !ok {
  53. return nil, false
  54. }
  55. stmts = append(stmts, ss...)
  56. 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, token.SHL_ASSIGN, token.SHR_ASSIGN:
  57. rhs, rts, ss, ok := cs.parseExpr(block, fname, stmt.Rhs[0], true)
  58. if !ok {
  59. return nil, false
  60. }
  61. stmts = append(stmts, ss...)
  62. lhs, lts, ss, ok := cs.parseExpr(block, fname, stmt.Lhs[0], true)
  63. if !ok {
  64. return nil, false
  65. }
  66. stmts = append(stmts, ss...)
  67. if lhs[0].Type == shaderir.UniformVariable {
  68. cs.addError(stmt.Pos(), "a uniform variable cannot be assigned")
  69. return nil, false
  70. }
  71. var op shaderir.Op
  72. switch stmt.Tok {
  73. case token.ADD_ASSIGN:
  74. op = shaderir.Add
  75. case token.SUB_ASSIGN:
  76. op = shaderir.Sub
  77. case token.MUL_ASSIGN:
  78. if lts[0].IsMatrix() || rts[0].IsMatrix() {
  79. op = shaderir.MatrixMul
  80. } else {
  81. op = shaderir.ComponentWiseMul
  82. }
  83. case token.QUO_ASSIGN:
  84. op = shaderir.Div
  85. case token.REM_ASSIGN:
  86. op = shaderir.ModOp
  87. case token.AND_ASSIGN:
  88. op = shaderir.And
  89. case token.OR_ASSIGN:
  90. op = shaderir.Or
  91. case token.XOR_ASSIGN:
  92. op = shaderir.Xor
  93. case token.SHL_ASSIGN:
  94. op = shaderir.LeftShift
  95. case token.SHR_ASSIGN:
  96. op = shaderir.RightShift
  97. default:
  98. cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
  99. return nil, false
  100. }
  101. if lts[0].Main == rts[0].Main {
  102. if op == shaderir.Div && rts[0].IsMatrix() {
  103. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
  104. return nil, false
  105. }
  106. if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
  107. if lts[0].Main != shaderir.Int && !lts[0].IsIntVector() {
  108. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
  109. return nil, false
  110. }
  111. if rts[0].Main != shaderir.Int && !rts[0].IsIntVector() {
  112. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, rts[0].String()))
  113. return nil, false
  114. }
  115. }
  116. if lts[0].Main == shaderir.Int && rhs[0].Const != nil {
  117. if !cs.forceToInt(stmt, &rhs[0]) {
  118. return nil, false
  119. }
  120. }
  121. } else {
  122. switch lts[0].Main {
  123. case shaderir.Int, shaderir.IVec2, shaderir.IVec3, shaderir.IVec4:
  124. if rts[0].Main != shaderir.Int {
  125. if !rts[0].Equal(&shaderir.Type{}) {
  126. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
  127. return nil, false
  128. }
  129. if !cs.forceToInt(stmt, &rhs[0]) {
  130. return nil, false
  131. }
  132. }
  133. case shaderir.Float:
  134. if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
  135. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
  136. } else if rhs[0].Const != nil &&
  137. (rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
  138. gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown {
  139. rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
  140. } else {
  141. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
  142. return nil, false
  143. }
  144. case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
  145. if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
  146. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
  147. } else if (op == shaderir.MatrixMul || op == shaderir.Div) &&
  148. (rts[0].Main == shaderir.Float ||
  149. (rhs[0].Const != nil &&
  150. (rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
  151. gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
  152. if rhs[0].Const != nil {
  153. rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
  154. }
  155. } else if op == shaderir.MatrixMul && ((lts[0].Main == shaderir.Vec2 && rts[0].Main == shaderir.Mat2) ||
  156. (lts[0].Main == shaderir.Vec3 && rts[0].Main == shaderir.Mat3) ||
  157. (lts[0].Main == shaderir.Vec4 && rts[0].Main == shaderir.Mat4)) {
  158. // OK
  159. } else if (op == shaderir.MatrixMul || op == shaderir.ComponentWiseMul || lts[0].IsFloatVector()) &&
  160. (rts[0].Main == shaderir.Float ||
  161. (rhs[0].Const != nil &&
  162. (rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
  163. gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
  164. if rhs[0].Const != nil {
  165. rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
  166. }
  167. } else {
  168. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
  169. return nil, false
  170. }
  171. default:
  172. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
  173. return nil, false
  174. }
  175. }
  176. if op == shaderir.ModOp && lts[0].Main != shaderir.Int && lts[0].Main != shaderir.IVec2 && lts[0].Main != shaderir.IVec3 && lts[0].Main != shaderir.IVec4 {
  177. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %% not defined on %s", lts[0].String()))
  178. return nil, false
  179. }
  180. stmts = append(stmts, shaderir.Stmt{
  181. Type: shaderir.Assign,
  182. Exprs: []shaderir.Expr{
  183. lhs[0],
  184. {
  185. Type: shaderir.Binary,
  186. Op: op,
  187. Exprs: []shaderir.Expr{
  188. lhs[0],
  189. rhs[0],
  190. },
  191. },
  192. },
  193. })
  194. default:
  195. cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
  196. }
  197. case *ast.BlockStmt:
  198. b, ok := cs.parseBlock(block, fname, stmt.List, inParams, outParams, returnType, true)
  199. if !ok {
  200. return nil, false
  201. }
  202. stmts = append(stmts, shaderir.Stmt{
  203. Type: shaderir.BlockStmt,
  204. Blocks: []*shaderir.Block{
  205. b.ir,
  206. },
  207. })
  208. case *ast.DeclStmt:
  209. ss, ok := cs.parseDecl(block, fname, stmt.Decl)
  210. if !ok {
  211. return nil, false
  212. }
  213. stmts = append(stmts, ss...)
  214. case *ast.ForStmt:
  215. ss, ok := cs.parseFor(block, fname, stmt, inParams, outParams, returnType, true)
  216. if !ok {
  217. return nil, false
  218. }
  219. stmts = append(stmts, ss...)
  220. case *ast.IfStmt:
  221. if stmt.Init != nil {
  222. init := stmt.Init
  223. stmt.Init = nil
  224. b, ok := cs.parseBlock(block, fname, []ast.Stmt{init, stmt}, inParams, outParams, returnType, true)
  225. if !ok {
  226. return nil, false
  227. }
  228. stmts = append(stmts, shaderir.Stmt{
  229. Type: shaderir.BlockStmt,
  230. Blocks: []*shaderir.Block{b.ir},
  231. })
  232. return stmts, true
  233. }
  234. exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.Cond, true)
  235. if !ok {
  236. return nil, false
  237. }
  238. if len(ts) != 1 {
  239. var tss []string
  240. for _, t := range ts {
  241. tss = append(tss, t.String())
  242. }
  243. cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", strings.Join(tss, ", ")))
  244. return nil, false
  245. }
  246. if !(ts[0].Main == shaderir.Bool || (ts[0].Main == shaderir.None && exprs[0].Const != nil && exprs[0].Const.Kind() == gconstant.Bool)) {
  247. cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", ts[0].String()))
  248. return nil, false
  249. }
  250. stmts = append(stmts, ss...)
  251. var bs []*shaderir.Block
  252. b, ok := cs.parseBlock(block, fname, stmt.Body.List, inParams, outParams, returnType, true)
  253. if !ok {
  254. return nil, false
  255. }
  256. bs = append(bs, b.ir)
  257. if stmt.Else != nil {
  258. switch s := stmt.Else.(type) {
  259. case *ast.BlockStmt:
  260. b, ok := cs.parseBlock(block, fname, s.List, inParams, outParams, returnType, true)
  261. if !ok {
  262. return nil, false
  263. }
  264. bs = append(bs, b.ir)
  265. default:
  266. b, ok := cs.parseBlock(block, fname, []ast.Stmt{s}, inParams, outParams, returnType, true)
  267. if !ok {
  268. return nil, false
  269. }
  270. bs = append(bs, b.ir)
  271. }
  272. }
  273. stmts = append(stmts, shaderir.Stmt{
  274. Type: shaderir.If,
  275. Exprs: exprs,
  276. Blocks: bs,
  277. })
  278. case *ast.IncDecStmt:
  279. exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
  280. if !ok {
  281. return nil, false
  282. }
  283. stmts = append(stmts, ss...)
  284. var op shaderir.Op
  285. switch stmt.Tok {
  286. case token.INC:
  287. op = shaderir.Add
  288. case token.DEC:
  289. op = shaderir.Sub
  290. }
  291. var c gconstant.Value
  292. switch {
  293. case ts[0].Main == shaderir.Int, ts[0].IsIntVector():
  294. c = gconstant.MakeInt64(1)
  295. case ts[0].Main == shaderir.Float, ts[0].IsFloatVector():
  296. c = gconstant.MakeFloat64(1)
  297. default:
  298. cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation %s (non-numeric type %s)", stmt.Tok.String(), ts[0].String()))
  299. return nil, false
  300. }
  301. stmts = append(stmts, shaderir.Stmt{
  302. Type: shaderir.Assign,
  303. Exprs: []shaderir.Expr{
  304. exprs[0],
  305. {
  306. Type: shaderir.Binary,
  307. Op: op,
  308. Exprs: []shaderir.Expr{
  309. exprs[0],
  310. {
  311. Type: shaderir.NumberExpr,
  312. Const: c,
  313. },
  314. },
  315. },
  316. },
  317. })
  318. case *ast.ReturnStmt:
  319. if len(stmt.Results) != len(outParams) && len(stmt.Results) != 1 {
  320. if !(len(stmt.Results) == 0 && len(outParams) > 0 && outParams[0].name != "") {
  321. // TODO: Check variable shadowings.
  322. // https://go.dev/ref/spec#Return_statements
  323. cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
  324. return nil, false
  325. }
  326. }
  327. var exprs []shaderir.Expr
  328. var types []shaderir.Type
  329. for _, r := range stmt.Results {
  330. es, ts, ss, ok := cs.parseExpr(block, fname, r, true)
  331. if !ok {
  332. return nil, false
  333. }
  334. stmts = append(stmts, ss...)
  335. if len(es) > 1 && (len(stmt.Results) > 1 || len(outParams) == 1) {
  336. cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
  337. return nil, false
  338. }
  339. if len(outParams) > 1 && len(stmt.Results) == 1 {
  340. if len(es) == 1 {
  341. cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
  342. return nil, false
  343. }
  344. if len(es) > 1 && len(es) != len(outParams) {
  345. cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(es)))
  346. return nil, false
  347. }
  348. }
  349. exprs = append(exprs, es...)
  350. types = append(types, ts...)
  351. }
  352. for i, t := range types {
  353. expr := exprs[i]
  354. var outT shaderir.Type
  355. if len(outParams) == 0 {
  356. outT = returnType
  357. } else {
  358. outT = outParams[i].typ
  359. }
  360. if expr.Const != nil {
  361. switch outT.Main {
  362. case shaderir.Bool:
  363. if expr.Const.Kind() != gconstant.Bool {
  364. cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
  365. return nil, false
  366. }
  367. t = shaderir.Type{Main: shaderir.Bool}
  368. case shaderir.Int:
  369. if gconstant.ToInt(expr.Const).Kind() == gconstant.Unknown {
  370. cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
  371. return nil, false
  372. }
  373. expr.Const = gconstant.ToInt(expr.Const)
  374. t = shaderir.Type{Main: shaderir.Int}
  375. case shaderir.Float:
  376. if gconstant.ToFloat(expr.Const).Kind() == gconstant.Unknown {
  377. cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
  378. return nil, false
  379. }
  380. expr.Const = gconstant.ToFloat(expr.Const)
  381. t = shaderir.Type{Main: shaderir.Float}
  382. }
  383. }
  384. if !t.Equal(&outT) {
  385. cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
  386. return nil, false
  387. }
  388. if len(outParams) > 0 {
  389. stmts = append(stmts, shaderir.Stmt{
  390. Type: shaderir.Assign,
  391. Exprs: []shaderir.Expr{
  392. {
  393. Type: shaderir.LocalVariable,
  394. Index: len(inParams) + i,
  395. },
  396. expr,
  397. },
  398. })
  399. } else {
  400. stmts = append(stmts, shaderir.Stmt{
  401. Type: shaderir.Return,
  402. Exprs: []shaderir.Expr{expr},
  403. })
  404. // When a return type is specified, there should be only one expr here.
  405. break
  406. }
  407. }
  408. if len(outParams) > 0 {
  409. stmts = append(stmts, shaderir.Stmt{
  410. Type: shaderir.Return,
  411. })
  412. }
  413. case *ast.BranchStmt:
  414. switch stmt.Tok {
  415. case token.BREAK:
  416. stmts = append(stmts, shaderir.Stmt{
  417. Type: shaderir.Break,
  418. })
  419. case token.CONTINUE:
  420. stmts = append(stmts, shaderir.Stmt{
  421. Type: shaderir.Continue,
  422. })
  423. default:
  424. cs.addError(stmt.Pos(), fmt.Sprintf("invalid token: %s", stmt.Tok))
  425. return nil, false
  426. }
  427. case *ast.ExprStmt:
  428. if _, ok := stmt.X.(*ast.CallExpr); !ok {
  429. cs.addError(stmt.Pos(), "the statement is evaluated but not used")
  430. return nil, false
  431. }
  432. exprs, _, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
  433. if !ok {
  434. return nil, false
  435. }
  436. stmts = append(stmts, ss...)
  437. for _, expr := range exprs {
  438. // There can be a non-call expr like LocalVariable expressions.
  439. // These are necessary to be used as arguments for an outside function callers.
  440. if expr.Type != shaderir.Call {
  441. continue
  442. }
  443. if expr.Exprs[0].Type == shaderir.BuiltinFuncExpr {
  444. cs.addError(stmt.Pos(), "the statement is evaluated but not used")
  445. return nil, false
  446. }
  447. stmts = append(stmts, shaderir.Stmt{
  448. Type: shaderir.ExprStmt,
  449. Exprs: []shaderir.Expr{expr},
  450. })
  451. }
  452. default:
  453. cs.addError(stmt.Pos(), fmt.Sprintf("unexpected statement: %#v", stmt))
  454. return nil, false
  455. }
  456. return stmts, true
  457. }
  458. func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, rhs []ast.Expr, inParams []variable, define bool) ([]shaderir.Stmt, bool) {
  459. var stmts []shaderir.Stmt
  460. var rhsExprs []shaderir.Expr
  461. var rhsTypes []shaderir.Type
  462. allblank := true
  463. if len(lhs) == len(rhs) {
  464. for i, e := range lhs {
  465. // Prase RHS first for the order of the statements.
  466. r, rts, ss, ok := cs.parseExpr(block, fname, rhs[i], true)
  467. if !ok {
  468. return nil, false
  469. }
  470. stmts = append(stmts, ss...)
  471. if define {
  472. if _, ok := e.(*ast.Ident); !ok {
  473. cs.addError(pos, "non-name on the left side of :=")
  474. return nil, false
  475. }
  476. name := e.(*ast.Ident).Name
  477. if name != "_" {
  478. for _, v := range block.vars {
  479. if v.name == name {
  480. cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
  481. return nil, false
  482. }
  483. }
  484. }
  485. ts, ok := cs.functionReturnTypes(block, rhs[i])
  486. if !ok {
  487. ts = rts
  488. }
  489. if len(ts) > 1 {
  490. cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
  491. return nil, false
  492. }
  493. t := ts[0]
  494. if t.Main == shaderir.None {
  495. t = toDefaultType(r[0].Const)
  496. }
  497. block.addNamedLocalVariable(name, t, e.Pos())
  498. }
  499. if len(r) > 1 {
  500. cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
  501. return nil, false
  502. }
  503. l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
  504. if !ok {
  505. return nil, false
  506. }
  507. stmts = append(stmts, ss...)
  508. if len(l) != len(r) {
  509. if len(r) == 0 {
  510. cs.addError(pos, "right-hand side (no value) used as value")
  511. } else {
  512. cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but the right-hand side has %d values", len(l), len(r)))
  513. }
  514. return nil, false
  515. }
  516. if l[0].Type == shaderir.Blank {
  517. continue
  518. }
  519. var isAssignmentForbidden func(e *shaderir.Expr) bool
  520. isAssignmentForbidden = func(e *shaderir.Expr) bool {
  521. switch e.Type {
  522. case shaderir.UniformVariable:
  523. return true
  524. case shaderir.LocalVariable:
  525. if fname == cs.vertexEntry || fname == cs.fragmentEntry {
  526. return e.Index < len(inParams)
  527. }
  528. case shaderir.FieldSelector:
  529. return isAssignmentForbidden(&e.Exprs[0])
  530. case shaderir.Index:
  531. return isAssignmentForbidden(&e.Exprs[0])
  532. }
  533. return false
  534. }
  535. if isAssignmentForbidden(&l[0]) {
  536. cs.addError(pos, "a uniform variable cannot be assigned")
  537. return nil, false
  538. }
  539. allblank = false
  540. for i := range lts {
  541. if !canAssign(&lts[i], &rts[i], r[i].Const) {
  542. cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String()))
  543. return nil, false
  544. }
  545. switch lts[0].Main {
  546. case shaderir.Int:
  547. r[i].Const = gconstant.ToInt(r[i].Const)
  548. case shaderir.Float:
  549. r[i].Const = gconstant.ToFloat(r[i].Const)
  550. }
  551. }
  552. if len(lhs) == 1 {
  553. stmts = append(stmts, shaderir.Stmt{
  554. Type: shaderir.Assign,
  555. Exprs: []shaderir.Expr{l[0], r[0]},
  556. })
  557. } else {
  558. // For variable swapping, use temporary variables.
  559. t := rts[0]
  560. if t.Main == shaderir.None {
  561. t = toDefaultType(r[0].Const)
  562. }
  563. block.vars = append(block.vars, variable{
  564. typ: t,
  565. })
  566. idx := block.totalLocalVariableCount() - 1
  567. stmts = append(stmts,
  568. shaderir.Stmt{
  569. Type: shaderir.Assign,
  570. Exprs: []shaderir.Expr{
  571. {
  572. Type: shaderir.LocalVariable,
  573. Index: idx,
  574. },
  575. r[0],
  576. },
  577. },
  578. shaderir.Stmt{
  579. Type: shaderir.Assign,
  580. Exprs: []shaderir.Expr{
  581. l[0],
  582. {
  583. Type: shaderir.LocalVariable,
  584. Index: idx,
  585. },
  586. },
  587. })
  588. }
  589. }
  590. } else {
  591. var ss []shaderir.Stmt
  592. var ok bool
  593. rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, fname, rhs[0], true)
  594. if !ok {
  595. return nil, false
  596. }
  597. if len(lhs) != len(rhsExprs) {
  598. cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but %d", len(lhs), len(rhsExprs)))
  599. return nil, false
  600. }
  601. stmts = append(stmts, ss...)
  602. for i, e := range lhs {
  603. if define {
  604. if _, ok := e.(*ast.Ident); !ok {
  605. cs.addError(pos, "non-name on the left side of :=")
  606. return nil, false
  607. }
  608. name := e.(*ast.Ident).Name
  609. if name != "_" {
  610. for _, v := range block.vars {
  611. if v.name == name {
  612. cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
  613. return nil, false
  614. }
  615. }
  616. }
  617. t := rhsTypes[i]
  618. if t.Main == shaderir.None {
  619. // TODO: This is to determine a type when the rhs values are constants (not literals),
  620. // but there are no actual cases when len(lhs) != len(rhs). Is this correct?
  621. t = toDefaultType(rhsExprs[i].Const)
  622. }
  623. block.addNamedLocalVariable(name, t, e.Pos())
  624. }
  625. l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
  626. if !ok {
  627. return nil, false
  628. }
  629. stmts = append(stmts, ss...)
  630. if len(l) != 1 {
  631. cs.addError(pos, fmt.Sprintf("unexpected count of types in lhs: %d", len(l)))
  632. return nil, false
  633. }
  634. if len(lts) != 1 {
  635. cs.addError(pos, fmt.Sprintf("unexpected count of expressions in lhs: %d", len(l)))
  636. return nil, false
  637. }
  638. if l[0].Type == shaderir.Blank {
  639. continue
  640. }
  641. allblank = false
  642. if !canAssign(&lts[0], &rhsTypes[i], rhsExprs[i].Const) {
  643. cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rhsTypes[i].String(), lts[0].String()))
  644. return nil, false
  645. }
  646. stmts = append(stmts, shaderir.Stmt{
  647. Type: shaderir.Assign,
  648. Exprs: []shaderir.Expr{l[0], rhsExprs[i]},
  649. })
  650. }
  651. }
  652. if define && allblank {
  653. cs.addError(pos, "no new variables on left side of :=")
  654. return nil, false
  655. }
  656. return stmts, true
  657. }
  658. func toDefaultType(v gconstant.Value) shaderir.Type {
  659. switch v.Kind() {
  660. case gconstant.Bool:
  661. return shaderir.Type{Main: shaderir.Bool}
  662. case gconstant.Int:
  663. return shaderir.Type{Main: shaderir.Int}
  664. case gconstant.Float:
  665. return shaderir.Type{Main: shaderir.Float}
  666. }
  667. // TODO: Should this be an error?
  668. return shaderir.Type{}
  669. }
  670. func canAssign(lt *shaderir.Type, rt *shaderir.Type, rc gconstant.Value) bool {
  671. if lt.Equal(rt) {
  672. return true
  673. }
  674. if rc == nil {
  675. return false
  676. }
  677. if !rt.Equal(&shaderir.Type{}) {
  678. return false
  679. }
  680. switch lt.Main {
  681. case shaderir.Bool:
  682. return rc.Kind() == gconstant.Bool
  683. case shaderir.Int:
  684. return gconstant.ToInt(rc).Kind() != gconstant.Unknown
  685. case shaderir.Float:
  686. return gconstant.ToFloat(rc).Kind() != gconstant.Unknown
  687. }
  688. return false
  689. }
  690. func (cs *compileState) parseFor(block *block, fname string, stmt *ast.ForStmt, inParams, outParams []variable, returnType shaderir.Type, checkLocalVariableUsage bool) ([]shaderir.Stmt, bool) {
  691. msg := "for-statement must follow this format: for (varname) := (constant); (varname) (op) (constant); (varname) (op) (constant) { ..."
  692. if stmt.Init == nil {
  693. cs.addError(stmt.Pos(), msg)
  694. return nil, false
  695. }
  696. if stmt.Cond == nil {
  697. cs.addError(stmt.Pos(), msg)
  698. return nil, false
  699. }
  700. if stmt.Post == nil {
  701. cs.addError(stmt.Pos(), msg)
  702. return nil, false
  703. }
  704. // Create a new pseudo block for the initial statement, so that the counter variable belongs to the
  705. // new pseudo block for each for-loop. Without this, the same-named counter variables in different
  706. // for-loops confuses the parser.
  707. pseudoBlock, ok := cs.parseBlock(block, fname, []ast.Stmt{stmt.Init}, inParams, outParams, returnType, false)
  708. if !ok {
  709. return nil, false
  710. }
  711. ss := pseudoBlock.ir.Stmts
  712. if len(ss) != 1 {
  713. cs.addError(stmt.Pos(), msg)
  714. return nil, false
  715. }
  716. if ss[0].Type != shaderir.Assign {
  717. cs.addError(stmt.Pos(), msg)
  718. return nil, false
  719. }
  720. if ss[0].Exprs[0].Type != shaderir.LocalVariable {
  721. cs.addError(stmt.Pos(), msg)
  722. return nil, false
  723. }
  724. varidx := ss[0].Exprs[0].Index
  725. if ss[0].Exprs[1].Const == nil {
  726. cs.addError(stmt.Pos(), msg)
  727. return nil, false
  728. }
  729. if len(pseudoBlock.vars) != 1 {
  730. cs.addError(stmt.Pos(), msg)
  731. return nil, false
  732. }
  733. vartype := pseudoBlock.vars[0].typ
  734. init := ss[0].Exprs[1].Const
  735. exprs, ts, ss, ok := cs.parseExpr(pseudoBlock, fname, stmt.Cond, true)
  736. if !ok {
  737. return nil, false
  738. }
  739. if len(exprs) != 1 {
  740. cs.addError(stmt.Pos(), msg)
  741. return nil, false
  742. }
  743. if len(ts) != 1 || ts[0].Main != shaderir.Bool {
  744. cs.addError(stmt.Pos(), "for-statement's condition must be bool")
  745. return nil, false
  746. }
  747. if len(ss) != 0 {
  748. cs.addError(stmt.Pos(), msg)
  749. return nil, false
  750. }
  751. if exprs[0].Type != shaderir.Binary {
  752. cs.addError(stmt.Pos(), msg)
  753. return nil, false
  754. }
  755. op := exprs[0].Op
  756. if op != shaderir.LessThanOp && op != shaderir.LessThanEqualOp && op != shaderir.GreaterThanOp && op != shaderir.GreaterThanEqualOp && op != shaderir.EqualOp && op != shaderir.NotEqualOp {
  757. cs.addError(stmt.Pos(), "for-statement's condition must have one of these operators: <, <=, >, >=, ==, !=")
  758. return nil, false
  759. }
  760. if exprs[0].Exprs[0].Type != shaderir.LocalVariable {
  761. cs.addError(stmt.Pos(), msg)
  762. return nil, false
  763. }
  764. if exprs[0].Exprs[0].Index != varidx {
  765. cs.addError(stmt.Pos(), msg)
  766. return nil, false
  767. }
  768. if exprs[0].Exprs[1].Const == nil {
  769. cs.addError(stmt.Pos(), msg)
  770. return nil, false
  771. }
  772. end := exprs[0].Exprs[1].Const
  773. postSs, ok := cs.parseStmt(pseudoBlock, fname, stmt.Post, inParams, outParams, returnType)
  774. if !ok {
  775. return nil, false
  776. }
  777. if len(postSs) != 1 {
  778. cs.addError(stmt.Pos(), msg)
  779. return nil, false
  780. }
  781. if postSs[0].Type != shaderir.Assign {
  782. cs.addError(stmt.Pos(), msg)
  783. return nil, false
  784. }
  785. if postSs[0].Exprs[0].Type != shaderir.LocalVariable {
  786. cs.addError(stmt.Pos(), msg)
  787. return nil, false
  788. }
  789. if postSs[0].Exprs[0].Index != varidx {
  790. cs.addError(stmt.Pos(), msg)
  791. return nil, false
  792. }
  793. if postSs[0].Exprs[1].Type != shaderir.Binary {
  794. cs.addError(stmt.Pos(), msg)
  795. return nil, false
  796. }
  797. if postSs[0].Exprs[1].Exprs[0].Type != shaderir.LocalVariable {
  798. cs.addError(stmt.Pos(), msg)
  799. return nil, false
  800. }
  801. if postSs[0].Exprs[1].Exprs[0].Index != varidx {
  802. cs.addError(stmt.Pos(), msg)
  803. return nil, false
  804. }
  805. if postSs[0].Exprs[1].Exprs[1].Const == nil {
  806. cs.addError(stmt.Pos(), msg)
  807. return nil, false
  808. }
  809. delta := postSs[0].Exprs[1].Exprs[1].Const
  810. switch postSs[0].Exprs[1].Op {
  811. case shaderir.Add:
  812. case shaderir.Sub:
  813. delta = gconstant.UnaryOp(token.SUB, delta, 0)
  814. default:
  815. cs.addError(stmt.Pos(), "for-statement's post statement must have one of these operators: +=, -=, ++, --")
  816. return nil, false
  817. }
  818. b, ok := cs.parseBlock(pseudoBlock, fname, []ast.Stmt{stmt.Body}, inParams, outParams, returnType, true)
  819. if !ok {
  820. return nil, false
  821. }
  822. bodyir := b.ir
  823. for len(bodyir.Stmts) == 1 && bodyir.Stmts[0].Type == shaderir.BlockStmt {
  824. bodyir = bodyir.Stmts[0].Blocks[0]
  825. }
  826. // As the pseudo block is not actually used, copy the variable part to the actual block.
  827. // This must be done after parsing the for-loop is done, or the duplicated variables confuses the
  828. // parsing.
  829. v := pseudoBlock.vars[0]
  830. v.forLoopCounter = true
  831. block.vars = append(block.vars, v)
  832. return []shaderir.Stmt{
  833. {
  834. Type: shaderir.For,
  835. Blocks: []*shaderir.Block{bodyir},
  836. ForVarType: vartype,
  837. ForVarIndex: varidx,
  838. ForInit: init,
  839. ForEnd: end,
  840. ForOp: op,
  841. ForDelta: delta,
  842. },
  843. }, true
  844. }