/
function.go
89 lines (75 loc) · 3.31 KB
/
function.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
// Copyright 2015 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package codedom
import (
"github.com/serulian/compiler/compilergraph"
"github.com/serulian/compiler/graphs/scopegraph"
"github.com/serulian/compiler/graphs/srg"
"github.com/serulian/compiler/graphs/typegraph"
)
type SpecializedFunction int
const (
// None marks a function as having no specialization.
NormalFunction SpecializedFunction = iota
// AsynchronousWorkerFunction marks a function as being executed asynchronously via
// a worker.
AsynchronousWorkerFunction
// GeneratorFunction marks a function as being a generator.
GeneratorFunction
)
// FunctionDefinitionNode represents the definition of a function.
type FunctionDefinitionNode struct {
expressionBase
Generics []string // The names of the generics of the function, if any.
Parameters []string // The names of the parameters of the function, if any.
Body StatementOrExpression // The body for the function.
RequiresThis bool // Whether the function needs '$this' defined.
GeneratorYieldType *typegraph.TypeReference // The type of items being yielded, if this is a generator.
Specialization SpecializedFunction // The specialization for this function, if any.
}
// FunctionDefinition constructs a new function definition.
func FunctionDefinition(generics []string, parameters []string, body StatementOrExpression, requiresThis bool, specialization SpecializedFunction, basisNode compilergraph.GraphNode) *FunctionDefinitionNode {
return &FunctionDefinitionNode{
expressionBase{domBase{basisNode}},
generics,
parameters,
body,
requiresThis,
nil,
specialization,
}
}
// GeneratorDefinition constructs a new function definition for a generator function.
func GeneratorDefinition(generics []string, parameters []string, body StatementOrExpression, requiresThis bool, yieldType typegraph.TypeReference, basisNode compilergraph.GraphNode) *FunctionDefinitionNode {
return &FunctionDefinitionNode{
expressionBase{domBase{basisNode}},
generics,
parameters,
body,
requiresThis,
&yieldType,
GeneratorFunction,
}
}
// ManagesResources returns whether any of the statements in the function's body are ResourceBlock's.
func (f FunctionDefinitionNode) ManagesResources() bool {
return IsManagingResources(f.Body)
}
// IsAsynchronous returns whether the function's implementation is asynchronous in some way.
func (f FunctionDefinitionNode) IsAsynchronous(scopegraph *scopegraph.ScopeGraph) bool {
return f.Specialization == NormalFunction && IsAsynchronous(f.Body, scopegraph)
}
// IsGenerator returns whether the function is a generator.
func (f FunctionDefinitionNode) IsGenerator() bool {
return f.Specialization == GeneratorFunction
}
// WorkerExecute returns whether the function should be executed by an async web worker.
func (f FunctionDefinitionNode) WorkerExecute() bool {
return f.Specialization == AsynchronousWorkerFunction
}
// UniqueId returns a unique ID for this function definition. Note that this is intended to be stable
// across compilations if the input source has not changed.
func (f FunctionDefinitionNode) UniqueId() string {
return srg.GetUniqueId(f.Body.BasisNode())
}