From: Jürg Billeter Date: Fri, 31 Oct 2008 08:27:51 +0000 (+0000) Subject: Move parameter checking to FormalParameter.check X-Git-Tag: VALA_0_5_1~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f3b32d5a6e0177b06fc0a8fe55e08ffe33599353;p=thirdparty%2Fvala.git Move parameter checking to FormalParameter.check 2008-10-31 Jürg Billeter * vala/valaformalparameter.vala: * vala/valasemanticanalyzer.vala: Move parameter checking to FormalParameter.check svn path=/trunk/; revision=1943 --- diff --git a/ChangeLog b/ChangeLog index 5c72f912b..722c03cee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-10-31 Jürg Billeter + + * vala/valaformalparameter.vala: + * vala/valasemanticanalyzer.vala: + + Move parameter checking to FormalParameter.check + 2008-10-31 Jürg Billeter * vala/valasemanticanalyzer.vala: diff --git a/vala/valaformalparameter.vala b/vala/valaformalparameter.vala index d0693759a..b24f42b2b 100644 --- a/vala/valaformalparameter.vala +++ b/vala/valaformalparameter.vala @@ -166,6 +166,53 @@ public class Vala.FormalParameter : Symbol { return new FormalParameter.with_ellipsis (); } } + + public override bool check (SemanticAnalyzer analyzer) { + accept_children (analyzer); + + if (analyzer.context.non_null && default_expression != null) { + if (default_expression is NullLiteral + && !parameter_type.nullable + && direction != ParameterDirection.OUT) { + Report.warning (source_reference, "`null' incompatible with parameter type `%s`".printf (parameter_type.to_string ())); + } + } + + if (!ellipsis) { + if (!is_internal_symbol ()) { + if (parameter_type is ValueType && !parameter_type.is_real_struct_type ()) { + analyzer.current_source_file.add_type_dependency (parameter_type, SourceFileDependencyType.HEADER_FULL); + } else { + analyzer.current_source_file.add_type_dependency (parameter_type, SourceFileDependencyType.HEADER_SHALLOW); + } + } + analyzer.current_source_file.add_type_dependency (parameter_type, SourceFileDependencyType.SOURCE); + + // check whether parameter type is at least as accessible as the method + if (!analyzer.is_type_accessible (this, parameter_type)) { + error = true; + Report.error (source_reference, "parameter type `%s` is less accessible than method `%s`".printf (parameter_type.to_string (), parent_symbol.get_full_name ())); + return false; + } + } + + /* special treatment for construct formal parameters used in creation methods */ + if (construct_parameter) { + if (!(parent_symbol is CreationMethod)) { + error = true; + Report.error (source_reference, "construct parameters are only allowed in type creation methods"); + return false; + } + + var method_body = ((CreationMethod) parent_symbol).body; + var left = new MemberAccess (new MemberAccess.simple ("this"), name); + var right = new MemberAccess.simple (name); + + method_body.add_statement (new ExpressionStatement (new Assignment (left, right), source_reference)); + } + + return true; + } } public enum Vala.ParameterDirection { diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala index 7fb6cc0df..bb89fe294 100644 --- a/vala/valasemanticanalyzer.vala +++ b/vala/valasemanticanalyzer.vala @@ -28,11 +28,11 @@ using Gee; * Code visitor analyzing and checking code. */ public class Vala.SemanticAnalyzer : CodeVisitor { - private CodeContext context; + public CodeContext context { get; set; } Symbol root_symbol; Symbol current_symbol; - SourceFile current_source_file; + public SourceFile current_source_file { get; set; } DataType current_return_type; Class current_class; Struct current_struct; @@ -594,52 +594,11 @@ public class Vala.SemanticAnalyzer : CodeVisitor { } public override void visit_formal_parameter (FormalParameter p) { - p.accept_children (this); - - if (context.non_null && p.default_expression != null) { - if (p.default_expression is NullLiteral - && !p.parameter_type.nullable - && p.direction != ParameterDirection.OUT) { - Report.warning (p.source_reference, "`null' incompatible with parameter type `%s`".printf (p.parameter_type.to_string ())); - } - } - - if (!p.ellipsis) { - if (!p.is_internal_symbol ()) { - if (p.parameter_type is ValueType && !p.parameter_type.is_real_struct_type ()) { - current_source_file.add_type_dependency (p.parameter_type, SourceFileDependencyType.HEADER_FULL); - } else { - current_source_file.add_type_dependency (p.parameter_type, SourceFileDependencyType.HEADER_SHALLOW); - } - } - current_source_file.add_type_dependency (p.parameter_type, SourceFileDependencyType.SOURCE); - - // check whether parameter type is at least as accessible as the method - if (!is_type_accessible (p, p.parameter_type)) { - p.error = true; - Report.error (p.source_reference, "parameter type `%s` is less accessible than method `%s`".printf (p.parameter_type.to_string (), p.parent_symbol.get_full_name ())); - return; - } - } - - /* special treatment for construct formal parameters used in creation methods */ - if (p.construct_parameter) { - if (!(p.parent_symbol is CreationMethod)) { - p.error = true; - Report.error (p.source_reference, "construct parameters are only allowed in type creation methods"); - return; - } - - var method_body = ((CreationMethod) p.parent_symbol).body; - var left = new MemberAccess (new MemberAccess.simple ("this"), p.name); - var right = new MemberAccess.simple (p.name); - - method_body.add_statement (new ExpressionStatement (new Assignment (left, right), p.source_reference)); - } + p.check (this); } // check whether type is at least as accessible as the specified symbol - private bool is_type_accessible (Symbol sym, DataType type) { + public bool is_type_accessible (Symbol sym, DataType type) { foreach (Symbol type_symbol in type.get_symbols ()) { Scope method_scope = sym.get_top_accessible_scope (); Scope type_scope = type_symbol.get_top_accessible_scope ();