blob: 4dd5435ce71a965cef04e8819fec1094397d39ee [file] [log] [blame]
Fred Drakeea9cb5a2000-07-09 00:20:36 +00001#ifndef Py_COMPILE_H
2#define Py_COMPILE_H
Thomas Woutersca82a8b2006-02-28 23:09:08 +00003
Victor Stinner2bdc7f52011-09-29 01:04:08 +02004#ifndef Py_LIMITED_API
Thomas Woutersca82a8b2006-02-28 23:09:08 +00005
Fred Drakeea9cb5a2000-07-09 00:20:36 +00006#ifdef __cplusplus
7extern "C" {
8#endif
9
Guido van Rossum3f5da241990-12-20 15:06:42 +000010/* Public interface */
Eric Snow6b4be192017-05-22 21:36:03 -070011#define PyCF_MASK (CO_FUTURE_DIVISION | CO_FUTURE_ABSOLUTE_IMPORT | \
12 CO_FUTURE_WITH_STATEMENT | CO_FUTURE_PRINT_FUNCTION | \
13 CO_FUTURE_UNICODE_LITERALS | CO_FUTURE_BARRY_AS_BDFL | \
Guido van Rossum95e4d582018-01-26 08:20:18 -080014 CO_FUTURE_GENERATOR_STOP | CO_FUTURE_ANNOTATIONS)
Eric Snow6b4be192017-05-22 21:36:03 -070015#define PyCF_MASK_OBSOLETE (CO_NESTED)
Batuhan Taşkaya44540572020-04-22 19:09:03 +030016
17/* bpo-39562: CO_FUTURE_ and PyCF_ constants must be kept unique.
18 PyCF_ constants can use bits from 0x0100 to 0x10000.
19 CO_FUTURE_ constants use bits starting at 0x20000. */
Eric Snow6b4be192017-05-22 21:36:03 -070020#define PyCF_SOURCE_IS_UTF8 0x0100
21#define PyCF_DONT_IMPLY_DEDENT 0x0200
22#define PyCF_ONLY_AST 0x0400
23#define PyCF_IGNORE_COOKIE 0x0800
Guido van Rossumdcfcd142019-01-31 03:40:27 -080024#define PyCF_TYPE_COMMENTS 0x1000
Matthias Bussonnier565b4f12019-05-21 13:12:03 -070025#define PyCF_ALLOW_TOP_LEVEL_AWAIT 0x2000
Batuhan Taşkaya44540572020-04-22 19:09:03 +030026#define PyCF_COMPILE_MASK (PyCF_ONLY_AST | PyCF_ALLOW_TOP_LEVEL_AWAIT | \
27 PyCF_TYPE_COMMENTS | PyCF_DONT_IMPLY_DEDENT)
Eric Snow6b4be192017-05-22 21:36:03 -070028
29#ifndef Py_LIMITED_API
30typedef struct {
31 int cf_flags; /* bitmask of CO_xxx flags relevant to future */
Guido van Rossum495da292019-03-07 12:38:08 -080032 int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */
Eric Snow6b4be192017-05-22 21:36:03 -070033} PyCompilerFlags;
Victor Stinner37d66d72019-06-13 02:16:41 +020034
35#define _PyCompilerFlags_INIT \
36 (PyCompilerFlags){.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}
Eric Snow6b4be192017-05-22 21:36:03 -070037#endif
Guido van Rossuma3309961993-07-28 09:05:47 +000038
Jeremy Hylton4db62b12001-02-27 19:07:02 +000039/* Future feature support */
40
41typedef struct {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042 int ff_features; /* flags set by future statements */
43 int ff_lineno; /* line number of last future statement */
Jeremy Hylton4db62b12001-02-27 19:07:02 +000044} PyFutureFeatures;
45
Jeremy Hylton4db62b12001-02-27 19:07:02 +000046#define FUTURE_NESTED_SCOPES "nested_scopes"
Guido van Rossumb09f7ed2001-07-15 21:08:29 +000047#define FUTURE_GENERATORS "generators"
Guido van Rossum4668b002001-08-08 05:00:18 +000048#define FUTURE_DIVISION "division"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049#define FUTURE_ABSOLUTE_IMPORT "absolute_import"
Thomas Wouters34aa7ba2006-02-28 19:02:24 +000050#define FUTURE_WITH_STATEMENT "with_statement"
Eric Smith87824082008-03-20 23:02:08 +000051#define FUTURE_PRINT_FUNCTION "print_function"
Christian Heimes4d6ec852008-03-26 22:34:47 +000052#define FUTURE_UNICODE_LITERALS "unicode_literals"
Brett Cannone3944a52009-04-01 05:08:41 +000053#define FUTURE_BARRY_AS_BDFL "barry_as_FLUFL"
Yury Selivanov8170e8c2015-05-09 11:44:30 -040054#define FUTURE_GENERATOR_STOP "generator_stop"
Guido van Rossum95e4d582018-01-26 08:20:18 -080055#define FUTURE_ANNOTATIONS "annotations"
Guido van Rossum4668b002001-08-08 05:00:18 +000056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057struct _mod; /* Declare the existence of this type */
Georg Brandl8334fd92010-12-04 10:26:46 +000058#define PyAST_Compile(mod, s, f, ar) PyAST_CompileEx(mod, s, f, -1, ar)
Victor Stinner00676d12010-12-27 01:49:31 +000059PyAPI_FUNC(PyCodeObject *) PyAST_CompileEx(
Victor Stinner0d711162010-12-27 02:39:20 +000060 struct _mod *mod,
Victor Stinner00676d12010-12-27 01:49:31 +000061 const char *filename, /* decoded from the filesystem encoding */
62 PyCompilerFlags *flags,
63 int optimize,
64 PyArena *arena);
Victor Stinner14e461d2013-08-26 22:28:21 +020065PyAPI_FUNC(PyCodeObject *) PyAST_CompileObject(
66 struct _mod *mod,
67 PyObject *filename,
68 PyCompilerFlags *flags,
69 int optimize,
70 PyArena *arena);
71PyAPI_FUNC(PyFutureFeatures *) PyFuture_FromAST(
72 struct _mod * mod,
73 const char *filename /* decoded from the filesystem encoding */
74 );
75PyAPI_FUNC(PyFutureFeatures *) PyFuture_FromASTObject(
76 struct _mod * mod,
77 PyObject *filename
78 );
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079
Victor Stinner2bdc7f52011-09-29 01:04:08 +020080/* _Py_Mangle is defined in compile.c */
81PyAPI_FUNC(PyObject*) _Py_Mangle(PyObject *p, PyObject *name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
Larry Hastings3a907972013-11-23 14:49:22 -080083#define PY_INVALID_STACK_EFFECT INT_MAX
84PyAPI_FUNC(int) PyCompile_OpcodeStackEffect(int opcode, int oparg);
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +030085PyAPI_FUNC(int) PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump);
Larry Hastings3a907972013-11-23 14:49:22 -080086
Pablo Galindod112c602020-03-18 23:02:09 +000087typedef struct {
88 int optimize;
89 int ff_features;
90} _PyASTOptimizeState;
91
92PyAPI_FUNC(int) _PyAST_Optimize(struct _mod *, PyArena *arena, _PyASTOptimizeState *state);
INADA Naoki7ea143a2017-12-14 16:47:20 +090093
Guido van Rossuma3309961993-07-28 09:05:47 +000094#ifdef __cplusplus
95}
96#endif
Victor Stinner2bdc7f52011-09-29 01:04:08 +020097
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000098#endif /* !Py_LIMITED_API */
Victor Stinner2bdc7f52011-09-29 01:04:08 +020099
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800100/* These definitions must match corresponding definitions in graminit.h. */
Victor Stinner2bdc7f52011-09-29 01:04:08 +0200101#define Py_single_input 256
102#define Py_file_input 257
103#define Py_eval_input 258
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800104#define Py_func_type_input 345
Victor Stinner2bdc7f52011-09-29 01:04:08 +0200105
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100106/* This doesn't need to match anything */
107#define Py_fstring_input 800
108
Victor Stinner2bdc7f52011-09-29 01:04:08 +0200109#endif /* !Py_COMPILE_H */