-
Notifications
You must be signed in to change notification settings - Fork 5
/
declaration.go
368 lines (303 loc) · 11.2 KB
/
declaration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"go/ast"
"go/token"
"github.com/NickyBoy89/java2go/nodeutil"
"github.com/NickyBoy89/java2go/symbol"
log "github.com/sirupsen/logrus"
sitter "github.com/smacker/go-tree-sitter"
)
// ParseDecls represents any type that returns a list of top-level declarations,
// this is any class, interface, or enum declaration
func ParseDecls(node *sitter.Node, source []byte, ctx Ctx) []ast.Decl {
switch node.Type() {
case "class_declaration":
// TODO: Currently ignores implements and extends with the following tags:
//"superclass"
//"interfaces"
// The declarations and fields for the class
declarations := []ast.Decl{}
fields := &ast.FieldList{}
// Global variables
globalVariables := &ast.GenDecl{Tok: token.VAR}
ctx.className = ctx.currentFile.FindClass(node.ChildByFieldName("name").Content(source)).Name
// First, look through the class's body for field declarations
for _, child := range nodeutil.NamedChildrenOf(node.ChildByFieldName("body")) {
if child.Type() == "field_declaration" {
var staticField bool
comments := []*ast.Comment{}
// Handle any modifiers that the field might have
if child.NamedChild(0).Type() == "modifiers" {
for _, modifier := range nodeutil.UnnamedChildrenOf(child.NamedChild(0)) {
switch modifier.Type() {
case "static":
staticField = true
case "marker_annotation", "annotation":
comments = append(comments, &ast.Comment{Text: "//" + modifier.Content(source)})
if _, in := excludedAnnotations[modifier.Content(source)]; in {
// Skip this field if there is an ignored annotation
continue
}
}
}
}
// TODO: If a field is initialized to a value, that value is discarded
field := &ast.Field{}
if len(comments) > 0 {
field.Doc = &ast.CommentGroup{List: comments}
}
fieldName := child.ChildByFieldName("declarator").ChildByFieldName("name").Content(source)
fieldDef := ctx.currentClass.FindField().ByOriginalName(fieldName)[0]
field.Names, field.Type = []*ast.Ident{&ast.Ident{Name: fieldDef.Name}}, &ast.Ident{Name: fieldDef.Type}
if staticField {
globalVariables.Specs = append(globalVariables.Specs, &ast.ValueSpec{Names: field.Names, Type: field.Type})
} else {
fields.List = append(fields.List, field)
}
}
}
// Add the global variables
if len(globalVariables.Specs) > 0 {
declarations = append(declarations, globalVariables)
}
// Add any type paramters defined in the class
if node.ChildByFieldName("type_parameters") != nil {
declarations = append(declarations, ParseDecls(node.ChildByFieldName("type_parameters"), source, ctx)...)
}
// Add the struct for the class
declarations = append(declarations, GenStruct(ctx.className, fields))
// Add all the declarations that appear in the class
declarations = append(declarations, ParseDecls(node.ChildByFieldName("body"), source, ctx)...)
return declarations
case "class_body": // The body of the currently parsed class
decls := []ast.Decl{}
// To switch to parsing the subclasses of a class, since we assume that
// all the class's subclass definitions are in-order, if we find some number
// of subclasses in a class, we can refer to them by index
var subclassIndex int
for _, child := range nodeutil.NamedChildrenOf(node) {
switch child.Type() {
// Skip fields and comments
case "field_declaration", "comment":
case "constructor_declaration", "method_declaration", "static_initializer":
d := ParseDecl(child, source, ctx)
// If the declaration is bad, skip it
_, bad := d.(*ast.BadDecl)
if !bad {
decls = append(decls, d)
}
// Subclasses
case "class_declaration", "interface_declaration", "enum_declaration":
newCtx := ctx.Clone()
newCtx.currentClass = ctx.currentClass.Subclasses[subclassIndex]
subclassIndex++
decls = append(decls, ParseDecls(child, source, newCtx)...)
}
}
return decls
case "interface_body":
methods := &ast.FieldList{}
for _, c := range nodeutil.NamedChildrenOf(node) {
if c.Type() == "method_declaration" {
parsedMethod := ParseNode(c, source, ctx).(*ast.Field)
// If the method was ignored with an annotation, it will return a blank
// field, so ignore that
if parsedMethod.Type != nil {
methods.List = append(methods.List, parsedMethod)
}
}
}
return []ast.Decl{GenInterface(ctx.className, methods)}
case "interface_declaration":
ctx.className = ctx.currentFile.FindClass(node.ChildByFieldName("name").Content(source)).Name
return ParseDecls(node.ChildByFieldName("body"), source, ctx)
case "enum_declaration":
// An enum is treated as both a struct, and a list of values that define
// the states that the enum can be in
ctx.className = ctx.currentFile.FindClass(node.ChildByFieldName("name").Content(source)).Name
// TODO: Handle an enum correctly
//return ParseDecls(node.ChildByFieldName("body"), source, ctx)
return []ast.Decl{}
case "type_parameters":
var declarations []ast.Decl
// A list of generic type parameters
for _, param := range nodeutil.NamedChildrenOf(node) {
switch param.Type() {
case "type_parameter":
declarations = append(declarations, GenTypeInterface(param.NamedChild(0).Content(source), []string{"any"}))
}
}
return declarations
}
panic("Unknown type to parse for decls: " + node.Type())
}
// ParseDecl parses a top-level declaration within a source file, including
// but not limited to fields and methods
func ParseDecl(node *sitter.Node, source []byte, ctx Ctx) ast.Decl {
switch node.Type() {
case "constructor_declaration":
paramNode := node.ChildByFieldName("parameters")
constructorName := node.ChildByFieldName("name").Content(source)
comparison := func(d *symbol.Definition) bool {
// The names must match
if constructorName != d.OriginalName {
return false
}
// Size of parameters must match
if int(paramNode.NamedChildCount()) != len(d.Parameters) {
return false
}
// Go through the types and check to see if they differ
for index, param := range nodeutil.NamedChildrenOf(paramNode) {
var paramType string
if param.Type() == "spread_parameter" {
paramType = param.NamedChild(0).Content(source)
} else {
paramType = param.ChildByFieldName("type").Content(source)
}
if paramType != d.Parameters[index].OriginalType {
return false
}
}
return true
}
// Search through the current class for the constructor, which is simply labeled as a method
ctx.localScope = ctx.currentClass.FindMethod().By(comparison)[0]
body := ParseStmt(node.ChildByFieldName("body"), source, ctx).(*ast.BlockStmt)
body.List = append([]ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{&ast.Ident{Name: ShortName(ctx.className)}},
Tok: token.DEFINE,
Rhs: []ast.Expr{&ast.CallExpr{Fun: &ast.Ident{Name: "new"}, Args: []ast.Expr{&ast.Ident{Name: ctx.className}}}},
},
}, body.List...)
body.List = append(body.List, &ast.ReturnStmt{Results: []ast.Expr{&ast.Ident{Name: ShortName(ctx.className)}}})
return &ast.FuncDecl{
Name: &ast.Ident{Name: ctx.localScope.Name},
Type: &ast.FuncType{
Params: ParseNode(node.ChildByFieldName("parameters"), source, ctx).(*ast.FieldList),
Results: &ast.FieldList{List: []*ast.Field{&ast.Field{
Type: &ast.Ident{Name: ctx.localScope.Type},
}}},
},
Body: body,
}
case "method_declaration":
var static bool
// Store the annotations as comments on the method
comments := []*ast.Comment{}
if node.NamedChild(0).Type() == "modifiers" {
for _, modifier := range nodeutil.UnnamedChildrenOf(node.NamedChild(0)) {
switch modifier.Type() {
case "static":
static = true
case "abstract":
log.Warn("Unhandled abstract class")
// TODO: Handle abstract methods correctly
return &ast.BadDecl{}
case "marker_annotation", "annotation":
comments = append(comments, &ast.Comment{Text: "//" + modifier.Content(source)})
// If the annotation was on the list of ignored annotations, don't
// parse the method
if _, in := excludedAnnotations[modifier.Content(source)]; in {
return &ast.BadDecl{}
}
}
}
}
var receiver *ast.FieldList
// If a function is non-static, it has a method receiver
if !static {
receiver = &ast.FieldList{
List: []*ast.Field{
&ast.Field{
Names: []*ast.Ident{&ast.Ident{Name: ShortName(ctx.className)}},
Type: &ast.StarExpr{X: &ast.Ident{Name: ctx.className}},
},
},
}
}
methodName := ParseExpr(node.ChildByFieldName("name"), source, ctx).(*ast.Ident)
methodParameters := node.ChildByFieldName("parameters")
// Find the declaration for the method that we are defining
// Find a method that is more or less exactly the same
comparison := func(d *symbol.Definition) bool {
// Throw out any methods that aren't named the same
if d.OriginalName != methodName.Name {
return false
}
// Now, even though the method might have the same name, it could be overloaded,
// so we have to check the parameters as well
// Number of parameters are not the same, invalid
if len(d.Parameters) != int(methodParameters.NamedChildCount()) {
return false
}
// Go through the types and check to see if they differ
for index, param := range nodeutil.NamedChildrenOf(methodParameters) {
var paramType string
if param.Type() == "spread_parameter" {
paramType = param.NamedChild(0).Content(source)
} else {
paramType = param.ChildByFieldName("type").Content(source)
}
if d.Parameters[index].OriginalType != paramType {
return false
}
}
// We found the correct method
return true
}
methodDefinition := ctx.currentClass.FindMethod().By(comparison)
// No definition was found
if len(methodDefinition) == 0 {
log.WithFields(log.Fields{
"methodName": methodName.Name,
}).Panic("No matching definition found for method")
}
ctx.localScope = methodDefinition[0]
body := ParseStmt(node.ChildByFieldName("body"), source, ctx).(*ast.BlockStmt)
params := ParseNode(node.ChildByFieldName("parameters"), source, ctx).(*ast.FieldList)
// Special case for the main method, because in Java, this method has the
// command line args passed in as a parameter
if methodName.Name == "main" {
params = nil
body.List = append([]ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{&ast.Ident{Name: "args"}},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.SelectorExpr{
X: &ast.Ident{Name: "os"},
Sel: &ast.Ident{Name: "Args"},
},
},
},
}, body.List...)
}
return &ast.FuncDecl{
Doc: &ast.CommentGroup{List: comments},
Name: &ast.Ident{Name: ctx.localScope.Name},
Recv: receiver,
Type: &ast.FuncType{
Params: params,
Results: &ast.FieldList{
List: []*ast.Field{
&ast.Field{Type: &ast.Ident{Name: ctx.localScope.Type}},
},
},
},
Body: body,
}
case "static_initializer":
ctx.localScope = &symbol.Definition{}
// A block of `static`, which is run before the main function
return &ast.FuncDecl{
Name: &ast.Ident{Name: "init"},
Type: &ast.FuncType{
Params: &ast.FieldList{List: []*ast.Field{}},
},
Body: ParseStmt(node.NamedChild(0), source, ctx).(*ast.BlockStmt),
}
}
panic("Unknown node type for declaration: " + node.Type())
}