Commit 678c29d3 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Warn when hiding members, support `new' keyword to suppress warning, based

2009-02-12  Jürg Billeter  <j@bitron.ch>

	* vala/valaclass.vala:
	* vala/valaconstant.vala:
	* vala/valafield.vala:
	* vala/valamember.vala:
	* vala/valamethod.vala:
	* vala/valaproperty.vala:
	* vala/valastruct.vala:

	Warn when hiding members, support `new' keyword to suppress
	warning, based on patch by Andreas Brauchli, fixes bug 567743

	* vala/valacodenode.vala:
	* vala/valacreationmethod.vala:
	* vala/valaenum.vala:
	* vala/valaerrordomain.vala:
	* vala/valanamespace.vala:
	* ccode/valaccodelinedirective.vala:

	Avoid hidden members

svn path=/trunk/; revision=2437
parent 84c5de5a
2009-02-12 Jürg Billeter <j@bitron.ch>
* vala/valaclass.vala:
* vala/valaconstant.vala:
* vala/valafield.vala:
* vala/valamember.vala:
* vala/valamethod.vala:
* vala/valaproperty.vala:
* vala/valastruct.vala:
Warn when hiding members, support `new' keyword to suppress
warning, based on patch by Andreas Brauchli, fixes bug 567743
* vala/valacodenode.vala:
* vala/valacreationmethod.vala:
* vala/valaenum.vala:
* vala/valaerrordomain.vala:
* vala/valanamespace.vala:
* ccode/valaccodelinedirective.vala:
Avoid hidden members
2009-02-12 Jürg Billeter <j@bitron.ch> 2009-02-12 Jürg Billeter <j@bitron.ch>
* vala/valastruct.vala: * vala/valastruct.vala:
......
...@@ -34,18 +34,18 @@ public class Vala.CCodeLineDirective : CCodeNode { ...@@ -34,18 +34,18 @@ public class Vala.CCodeLineDirective : CCodeNode {
/** /**
* The line number in the source file to be presumed. * The line number in the source file to be presumed.
*/ */
public int line { get; set; } public int line_number { get; set; }
public CCodeLineDirective (string _filename, int _line) { public CCodeLineDirective (string _filename, int _line) {
filename = _filename; filename = _filename;
line = _line; line_number = _line;
} }
public override void write (CCodeWriter writer) { public override void write (CCodeWriter writer) {
if (!writer.bol) { if (!writer.bol) {
writer.write_newline (); writer.write_newline ();
} }
writer.write_string ("#line %d \"%s\"".printf (line, filename)); writer.write_string ("#line %d \"%s\"".printf (line_number, filename));
writer.write_newline (); writer.write_newline ();
} }
} }
...@@ -329,9 +329,9 @@ public class Vala.Class : ObjectTypeSymbol { ...@@ -329,9 +329,9 @@ public class Vala.Class : ObjectTypeSymbol {
} }
var cm = (CreationMethod) m; var cm = (CreationMethod) m;
if (cm.type_name != null && cm.type_name != name) { if (cm.class_name != null && cm.class_name != name) {
// type_name is null for constructors generated by GIdlParser // class_name is null for constructors generated by GIdlParser
Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (get_full_name (), cm.type_name)); Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (get_full_name (), cm.class_name));
m.error = true; m.error = true;
return; return;
} }
......
...@@ -45,6 +45,10 @@ public abstract class Vala.CodeNode { ...@@ -45,6 +45,10 @@ public abstract class Vala.CodeNode {
* Contains all attributes that have been specified for this code node. * Contains all attributes that have been specified for this code node.
*/ */
public GLib.List<Attribute> attributes; public GLib.List<Attribute> attributes;
public string type_name {
get { return Type.from_instance (this).name (); }
}
/** /**
* Generated CCodeNode that corresponds to this code node. * Generated CCodeNode that corresponds to this code node.
...@@ -189,8 +193,4 @@ public abstract class Vala.CodeNode { ...@@ -189,8 +193,4 @@ public abstract class Vala.CodeNode {
public string get_temp_name () { public string get_temp_name () {
return "." + (++last_temp_nr).to_string (); return "." + (++last_temp_nr).to_string ();
} }
public weak string get_type_name () {
return Type.from_instance (this).name ();
}
} }
...@@ -181,6 +181,10 @@ public class Vala.Constant : Member, Lockable { ...@@ -181,6 +181,10 @@ public class Vala.Constant : Member, Lockable {
} }
} }
if (!external_package && !hides && get_hidden_member () != null) {
Report.warning (source_reference, "%s hides inherited constant `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
}
analyzer.current_source_file = old_source_file; analyzer.current_source_file = old_source_file;
analyzer.current_symbol = old_symbol; analyzer.current_symbol = old_symbol;
......
...@@ -31,7 +31,7 @@ public class Vala.CreationMethod : Method { ...@@ -31,7 +31,7 @@ public class Vala.CreationMethod : Method {
/** /**
* Specifies the name of the type this creation method belongs to. * Specifies the name of the type this creation method belongs to.
*/ */
public string type_name { get; set; } public string class_name { get; set; }
/** /**
* Specifies the number of parameters this creation method sets. * Specifies the number of parameters this creation method sets.
...@@ -59,9 +59,9 @@ public class Vala.CreationMethod : Method { ...@@ -59,9 +59,9 @@ public class Vala.CreationMethod : Method {
* @param source_reference reference to source code * @param source_reference reference to source code
* @return newly created method * @return newly created method
*/ */
public CreationMethod (string? type_name, string? name, SourceReference? source_reference = null) { public CreationMethod (string? class_name, string? name, SourceReference? source_reference = null) {
base (name, new VoidType (), source_reference); base (name, new VoidType (), source_reference);
this.type_name = type_name; this.class_name = class_name;
carray_length_parameter_position = -3; carray_length_parameter_position = -3;
cdelegate_target_parameter_position = -3; cdelegate_target_parameter_position = -3;
...@@ -125,9 +125,9 @@ public class Vala.CreationMethod : Method { ...@@ -125,9 +125,9 @@ public class Vala.CreationMethod : Method {
process_attributes (); process_attributes ();
if (type_name != null && type_name != parent_symbol.name) { if (class_name != null && class_name != parent_symbol.name) {
// type_name is null for constructors generated by GIdlParser // class_name is null for constructors generated by GIdlParser
Report.error (source_reference, "missing return type in method `%s.%s´".printf (analyzer.current_symbol.get_full_name (), type_name)); Report.error (source_reference, "missing return type in method `%s.%s´".printf (analyzer.current_symbol.get_full_name (), class_name));
error = true; error = true;
return false; return false;
} }
......
...@@ -165,13 +165,7 @@ public class Vala.Enum : TypeSymbol { ...@@ -165,13 +165,7 @@ public class Vala.Enum : TypeSymbol {
return false; return false;
} }
/** public override string get_cprefix () {
* Returns the string to be prepended to the name of members of this
* enum when used in C code.
*
* @return the prefix to be used in C code
*/
public string get_cprefix () {
if (cprefix == null) { if (cprefix == null) {
cprefix = "%s_".printf (get_upper_case_cname ()); cprefix = "%s_".printf (get_upper_case_cname ());
} }
......
...@@ -148,13 +148,7 @@ public class Vala.ErrorDomain : TypeSymbol { ...@@ -148,13 +148,7 @@ public class Vala.ErrorDomain : TypeSymbol {
this.cname = cname; this.cname = cname;
} }
/** public override string get_cprefix () {
* Returns the string to be prepended to the name of members of this
* error domain when used in C code.
*
* @return the prefix to be used in C code
*/
public string get_cprefix () {
if (cprefix == null) { if (cprefix == null) {
cprefix = "%s_".printf (get_upper_case_cname (null)); cprefix = "%s_".printf (get_upper_case_cname (null));
} }
......
...@@ -321,6 +321,10 @@ public class Vala.Field : Member, Lockable { ...@@ -321,6 +321,10 @@ public class Vala.Field : Member, Lockable {
} }
} }
if (!external_package && !hides && get_hidden_member () != null) {
Report.warning (source_reference, "%s hides inherited field `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
}
if (field_in_header) { if (field_in_header) {
if (field_type is ValueType) { if (field_type is ValueType) {
analyzer.current_source_file.add_type_dependency (field_type, SourceFileDependencyType.HEADER_FULL); analyzer.current_source_file.add_type_dependency (field_type, SourceFileDependencyType.HEADER_FULL);
......
...@@ -68,6 +68,32 @@ public abstract class Vala.Member : Symbol { ...@@ -68,6 +68,32 @@ public abstract class Vala.Member : Symbol {
public void add_cheader_filename (string filename) { public void add_cheader_filename (string filename) {
cheader_filenames.add (filename); cheader_filenames.add (filename);
} }
public Symbol? get_hidden_member () {
Symbol sym = null;
if (parent_symbol is Class) {
var cl = ((Class) parent_symbol).base_class;
while (cl != null) {
sym = cl.scope.lookup (name);
if (sym != null && sym.access != SymbolAccessibility.PRIVATE) {
return sym;
}
cl = cl.base_class;
}
} else if (parent_symbol is Struct) {
var st = ((Struct) parent_symbol).base_struct;
while (st != null) {
sym = st.scope.lookup (name);
if (sym != null && sym.access != SymbolAccessibility.PRIVATE) {
return sym;
}
st = st.base_struct;
}
}
return null;
}
} }
public enum MemberBinding { public enum MemberBinding {
......
...@@ -744,6 +744,10 @@ public class Vala.Method : Member { ...@@ -744,6 +744,10 @@ public class Vala.Method : Member {
Report.error (source_reference, "%s: no suitable method found to override".printf (get_full_name ())); Report.error (source_reference, "%s: no suitable method found to override".printf (get_full_name ()));
} }
if (!external_package && !overrides && !hides && get_hidden_member () != null) {
Report.warning (source_reference, "%s hides inherited method `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
}
// check whether return type is at least as accessible as the method // check whether return type is at least as accessible as the method
if (!analyzer.is_type_accessible (this, return_type)) { if (!analyzer.is_type_accessible (this, return_type)) {
error = true; error = true;
......
...@@ -86,7 +86,7 @@ public class Vala.Namespace : Symbol { ...@@ -86,7 +86,7 @@ public class Vala.Namespace : Symbol {
foreach (Enum en in ns.get_enums ()) { foreach (Enum en in ns.get_enums ()) {
old_ns.add_enum (en); old_ns.add_enum (en);
} }
foreach (ErrorDomain ed in ns.get_error_types ()) { foreach (ErrorDomain ed in ns.get_error_domains ()) {
old_ns.add_error_domain (ed); old_ns.add_error_domain (ed);
} }
foreach (Constant c in ns.get_constants ()) { foreach (Constant c in ns.get_constants ()) {
...@@ -254,7 +254,7 @@ public class Vala.Namespace : Symbol { ...@@ -254,7 +254,7 @@ public class Vala.Namespace : Symbol {
* *
* @return error domain list * @return error domain list
*/ */
public Gee.List<ErrorDomain> get_error_types () { public Gee.List<ErrorDomain> get_error_domains () {
return new ReadOnlyList<ErrorDomain> (error_domains); return new ReadOnlyList<ErrorDomain> (error_domains);
} }
......
...@@ -448,6 +448,10 @@ public class Vala.Property : Member, Lockable { ...@@ -448,6 +448,10 @@ public class Vala.Property : Member, Lockable {
Report.error (source_reference, "%s: no suitable property found to override".printf (get_full_name ())); Report.error (source_reference, "%s: no suitable property found to override".printf (get_full_name ()));
} }
if (!external_package && !overrides && !hides && get_hidden_member () != null) {
Report.warning (source_reference, "%s hides inherited property `%s'. Use the `new' keyword if hiding was intentional".printf (get_full_name (), get_hidden_member ().get_full_name ()));
}
/* construct properties must be public */ /* construct properties must be public */
if (set_accessor != null && set_accessor.construction) { if (set_accessor != null && set_accessor.construction) {
if (access != SymbolAccessibility.PUBLIC) { if (access != SymbolAccessibility.PUBLIC) {
......
...@@ -177,9 +177,9 @@ public class Vala.Struct : TypeSymbol { ...@@ -177,9 +177,9 @@ public class Vala.Struct : TypeSymbol {
} }
var cm = (CreationMethod) m; var cm = (CreationMethod) m;
if (cm.type_name != null && cm.type_name != name) { if (cm.class_name != null && cm.class_name != name) {
// type_name is null for constructors generated by GIdlParser // type_name is null for constructors generated by GIdlParser
Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (get_full_name (), cm.type_name)); Report.error (m.source_reference, "missing return type in method `%s.%s´".printf (get_full_name (), cm.class_name));
m.error = true; m.error = true;
return; return;
} }
......
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