Ident GetFuncSelf() { Node n = this.Parent; while (!(n is FuncDeclare)) { if (n == null) { break; } n = n.Parent; } FuncDeclare funcD = n as FuncDeclare; if (funcD == null) { throw new CompileException("'base' should be used in class method", DotPos); } if (funcD.TypeInfo.Params.Count == 0) { throw new CompileException("Expect 'self' in method", funcD.TypeInfo.FuncPos); } var selfIDent = funcD.TypeInfo.Params[0]; if (selfIDent.Symbol == null) { throw new CompileException("invalid method param", funcD.TypeInfo.FuncPos); } if (selfIDent.Symbol.Usage != SymbolUsage.SelfParameter) { throw new CompileException("invalid self method param", funcD.TypeInfo.FuncPos); } return(selfIDent); }
Stmt ParseFuncDecl() { var funcPos = CurrTokenPos; Expect(TokenType.Func); var scope = ScopeMgr.OpenScope(ScopeType.Function, funcPos); Ident funcName; Ident className = null; var NameA = ParseIdent(); Scope funcAtScope; if (CurrTokenType == TokenType.Dot) { Next(); funcName = ParseIdent(); className = NameA; funcAtScope = ScopeMgr.GetClassScope(className.Name); // 主定义文件还未出现, 所以暂时创建空的scope if (funcAtScope == null) { funcAtScope = ScopeMgr.OpenClassScope(className.Name, funcPos); ScopeMgr.CloseScope(); } } else { funcAtScope = ScopeMgr.PackageScope; funcName = NameA; } var paramlist = ParseParameters(scope, className != null); if (CurrTokenType == TokenType.LBrace) { var decl = new FuncDeclare(funcName, new FuncType(funcPos, paramlist, scope)); decl.ClassName = className; funcName.Symbol = ScopeManager.Declare(decl, funcAtScope, funcName.Name, funcName.DefinePos, SymbolUsage.Func); decl.Body = ParseBody(scope); decl.BuildRelation(); return(decl); } else { // 声明已经结束 ScopeMgr.CloseScope(); // 函数前置声明 var decl = new DelegateDeclare(funcName, new FuncType(funcPos, paramlist, scope)); funcName.Symbol = ScopeManager.Declare(decl, ScopeMgr.PackageScope, funcName.Name, funcName.DefinePos, SymbolUsage.Delegate); return(decl); } }