reflection_api.h

Includes:
<stdint.h>
<stdbool.h>
<stddef.h>

Introduction

AST Reflection and Introspection API for JCC

Discussion

This header provides comprehensive APIs for: - Type introspection and lookup - Enum reflection - Struct/union member introspection - AST node construction (literals, expressions, statements) - Control flow builders (switch, return, if, while, for) - Function and struct construction

All functions require an explicit JCC *vm parameter. For convenience wrappers that hide the vm parameter in pragma macros, see pragma_api.h.



Functions

ast_enum_at
ast_enum_constant_name
ast_enum_constant_value
ast_enum_count
ast_enum_find
ast_enum_name
ast_enum_value
ast_enum_value_count
ast_enum_value_name
ast_find_global
ast_find_type
ast_func_body
ast_func_param_at
ast_func_param_count
ast_function
ast_function_add_param
ast_function_set_body
ast_get_type
ast_global_at
ast_global_count
ast_int_literal
ast_is_array
ast_is_enum
ast_is_flonum
ast_is_function
ast_is_integer
ast_is_pointer
ast_is_struct
ast_is_union
ast_make_array
ast_make_function
ast_make_pointer
ast_member_bitfield_width
ast_member_is_bitfield
ast_member_name
ast_member_offset
ast_member_type
ast_node_arg_at
ast_node_arg_count
ast_node_binary
ast_node_block
ast_node_call
ast_node_cast
ast_node_float
ast_node_float_value
ast_node_func_name
ast_node_ident
ast_node_int_value
ast_node_kind
ast_node_left
ast_node_member
ast_node_num
ast_node_right
ast_node_stmt_at
ast_node_stmt_count
ast_node_string
ast_node_string_value
ast_node_token
ast_node_type
ast_node_unary
ast_node_var
ast_obj_is_definition
ast_obj_is_function
ast_obj_is_static
ast_obj_name
ast_obj_type
ast_return
ast_string_literal
ast_struct
ast_struct_add_field
ast_struct_member_at
ast_struct_member_count
ast_struct_member_find
ast_switch
ast_switch_add_case
ast_switch_set_default
ast_token_column
ast_token_filename
ast_token_line
ast_token_text
ast_type_align
ast_type_array_len
ast_type_base
ast_type_exists
ast_type_is_const
ast_type_is_unsigned
ast_type_is_variadic
ast_type_kind
ast_type_name
ast_type_param_at
ast_type_param_count
ast_type_return_type
ast_type_size
ast_var_ref

ast_enum_at


EnumConstant *ast_enum_at(
    JCC *vm,
    Type *enum_type,
    int index);  
Discussion

Get enum constant at index (0-based). Returns NULL if out of bounds.


ast_enum_constant_name


Discussion

Get enum constant name.


ast_enum_constant_value


Discussion

Get enum constant value.


ast_enum_count


int ast_enum_count(
    JCC *vm,
    Type *enum_type);  
Discussion

Get the number of enum constants. Returns -1 if not an enum.


ast_enum_find


EnumConstant *ast_enum_find(
    JCC *vm,
    Type *enum_type,
    const char *name);  
Discussion

Find enum constant by name. Returns NULL if not found.


ast_enum_name


const char *ast_enum_name(
    Type *e);  
Discussion

Get the name of an enum type.


ast_enum_value


int64_t ast_enum_value(
    Type *e,
    size_t index);  
Discussion

Get the integer value of an enum constant at index.


ast_enum_value_count


size_t ast_enum_value_count(
    Type *e);  
Discussion

Get the number of values in an enum.


ast_enum_value_name


const char *ast_enum_value_name(
    Type *e,
    size_t index);  
Discussion

Get the name of an enum value at index.


ast_find_global


Obj *ast_find_global(
    JCC *vm,
    const char *name);  
Discussion

Find a global symbol by name. Returns NULL if not found.


ast_find_type


Type *ast_find_type(
    JCC *vm,
    const char *name);  
Discussion

Find a type by name. Returns NULL if not found.


ast_func_body


Node *ast_func_body(
    Obj *func);  
Discussion

For functions: get function body AST. Returns NULL if no body.


ast_func_param_at


Obj *ast_func_param_at(
    Obj *func,
    int index);  
Discussion

For functions: get parameter at index. Returns NULL if out of bounds.


ast_func_param_count


int ast_func_param_count(
    Obj *func);  
Discussion

For functions: get parameter count. Returns -1 if not a function.


ast_function


Node *ast_function(
    JCC *vm,
    const char *name,
    Type *return_type);  
Discussion

Create a function declaration/definition node.


ast_function_add_param


void ast_function_add_param(
    JCC *vm,
    Node *func_node,
    const char *name,
    Type *type);  
Discussion

Add a parameter to a function.


