-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathNFunctionDef.java
More file actions
258 lines (223 loc) · 8.29 KB
/
NFunctionDef.java
File metadata and controls
258 lines (223 loc) · 8.29 KB
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
/**
* Copyright 2009, Google Inc. All rights reserved.
* Licensed to PSF under a Contributor Agreement.
*/
package org.python.indexer.ast;
import org.python.indexer.Builtins;
import org.python.indexer.Indexer;
import org.python.indexer.NBinding;
import org.python.indexer.Scope;
import org.python.indexer.types.NDictType;
import org.python.indexer.types.NFuncType;
import org.python.indexer.types.NListType;
import org.python.indexer.types.NTupleType;
import org.python.indexer.types.NType;
import org.python.indexer.types.NUnknownType;
import java.util.ArrayList;
import java.util.List;
import static org.python.indexer.NBinding.Kind.ATTRIBUTE;
import static org.python.indexer.NBinding.Kind.CLASS;
import static org.python.indexer.NBinding.Kind.CONSTRUCTOR;
import static org.python.indexer.NBinding.Kind.FUNCTION;
import static org.python.indexer.NBinding.Kind.METHOD;
import static org.python.indexer.NBinding.Kind.PARAMETER;
public class NFunctionDef extends NNode {
static final long serialVersionUID = 5495886181960463846L;
public NName name;
public List<NNode> args;
public List<NNode> defaults;
public NName varargs; // *args
public NName kwargs; // **kwargs
public NNode body;
private List<NNode> decoratorList;
public NFunctionDef(NName name, List<NNode> args, NBlock body, List<NNode> defaults,
NName varargs, NName kwargs) {
this(name, args, body, defaults, kwargs, varargs, 0, 1);
}
public NFunctionDef(NName name, List<NNode> args, NBlock body, List<NNode> defaults,
NName varargs, NName kwargs, int start, int end) {
super(start, end);
this.name = name;
this.args = args;
this.body = body != null ? new NBody(body) : new NBlock(null);
this.defaults = defaults;
this.varargs = varargs;
this.kwargs = kwargs;
addChildren(name);
addChildren(args);
addChildren(defaults);
addChildren(varargs, kwargs, this.body);
}
public void setDecoratorList(List<NNode> decoratorList) {
this.decoratorList = decoratorList;
addChildren(decoratorList);
}
public List<NNode> getDecoratorList() {
if (decoratorList == null) {
decoratorList = new ArrayList<NNode>();
}
return decoratorList;
}
@Override
public boolean isFunctionDef() {
return true;
}
@Override
public boolean bindsName() {
return true;
}
/**
* Returns the name of the function for indexing/qname purposes.
* Lambdas will return a generated name.
*/
protected String getBindingName(Scope s) {
return name.id;
}
@Override
protected void bindNames(Scope s) throws Exception {
Scope owner = s.getScopeSymtab(); // enclosing class, function or module
setType(new NFuncType());
Scope funcTable = new Scope(s.getEnclosingLexicalScope(), Scope.Type.FUNCTION);
getType().setTable(funcTable);
funcTable.setPath(owner.extendPath(getBindingName(owner)));
// If we already defined this function in this scope, don't try it again.
NType existing = owner.lookupType(getBindingName(owner), true /* local scope */);
if (existing != null && existing.isFuncType()) {
return;
}
bindFunctionName(owner);
bindFunctionParams(funcTable);
bindFunctionDefaults(s);
bindMethodAttrs(owner);
}
protected void bindFunctionName(Scope owner) throws Exception {
NBinding.Kind funkind = FUNCTION;
if (owner.getScopeType() == Scope.Type.CLASS) {
if ("__init__".equals(name.id)) {
funkind = CONSTRUCTOR;
} else {
funkind = METHOD;
}
}
NameBinder.make(funkind).bindName(owner, name, getType());
}
protected void bindFunctionParams(Scope funcTable) throws Exception {
NameBinder param = NameBinder.make(PARAMETER);
for (NNode a : args) {
param.bind(funcTable, a, new NUnknownType());
}
if (varargs != null) {
param.bind(funcTable, varargs, new NListType());
}
if (kwargs != null) {
param.bind(funcTable, kwargs, new NDictType());
}
}
/**
* Processes any name-binding constructs appearing as parameter defaults.
* For instance, in {@code def foo(converter=lambda name: name.upper()): ...}
* the lambda is a name-binding construct.
*/
protected void bindFunctionDefaults(Scope s) throws Exception {
for (NNode n : defaults) {
if (n.bindsName()) {
n.bindNames(s);
}
}
}
protected void bindMethodAttrs(Scope owner) throws Exception {
NType cls = Indexer.idx.lookupQnameType(owner.getPath());
if (cls == null || !cls.isClassType()) {
return;
}
// We don't currently differentiate between classes and instances.
addReadOnlyAttr("im_class", cls, CLASS);
addReadOnlyAttr("__class__", cls, CLASS);
addReadOnlyAttr("im_self", cls, ATTRIBUTE);
addReadOnlyAttr("__self__", cls, ATTRIBUTE);
}
protected NBinding addSpecialAttr(String name, NType atype, NBinding.Kind kind) {
NBinding b = getTable().update(name,
Builtins.newDataModelUrl("the-standard-type-hierarchy"),
atype, kind);
b.markSynthetic();
b.markStatic();
return b;
}
protected NBinding addReadOnlyAttr(String name, NType type, NBinding.Kind kind) {
NBinding b = addSpecialAttr(name, type, kind);
b.markReadOnly();
return b;
}
@Override
public NType resolve(Scope outer) throws Exception {
resolveList(defaults, outer);
resolveList(decoratorList, outer);
Scope funcTable = getTable();
NBinding selfBinding = funcTable.lookup("__self__");
if (selfBinding != null && !selfBinding.getType().isClassType()) {
selfBinding = null;
}
if (selfBinding != null) {
if (args.size() < 1) {
addWarning(name, "method should have at least one argument (self)");
} else if (!(args.get(0) instanceof NName)) {
addError(name, "self parameter must be an identifier");
}
}
NTupleType fromType = new NTupleType();
bindParamsToDefaults(selfBinding, fromType);
if (varargs != null) {
NBinding b = funcTable.lookupLocal(varargs.id);
if (b != null) {
fromType.add(b.getType());
}
}
if (kwargs != null) {
NBinding b = funcTable.lookupLocal(kwargs.id);
if (b != null) {
fromType.add(b.getType());
}
}
NType toType = resolveExpr(body, funcTable);
getType().asFuncType().setReturnType(toType);
return getType();
}
private void bindParamsToDefaults(NBinding selfBinding, NTupleType fromType) throws Exception {
NameBinder param = NameBinder.make(PARAMETER);
Scope funcTable = getTable();
for (int i = 0; i < args.size(); i++) {
NNode arg = args.get(i);
NType argtype = ((i == 0 && selfBinding != null)
? selfBinding.getType()
: getArgType(args, defaults, i));
param.bind(funcTable, arg, argtype);
fromType.add(argtype);
}
}
static NType getArgType(List<NNode> args, List<NNode> defaults, int argnum) {
if (defaults == null) {
return new NUnknownType();
}
int firstDefault = args.size() - defaults.size();
if (firstDefault >= 0 && argnum >= firstDefault) {
return defaults.get(argnum - firstDefault).getType();
}
return new NUnknownType();
}
@Override
public String toString() {
return "<Function:" + start() + ":" + name + ">";
}
@Override
public void visit(NNodeVisitor v) {
if (v.visit(this)) {
visitNode(name, v);
visitNodeList(args, v);
visitNodeList(defaults, v);
visitNode(kwargs, v);
visitNode(varargs, v);
visitNode(body, v);
}
}
}