[vala/staging] vala: Move find_parent_type_symbol/get_this_type() to SemanticAnalyzer



commit ba8122d3a64e44eac44c5254c93fb181df601b0a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Aug 25 15:18:04 2019 +0200

    vala: Move find_parent_type_symbol/get_this_type() to SemanticAnalyzer

 codegen/valaccodemethodmodule.vala | 29 ++--------------------
 vala/valasemanticanalyzer.vala     | 50 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 27 deletions(-)
---
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 7b8702e11..864d6a93e 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -946,20 +946,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        }
                } else if ((m.binding == MemberBinding.INSTANCE || (m.parent_symbol is Struct && m is 
CreationMethod))
                    && (direction != 2 || get_ccode_finish_instance (m))) {
-                       TypeSymbol parent_type = find_parent_type (m);
-                       DataType this_type;
-                       if (parent_type is Class) {
-                               this_type = new ObjectType ((Class) parent_type);
-                       } else if (parent_type is Interface) {
-                               this_type = new ObjectType ((Interface) parent_type);
-                       } else if (parent_type is Struct) {
-                               this_type = new StructValueType ((Struct) parent_type);
-                       } else if (parent_type is Enum) {
-                               this_type = new EnumValueType ((Enum) parent_type);
-                       } else {
-                               Report.error (parent_type.source_reference, "internal: Unsupported symbol 
type");
-                               this_type = new InvalidType ();
-                       }
+                       var this_type = SemanticAnalyzer.get_this_type (m);
 
                        generate_type_declaration (this_type, decl_space);
 
@@ -979,9 +966,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                        }
                        cparam_map.set (get_param_pos (get_ccode_instance_pos (m)), instance_param);
                } else if (m.binding == MemberBinding.CLASS) {
-                       TypeSymbol parent_type = find_parent_type (m);
-                       DataType this_type;
-                       this_type = new ClassType ((Class) parent_type);
+                       var this_type = SemanticAnalyzer.get_this_type (m);
                        var class_param = new CCodeParameter ("klass", get_ccode_name (this_type));
                        cparam_map.set (get_param_pos (get_ccode_instance_pos (m)), class_param);
                }
@@ -1216,16 +1201,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                ccode.add_expression (ccheck);
        }
 
-       private TypeSymbol? find_parent_type (Symbol sym) {
-               while (sym != null) {
-                       if (sym is TypeSymbol) {
-                               return (TypeSymbol) sym;
-                       }
-                       sym = sym.parent_symbol;
-               }
-               return null;
-       }
-
        public override void visit_creation_method (CreationMethod m) {
                push_line (m.source_reference);
 
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index fa90deb7a..f81921296 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -1116,6 +1116,56 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                }
        }
 
+       public static unowned TypeSymbol? find_parent_type_symbol (Symbol sym) {
+               while (sym != null) {
+                       if (sym is TypeSymbol) {
+                               return (TypeSymbol) sym;
+                       }
+                       sym = sym.parent_symbol;
+               }
+               return null;
+       }
+
+       public static DataType? get_this_type (Method m) {
+               unowned TypeSymbol? parent_type = find_parent_type_symbol (m);
+               if (parent_type == null) {
+                       Report.error (parent_type.source_reference, "internal: Unsupported symbol type");
+                       return new InvalidType ();
+               }
+
+               DataType? this_type = null;
+               switch (m.binding) {
+               case MemberBinding.INSTANCE:
+                       if (parent_type is Class) {
+                               this_type = new ObjectType ((Class) parent_type);
+                       } else if (parent_type is Interface) {
+                               this_type = new ObjectType ((Interface) parent_type);
+                       } else if (parent_type is Struct) {
+                               this_type = new StructValueType ((Struct) parent_type);
+                       } else if (parent_type is Enum) {
+                               this_type = new EnumValueType ((Enum) parent_type);
+                       } else {
+                               Report.error (parent_type.source_reference, "internal: Unsupported symbol 
type");
+                               this_type = new InvalidType ();
+                       }
+                       break;
+               case MemberBinding.CLASS:
+                       if (parent_type is Class) {
+                               this_type = new ClassType ((Class) parent_type);
+                       } else {
+                               Report.error (parent_type.source_reference, "internal: Unsupported symbol 
type");
+                               this_type = new InvalidType ();
+                       }
+                       break;
+               case MemberBinding.STATIC:
+               default:
+                       Report.error (m.source_reference, "internal: Does not support a parent instance");
+                       this_type = new InvalidType ();
+                       break;
+               }
+               return this_type;
+       }
+
        public bool is_in_constructor () {
                var sym = current_symbol;
                while (sym != null) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]