Created
January 10, 2025 19:43
-
-
Save alexover1/2a127065c059c322d2b67aba1cfdb049 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Basic"; | |
#import "Compiler"; | |
#import "Hash_Table"; | |
get_plugin :: () -> *Metaprogram_Plugin { | |
p := New(Metaprogram_Plugin); | |
p.message = message; | |
return p; | |
} | |
message :: (p: *Metaprogram_Plugin, message: *Message) { | |
if message.kind == { | |
case .TYPECHECKED; | |
tc := cast(*Message_Typechecked) message; | |
for tc.declarations { | |
declaration := it.expression; | |
if !declaration.enclosing_load.enclosing_import || declaration.enclosing_load.enclosing_import.module_type != .MAIN_PROGRAM { | |
continue; | |
} | |
array_add(*global_declarations, declaration); | |
} | |
for tc.procedure_bodies { | |
body := it.expression; | |
if !body.enclosing_load.enclosing_import || body.enclosing_load.enclosing_import.module_type != .MAIN_PROGRAM { | |
continue; | |
} | |
table_set(*procedure_bodies, it.expression, it); | |
} | |
case .PHASE; | |
ph := cast(*Message_Phase) message; | |
if ph.phase == .TYPECHECKED_ALL_WE_CAN { | |
variable_was_modified: Table(*Code_Declaration, bool); | |
for procedure_bodies { | |
for it.subexpressions { | |
if it.kind == { | |
case .DECLARATION; | |
table_set(*variable_was_modified, cast(*Code_Declaration)it, false); | |
case .IDENT; | |
ident := cast(*Code_Ident) it; | |
table_set(*variable_was_modified, ident.resolved_declaration, true); | |
} | |
} | |
} | |
for was_modified, declaration : variable_was_modified { | |
if !was_modified { | |
for global : global_declarations { | |
if declaration.name == global.name { | |
compiler_report(get_filename(declaration), declaration.l0, declaration.c0, tprint("Declaration '%' is shadowing declaration with the same name in global scope.\n", declaration.name), mode = .WARNING); | |
compiler_report(get_filename(global), global.l0, global.c0, tprint("Here is the declaration being shadowed.\n"), mode = .INFO); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
#scope_module | |
global_declarations: [..] *Code_Declaration; | |
procedure_bodies: Table(*Code_Procedure_Body, Typechecked(Code_Procedure_Body)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment