fixed label scope/mangling

This commit is contained in:
Mihai Bazon 2012-09-18 19:26:46 +03:00
parent 669874d46b
commit a4d2340c73
2 changed files with 14 additions and 6 deletions

View File

@ -614,7 +614,7 @@ var AST_SymbolCatch = DEFNODE("SymbolCatch", null, {
$documentation: "Symbol naming the exception in catch", $documentation: "Symbol naming the exception in catch",
}, AST_SymbolDeclaration); }, AST_SymbolDeclaration);
var AST_Label = DEFNODE("Label", "label_target", { var AST_Label = DEFNODE("Label", "references label_target", {
$documentation: "Symbol naming a label (declaration)", $documentation: "Symbol naming a label (declaration)",
}, AST_SymbolDeclaration); }, AST_SymbolDeclaration);

View File

@ -105,6 +105,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
node.scope = scope; node.scope = scope;
} }
if (node instanceof AST_Label) { if (node instanceof AST_Label) {
node.thedef = node;
node.init_scope_vars(); node.init_scope_vars();
var p = tw.parent(); // AST_LabeledStatement var p = tw.parent(); // AST_LabeledStatement
var block = p.body; var block = p.body;
@ -152,8 +153,12 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
} }
if (node instanceof AST_LabelRef) { if (node instanceof AST_LabelRef) {
var sym = labels[node.name]; var sym = labels[node.name];
if (!sym) throw new Error("Undefined label " + node.name); if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
node.reference(sym); name: node.name,
line: node.start.line,
col: node.start.col
}));
node.thedef = sym;
} }
}); });
self.walk(tw); self.walk(tw);
@ -169,6 +174,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
func = prev_func; func = prev_func;
return true; return true;
} }
if (node instanceof AST_LabelRef) {
node.reference();
return true;
}
if (node instanceof AST_SymbolRef) { if (node instanceof AST_SymbolRef) {
var name = node.name; var name = node.name;
var sym = node.scope.find_variable(name); var sym = node.scope.find_variable(name);
@ -227,9 +236,8 @@ AST_Label.DEFMETHOD("init_scope_vars", function(){
this.references = []; this.references = [];
}); });
AST_LabelRef.DEFMETHOD("reference", function(def){ AST_LabelRef.DEFMETHOD("reference", function(){
this.thedef = def; this.thedef.references.push(this);
def.references.push(this);
}); });
AST_Scope.DEFMETHOD("find_variable", function(name){ AST_Scope.DEFMETHOD("find_variable", function(name){