ast_function_set_body


void ast_function_set_body(
    JCC *vm,
    Node *func_node,
    Node *body);  
Discussion

Set the body of a function.


ast_get_type


Type *ast_get_type(
    JCC *vm,
    const char *name);  
Discussion

Lookup a type by name in the current scope (high-level wrapper).


ast_global_at


Obj *ast_global_at(
    JCC *vm,
    int index);  
Discussion

Get global symbol at index (0-based). Returns NULL if out of bounds.


ast_global_count


int ast_global_count(
    JCC *vm);  
Discussion

Get the total number of global symbols.


ast_int_literal


Node *ast_int_literal(
    JCC *vm,
    int64_t value);  
Discussion

Create an integer literal node (high-level).


ast_is_array


bool ast_is_array(
    Type *ty);  
Discussion

Check if type is an array type.


ast_is_enum


bool ast_is_enum(
    Type *ty);  
Discussion

Check if type is an enum type.


ast_is_flonum


bool ast_is_flonum(
    Type *ty);  
Discussion

Check if type is a floating-point type.


ast_is_function


bool ast_is_function(
    Type *ty);  
Discussion

Check if type is a function type.


ast_is_integer


bool ast_is_integer(
    Type *ty);  
Discussion

Check if type is an integer type.


ast_is_pointer


bool ast_is_pointer(
    Type *ty);  
Discussion

Check if type is a pointer type.


ast_is_struct


bool ast_is_struct(
    Type *ty);  
Discussion

Check if type is a struct type.


ast_is_union


bool ast_is_union(
    Type *ty);  
Discussion

Check if type is a union type.


ast_make_array


Type *ast_make_array(
    Type *base,
    int length);  
Discussion

Create an array type with specified length.


ast_make_function


Type *ast_make_function(
    Type *return_type,
    Type **param_types,
    int param_count,
    bool is_variadic);  
Discussion

Create a function type.


ast_make_pointer


Discussion

Create a pointer type to base.


ast_member_bitfield_width


Discussion

Get bitfield width (returns 0 if not a bitfield).


ast_member_is_bitfield


Discussion

Check if member is a bitfield.


ast_member_name


const char *ast_member_name(
    Member *m);  
Discussion

Get member name.


ast_member_offset


Discussion

Get member offset in bytes.


ast_member_type


Discussion

Get member type.


ast_node_arg_at


Node *ast_node_arg_at(
    Node *node,
    int index);  
Discussion

For function calls: get argument at index.


ast_node_arg_count


int ast_node_arg_count(
    Node *node);  
Discussion

For function calls: get argument count.


ast_node_binary


Node *ast_node_binary(
    JCC *vm,
    NodeKind op,
    Node *left,
    Node *right);  
Discussion

Create a binary operation node.


ast_node_block


Node *ast_node_block(
    JCC *vm,
    Node **stmts,
    int stmt_count);  
Discussion

Create a block (compound statement) node.


ast_node_call


Node *ast_node_call(
    JCC *vm,
    const char *func_name,
    Node **args,
    int arg_count);  
Discussion

Create a function call node.


ast_node_cast


Node *ast_node_cast(
    JCC *vm,
    Node *expr,
    Type *target_type);  
Discussion

Create a type cast node.


ast_node_float


Node *ast_node_float(
    JCC *vm,
    double value);  
Discussion

Create a floating-point literal node.


ast_node_float_value


double ast_node_float_value(
    Node *node);  
Discussion

For floating-point literals: get value.


ast_node_func_name


const char *ast_node_func_name(
    Node *node);  
Discussion

For function calls: get function name.


ast_node_ident


Node *ast_node_ident(
    JCC *vm,
    const char *name);  
Discussion

Create an identifier (variable reference) node.


ast_node_int_value


long long ast_node_int_value(
    Node *node);  
Discussion

For numeric literals: get integer value.


ast_node_kind


Discussion

Get the NodeKind of a node.


ast_node_left


Node *ast_node_left(
    Node *node);  
Discussion

Get left child node (for binary/unary operations).


ast_node_member


Node *ast_node_member(
    JCC *vm,
    Node *object,
    const char *member_name);  
Discussion

Create a member access node (struct.member or ptr->member).


ast_node_num


Node *ast_node_num(
    JCC *vm,
    long long value);  
Discussion

Create a numeric literal node.


ast_node_right


Discussion

Get right child node (for binary operations).


ast_node_stmt_at


Node *ast_node_stmt_at(
    Node *node,
    int index);  
Discussion

For blocks: get statement at index.


ast_node_stmt_count


int ast_node_stmt_count(
    Node *node);  
Discussion

For blocks: get statement count.


ast_node_string


Node *ast_node_string(
    JCC *vm,
    const char *str);  
Discussion

