Rune - Generation work
[rune.git] / librune / stmt.h
1 /*
2  * STMT.H
3  *
4  * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved.  See the  
5  *    COPYRIGHT file at the base of the distribution.
6  */
7
8 struct Parse;
9 struct Stmt;
10
11 typedef runctx_p (*st_run_t)(runctx_p ct, struct Stmt *st);
12 typedef genctx_p (*st_gen_t)(genctx_p ct, struct Stmt *st);
13
14 /*
15  * Compiler/Interpreter.  The return value is designed to allow the
16  * interpreter to deal with break/continue, and even though we do not
17  * have exceptions we can theoretically use it to deal with those too.
18  */
19 typedef RUNE_HEAD(stmtlist, Stmt) stmtlist_t;
20
21 typedef struct Stmt {
22         RUNE_ENTRY(Stmt) st_Node;       /* node in list */
23         st_run_t        st_Run;         /* function vector for interpreter */
24         st_gen_t        st_Gen;         /* function vector for generator */
25         struct Stmt     *st_Parent;     /* parent statement */
26         int             st_Op;          /* ST_ code */
27         int             st_Flags;       /* flags */
28         int             st_SaveOffset;  /* when saving */
29         stmtlist_t      st_List;        /* base of list / parse tree */
30         SemGroup        *st_MyGroup;
31         LexRef          st_LexRef;      /* for error reporting */
32         union {
33                 struct {
34                         struct Parse    *es_Parser;     /* possibly shared */
35                         string_t        es_AsId;        /* NULL if self */
36                         Declaration     *es_Decl;
37                         char            *es_Path;
38                         char            *es_File;
39                         SemGroup        *es_SemGroup;   /* top-level SemGroup */
40                         void            *es_DLL;        /* DLL */
41                 } ImportStmt;
42                 struct {
43                         Declaration     *es_Decl;
44                         Type            *es_Super;
45                         /* es_Super is same as es_Decl->d_ClassDecl.ed.. */
46                 } ClassStmt;
47                 struct {
48                         Declaration     *es_Decl;
49                 } TypedefStmt;
50                 /*
51                  * NOTE: declarations are stored in their SemGroup's
52                  *       linked list.  The DeclStmt below points into a
53                  *       subset of this list.
54                  */
55                 struct {
56                         Declaration     *es_Decl;       /* first declaration */
57                         int             es_DeclCount;   /* number of decls */
58                         Scope           es_Scope;
59                         int             es_CallCount;   /* heuristic */
60                         runesize_t      es_TmpOffset;   /* type def init */
61                 } DeclStmt;
62                 struct {
63                 } BlockStmt;
64                 struct {
65                 } NopStmt;
66                 struct {
67                 } ModuleStmt;
68                 struct {
69                         struct Stmt     *es_Init;
70                         Exp             *es_BCond;
71                         Exp             *es_ACond;
72                         Exp             *es_AExp;
73                         struct Stmt     *es_Body;
74                 } LoopStmt;
75                 struct {
76                         int             es_IsCont;
77                         int             es_Count;       /* +N break, -N cont */
78                         int             es_StmtType;
79                 } BreakStmt;
80                 struct {
81                 } BadStmt;
82                 struct {
83                         Exp             *es_Exp;
84                         struct Stmt     *es_TrueStmt;
85                         struct Stmt     *es_FalseStmt;
86                 } IfStmt;
87                 struct {
88                         int             es_Count;
89                         Exp             *es_Exp;
90                         Type            *es_ProcRetType;
91                 } RetStmt;
92                 struct {
93                         int             es_Count;
94                         Exp             *es_Exp;
95                         Type            *es_ProcRetType;
96                 } ResStmt;
97                 struct {
98                         int             es_Mode;
99                 } ThreadSchedStmt;
100                 struct {
101                         Exp             *es_Exp;        /* switch expression */
102                         struct Stmt     *es_Default;    /* default statement or NULL */
103                 } SwStmt;
104                 struct {
105                         Exp             *es_Exp;
106                         genlabel_t      *es_Label;      /* (generator only) */
107                 } CaseStmt;
108                 struct {
109                         Exp             *es_Exp;
110                 } ExpStmt;
111         } u;
112 } Stmt;
113
114 #define st_BlockStmt    u.BlockStmt
115 #define st_ProcStmt     u.DeclStmt
116 #define st_ImportStmt   u.ImportStmt
117 #define st_ClassStmt    u.ClassStmt
118 #define st_TypedefStmt  u.TypedefStmt
119 #define st_DeclStmt     u.DeclStmt
120 #define st_NopStmt      u.NopStmt
121 #define st_ModuleStmt   u.ModuleStmt
122 #define st_LoopStmt     u.LoopStmt
123 #define st_BreakStmt    u.BreakStmt
124 #define st_BadStmt      u.BadStmt
125 #define st_IfStmt       u.IfStmt
126 #define st_RetStmt      u.RetStmt
127 #define st_ResStmt      u.ResStmt
128 #define st_SwStmt       u.SwStmt
129 #define st_CaseStmt     u.CaseStmt
130 #define st_ExpStmt      u.ExpStmt
131 #define st_ThreadSchedStmt      u.ThreadSchedStmt
132
133 #define ST_UNKNOWN      0       /* placemarker for fwd reference */
134 #define ST_Import       0x0001  /* import statement or top-level file */
135 #define ST_Class        0x0002  /* class */
136 #define ST_Typedef      0x0003  /* typedef */
137 #define ST_Decl         0x0004  /* declaration wrapped by a statement */
138 #define ST_Block        0x0005  /* braced block */
139 #define ST_Nop          0x0006  /* empty statement (e.g. just semicolon) */
140 #define ST_Loop         0x0007  /* for/while/do/until loop */
141 #define ST_BreakCont    0x0008  /* break or continue */
142 #define ST_Bad          0x0009  /* bad/illegal statement */
143 #define ST_IfElse       0x000A  /* if or if-else */
144 #define ST_Return       0x000B  /* return from procedure */
145 #define ST_Result       0x000C  /* return value and continue as thread */
146 #define ST_Switch       0x000D  /* switch */
147 #define ST_Case         0x000E  /* case/default in switch */
148 #define ST_Exp          0x000F  /* expression wrapped by a statement */
149 #define ST_Else         0x0010  /* (synthesized for break/continue) */
150 #define ST_Proc         0x0011  /* procedure body block statement */
151 #define ST_ThreadSched  0x0012  /* thread schedule statement */
152 #define ST_Module       0x0013  /* A module */
153
154 /*
155  * STF_SEMANTIC         - Indicates that this is a new semantic lavel for
156  *                        the purposes of a semantic search.
157  *
158  *                        (ST_Import, ST_Class, ST_Block, ST_Proc,
159  *                        ST_Loop, ST_Case, ST_Module)
160  *
161  * STF_SEMTOP           - ST_Import, ST_Class, ST_Proc,  ST_Module
162  *
163  * STF_FORWARD          - Indicates that forward references may occur
164  *                        at this semantic level.  This flag is typically
165  *                        only set in certain non-executable statements.
166  *
167  * STF_NESTED           - Indicates that a procedure (ST_Proc) is nested
168  */
169 #define STF_SEMANTIC            0x0001  /* a semantic rendezvous point */
170 #define STF_FORWARD             0x0002  /* forward references may occur */
171 #define STF_UNUSED04            0x0004
172 #define STF_RESOLVING           0x0008  /* resolver interlock */
173 #define STF_RESOLVED            0x0010  /* resolver interlock */
174 #define STF_MODEXIT             0x0020  /* (import) exit on return */
175 #define STF_SEMTOP              0x0040  /* top level in semantic group terms */
176 #define STF_SELFCONTAINED       0x0080  /* (import) self contained */
177 #define STF_SHARED              0x0100  /* (import) shared import */
178 #define STF_NESTED              0x0200  /* (proc) nested procedure */
179
180 #define dassert_stmt(stmt, cond)        if (!(cond)) StmtFatalError((stmt), 0)
181
182 #define StmtFatalError(stmt, type)                      \
183             do {                                        \
184                     StmtPrintError(stmt, type);         \
185                     dassert(0);                         \
186             } while(0)