Commit 59b8f55c authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter
Browse files

deny access to instance members from static methods, improve error

2007-08-29  Juerg Billeter  <j@bitron.ch>

	* vala/valaclass.vala, vala/valasemanticanalyzer.vala,
	  gobject/valacodegeneratorassignment.vala: deny access to instance
	  members from static methods, improve error reporting

svn path=/trunk/; revision=528
parent 05c7329e
2007-08-29 Jürg Billeter <j@bitron.ch>
* vala/valaclass.vala, vala/valasemanticanalyzer.vala,
gobject/valacodegeneratorassignment.vala: deny access to instance
members from static methods, improve error reporting
2007-08-29 Jürg Billeter <j@bitron.ch>
* vala/parser.y: fix internal warnings on syntax errors,
......
......@@ -35,7 +35,7 @@ public class Vala.CodeGenerator {
if (a.left.symbol_reference is Property) {
var prop = (Property) a.left.symbol_reference;
if (prop.set_accessor.construction && current_type_symbol is Class && ma.inner == null && in_creation_method) {
if (prop.set_accessor.construction && current_type_symbol is Class && in_creation_method) {
// this property is used as a construction parameter
var cpointer = new CCodeIdentifier ("__params_it");
......
......@@ -187,7 +187,7 @@ public class Vala.Class : DataType {
* @param m a method
*/
public void add_method (Method! m) {
if (m.instance) {
if (m.instance || m is CreationMethod) {
m.this_parameter = new FormalParameter ("this", new TypeReference ());
m.this_parameter.type_reference.data_type = this;
m.scope.add (m.this_parameter.name, m.this_parameter);
......
......@@ -510,21 +510,10 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
var method_body = ((CreationMethod) p.parent_symbol).body;
var left = new MemberAccess.simple (p.name);
var left = new MemberAccess (new MemberAccess.simple ("this"), p.name);
var right = new MemberAccess.simple (p.name);
/* try to lookup the requested property */
var prop_sym = symbol_lookup_inherited (current_class, p.name);
if (!(prop_sym is Property)) {
p.error = true;
Report.error (p.source_reference, "class `%s' does not contain a property named `%s'".printf (current_class.get_full_name (), p.name));
return;
}
left.symbol_reference = prop_sym;
right.symbol_reference = p;
method_body.add_statement (new ExpressionStatement (new Assignment (left, right)));
method_body.add_statement (new ExpressionStatement (new Assignment (left, right), p.source_reference));
}
}
......@@ -613,14 +602,14 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
acc.body = new Block ();
if (acc.readable) {
acc.body.add_statement (new ReturnStatement (new MemberAccess.simple ("_%s".printf (acc.prop.name))));
acc.body.add_statement (new ReturnStatement (new MemberAccess.simple ("_%s".printf (acc.prop.name)), acc.source_reference));
} else {
acc.body.add_statement (new ExpressionStatement (new Assignment (new MemberAccess.simple ("_%s".printf (acc.prop.name)), new MemberAccess.simple ("value"))));
acc.body.add_statement (new ExpressionStatement (new Assignment (new MemberAccess.simple ("_%s".printf (acc.prop.name)), new MemberAccess.simple ("value")), acc.source_reference));
}
}
if (acc.body != null && (acc.writable || acc.construction)) {
acc.value_parameter = new FormalParameter ("value", acc.prop.type_reference);
acc.value_parameter = new FormalParameter ("value", acc.prop.type_reference, acc.source_reference);
acc.body.scope.add (acc.value_parameter.name, acc.value_parameter);
}
}
......@@ -907,6 +896,11 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
public override void visit_end_return_statement (ReturnStatement! stmt) {
if (stmt.return_expression != null && stmt.return_expression.error) {
// ignore inner error
return;
}
if (current_return_type == null) {
stmt.error = true;
Report.error (stmt.source_reference, "Return not allowed in this context");
......@@ -1217,12 +1211,22 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
public override void visit_member_access (MemberAccess! expr) {
Symbol base_symbol = null;
bool may_access_instance_members = false;
expr.symbol_reference = null;
if (expr.inner == null) {
base_symbol = current_symbol;
var sym = current_symbol;
while (sym != null && expr.symbol_reference == null) {
if (sym is CreationMethod || sym is Property || sym is Constructor || sym is Destructor) {
may_access_instance_members = true;
} else if (sym is Method) {
var m = (Method) sym;
may_access_instance_members = m.instance;
}
expr.symbol_reference = symbol_lookup_inherited (sym, expr.member_name);
sym = sym.parent_symbol;
}
......@@ -1251,12 +1255,22 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
base_symbol = expr.inner.symbol_reference;
if (base_symbol is Namespace || base_symbol is DataType) {
expr.symbol_reference = base_symbol.scope.lookup (expr.member_name);
if (expr.inner is BaseAccess) {
// inner expression is base access
// access to instance members of the base type possible
may_access_instance_members = true;
}
}
}
if (expr.symbol_reference == null && expr.inner.static_type != null) {
base_symbol = expr.inner.static_type.data_type;
expr.symbol_reference = symbol_lookup_inherited (base_symbol, expr.member_name);
if (expr.symbol_reference != null) {
// inner expression is variable, field, or parameter
// access to instance members of the corresponding type possible
may_access_instance_members = true;
}
}
}
......@@ -1278,10 +1292,17 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
var member = expr.symbol_reference;
MemberAccessibility access = MemberAccessibility.PUBLIC;
bool instance = false;
if (member is Field) {
access = ((Field) member).access;
var f = (Field) member;
access = f.access;
instance = f.instance;
} else if (member is Method) {
access = ((Method) member).access;
var m = (Method) member;
access = m.access;
instance = m.instance;
} else if (member is Property || member is Signal) {
instance = true;
}
if (access == MemberAccessibility.PRIVATE) {
......@@ -1301,6 +1322,11 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
}
if (instance && !may_access_instance_members) {
expr.error = true;
Report.error (expr.source_reference, "Access to instance member `%s' denied".printf (member.get_full_name ()));
return;
}
current_source_file.add_symbol_dependency (expr.symbol_reference, SourceFileDependencyType.SOURCE);
......@@ -2415,9 +2441,9 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
block.scope.parent_scope = l.method.scope;
if (l.method.return_type.data_type != null) {
block.add_statement (new ReturnStatement (l.expression_body));
block.add_statement (new ReturnStatement (l.expression_body, l.source_reference));
} else {
block.add_statement (new ExpressionStatement (l.expression_body));
block.add_statement (new ExpressionStatement (l.expression_body, l.source_reference));
}
l.method.body = block;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment