LCOV - code coverage report
Current view: top level - vala - valaobjecttypesymbol.vala (source / functions) Coverage Total Hit
Test: vala 0.57.0.298-a8cae1 Lines: 93.3 % 135 126
Test Date: 2024-04-25 11:34:36 Functions: - 0 0

            Line data    Source code
       1              : /* valaobjecttypesymbol.vala
       2              :  *
       3              :  * Copyright (C) 2008-2009  Jürg Billeter
       4              :  * Copyright (C) 2008  Philip Van Hoof
       5              :  *
       6              :  * This library is free software; you can redistribute it and/or
       7              :  * modify it under the terms of the GNU Lesser General Public
       8              :  * License as published by the Free Software Foundation; either
       9              :  * version 2.1 of the License, or (at your option) any later version.
      10              : 
      11              :  * This library is distributed in the hope that it will be useful,
      12              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :  * Lesser General Public License for more details.
      15              : 
      16              :  * You should have received a copy of the GNU Lesser General Public
      17              :  * License along with this library; if not, write to the Free Software
      18              :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
      19              :  *
      20              :  * Author:
      21              :  *      Jürg Billeter <j@bitron.ch>
      22              :  *      Philip Van Hoof <pvanhoof@gnome.org>
      23              :  */
      24              : 
      25              : 
      26              : /**
      27              :  * Represents a runtime data type for objects and interfaces. This data type may
      28              :  * be defined in Vala source code or imported from an external library with a
      29              :  * Vala API file.
      30              :  */
      31       868932 : public abstract class Vala.ObjectTypeSymbol : TypeSymbol, GenericSymbol {
      32       865856 :         private List<TypeParameter> type_parameters = new ArrayList<TypeParameter> ();
      33              : 
      34       865856 :         private List<Symbol> members = new ArrayList<Symbol> ();
      35              : 
      36              :         // member symbols
      37       865856 :         private List<Field> fields = new ArrayList<Field> ();
      38       865856 :         private List<Method> methods = new ArrayList<Method> ();
      39       865856 :         private List<Property> properties = new ArrayList<Property> ();
      40       865856 :         private List<Signal> signals = new ArrayList<Signal> ();
      41              : 
      42              :         // inner types
      43       865856 :         private List<Class> classes = new ArrayList<Class> ();
      44       865856 :         private List<Interface> interfaces = new ArrayList<Interface> ();
      45       865856 :         private List<Struct> structs = new ArrayList<Struct> ();
      46       865856 :         private List<Enum> enums = new ArrayList<Enum> ();
      47       865856 :         private List<Delegate> delegates = new ArrayList<Delegate> ();
      48              : 
      49       865856 :         private List<Constant> constants = new ArrayList<Constant> ();
      50              : 
      51       865858 :         protected ObjectTypeSymbol (string name, SourceReference? source_reference = null, Comment? comment = null) {
      52       432929 :                 base (name, source_reference, comment);
      53              :         }
      54              : 
      55              :         /**
      56              :          * Returns the list of members.
      57              :          *
      58              :          * @return list of members
      59              :          */
      60        14083 :         public unowned List<Symbol> get_members () {
      61        14083 :                 return members;
      62              :         }
      63              : 
      64              :         /**
      65              :          * Returns the list of fields.
      66              :          *
      67              :          * @return list of fields
      68              :          */
      69      1345085 :         public unowned List<Field> get_fields () {
      70      1345085 :                 return fields;
      71              :         }
      72              : 
      73              :         /**
      74              :          * Returns the list of methods.
      75              :          *
      76              :          * @return list of methods
      77              :          */
      78      1361397 :         public unowned List<Method> get_methods () {
      79      1361397 :                 return methods;
      80              :         }
      81              : 
      82              :         /**
      83              :          * Returns the list of properties.
      84              :          *
      85              :          * @return list of properties
      86              :          */
      87      1347940 :         public unowned List<Property> get_properties () {
      88      1347940 :                 return properties;
      89              :         }
      90              : 
      91              :         /**
      92              :          * Returns the list of signals.
      93              :          *
      94              :          * @return list of signals
      95              :          */
      96      1345447 :         public unowned List<Signal> get_signals () {
      97      1345447 :                 return signals;
      98              :         }
      99              : 
     100              :         /**
     101              :          * Adds the specified field as a member to this object-symbol.
     102              :          *
     103              :          * @param f a field
     104              :          */
     105       285577 :         public override void add_field (Field f) {
     106       285577 :                 fields.add (f);
     107       285577 :                 members.add (f);
     108       285577 :                 scope.add (f.name, f);
     109              :         }
     110              : 
     111              :         /**
     112              :          * Adds the specified method as a member to this object-symbol.
     113              :          *
     114              :          * @param m a method
     115              :          */
     116      4733303 :         public override void add_method (Method m) {
     117      4733303 :                 methods.add (m);
     118      4733303 :                 members.add (m);
     119              : 
     120              :                 // explicit interface method implementation
     121              :                 // virtual/abstract methods needs to be scoped and overridable
     122      4733303 :                 if (this is Class && m.base_interface_type != null && !(m.is_abstract || m.is_virtual)) {
     123           11 :                         scope.add (null, m);
     124              :                 } else {
     125      4733292 :                         scope.add (m.name, m);
     126              :                 }
     127              :         }
     128              : 
     129              :         /**
     130              :          * Adds the specified property as a member to this object-symbol.
     131              :          *
     132              :          * @param prop a property
     133              :          */
     134       449347 :         public override void add_property (Property prop) {
     135       449347 :                 properties.add (prop);
     136       449347 :                 members.add (prop);
     137       449347 :                 scope.add (prop.name, prop);
     138              :         }
     139              : 
     140              :         /**
     141              :          * Adds the specified signal as a member to this object-symbol.
     142              :          *
     143              :          * @param sig a signal
     144              :          */
     145       136715 :         public override void add_signal (Signal sig) {
     146       136715 :                 signals.add (sig);
     147       136715 :                 members.add (sig);
     148       136715 :                 scope.add (sig.name, sig);
     149              :         }
     150              : 
     151              :         /**
     152              :          * Returns the list of classes.
     153              :          *
     154              :          * @return list of classes
     155              :          */
     156      1365964 :         public unowned List<Class> get_classes () {
     157      1365964 :                 return classes;
     158              :         }
     159              : 
     160              :         /**
     161              :          * Returns the list of interfaces.
     162              :          *
     163              :          * @return list of interfaces
     164              :          */
     165      1354681 :         public unowned List<Interface> get_interfaces () {
     166      1354681 :                 return interfaces;
     167              :         }
     168              : 
     169              :         /**
     170              :          * Returns the list of structs.
     171              :          *
     172              :          * @return list of structs
     173              :          */
     174      1354681 :         public unowned List<Struct> get_structs () {
     175      1354681 :                 return structs;
     176              :         }
     177              : 
     178              :         /**
     179              :          * Returns the list of enums.
     180              :          *
     181              :          * @return list of enums
     182              :          */
     183      1354682 :         public unowned List<Enum> get_enums () {
     184      1354682 :                 return enums;
     185              :         }
     186              : 
     187              :         /**
     188              :          * Returns the list of delegates.
     189              :          *
     190              :          * @return list of delegates
     191              :          */
     192      1354681 :         public unowned List<Delegate> get_delegates () {
     193      1354681 :                 return delegates;
     194              :         }
     195              : 
     196              :         /**
     197              :          * Adds the specified class as an inner class.
     198              :          *
     199              :          * @param cl a class
     200              :          */
     201           24 :         public override void add_class (Class cl) {
     202           24 :                 classes.add (cl);
     203           24 :                 scope.add (cl.name, cl);
     204              :         }
     205              : 
     206              :         /**
     207              :          * Adds the specified interface as an inner interface.
     208              :          *
     209              :          * @param iface an interface
     210              :          */
     211            6 :         public override void add_interface (Interface iface) {
     212            6 :                 interfaces.add (iface);
     213            6 :                 scope.add (iface.name, iface);
     214              :         }
     215              : 
     216              :         /**
     217              :          * Adds the specified struct as an inner struct.
     218              :          *
     219              :          * @param st a struct
     220              :          */
     221            4 :         public override void add_struct (Struct st) {
     222            4 :                 structs.add (st);
     223            4 :                 scope.add (st.name, st);
     224              :         }
     225              : 
     226              :         /**
     227              :          * Adds the specified enum as an inner enum.
     228              :          *
     229              :          * @param en an enum
     230              :          */
     231         1570 :         public override void add_enum (Enum en) {
     232         1570 :                 enums.add (en);
     233         1570 :                 scope.add (en.name, en);
     234              :         }
     235              : 
     236              :         /**
     237              :          * Adds the specified delegate as an inner delegate.
     238              :          *
     239              :          * @param d a delegate
     240              :          */
     241           21 :         public override void add_delegate (Delegate d) {
     242           21 :                 delegates.add (d);
     243           21 :                 scope.add (d.name, d);
     244              :         }
     245              : 
     246              :         /**
     247              :          * Adds the specified constant as a member to this interface.
     248              :          *
     249              :          * @param c a constant
     250              :          */
     251        45577 :         public override void add_constant (Constant c) {
     252        45577 :                 constants.add (c);
     253        45577 :                 scope.add (c.name, c);
     254              :         }
     255              : 
     256              :         /**
     257              :          * Returns the list of constants.
     258              :          *
     259              :          * @return list of constants
     260              :          */
     261      1354682 :         public unowned List<Constant> get_constants () {
     262      1354682 :                 return constants;
     263              :         }
     264              : 
     265              :         /**
     266              :          * Appends the specified parameter to the list of type parameters.
     267              :          *
     268              :          * @param p a type parameter
     269              :          */
     270        27829 :         public void add_type_parameter (TypeParameter p) {
     271        27829 :                 type_parameters.add (p);
     272        27829 :                 scope.add (p.name, p);
     273              :         }
     274              : 
     275              :         /**
     276              :          * Returns the type parameter list.
     277              :          *
     278              :          * @return list of type parameters
     279              :          */
     280     28234159 :         public unowned List<TypeParameter> get_type_parameters () {
     281     28234159 :                 return type_parameters;
     282              :         }
     283              : 
     284       145010 :         public bool has_type_parameters () {
     285       145010 :                 return (type_parameters != null && type_parameters.size > 0);
     286              :         }
     287              : 
     288        93964 :         public override int get_type_parameter_index (string name) {
     289        93964 :                 int i = 0;
     290       102572 :                 foreach (TypeParameter parameter in type_parameters) {
     291        98268 :                         if (parameter.name == name) {
     292        93964 :                                 return i;
     293              :                         }
     294         4304 :                         i++;
     295              :                 }
     296        93964 :                 return -1;
     297              :         }
     298              : 
     299              :         /**
     300              :          * Adds the specified method as a hidden member to this class,
     301              :          * primarily used for default signal handlers.
     302              :          *
     303              :          * The hidden methods are not part of the `methods` collection.
     304              :          *
     305              :          * There may also be other use cases, eg, convert array.resize() to
     306              :          * this type of method?
     307              :          *
     308              :          * @param m a method
     309              :          */
     310       129089 :         public void add_hidden_method (Method m) {
     311       129089 :                 if (m.binding == MemberBinding.INSTANCE) {
     312       129089 :                         if (m.this_parameter != null) {
     313            0 :                                 m.scope.remove (m.this_parameter.name);
     314              :                         }
     315       129089 :                         m.this_parameter = new Parameter ("this", SemanticAnalyzer.get_this_type (m, this), m.source_reference);
     316       129089 :                         m.scope.add (m.this_parameter.name, m.this_parameter);
     317              :                 }
     318       129089 :                 if (!(m.return_type is VoidType) && m.get_postconditions ().size > 0) {
     319            0 :                         if (m.result_var != null) {
     320            0 :                                 m.scope.remove (m.result_var.name);
     321              :                         }
     322            0 :                         m.result_var = new LocalVariable (m.return_type.copy (), "result", null, m.source_reference);
     323            0 :                         m.result_var.is_result = true;
     324              :                 }
     325              : 
     326       129089 :                 scope.add (null, m);
     327              :         }
     328              : 
     329       952203 :         public override void accept_children (CodeVisitor visitor) {
     330      1073907 :                 foreach (TypeParameter p in get_type_parameters ()) {
     331        60852 :                         p.accept (visitor);
     332              :                 }
     333              : 
     334              :                 /* process enums first to avoid order problems in C code */
     335       959137 :                 foreach (Enum en in get_enums ()) {
     336         3467 :                         en.accept (visitor);
     337              :                 }
     338              : 
     339      1150623 :                 foreach (Constant c in get_constants ()) {
     340        99210 :                         c.accept (visitor);
     341              :                 }
     342              : 
     343       952203 :                 if (CodeContext.get ().abi_stability) {
     344       338030 :                         foreach (Symbol s in get_members ()) {
     345       162250 :                                 s.accept (visitor);
     346              :                         }
     347              :                 } else {
     348      2191541 :                         foreach (Field f in get_fields ()) {
     349       626434 :                                 f.accept (visitor);
     350              :                         }
     351     21484349 :                         foreach (Method m in get_methods ()) {
     352     10272838 :                                 m.accept (visitor);
     353              :                         }
     354      2892239 :                         foreach (Property prop in get_properties ()) {
     355       976783 :                                 prop.accept (visitor);
     356              :                         }
     357      1545569 :                         foreach (Signal sig in get_signals ()) {
     358       303448 :                                 sig.accept (visitor);
     359              :                         }
     360              :                 }
     361              : 
     362       952375 :                 foreach (Class cl in get_classes ()) {
     363           86 :                         cl.accept (visitor);
     364              :                 }
     365              : 
     366       952239 :                 foreach (Interface iface in get_interfaces ()) {
     367           18 :                         iface.accept (visitor);
     368              :                 }
     369              : 
     370       952235 :                 foreach (Struct st in get_structs ()) {
     371           16 :                         st.accept (visitor);
     372              :                 }
     373              : 
     374       952341 :                 foreach (Delegate d in get_delegates ()) {
     375           69 :                         d.accept (visitor);
     376              :                 }
     377              :         }
     378              : 
     379       401575 :         public override bool check (CodeContext context) {
     380       401575 :                 if (checked) {
     381            0 :                         return !error;
     382              :                 }
     383              : 
     384       401575 :                 if (!external_package && has_attribute ("DBus") && !context.has_package ("gio-2.0")) {
     385            0 :                         error = true;
     386            0 :                         Report.error (source_reference, "gio-2.0 package required for DBus support");
     387              :                 }
     388              : 
     389       453129 :                 foreach (TypeParameter p in get_type_parameters ()) {
     390        25777 :                         if (!p.check (context)) {
     391            0 :                                 error = true;
     392              :                         }
     393              :                 }
     394              : 
     395       401575 :                 return !error;
     396              :         }
     397              : }
        

Generated by: LCOV version 2.0-1