stmt.go 26 KB

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