17#include "../ast/ast.h"
18#include "../c_libs/memory/memory.h"
19#include "../helper/help.h"
20#include "../lexer/lexer.h"
135 size_t column,
const char *var_name,
136 const char *function_name,
Token *tokens,
137 size_t token_count,
const char *file_path);
139 const char *var_name,
const char *function_name,
140 bool is_conditional);
144 const char *var_name,
size_t line,
146 Token *tokens,
int token_count,
147 const char *file_path,
148 const char *function_name);
153 const char *new_var,
const char *source_var,
154 const char *function_name);
156 const char *var_name,
157 const char *function_name);
159 const char *var_name,
size_t line,
161 Token *tokens,
int token_count,
162 const char *file_path,
163 const char *function_name,
177 bool is_mutable,
bool returns_ownership,
178 bool takes_ownership,
183 Scope *requesting_module_scope);
186 Scope *requesting_module_scope);
196 AstNode **modules,
size_t module_count,
210 const char *alias,
Scope *module_scope,
213 const char *symbol_name);
228void tc_error(
AstNode *node,
const char *error_type,
const char *format, ...);
230 const char *format, ...);
232 const char *format, ...);
270 AstNode **member_types,
const char **member_names,
271 size_t member_count,
size_t line,
size_t column);
280 const char ***member_names,
size_t *member_count);
Type * array_type(Parser *parser)
Definition type.c:22
Expr * index_expr(Parser *parser)
Arena allocator structure.
Definition memory.h:101
Configuration structure to hold build options parsed from CLI.
Definition help.h:48
Growable array backed by an arena allocator.
Definition memory.h:112
const char * module_name
Definition type.h:104
bool processed
Definition type.h:106
GrowableArray dependencies
Definition type.h:105
Represents an imported module with optional aliasing.
Definition type.h:97
Scope * module_scope
Definition type.h:100
const char * module_name
Definition type.h:98
const char * alias
Definition type.h:99
Represents a lexical scope with hierarchical relationships.
Definition type.h:67
GrowableArray link_libs
Definition type.h:88
GrowableArray imported_modules
Definition type.h:79
GrowableArray children
Definition type.h:70
BuildConfig * config
Definition type.h:91
GrowableArray symbols
Definition type.h:69
const char * scope_name
Definition type.h:71
bool takes_ownership
Definition type.h:82
const char * module_name
Definition type.h:78
StaticMemoryAnalyzer * memory_analyzer
Definition type.h:85
AstNode * associated_node
Definition type.h:74
struct Scope * parent
Definition type.h:68
bool is_module_scope
Definition type.h:77
size_t depth
Definition type.h:72
bool returns_ownership
Definition type.h:81
GrowableArray deferred_frees
Definition type.h:86
bool is_function_scope
Definition type.h:73
const char * function_name
Definition type.h:41
size_t column
Definition type.h:33
size_t line
Definition type.h:32
bool has_matching_free
Definition type.h:35
bool reported
Definition type.h:39
int conditional_free_count
Definition type.h:37
bool address_taken
Definition type.h:40
const char * variable_name
Definition type.h:34
int free_count
Definition type.h:36
GrowableArray aliases
Definition type.h:38
const char * file_path
Definition type.h:42
GrowableArray allocations
Definition type.h:46
ArenaAllocator * arena
Definition type.h:47
bool skip_memory_tracking
Definition type.h:48
Represents a symbol with associated type and metadata.
Definition type.h:54
AstNode * type
Definition type.h:56
bool is_public
Definition type.h:57
const char * name
Definition type.h:55
bool is_mutable
Definition type.h:58
size_t scope_depth
Definition type.h:59
bool takes_ownership
Definition type.h:61
bool returns_ownership
Definition type.h:60
Represents a single token extracted by the lexer.
Definition lexer.h:151
Represents an error encountered during type checking.
Definition type.h:121
const char * context
Definition type.h:125
size_t line
Definition type.h:123
size_t column
Definition type.h:124
const char * message
Definition type.h:122
bool check_array_bounds(AstNode *array_type, AstNode *index_expr, ArenaAllocator *arena)
Definition array.c:67
AstNode * create_struct_type(ArenaAllocator *arena, const char *name, AstNode **member_types, const char **member_names, size_t member_count, size_t line, size_t column)
Definition type.c:458
bool is_cast_valid(AstNode *from_type, AstNode *to_type)
Definition type.c:285
bool typecheck_use_stmt(AstNode *node, Scope *current_scope, Scope *global_scope, ArenaAllocator *arena)
Definition stmt.c:1438
AstNode * typecheck_deref_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1313
void static_memory_track_alloc(StaticMemoryAnalyzer *analyzer, size_t line, size_t column, const char *var_name, const char *function_name, Token *tokens, size_t token_count, const char *file_path)
Definition static_mem_tracker.c:14
ArenaAllocator * g_arena
Definition error.c:11
void static_memory_mark_addr_taken(StaticMemoryAnalyzer *analyzer, const char *var_name, const char *function_name)
Definition static_mem_tracker.c:236
bool typecheck_os_stmt(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition module.c:315
bool is_bool_type(AstNode *type)
Definition type.c:269
Symbol * scope_lookup_current_only_with_visibility(Scope *scope, const char *name, Scope *requesting_module_scope)
Look up a symbol only in the current scope with visibility rules.
Definition scope.c:205
bool is_pointer_to_function_type(AstNode *type)
Definition type.c:347
bool typecheck_module_stmt(AstNode *node, Scope *global_scope, ArenaAllocator *arena)
Definition stmt.c:1385
bool typecheck_enum_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1091
Symbol * scope_lookup(Scope *scope, const char *name)
Original scope_lookup - now wraps the visibility version.
Definition scope.c:197
void static_memory_check_free_nonalloc(StaticMemoryAnalyzer *analyzer, const char *var_name, size_t line, size_t column, Token *tokens, int token_count, const char *file_path, const char *function_name, ArenaAllocator *arena)
Definition static_mem_tracker.c:88
void static_memory_track_alias(StaticMemoryAnalyzer *analyzer, const char *new_var, const char *source_var, const char *function_name)
Definition static_mem_tracker.c:194
AstNode * typecheck_member_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:911
AstNode * typecheck_sizeof_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1729
const char * type_to_string(AstNode *type, ArenaAllocator *arena)
Definition type.c:354
bool typecheck_var_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:129
Scope * find_containing_module(Scope *scope)
Find the containing module scope for a given scope.
Definition scope.c:183
Symbol * scope_lookup_current_only(Scope *scope, const char *name)
Original scope_lookup_current_only - now wraps the visibility version.
Definition scope.c:238
bool scope_add_symbol_with_ownership(Scope *scope, const char *name, AstNode *type, bool is_public, bool is_mutable, bool returns_ownership, bool takes_ownership, ArenaAllocator *arena)
Definition scope.c:143
TypeMatchResult types_match(AstNode *type1, AstNode *type2)
Definition type.c:18
Symbol * lookup_qualified_symbol(Scope *scope, const char *module_alias, const char *symbol_name)
Look up a qualified symbol (module_alias.symbol_name) with visibility rules.
Definition module.c:61
AstNode * typecheck_array_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1749
bool add_module_import(Scope *importing_scope, const char *module_name, const char *alias, Scope *module_scope, ArenaAllocator *arena)
Add a module import to a scope.
Definition module.c:41
bool typecheck_program_multipass(AstNode *program, Scope *global_scope, ArenaAllocator *arena)
Three-pass typechecking for modules to handle forward references.
Definition lookup.c:189
AstNode * typecheck_system_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1612
void print_type_error(TypeError *error)
TypeMatchResult
Result of type compatibility checking.
Definition type.h:112
@ TYPE_MATCH_NONE
Definition type.h:115
@ TYPE_MATCH_EXACT
Definition type.h:113
@ TYPE_MATCH_COMPATIBLE
Definition type.h:114
AstNode * get_element_type(AstNode *array_or_pointer_type, ArenaAllocator *arena)
void debug_print_scope(Scope *scope, int indent_level)
Definition type.c:559
AstNode * typecheck_assignment_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:200
void static_memory_analyzer_init(StaticMemoryAnalyzer *analyzer, ArenaAllocator *arena)
Definition static_mem_tracker.c:6
AstNode * typecheck_call_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:335
AstNode * typecheck_cast_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1508
const char * get_current_function_name(Scope *scope)
Definition scope.c:132
bool validate_array_type(AstNode *array_type, Scope *scope, ArenaAllocator *arena)
Definition array.c:3
bool struct_has_member(AstNode *struct_type, const char *member_name)
Definition type.c:507
bool is_integer_type(AstNode *type)
Definition type.c:244
void tc_error_help(AstNode *node, const char *error_type, const char *help, const char *format,...)
Error with help message.
Definition error.c:68
void debug_print_struct_type(AstNode *struct_type, int indent)
Definition type.c:511
StaticMemoryAnalyzer * get_static_analyzer(Scope *scope)
Definition static_mem_tracker.c:225
const char * extract_variable_name(AstNode *expr)
Definition stmt.c:55
AstNode * get_struct_member_type(AstNode *struct_type, const char *member_name)
Definition type.c:488
bool typecheck(AstNode *node, Scope *scope, ArenaAllocator *arena, BuildConfig *config)
Definition tc.c:6
AstNode * typecheck_binary_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:7
bool typecheck_statement(AstNode *stmt, Scope *scope, ArenaAllocator *arena)
Definition lookup.c:6
bool typecheck_struct_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:665
bool process_module_in_order(const char *module_name, GrowableArray *dep_graph, AstNode **modules, size_t module_count, Scope *global_scope, ArenaAllocator *arena)
Process modules in dependency order (topological sort)
Definition module.c:137
bool scope_add_symbol(Scope *scope, const char *name, AstNode *type, bool is_public, bool is_mutable, ArenaAllocator *arena)
Definition scope.c:174
AstNode * typecheck_alloc_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1396
AstNode * typecheck_memcpy_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1500
AstNode * typecheck_input_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1538
Symbol * scope_lookup_with_visibility(Scope *scope, const char *name, Scope *requesting_module_scope)
Definition scope.c:81
bool typecheck_while_loop_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1490
bool is_numeric_type(AstNode *type)
Definition type.c:232
bool validate_array_initializer(AstNode *declared_type, AstNode *initializer, Scope *scope, ArenaAllocator *arena)
Definition array.c:184
void tc_error(AstNode *node, const char *error_type, const char *format,...)
Simple error reporting - replaces fprintf(stderr, ...) Usage: tc_error(node, "Type Error",...
Definition error.c:29
AstNode * typecheck_index_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:827
AstNode * typecheck_syscall_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Type check a syscall expression.
Definition expr.c:1659
bool typecheck_func_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:296
bool is_pointer_type(AstNode *type)
Definition type.c:253
AstNode * typecheck_addr_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1366
int g_token_count
Definition error.c:9
Scope * create_module_scope(Scope *global_scope, const char *module_name, ArenaAllocator *arena)
Create a new module scope.
Definition module.c:93
bool static_memory_check_use_after_free(StaticMemoryAnalyzer *analyzer, const char *var_name, size_t line, size_t column, ArenaAllocator *arena, Token *tokens, int token_count, const char *file_path, const char *function_name)
Definition static_mem_tracker.c:154
bool typecheck_switch_stmt(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1621
bool typecheck_infinite_loop_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1471
bool typecheck_return_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1127
bool is_struct_type_node(AstNode *type)
Definition type.c:275
bool register_module(Scope *global_scope, const char *module_name, Scope *module_scope, ArenaAllocator *arena)
Register a module scope in the global scope.
Definition module.c:8
bool typecheck_for_loop_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1521
void tc_error_init(Token *tokens, int token_count, const char *file_path, ArenaAllocator *arena)
Set global context for error reporting Call this once at the start of typechecking.
Definition error.c:17
bool typecheck_link_stmt(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition module.c:389
AstNode * typecheck_expression(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition lookup.c:98
Token * g_tokens
Definition error.c:8
const char * g_file_path
Definition error.c:10
bool is_function_type(AstNode *type)
Definition type.c:280
void static_memory_track_free(StaticMemoryAnalyzer *analyzer, const char *var_name, const char *function_name, bool is_conditional)
Definition static_mem_tracker.c:117
bool typecheck_loop_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1565
bool typecheck_spread_decl(AstNode *spread, Scope *scope, ArenaAllocator *arena, const char *child_name, GrowableArray *seen_names, AstNode ***member_types, const char ***member_names, size_t *member_count)
Definition stmt.c:554
void init_scope(Scope *scope, Scope *parent, const char *name, ArenaAllocator *arena)
Initialize a scope structure with enhanced metadata.
Definition scope.c:54
bool is_void_type(AstNode *type)
Definition type.c:263
Scope * find_module_scope(Scope *global_scope, const char *module_name)
Find a module scope by name.
Definition module.c:24
int static_memory_check_and_report(StaticMemoryAnalyzer *analyzer, ArenaAllocator *arena)
Definition static_mem_tracker.c:248
AstNode * typecheck_struct_expr_internal(AstNode *expr, Scope *scope, ArenaAllocator *arena, AstNode *expected_type)
Definition expr.c:1825
AstNode * typecheck_free_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:1417
bool is_array_type(AstNode *type)
Definition type.c:258
TypeMatchResult check_array_compatibility(AstNode *type1, AstNode *type2)
Definition array.c:143
bool typecheck_case_stmt(AstNode *node, Scope *scope, ArenaAllocator *arena, AstNode *expected_type)
Definition stmt.c:1796
bool typecheck_if_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1255
void tc_error_id(AstNode *node, const char *identifier, const char *error_type, const char *format,...)
Error with identifier highlighting.
Definition error.c:109
AstNode * typecheck_multidim_array_access(AstNode *base_type, AstNode **indices, size_t index_count, ArenaAllocator *arena)
Definition array.c:105
AstNode * typecheck_struct_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:2383
bool contains_alloc_expression(AstNode *expr)
Definition stmt.c:6
bool typecheck_defer_decl(AstNode *node, Scope *scope, ArenaAllocator *arena)
AstNode * get_enclosing_function_return_type(Scope *scope)
Definition type.c:448
AstNode * typecheck_unary_expr(AstNode *expr, Scope *scope, ArenaAllocator *arena)
Definition expr.c:157
bool typecheck_default_stmt(AstNode *node, Scope *scope, ArenaAllocator *arena)
Definition stmt.c:1910
bool is_pointer_assignment(AstNode *assignment)
Definition stmt.c:77
Scope * create_child_scope(Scope *parent, const char *name, ArenaAllocator *arena)
Create a new child scope under the specified parent.
Definition scope.c:273
void build_dependency_graph(AstNode **modules, size_t module_count, GrowableArray *dep_graph, ArenaAllocator *arena)
Definition module.c:105