114
114
import com .oracle .graal .python .builtins .modules .BuiltinFunctionsFactory .HexNodeFactory ;
115
115
import com .oracle .graal .python .builtins .modules .BuiltinFunctionsFactory .OctNodeFactory ;
116
116
import com .oracle .graal .python .builtins .modules .WarningsModuleBuiltins .WarnNode ;
117
+ import com .oracle .graal .python .builtins .modules .ast .AstModuleBuiltins ;
117
118
import com .oracle .graal .python .builtins .modules .io .IOModuleBuiltins ;
118
119
import com .oracle .graal .python .builtins .modules .io .IONodes ;
119
120
import com .oracle .graal .python .builtins .objects .PNone ;
151
152
import com .oracle .graal .python .builtins .objects .type .TypeBuiltins ;
152
153
import com .oracle .graal .python .builtins .objects .type .TypeNodes ;
153
154
import com .oracle .graal .python .builtins .objects .type .TypeNodes .IsTypeNode ;
155
+ import com .oracle .graal .python .compiler .Compiler ;
156
+ import com .oracle .graal .python .compiler .RaisePythonExceptionErrorCallback ;
154
157
import com .oracle .graal .python .lib .PyCallableCheckNode ;
155
158
import com .oracle .graal .python .lib .PyEvalGetGlobals ;
156
159
import com .oracle .graal .python .lib .PyMappingCheckNode ;
228
231
import com .oracle .graal .python .nodes .util .CastToJavaLongExactNode ;
229
232
import com .oracle .graal .python .nodes .util .CastToTruffleStringNode ;
230
233
import com .oracle .graal .python .pegparser .InputType ;
234
+ import com .oracle .graal .python .pegparser .Parser ;
235
+ import com .oracle .graal .python .pegparser .sst .ModTy ;
231
236
import com .oracle .graal .python .runtime .ExecutionContext .IndirectCallContext ;
232
237
import com .oracle .graal .python .runtime .PythonContext ;
233
238
import com .oracle .graal .python .runtime .PythonOptions ;
@@ -854,7 +859,7 @@ protected boolean isAnyNone(Object object) {
854
859
}
855
860
856
861
protected PCode createAndCheckCode (VirtualFrame frame , Object source ) {
857
- PCode code = getCompileNode ().execute (frame , source , T_STRING_SOURCE , getMode (), 0 , false , -1 );
862
+ PCode code = getCompileNode ().compile (frame , source , T_STRING_SOURCE , getMode (), -1 );
858
863
assertNoFreeVars (code );
859
864
return code ;
860
865
}
@@ -1021,10 +1026,14 @@ public abstract static class CompileNode extends PythonBuiltinNode {
1021
1026
private static final int PyCF_MASK_OBSOLETE = CO_NESTED ;
1022
1027
1023
1028
private static final int PyCF_DONT_IMPLY_DEDENT = 0x0200 ;
1024
- private static final int PyCF_ONLY_AST = 0x0400 ;
1025
- private static final int PyCF_TYPE_COMMENTS = 0x1000 ;
1029
+ public static final int PyCF_ONLY_AST = 0x0400 ;
1030
+ public static final int PyCF_TYPE_COMMENTS = 0x1000 ;
1031
+ public static final int PyCF_ALLOW_TOP_LEVEL_AWAIT = 0x2000 ;
1032
+ private static final int PyCF_ALLOW_INCOMPLETE_INPUT = 0x4000 ;
1033
+ private static final int PyCF_COMPILE_MASK = PyCF_ONLY_AST | PyCF_ALLOW_TOP_LEVEL_AWAIT | PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT ;
1026
1034
1027
1035
private static final TruffleString T_SINGLE = tsLiteral ("single" );
1036
+ private static final TruffleString T_FUNC_EVAL = tsLiteral ("func_eval" );
1028
1037
1029
1038
/**
1030
1039
* Decides whether this node should attempt to map the filename to a URI for the benefit of
@@ -1043,12 +1052,16 @@ public CompileNode() {
1043
1052
this .lstrip = false ;
1044
1053
}
1045
1054
1046
- public abstract PCode execute (VirtualFrame frame , Object source , TruffleString filename , TruffleString mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
1055
+ public final PCode compile (VirtualFrame frame , Object source , TruffleString filename , TruffleString mode , Object kwOptimize ) {
1056
+ return (PCode ) executeInternal (frame , source , filename , mode , 0 , false , kwOptimize );
1057
+ }
1058
+
1059
+ protected abstract Object executeInternal (VirtualFrame frame , Object source , TruffleString filename , TruffleString mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
1047
1060
1048
1061
@ SuppressWarnings ("unused" )
1049
1062
@ Specialization
1050
1063
@ TruffleBoundary
1051
- PCode compile (TruffleString expression , TruffleString filename , TruffleString mode , int kwFlags , Object kwDontInherit , int kwOptimize ) {
1064
+ Object compile (TruffleString expression , TruffleString filename , TruffleString mode , int kwFlags , Object kwDontInherit , int kwOptimize ) {
1052
1065
checkFlags (kwFlags );
1053
1066
checkOptimize (kwOptimize , kwOptimize );
1054
1067
checkSource (expression );
@@ -1071,15 +1084,48 @@ PCode compile(TruffleString expression, TruffleString filename, TruffleString mo
1071
1084
pm = ParserMode .Eval ;
1072
1085
} else if (mode .equalsUncached (T_SINGLE , TS_ENCODING )) {
1073
1086
pm = ParserMode .Statement ;
1087
+ } else if (mode .equalsUncached (T_FUNC_EVAL , TS_ENCODING )) {
1088
+ if ((kwFlags & PyCF_ONLY_AST ) == 0 ) {
1089
+ throw raise (ValueError , ErrorMessages .COMPILE_MODE_FUNC_TYPE_REQUIED_FLAG_ONLY_AST );
1090
+ }
1091
+ pm = ParserMode .FuncType ;
1074
1092
} else {
1075
- throw raise (ValueError , ErrorMessages .COMPILE_MUST_BE );
1093
+ if ((kwFlags & PyCF_ONLY_AST ) != 0 ) {
1094
+ throw raise (ValueError , ErrorMessages .COMPILE_MODE_MUST_BE_AST_ONLY );
1095
+ } else {
1096
+ throw raise (ValueError , ErrorMessages .COMPILE_MODE_MUST_BE );
1097
+ }
1076
1098
}
1077
1099
if (lstrip && !code .isEmpty ()) {
1078
1100
int c = code .codePointAtIndexUncached (0 , TS_ENCODING );
1079
1101
if (c == ' ' || c == '\t' ) {
1080
1102
code = code .substringUncached (1 , code .codePointLengthUncached (TS_ENCODING ) - 1 , TS_ENCODING , true );
1081
1103
}
1082
1104
}
1105
+ if ((kwFlags & PyCF_ONLY_AST ) != 0 ) {
1106
+ InputType type ;
1107
+ switch (pm ) {
1108
+ case File :
1109
+ type = InputType .FILE ;
1110
+ break ;
1111
+ case Eval :
1112
+ type = InputType .EVAL ;
1113
+ break ;
1114
+ case Statement :
1115
+ type = InputType .SINGLE ;
1116
+ break ;
1117
+ case FuncType :
1118
+ type = InputType .FUNCTION_TYPE ;
1119
+ break ;
1120
+ default :
1121
+ throw CompilerDirectives .shouldNotReachHere ();
1122
+ }
1123
+ Source source = PythonLanguage .newSource (context , code , filename , mayBeFromFile , PythonLanguage .MIME_TYPE );
1124
+ RaisePythonExceptionErrorCallback errorCb = new RaisePythonExceptionErrorCallback (source , PythonOptions .isPExceptionWithJavaStacktrace (getLanguage ()));
1125
+ Parser parser = Compiler .createParser (code .toJavaStringUncached (), errorCb , type , false );
1126
+ ModTy mod = (ModTy ) parser .parse ();
1127
+ return AstModuleBuiltins .sst2Obj (getContext (), mod );
1128
+ }
1083
1129
CallTarget ct ;
1084
1130
TruffleString finalCode = code ;
1085
1131
Supplier <CallTarget > createCode = () -> {
@@ -1113,7 +1159,7 @@ PCode compile(TruffleString expression, TruffleString filename, TruffleString mo
1113
1159
}
1114
1160
1115
1161
@ Specialization (limit = "3" )
1116
- PCode generic (VirtualFrame frame , Object wSource , Object wFilename , Object wMode , Object kwFlags , Object kwDontInherit , Object kwOptimize ,
1162
+ Object generic (VirtualFrame frame , Object wSource , Object wFilename , Object wMode , Object kwFlags , Object kwDontInherit , Object kwOptimize ,
1117
1163
@ CachedLibrary (limit = "3" ) PythonBufferAcquireLibrary acquireLib ,
1118
1164
@ CachedLibrary (limit = "3" ) PythonBufferAccessLibrary bufferLib ,
1119
1165
@ Cached CastToTruffleStringNode castStr ,
@@ -1126,7 +1172,7 @@ PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode
1126
1172
@ Cached TruffleString .FromByteArrayNode fromByteArrayNode ,
1127
1173
@ Cached TruffleString .SwitchEncodingNode switchEncodingNode ) {
1128
1174
if (wSource instanceof PCode ) {
1129
- return ( PCode ) wSource ;
1175
+ return wSource ;
1130
1176
}
1131
1177
TruffleString filename ;
1132
1178
// TODO use PyUnicode_FSDecode
@@ -1186,7 +1232,7 @@ private void checkOptimize(int optimize, Object kwOptimize) throws PException {
1186
1232
}
1187
1233
1188
1234
private void checkFlags (int flags ) {
1189
- if ((flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS )) > 0 ) {
1235
+ if ((flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_COMPILE_MASK )) != 0 ) {
1190
1236
throw raise (ValueError , ErrorMessages .UNRECOGNIZED_FLAGS );
1191
1237
}
1192
1238
}
0 commit comments