reflection_api.h
IntroductionAST Reflection and Introspection API for JCC DiscussionThis 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_atEnumConstant *ast_enum_at( JCC *vm, Type *enum_type, int index); DiscussionGet enum constant at index (0-based). Returns NULL if out of bounds. ast_enum_constant_nameconst char *ast_enum_constant_name( EnumConstant *ec); DiscussionGet enum constant name. ast_enum_constant_valueint ast_enum_constant_value( EnumConstant *ec); DiscussionGet enum constant value. ast_enum_countint ast_enum_count( JCC *vm, Type *enum_type); DiscussionGet the number of enum constants. Returns -1 if not an enum. ast_enum_findEnumConstant *ast_enum_find( JCC *vm, Type *enum_type, const char *name); DiscussionFind enum constant by name. Returns NULL if not found. ast_enum_nameconst char *ast_enum_name( Type *e); DiscussionGet the name of an enum type. ast_enum_valueint64_t ast_enum_value( Type *e, size_t index); DiscussionGet the integer value of an enum constant at index. ast_enum_value_countsize_t ast_enum_value_count( Type *e); DiscussionGet the number of values in an enum. ast_enum_value_nameconst char *ast_enum_value_name( Type *e, size_t index); DiscussionGet the name of an enum value at index. ast_find_globalObj *ast_find_global( JCC *vm, const char *name); DiscussionFind a global symbol by name. Returns NULL if not found. ast_find_typeType *ast_find_type( JCC *vm, const char *name); DiscussionFind a type by name. Returns NULL if not found. ast_func_bodyNode *ast_func_body( Obj *func); DiscussionFor functions: get function body AST. Returns NULL if no body. ast_func_param_atObj *ast_func_param_at( Obj *func, int index); DiscussionFor functions: get parameter at index. Returns NULL if out of bounds. ast_func_param_countint ast_func_param_count( Obj *func); DiscussionFor functions: get parameter count. Returns -1 if not a function. ast_functionNode *ast_function( JCC *vm, const char *name, Type *return_type); DiscussionCreate a function declaration/definition node. ast_function_add_paramvoid ast_function_add_param( JCC *vm, Node *func_node, const char *name, Type *type); DiscussionAdd a parameter to a function. ast_function_set_bodyvoid ast_function_set_body( JCC *vm, Node *func_node, Node *body); DiscussionSet the body of a function. ast_get_typeType *ast_get_type( JCC *vm, const char *name); DiscussionLookup a type by name in the current scope (high-level wrapper). ast_global_atObj *ast_global_at( JCC *vm, int index); DiscussionGet global symbol at index (0-based). Returns NULL if out of bounds. ast_global_countint ast_global_count( JCC *vm); DiscussionGet the total number of global symbols. ast_int_literalNode *ast_int_literal( JCC *vm, int64_t value); DiscussionCreate an integer literal node (high-level). ast_is_arraybool ast_is_array( Type *ty); DiscussionCheck if type is an array type. ast_is_enumbool ast_is_enum( Type *ty); DiscussionCheck if type is an enum type. ast_is_flonumbool ast_is_flonum( Type *ty); DiscussionCheck if type is a floating-point type. ast_is_functionbool ast_is_function( Type *ty); DiscussionCheck if type is a function type. ast_is_integerbool ast_is_integer( Type *ty); DiscussionCheck if type is an integer type. ast_is_pointerbool ast_is_pointer( Type *ty); DiscussionCheck if type is a pointer type. ast_is_structbool ast_is_struct( Type *ty); DiscussionCheck if type is a struct type. ast_is_unionbool ast_is_union( Type *ty); DiscussionCheck if type is a union type. ast_make_arrayType *ast_make_array( Type *base, int length); DiscussionCreate an array type with specified length. ast_make_functionType *ast_make_function( Type *return_type, Type **param_types, int param_count, bool is_variadic); DiscussionCreate a function type. ast_make_pointerType *ast_make_pointer( Type *base); DiscussionCreate a pointer type to base. ast_member_bitfield_widthint ast_member_bitfield_width( Member *m); DiscussionGet bitfield width (returns 0 if not a bitfield). ast_member_is_bitfieldbool ast_member_is_bitfield( Member *m); DiscussionCheck if member is a bitfield. ast_member_nameconst char *ast_member_name( Member *m); DiscussionGet member name. ast_member_offsetint ast_member_offset( Member *m); DiscussionGet member offset in bytes. ast_member_typeType *ast_member_type( Member *m); DiscussionGet member type. ast_node_arg_atNode *ast_node_arg_at( Node *node, int index); DiscussionFor function calls: get argument at index. ast_node_arg_countint ast_node_arg_count( Node *node); DiscussionFor function calls: get argument count. ast_node_binaryDiscussionCreate a binary operation node. ast_node_blockNode *ast_node_block( JCC *vm, Node **stmts, int stmt_count); DiscussionCreate a block (compound statement) node. ast_node_callNode *ast_node_call( JCC *vm, const char *func_name, Node **args, int arg_count); DiscussionCreate a function call node. ast_node_castNode *ast_node_cast( JCC *vm, Node *expr, Type *target_type); DiscussionCreate a type cast node. ast_node_floatNode *ast_node_float( JCC *vm, double value); DiscussionCreate a floating-point literal node. ast_node_float_valuedouble ast_node_float_value( Node *node); DiscussionFor floating-point literals: get value. ast_node_func_nameconst char *ast_node_func_name( Node *node); DiscussionFor function calls: get function name. ast_node_identNode *ast_node_ident( JCC *vm, const char *name); DiscussionCreate an identifier (variable reference) node. ast_node_int_valuelong long ast_node_int_value( Node *node); DiscussionFor numeric literals: get integer value. ast_node_kindNodeKind ast_node_kind( Node *node); DiscussionGet the NodeKind of a node. ast_node_leftNode *ast_node_left( Node *node); DiscussionGet left child node (for binary/unary operations). ast_node_memberNode *ast_node_member( JCC *vm, Node *object, const char *member_name); DiscussionCreate a member access node (struct.member or ptr->member). ast_node_numNode *ast_node_num( JCC *vm, long long value); DiscussionCreate a numeric literal node. ast_node_rightNode *ast_node_right( Node *node); DiscussionGet right child node (for binary operations). ast_node_stmt_atNode *ast_node_stmt_at( Node *node, int index); DiscussionFor blocks: get statement at index. ast_node_stmt_countint ast_node_stmt_count( Node *node); DiscussionFor blocks: get statement count. ast_node_stringNode *ast_node_string( JCC *vm, const char *str); DiscussionCreate a string literal node. ast_node_string_valueconst char *ast_node_string_value( Node *node); DiscussionFor string literals: get string content. ast_node_tokenToken *ast_node_token( Node *node); DiscussionGet the source token for a node (for error reporting). ast_node_typeType *ast_node_type( Node *node); DiscussionGet the type of a node (after type resolution). ast_node_unaryNode *ast_node_unary( JCC *vm, NodeKind op, Node *operand); DiscussionCreate a unary operation node. ast_node_varObj *ast_node_var( Node *node); DiscussionFor variable references: get the Obj (variable/function object). ast_obj_is_definitionbool ast_obj_is_definition( Obj *obj); DiscussionCheck if object is a definition (not just a declaration). ast_obj_is_functionbool ast_obj_is_function( Obj *obj); DiscussionCheck if object is a function. ast_obj_is_staticbool ast_obj_is_static( Obj *obj); DiscussionCheck if object has static linkage. ast_obj_nameconst char *ast_obj_name( Obj *obj); DiscussionGet the name of an object (variable or function). ast_obj_typeType *ast_obj_type( Obj *obj); DiscussionGet the type of an object. ast_returnNode *ast_return( JCC *vm, Node *expr); DiscussionCreate a return statement node. ast_string_literalNode *ast_string_literal( JCC *vm, const char *str); DiscussionCreate a string literal node (high-level). ast_structNode *ast_struct( JCC *vm, const char *name); DiscussionCreate a struct type node. ast_struct_add_fieldvoid ast_struct_add_field( JCC *vm, Node *struct_node, const char *name, Type *type); DiscussionAdd a field to a struct. ast_struct_member_atMember *ast_struct_member_at( JCC *vm, Type *struct_type, int index); DiscussionGet member at index (0-based). Returns NULL if out of bounds. ast_struct_member_countint ast_struct_member_count( JCC *vm, Type *struct_type); DiscussionGet the number of members. Returns -1 if not a struct/union. ast_struct_member_findMember *ast_struct_member_find( JCC *vm, Type *struct_type, const char *name); DiscussionFind member by name. Returns NULL if not found. ast_switchNode *ast_switch( JCC *vm, Node *condition); DiscussionCreate a switch statement node. ast_switch_add_casevoid ast_switch_add_case( JCC *vm, Node *switch_node, Node *value, Node *body); DiscussionAdd a case to a switch statement. ast_switch_set_defaultvoid ast_switch_set_default( JCC *vm, Node *switch_node, Node *body); DiscussionSet the default case for a switch statement. ast_token_columnint ast_token_column( Token *tok); DiscussionGet the column number from a token. ast_token_filenameconst char *ast_token_filename( Token *tok); DiscussionGet the filename from a token. ast_token_lineint ast_token_line( Token *tok); DiscussionGet the line number from a token. ast_token_textint ast_token_text( Token *tok, char *buffer, int bufsize); DiscussionGet the text content of a token. ast_type_alignint ast_type_align( Type *ty); DiscussionGet alignment in bytes. ast_type_array_lenint ast_type_array_len( Type *ty); DiscussionFor array types: get the array length. Returns -1 if not an array or is VLA. ast_type_baseType *ast_type_base( Type *ty); DiscussionFor pointer/array types: get the base type. Returns NULL if not applicable. ast_type_existsbool ast_type_exists( JCC *vm, const char *name); DiscussionCheck if a type exists by name. ast_type_is_constbool ast_type_is_const( Type *ty); DiscussionCheck if type is const-qualified. ast_type_is_unsignedbool ast_type_is_unsigned( Type *ty); DiscussionCheck if type is unsigned. ast_type_is_variadicbool ast_type_is_variadic( Type *ty); DiscussionFor function types: check if variadic. Returns false if not a function. ast_type_kindTypeKind ast_type_kind( Type *ty); DiscussionGet the TypeKind of a type. ast_type_nameconst char *ast_type_name( Type *ty); DiscussionGet type name (for named types). Returns NULL if unnamed. ast_type_param_atType *ast_type_param_at( Type *ty, int index); DiscussionFor function types: get parameter type at index. Returns NULL if out of bounds. ast_type_param_countint ast_type_param_count( Type *ty); DiscussionFor function types: get parameter count. Returns -1 if not a function. ast_type_return_typeType *ast_type_return_type( Type *ty); DiscussionFor function types: get return type. Returns NULL if not a function. ast_type_sizeint ast_type_size( Type *ty); DiscussionGet sizeof() value in bytes. ast_var_refNode *ast_var_ref( JCC *vm, const char *name); DiscussionCreate a variable reference node (high-level). |