plcopen 0.22.0
载入中...
搜索中...
未找到
ast.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
7#include "st/token.h"
8#include "st/type_desc.h"
9#include "st/types.h"
10
11// L0 AST (load domain, allocation allowed): index-based pools for
12// deterministic iteration; no polymorphism (core builds with -fno-rtti).
13
15{
16
17using ExprIndex = std::int32_t;
18using StmtIndex = std::int32_t;
19inline constexpr ExprIndex kNoExpr = -1;
20
21enum class ExprKind : std::uint8_t
22{
23 literal_int, // unsigned_value + based flag
24 literal_real, // real_value
25 literal_bool, // unsigned_value 0/1
26 literal_time, // signed_value ns
27 literal_typed, // TYPE# literal: literal_type + int/real payload (L1a)
28 literal_enum, // UserType#Member (L1b1; resolved by sema)
34 variable, // name
35 pin_read, // name '.' pin_name (FB output read)
36 unary, // op: minus / not
37 binary, // op token kind
38 call, // name '(' arguments ')': conversion/standard function
40};
41
43{
44 bool field = false;
45 std::string name;
46 std::vector<ExprIndex> indices;
47};
48
50{
51 std::string name; // empty for positional entries
53};
54
55enum class UnaryOp : std::uint8_t
56{
59};
60
79
80struct Expr
81{
83 std::int32_t line = 0;
84 std::int32_t column = 0;
85
86 std::uint64_t unsigned_value = 0;
87 std::int64_t signed_value = 0;
88 double real_value = 0.0;
89 bool based = false;
90 bool real_form = false; // literal_typed real payload
91 Type literal_type = Type::bool_; // literal_typed target type
92
93 std::string name; // variable / instance / conversion name
94 std::string pin; // pin name for pin_read
95 std::string text; // raw string payload (without quotes)
96
101 std::vector<ExprIndex> arguments;
102 std::vector<AccessStep> access;
103 std::vector<InitItem> items;
104};
105
121
123{
124 // Constant labels resolved by sema; parser stores expressions.
126 ExprIndex high = kNoExpr; // kNoExpr for single-value labels
127};
128
130{
131 std::vector<CaseLabel> labels;
132 std::vector<StmtIndex> body;
133};
134
136{
137 std::string pin;
138 std::int32_t line = 0;
139 std::int32_t column = 0;
141};
142
143struct Stmt
144{
146 std::int32_t line = 0;
147 std::int32_t column = 0;
148 bool debug_provenance = false;
149 std::string debug_pou;
150 std::string debug_call_path;
151 std::int32_t debug_line = 0;
152 std::int32_t debug_column = 0;
153 std::uint16_t debug_call_depth = 0;
154
155 // assign
156 std::string target; // variable name (original spelling)
157 std::vector<AccessStep> target_access;
159
160 // if
161 std::vector<ExprIndex> conditions; // IF + ELSIF conditions
162 std::vector<std::vector<StmtIndex>> branches; // matching bodies
163 std::vector<StmtIndex> else_body;
164
165 // case
167 std::vector<CaseArm> arms;
168
169 // for
170 std::string control; // control variable name
173 ExprIndex by = kNoExpr; // kNoExpr => implicit 1
174 std::vector<StmtIndex> body; // for/while/repeat body
175
176 // while / repeat
177 ExprIndex condition = kNoExpr; // WHILE pre / REPEAT UNTIL post
178
179 // fb_call
180 std::string instance;
181 std::vector<CallParam> params;
182};
183
185{
186 std::string name; // original spelling
187 std::string lower; // lookup key
188 bool is_fb = false;
189 bool is_constant = false; // VAR CONSTANT block member (L1a 2.5)
190 bool is_retain = false;
191 bool is_persistent = false;
192 bool is_located = false;
193 char location_area = 0; // I/Q/M
194 char location_width = 0; // X/B/W/D/L
195 std::uint32_t location_byte = 0;
196 std::uint8_t location_bit = 0;
198 FbType fb_type = FbType::r_trig;
199 std::string type_name; // non-empty for a user-defined type
200 std::uint32_t string_capacity = 0;
201 bool wide_string = false;
202 ExprIndex init = kNoExpr; // constant expression or kNoExpr
203 std::int32_t line = 0;
204 std::int32_t column = 0;
205};
206
207enum class UserTypeKind : std::uint8_t
208{
213};
214
216{
217 std::string name;
218 std::string lower;
220 std::string type_name;
221 std::int32_t line = 0;
222 std::int32_t column = 0;
223};
224
226{
227 std::string name;
228 std::string lower;
229 bool explicit_value = false;
230 std::int64_t value = 0;
231 std::int32_t line = 0;
232 std::int32_t column = 0;
233};
234
236{
237 std::string name;
238 std::string lower;
241 std::vector<EnumMemberDecl> enum_members;
244 std::vector<ArrayBound> array_bounds;
246 std::string element_type_name;
247 std::vector<StructFieldDecl> struct_fields;
248 std::int32_t line = 0;
249 std::int32_t column = 0;
250};
251
252enum class SfcQualifier : std::uint8_t
253{
254 n = 0,
263};
264
266{
267 std::string action;
268 std::string lower;
270 std::int64_t duration_ns = 0;
271 std::int32_t line = 0;
272 std::int32_t column = 0;
273 std::int32_t end_line = 0;
274 std::int32_t end_column = 0;
275};
276
278{
279 std::string name;
280 std::string lower;
281 bool initial = false;
282 bool terminal = false;
283 std::vector<SfcActionBlockDecl> actions;
284 std::int32_t line = 0;
285 std::int32_t column = 0;
286 std::int32_t end_line = 0;
287 std::int32_t end_column = 0;
288};
289
291{
292 std::vector<std::string> sources;
293 std::vector<std::string> targets;
294 std::string condition;
295 bool simultaneous = false;
296 std::int32_t condition_line = 0;
297 std::int32_t condition_column = 0;
298 std::int32_t line = 0;
299 std::int32_t column = 0;
300 std::int32_t end_line = 0;
301 std::int32_t end_column = 0;
302};
303
305{
306 std::string name;
307 std::string lower;
308 std::string body;
309 std::int32_t body_line = 0;
310 std::int32_t body_column = 0;
311 std::int32_t line = 0;
312 std::int32_t column = 0;
313 std::int32_t end_line = 0;
314 std::int32_t end_column = 0;
315};
316
318{
319 std::string name;
320 std::string lower;
321 std::string program_lower;
322 std::vector<SfcStepDecl> steps;
323 std::vector<SfcTransitionDecl> transitions;
324 std::vector<SfcActionDecl> actions;
325 std::int32_t line = 0;
326 std::int32_t column = 0;
327};
328
329struct Ast
330{
331 std::string program_name;
332 std::vector<UserTypeDecl> user_types;
333 std::vector<VarDecl> vars;
334 std::vector<StmtIndex> body;
335 std::vector<SfcNetworkDecl> sfc_networks;
336 std::vector<Expr> exprs;
337 std::vector<Stmt> stmts;
338
340 {
341 exprs.push_back(static_cast<Expr &&>(expr));
342 return static_cast<ExprIndex>(exprs.size() - 1);
343 }
344
346 {
347 stmts.push_back(static_cast<Stmt &&>(stmt));
348 return static_cast<StmtIndex>(stmts.size() - 1);
349 }
350};
351
352} // namespace plcopen::core::st
constexpr ExprIndex kNoExpr
定义 ast.h:19
std::int32_t StmtIndex
定义 ast.h:18
std::int32_t ExprIndex
定义 ast.h:17
generated::StBindingFbType FbType
std::vector< ExprIndex > indices
定义 ast.h:46
std::vector< Expr > exprs
定义 ast.h:336
std::vector< StmtIndex > body
定义 ast.h:334
ExprIndex add_expr(Expr expr)
定义 ast.h:339
StmtIndex add_stmt(Stmt stmt)
定义 ast.h:345
std::vector< VarDecl > vars
定义 ast.h:333
std::vector< Stmt > stmts
定义 ast.h:337
std::vector< UserTypeDecl > user_types
定义 ast.h:332
std::string program_name
定义 ast.h:331
std::vector< SfcNetworkDecl > sfc_networks
定义 ast.h:335
std::vector< StmtIndex > body
定义 ast.h:132
std::vector< CaseLabel > labels
定义 ast.h:131
std::vector< ExprIndex > arguments
定义 ast.h:101
std::int32_t column
定义 ast.h:84
std::vector< InitItem > items
定义 ast.h:103
std::vector< AccessStep > access
定义 ast.h:102
std::uint64_t unsigned_value
定义 ast.h:86
std::int64_t signed_value
定义 ast.h:87
std::vector< SfcTransitionDecl > transitions
定义 ast.h:323
std::vector< SfcStepDecl > steps
定义 ast.h:322
std::vector< SfcActionDecl > actions
定义 ast.h:324
std::vector< SfcActionBlockDecl > actions
定义 ast.h:283
std::vector< std::string > sources
定义 ast.h:292
std::vector< std::string > targets
定义 ast.h:293
std::string debug_call_path
定义 ast.h:150
std::vector< StmtIndex > body
定义 ast.h:174
std::uint16_t debug_call_depth
定义 ast.h:153
std::int32_t debug_column
定义 ast.h:152
std::vector< CallParam > params
定义 ast.h:181
std::vector< std::vector< StmtIndex > > branches
定义 ast.h:162
std::vector< AccessStep > target_access
定义 ast.h:157
std::vector< ExprIndex > conditions
定义 ast.h:161
std::vector< StmtIndex > else_body
定义 ast.h:163
std::vector< CaseArm > arms
定义 ast.h:167
std::int32_t debug_line
定义 ast.h:151
std::vector< EnumMemberDecl > enum_members
定义 ast.h:241
std::vector< ArrayBound > array_bounds
定义 ast.h:244
std::vector< StructFieldDecl > struct_fields
定义 ast.h:247
std::uint32_t location_byte
定义 ast.h:195
std::uint32_t string_capacity
定义 ast.h:200