Repository: goplus/gop
File: cl/classfile.go (Go)

Code before:
			parent.tylds = append(parent.tylds, ld)
		}
	}
	ld.methods = append(ld.methods, func() {
		old, _ := pkg.SetCurFile(defaultGoFile, true)
		defer pkg.RestoreCurFile(old)
		doInitType(ld)

		t := pkg.Ref(classType).Type()
		recv := types.NewParam(token.NoPos, pkg.Types, "this", types.NewPointer(t))
		fn := pkg.NewFunc(recv, "Main", nil, nil, false)

		parent.inits = append(parent.inits, func() {
			old, _ := pkg.SetCurFile(defaultGoFile, true)
			defer pkg.RestoreCurFile(old)

			cb := fn.BodyStart(pkg).Typ(base.Type()).MemberVal("Main")
			stk := cb.InternalStack()

			// force remove //line comments for main func
			cb.SetComments(nil, false)

			mainFn := stk.Pop()
			sigParams := mainFn.Type.(*types.Signature).Params()
			callMain := func() {
				src := parent.node(filepath.Join(parent.nodeInterp.relBaseDir, proj.gameClass_+proj.gt.Ext))
				stk.Push(mainFn)
				if _, isPtr := sigParams.At(0).Type().(*types.Pointer); isPtr {
					cb.Val(recv, src).MemberRef(base.Name()).UnaryOp(gotoken.AND)
				} else {
					cb.Val(recv, src) // template recv method
				}
			}

			iobj := 0
			narg := sigParams.Len()
			if narg > 1 {
				sprites := proj.sprites
				if len(sprites) == 1 && sprites[0].proto == "" { // no work class prototype
					sp := sprites[0]
					narg = 1 + len(sp.types)
					genWorkClasses(pkg, parent, cb, recv, sp, iobj, -1, callMain)
				} else {
					lstNames := make([]string, narg)
					for i := 1; i < narg; i++ {
						tslice := sigParams.At(i).Type()
						tn := tslice.(*types.Slice).Elem().(*types.Named)
						sp := spriteByProto(sprites, tn.Obj().Name()) // work class
						if n := len(sp.types); n > 0 {
							lstNames[i] = genWorkClasses(pkg, parent, cb, recv, sp, iobj, i, nil)
							cb.SliceLitEx(tslice, n, false).EndInit(1)

Review comment (suggestion):
可以从 parent.classes 中获取 proj => ast.File
``` 
for f, cls := range parent.classes {
	if cls.proj == proj {
		// f.Name
	}
}
```

Code after:
			parent.tylds = append(parent.tylds, ld)
		}
	}
	ld.methods = append(ld.methods, func() {
		old, _ := pkg.SetCurFile(defaultGoFile, true)
		defer pkg.RestoreCurFile(old)
		doInitType(ld)

		t := pkg.Ref(classType).Type()
		recv := types.NewParam(token.NoPos, pkg.Types, "this", types.NewPointer(t))
		fn := pkg.NewFunc(recv, "Main", nil, nil, false)

		parent.inits = append(parent.inits, func() {
			old, _ := pkg.SetCurFile(defaultGoFile, true)
			defer pkg.RestoreCurFile(old)

			cb := fn.BodyStart(pkg).Typ(base.Type()).MemberVal("Main")
			stk := cb.InternalStack()

			// force remove //line comments for main func
			cb.SetComments(nil, false)

			mainFn := stk.Pop()
			sigParams := mainFn.Type.(*types.Signature).Params()
			callMain := func() {
				src := parent.lookupClassNode(proj.gameClass_)
				stk.Push(mainFn)
				if _, isPtr := sigParams.At(0).Type().(*types.Pointer); isPtr {
					cb.Val(recv, src).MemberRef(base.Name()).UnaryOp(gotoken.AND)
				} else {
					cb.Val(recv, src) // template recv method
				}
			}

			iobj := 0
			narg := sigParams.Len()
			if narg > 1 {
				sprites := proj.sprites
				if len(sprites) == 1 && sprites[0].proto == "" { // no work class prototype
					sp := sprites[0]
					narg = 1 + len(sp.types)
					genWorkClasses(pkg, parent, cb, recv, sp, iobj, -1, callMain)
				} else {
					lstNames := make([]string, narg)
					for i := 1; i < narg; i++ {
						tslice := sigParams.At(i).Type()
						tn := tslice.(*types.Slice).Elem().(*types.Named)
						sp := spriteByProto(sprites, tn.Obj().Name()) // work class
						if n := len(sp.types); n > 0 {
							lstNames[i] = genWorkClasses(pkg, parent, cb, recv, sp, iobj, i, nil)
							cb.SliceLitEx(tslice, n, false).EndInit(1)