Create a string literal node.


ast_node_string_value


const char *ast_node_string_value(
    Node *node);  
Discussion

For string literals: get string content.


ast_node_token


Discussion

Get the source token for a node (for error reporting).


ast_node_type


Type *ast_node_type(
    Node *node);  
Discussion

Get the type of a node (after type resolution).


ast_node_unary


Node *ast_node_unary(
    JCC *vm,
    NodeKind op,
    Node *operand);  
Discussion

Create a unary operation node.


ast_node_var


Obj *ast_node_var(
    Node *node);  
Discussion

For variable references: get the Obj (variable/function object).


ast_obj_is_definition


bool ast_obj_is_definition(
    Obj *obj);  
Discussion

Check if object is a definition (not just a declaration).


ast_obj_is_function


bool ast_obj_is_function(
    Obj *obj);  
Discussion

Check if object is a function.


ast_obj_is_static


bool ast_obj_is_static(
    Obj *obj);  
Discussion

Check if object has static linkage.


ast_obj_name


const char *ast_obj_name(
    Obj *obj);  
Discussion

Get the name of an object (variable or function).


ast_obj_type


Type *ast_obj_type(
    Obj *obj);  
Discussion

Get the type of an object.


ast_return


Node *ast_return(
    JCC *vm,
    Node *expr);  
Discussion

Create a return statement node.


ast_string_literal


Node *ast_string_literal(
    JCC *vm,
    const char *str);  
Discussion

Create a string literal node (high-level).


ast_struct


Node *ast_struct(
    JCC *vm,
    const char *name);  
Discussion

Create a struct type node.


ast_struct_add_field


void ast_struct_add_field(
    JCC *vm,
    Node *struct_node,
    const char *name,
    Type *type);  
Discussion

Add a field to a struct.


ast_struct_member_at


Member *ast_struct_member_at(
    JCC *vm,
    Type *struct_type,
    int index);  
Discussion

Get member at index (0-based). Returns NULL if out of bounds.


ast_struct_member_count


int ast_struct_member_count(
    JCC *vm,
    Type *struct_type);  
Discussion

Get the number of members. Returns -1 if not a struct/union.


ast_struct_member_find


Member *ast_struct_member_find(
    JCC *vm,
    Type *struct_type,
    const char *name);  
Discussion

Find member by name. Returns NULL if not found.


ast_switch


Node *ast_switch(
    JCC *vm,
    Node *condition);  
Discussion

Create a switch statement node.


ast_switch_add_case


void ast_switch_add_case(
    JCC *vm,
    Node *switch_node,
    Node *value,
    Node *body);  
Discussion

Add a case to a switch statement.


ast_switch_set_default


void ast_switch_set_default(
    JCC *vm,
    Node *switch_node,
    Node *body);  
Discussion

Set the default case for a switch statement.


ast_token_column


int ast_token_column(
    Token *tok);  
Discussion

Get the column number from a token.


ast_token_filename


const char *ast_token_filename(
    Token *tok);  
Discussion

Get the filename from a token.


ast_token_line


int ast_token_line(
    Token *tok);  
Discussion

Get the line number from a token.


ast_token_text


int ast_token_text(
    Token *tok,
    char *buffer,
    int bufsize);  
Discussion

Get the text content of a token.


ast_type_align


int ast_type_align(
    Type *ty);  
Discussion

Get alignment in bytes.


ast_type_array_len


Discussion

For array types: get the array length. Returns -1 if not an array or is VLA.


ast_type_base


Discussion

For pointer/array types: get the base type. Returns NULL if not applicable.


ast_type_exists


bool ast_type_exists(
    JCC *vm,
    const char *name);  
Discussion

Check if a type exists by name.


ast_type_is_const


bool ast_type_is_const(
    Type *ty);  
Discussion

Check if type is const-qualified.


ast_type_is_unsigned


Discussion

Check if type is unsigned.


ast_type_is_variadic


Discussion

For function types: check if variadic. Returns false if not a function.


ast_type_kind


Discussion

Get the TypeKind of a type.


ast_type_name


const char *ast_type_name(
    Type *ty);  
Discussion

Get type name (for named types). Returns NULL if unnamed.


ast_type_param_at


Type *ast_type_param_at(
    Type *ty,
    int index);  
Discussion

For function types: get parameter type at index. Returns NULL if out of bounds.


ast_type_param_count


Discussion

For function types: get parameter count. Returns -1 if not a function.


ast_type_return_type


Discussion

For function types: get return type. Returns NULL if not a function.


ast_type_size


int ast_type_size(
    Type *ty);  
Discussion

Get sizeof() value in bytes.


ast_var_ref


Node *ast_var_ref(
    JCC *vm,
    const char *name);  
Discussion

Create a variable reference node (high-level).