luaur_ast/records/ast_node.rs
1//! Faithful port of Luau `AstNode` — the base of every AST node
2//! (`Ast/include/Luau/Ast.h`).
3//!
4//! C++ `AstNode` is `{ const int classIndex; Location location; }` plus a vtable
5//! (`visit`, `as_expr`/`as_stat`/`as_type`/`as_attr`, and the `is<T>()`/`as<T>()`
6//! RTTI helpers). Luau nodes are standard-layout single-inheritance, so a
7//! `class X : Y` lays `Y` out at offset 0. We reproduce that with `#[repr(C)]`
8//! and a `pub base: Y` first field on every node, which makes the C++
9//! `static_cast<T*>(this)` downcast a plain pointer cast (see [`crate::rtti`]).
10//!
11//! `classIndex` is `const` in C++ (set once at construction); Rust has no const
12//! fields, so it is a plain `pub i32` written by each node's constructor to that
13//! node's [`crate::rtti::AstNodeClass::CLASS_INDEX`]. `visit` and the
14//! `as{Expr,Stat,Type,Attr}` virtuals are separate method items.
15
16use crate::records::location::Location;
17
18#[repr(C)]
19#[derive(Debug, Clone)]
20pub struct AstNode {
21 pub class_index: i32,
22 pub location: Location,
23}