MCPcopy Create free account
hub / github.com/crossoverJie/gscript / ExitVariableDeclarator

Method ExitVariableDeclarator

semantic_resolver.go:126–185  ·  view source on GitHub ↗

ExitVariableDeclarator 赋值类型校验

(ctx *parser.VariableDeclaratorContext)

Source from the content-addressed store, hash-verified

124
125// ExitVariableDeclarator 赋值类型校验
126func (s *SemanticResolver) ExitVariableDeclarator(ctx *parser.VariableDeclaratorContext) {
127 if ctx.VariableDeclaratorId() == nil {
128 return
129 }
130 sym := s.at.GetSymbolOfNode()[ctx.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext)]
131 variable, ok := sym.(*symbol.Variable)
132 if !ok {
133 return
134 }
135
136 if ctx.VariableInitializer() == nil || ctx.VariableInitializer().(*parser.VariableInitializerContext).Expr() == nil {
137 return
138 }
139 exprContext := ctx.VariableInitializer().(*parser.VariableInitializerContext).Expr().(*parser.ExprContext)
140 exprType := s.at.GetTypeOfNode()[exprContext]
141 if exprType == nil {
142 return
143 }
144
145 // 数组赋值校验
146 if variable.IsArray() && variable.GetType() != symbol.Any {
147 if !exprType.IsArray() {
148 s.at.Log(ctx, fmt.Sprintf("cannot use %s as type %s[]", variable.GetName(), variable.GetType().GetName()))
149 return
150 }
151 }
152
153 declareFunctionType, ok := exprType.(*symbol.DeclareFunctionType)
154 if ok {
155 // 变量为闭包变量 func int(int) f2 = f1();
156 declareFuncType, ok := variable.GetType().(*symbol.DeclareFunctionType)
157 if ok {
158 if declareFuncType.GetReturnType() != nil && !declareFuncType.GetReturnType().IsType(declareFunctionType.GetReturnType()) {
159 s.at.Log(ctx, fmt.Sprintf("cannot use type %s as type %s in assignment closure statment", declareFunctionType.GetReturnType().GetName(), declareFuncType.GetName()))
160 return
161 }
162 } else {
163 // = 闭包:
164 if !variable.GetType().IsType(declareFunctionType.GetReturnType()) {
165 s.at.Log(ctx, fmt.Sprintf("cannot use type %s as type %s in assignment closure statment", declareFunctionType.GetReturnType().GetName(), variable.GetType().GetName()))
166 return
167 }
168 }
169
170 return
171 }
172
173 functionType, ok := variable.GetType().(*symbol.DeclareFunctionType)
174 if ok {
175 // func int(int) f2 = f1(); f2 作为变量
176 if functionType.GetReturnType() != nil && !functionType.GetReturnType().IsType(exprType) {
177 s.at.Log(ctx, fmt.Sprintf("variable %s type error, %s not match %s in assignment closure statment", variable.GetName(), variable.GetType().GetName(), exprType.GetName()))
178 }
179 } else {
180 if !variable.GetType().IsType(exprType) {
181 s.at.Log(ctx, fmt.Sprintf("variable %s type error, %s not match %s", variable.GetName(), variable.GetType().GetName(), exprType.GetName()))
182 }
183 }

Callers

nothing calls this directly

Calls 11

GetSymbolOfNodeMethod · 0.80
GetTypeOfNodeMethod · 0.80
GetTypeMethod · 0.80
LogMethod · 0.80
IsArrayMethod · 0.65
GetNameMethod · 0.65
GetReturnTypeMethod · 0.65
IsTypeMethod · 0.65
VariableDeclaratorIdMethod · 0.45
VariableInitializerMethod · 0.45
ExprMethod · 0.45

Tested by

no test coverage detected