25
25
*/
26
26
package com .oracle .graal .python .nodes .function ;
27
27
28
+ import static com .oracle .graal .python .nodes .SpecialAttributeNames .__ANNOTATIONS__ ;
29
+
30
+ import java .util .Map ;
31
+
28
32
import com .oracle .graal .python .PythonLanguage ;
29
33
import com .oracle .graal .python .builtins .objects .cell .PCell ;
34
+ import com .oracle .graal .python .builtins .objects .common .HashingCollectionNodes ;
30
35
import com .oracle .graal .python .builtins .objects .dict .PDict ;
31
36
import com .oracle .graal .python .builtins .objects .function .PArguments ;
32
37
import com .oracle .graal .python .builtins .objects .function .PFunction ;
33
38
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
34
39
import com .oracle .graal .python .nodes .SpecialAttributeNames ;
35
- import static com .oracle .graal .python .nodes .SpecialAttributeNames .__ANNOTATIONS__ ;
36
40
import com .oracle .graal .python .nodes .attributes .WriteAttributeToDynamicObjectNode ;
37
41
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
38
42
import com .oracle .graal .python .nodes .expression .ExpressionNode ;
49
53
import com .oracle .truffle .api .frame .VirtualFrame ;
50
54
import com .oracle .truffle .api .nodes .ExplodeLoop ;
51
55
import com .oracle .truffle .api .nodes .RootNode ;
52
- import java .util .Map ;
53
56
54
57
public class FunctionDefinitionNode extends ExpressionDefinitionNode {
55
58
@ CompilationFinal private ContextReference <PythonContext > contextRef ;
@@ -63,6 +66,7 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
63
66
@ Child private WriteAttributeToObjectNode writeAttrNode = WriteAttributeToObjectNode .create ();
64
67
@ Child private WriteAttributeToDynamicObjectNode writeNameNode = WriteAttributeToDynamicObjectNode .create ();
65
68
@ Child private PythonObjectFactory factory = PythonObjectFactory .create ();
69
+ @ Child private HashingCollectionNodes .SetItemNode setItemNode ;
66
70
67
71
@ CompilerDirectives .CompilationFinal (dimensions = 1 ) private final String [] annotationNames ;
68
72
@ Children private ExpressionNode [] annotationTypes ;
@@ -134,10 +138,8 @@ public Object execute(VirtualFrame frame) {
134
138
135
139
// Processing annotated arguments.
136
140
// The __annotations__ dictionary is created even there are is not any annotated arg.
137
- PDict annotations = factory ().createDict ();
138
- writeAttrNode .execute (func , __ANNOTATIONS__ , annotations );
139
141
if (annotationNames != null ) {
140
- writeAnnotations (frame , annotations );
142
+ writeAnnotations (frame , func );
141
143
}
142
144
return func ;
143
145
}
@@ -175,12 +177,18 @@ private Object[] computeDefaultValues(VirtualFrame frame) {
175
177
}
176
178
177
179
@ ExplodeLoop
178
- private void writeAnnotations (VirtualFrame frame , PDict annotations ) {
180
+ private void writeAnnotations (VirtualFrame frame , PFunction func ) {
181
+ PDict annotations = factory ().createDict ();
182
+ writeAttrNode .execute (func , __ANNOTATIONS__ , annotations );
183
+ if (setItemNode == null ) {
184
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
185
+ setItemNode = insert (HashingCollectionNodes .SetItemNode .create ());
186
+ }
179
187
for (int i = 0 ; i < annotationNames .length ; i ++) {
180
188
// compute the types of the arg
181
189
Object type = annotationTypes [i ].execute (frame );
182
190
// set the annotations
183
- annotations . setItem ( annotationNames [i ], type );
191
+ setItemNode . execute ( frame , annotations , annotationNames [i ], type );
184
192
}
185
193
}
186
194
0 commit comments