erick-alcachofa 9626ac07c8
refactor(AST): Make optional some fields in AST nodes
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit refactors several AST nodes to use `Optional<T>` for fields
that are not always present. This includes `attribute` in
`ReflectionExpression`, `elseBranch` in `IfStatement` and
`WhileStatement`, `defaultCase` in `MatchStatement` and
`SwitchStatement`, and `preLoop` and `postLoop` in `CForStatement`. This
change improves the robustness and clarity of the AST by explicitly
modeling optionality.
2025-10-15 15:58:06 -06:00

140 lines
3.1 KiB
C++

#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Types.hpp>
#include <artichoke/Parser/AST/Statements.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct ModuleDeclaration;
struct StructDeclaration;
struct EnumDeclaration;
struct FunctionDeclaration;
struct ImportDeclaration;
struct AliasDeclaration;
/* Helper declaration node types */
struct EnumMember;
struct StructMember;
struct GenericParam;
struct FunctionParam;
} // namespace nodes
/* Public Aliases */
using ModuleDeclNode = Ptr<nodes::ModuleDeclaration>;
using StructDeclNode = Ptr<nodes::StructDeclaration>;
using EnumDeclNode = Ptr<nodes::EnumDeclaration>;
using FunctionDeclNode = Ptr<nodes::FunctionDeclaration>;
using ImportDeclNode = Ptr<nodes::ImportDeclaration>;
using AliasDeclNode = Ptr<nodes::AliasDeclaration>;
using GenericParamNode = Ptr<nodes::GenericParam>;
using FunctionParamNode = Ptr<nodes::FunctionParam>;
using EnumMemberNode = Ptr<nodes::EnumMember>;
using StructMemberNode = Ptr<nodes::StructMember>;
/* Variant nodes */
using TopLevelDeclNode = Variant<
ModuleDeclNode,
StructDeclNode,
EnumDeclNode,
FunctionDeclNode,
ImportDeclNode,
AliasDeclNode
>;
using ModuleInnerDeclNode = Variant<
StructDeclNode,
EnumDeclNode,
FunctionDeclNode
>;
/* Node definitions */
struct nodes::ModuleDeclaration {
SourceLocation location;
Boolean isExported;
NamespacedIdentifierNode name;
Vector<ModuleDeclNode> childModules;
Vector<ModuleInnerDeclNode> innerDeclarations;
Vector<AliasDeclNode> aliasDeclarations;
};
struct nodes::StructDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Vector<GenericParamNode> genericParams;
Vector<StructMemberNode> structMembers;
};
struct nodes::EnumDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Vector<GenericParamNode> genericParams;
Vector<EnumMemberNode> enumMembers;
};
struct nodes::FunctionDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Optional<TypeNode> returnType;
Vector<GenericParamNode> genericParams;
Vector<FunctionParamNode> functionParams;
CodeBlockStmtNode functionBody;
};
struct nodes::ImportDeclaration {
SourceLocation location;
Boolean importAll;
NamespacedIdentifierNode importTarget;
};
struct nodes::AliasDeclaration {
SourceLocation location;
String alias;
TypeNode target;
};
struct nodes::StructMember {
SourceLocation location;
String name;
TypeNode type;
};
struct nodes::EnumMember {
SourceLocation location;
String name;
Optional<TypeNode> type;
};
struct nodes::GenericParam {
SourceLocation location;
String name;
};
struct nodes::FunctionParam {
SourceLocation location;
Boolean isThis;
String name;
TypeNode type;
};
} // namespace arti::lang::ast