Logo Search packages:      
Sourcecode: vala version File versions

valaforstatement.c

/* valaforstatement.vala
 *
 * Copyright (C) 2006-2007  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 *    Jürg Billeter <j@bitron.ch>
 */

#include "valaforstatement.h"
#include <gee/arraylist.h>
#include <gee/list.h>
#include <gee/readonlycollection.h>
#include <vala/valaexpression.h>
#include <vala/valablock.h>
#include <vala/valasourcereference.h>
#include <vala/valaforstatement.h>
#include <vala/valacodevisitor.h>

struct _ValaForStatementPrivate {
      GeeList* initializer;
      GeeList* iterator;
      ValaExpression* _condition;
      ValaBlock* _body;
};
#define VALA_FOR_STATEMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALA_TYPE_FOR_STATEMENT, ValaForStatementPrivate))
enum  {
      VALA_FOR_STATEMENT_DUMMY_PROPERTY,
      VALA_FOR_STATEMENT_CONDITION,
      VALA_FOR_STATEMENT_BODY
};
static void vala_for_statement_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor);
static void vala_for_statement_real_replace (ValaCodeNode* base, ValaCodeNode* old_node, ValaCodeNode* new_node);
static gpointer vala_for_statement_parent_class = NULL;
static ValaStatementIface* vala_for_statement_vala_statement_parent_iface = NULL;
static void vala_for_statement_dispose (GObject * obj);


/**
 * Creates a new for statement.
 *
 * @param cond             loop condition
 * @param body             loop body
 * @param source_reference reference to source code
 * @return                 newly created for statement
 */
ValaForStatement* vala_for_statement_new (ValaExpression* condition, ValaBlock* body, ValaSourceReference* source_reference)
{
      GParameter * __params;
      GParameter * __params_it;
      ValaForStatement * self;
      g_return_val_if_fail (condition == NULL || VALA_IS_EXPRESSION (condition), NULL);
      g_return_val_if_fail (body == NULL || VALA_IS_BLOCK (body), NULL);
      g_return_val_if_fail (source_reference == NULL || VALA_IS_SOURCE_REFERENCE (source_reference), NULL);
      __params = g_new0 (GParameter, 1);
      __params_it = __params;
      (__params_it->name = "condition", g_value_init (&__params_it->value, VALA_TYPE_EXPRESSION), g_value_set_object (&__params_it->value, condition), __params_it++);
      self = g_object_newv (VALA_TYPE_FOR_STATEMENT, __params_it - __params, __params);
      vala_for_statement_set_body (self, body);
      vala_code_node_set_source_reference (VALA_CODE_NODE (self), source_reference);
      while (__params_it > __params) {
            --__params_it;
            g_value_unset (&__params_it->value);
      }
      g_free (__params);
      return self;
}


/**
 * Appends the specified expression to the list of initializers.
 *
 * @param init an initializer expression
 */
void vala_for_statement_add_initializer (ValaForStatement* self, ValaExpression* init)
{
      g_return_if_fail (VALA_IS_FOR_STATEMENT (self));
      g_return_if_fail (VALA_IS_EXPRESSION (init));
      vala_code_node_set_parent_node (VALA_CODE_NODE (init), VALA_CODE_NODE (self));
      gee_collection_add (GEE_COLLECTION (self->priv->initializer), init);
}


/**
 * Returns a copy of the list of initializers.
 *
 * @return initializer list
 */
GeeCollection* vala_for_statement_get_initializer (ValaForStatement* self)
{
      g_return_val_if_fail (VALA_IS_FOR_STATEMENT (self), NULL);
      return GEE_COLLECTION (gee_read_only_collection_new (g_object_ref, g_object_unref, GEE_COLLECTION (self->priv->initializer)));
}


/**
 * Appends the specified expression to the iterator.
 *
 * @param iter an iterator expression
 */
void vala_for_statement_add_iterator (ValaForStatement* self, ValaExpression* iter)
{
      g_return_if_fail (VALA_IS_FOR_STATEMENT (self));
      g_return_if_fail (VALA_IS_EXPRESSION (iter));
      vala_code_node_set_parent_node (VALA_CODE_NODE (iter), VALA_CODE_NODE (self));
      gee_collection_add (GEE_COLLECTION (self->priv->iterator), iter);
}


/**
 * Returns a copy of the iterator.
 *
 * @return iterator
 */
GeeCollection* vala_for_statement_get_iterator (ValaForStatement* self)
{
      g_return_val_if_fail (VALA_IS_FOR_STATEMENT (self), NULL);
      return GEE_COLLECTION (gee_read_only_collection_new (g_object_ref, g_object_unref, GEE_COLLECTION (self->priv->iterator)));
}


static void vala_for_statement_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor)
{
      ValaForStatement * self;
      self = ((ValaForStatement*) base);
      g_return_if_fail (VALA_IS_CODE_VISITOR (visitor));
      {
            GeeList* init_expr_collection;
            GeeIterator* init_expr_it;
            init_expr_collection = self->priv->initializer;
            init_expr_it = gee_iterable_iterator (GEE_ITERABLE (init_expr_collection));
            while (gee_iterator_next (init_expr_it)) {
                  ValaExpression* init_expr;
                  init_expr = gee_iterator_get (init_expr_it);
                  {
                        vala_code_node_accept (VALA_CODE_NODE (init_expr), visitor);
                        vala_code_visitor_visit_end_full_expression (visitor, init_expr);
                        (init_expr == NULL ? NULL : (init_expr = (g_object_unref (init_expr), NULL)));
                  }
            }
            (init_expr_it == NULL ? NULL : (init_expr_it = (g_object_unref (init_expr_it), NULL)));
      }
      vala_code_node_accept (VALA_CODE_NODE (vala_for_statement_get_condition (self)), visitor);
      vala_code_visitor_visit_end_full_expression (visitor, vala_for_statement_get_condition (self));
      {
            GeeList* it_expr_collection;
            GeeIterator* it_expr_it;
            it_expr_collection = self->priv->iterator;
            it_expr_it = gee_iterable_iterator (GEE_ITERABLE (it_expr_collection));
            while (gee_iterator_next (it_expr_it)) {
                  ValaExpression* it_expr;
                  it_expr = gee_iterator_get (it_expr_it);
                  {
                        vala_code_node_accept (VALA_CODE_NODE (it_expr), visitor);
                        vala_code_visitor_visit_end_full_expression (visitor, it_expr);
                        (it_expr == NULL ? NULL : (it_expr = (g_object_unref (it_expr), NULL)));
                  }
            }
            (it_expr_it == NULL ? NULL : (it_expr_it = (g_object_unref (it_expr_it), NULL)));
      }
      vala_code_node_accept (VALA_CODE_NODE (vala_for_statement_get_body (self)), visitor);
      vala_code_visitor_visit_for_statement (visitor, self);
}


static void vala_for_statement_real_replace (ValaCodeNode* base, ValaCodeNode* old_node, ValaCodeNode* new_node)
{
      ValaForStatement * self;
      self = ((ValaForStatement*) base);
      g_return_if_fail (VALA_IS_CODE_NODE (old_node));
      g_return_if_fail (VALA_IS_CODE_NODE (new_node));
      if (VALA_CODE_NODE (vala_for_statement_get_condition (self)) == old_node) {
            vala_for_statement_set_condition (self, VALA_EXPRESSION (new_node));
            return;
      }
      {
            gint i;
            i = 0;
            for (i = 0; i < gee_collection_get_size (((GeeCollection*) self->priv->initializer)); i++) {
                  ValaExpression* __temp2;
                  gboolean __temp3;
                  __temp2 = NULL;
                  if ((__temp3 = VALA_CODE_NODE ((__temp2 = gee_list_get (((GeeList*) self->priv->initializer), i))) == old_node, (__temp2 == NULL ? NULL : (__temp2 = (g_object_unref (__temp2), NULL))), __temp3)) {
                        gee_list_set (((GeeList*) self->priv->initializer), i, VALA_EXPRESSION (new_node));
                        return;
                  }
            }
      }
      {
            gint i;
            i = 0;
            for (i = 0; i < gee_collection_get_size (((GeeCollection*) self->priv->iterator)); i++) {
                  ValaExpression* __temp4;
                  gboolean __temp5;
                  __temp4 = NULL;
                  if ((__temp5 = VALA_CODE_NODE ((__temp4 = gee_list_get (((GeeList*) self->priv->iterator), i))) == old_node, (__temp4 == NULL ? NULL : (__temp4 = (g_object_unref (__temp4), NULL))), __temp5)) {
                        gee_list_set (((GeeList*) self->priv->iterator), i, VALA_EXPRESSION (new_node));
                        return;
                  }
            }
      }
}


ValaExpression* vala_for_statement_get_condition (ValaForStatement* self)
{
      g_return_val_if_fail (VALA_IS_FOR_STATEMENT (self), NULL);
      return self->priv->_condition;
}


void vala_for_statement_set_condition (ValaForStatement* self, ValaExpression* value)
{
      ValaExpression* __temp7;
      g_return_if_fail (VALA_IS_FOR_STATEMENT (self));
      __temp7 = NULL;
      self->priv->_condition = (__temp7 = g_object_ref (value), (self->priv->_condition == NULL ? NULL : (self->priv->_condition = (g_object_unref (self->priv->_condition), NULL))), __temp7);
      vala_code_node_set_parent_node (VALA_CODE_NODE (self->priv->_condition), VALA_CODE_NODE (self));
}


ValaBlock* vala_for_statement_get_body (ValaForStatement* self)
{
      g_return_val_if_fail (VALA_IS_FOR_STATEMENT (self), NULL);
      return self->priv->_body;
}


void vala_for_statement_set_body (ValaForStatement* self, ValaBlock* value)
{
      ValaBlock* __temp10;
      ValaBlock* __temp9;
      g_return_if_fail (VALA_IS_FOR_STATEMENT (self));
      __temp10 = NULL;
      __temp9 = NULL;
      self->priv->_body = (__temp10 = (__temp9 = value, (__temp9 == NULL ? NULL : g_object_ref (__temp9))), (self->priv->_body == NULL ? NULL : (self->priv->_body = (g_object_unref (self->priv->_body), NULL))), __temp10);
      vala_code_node_set_parent_node (VALA_CODE_NODE (self->priv->_body), VALA_CODE_NODE (self));
}


static void vala_for_statement_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec)
{
      ValaForStatement * self;
      self = VALA_FOR_STATEMENT (object);
      switch (property_id) {
            case VALA_FOR_STATEMENT_CONDITION:
            g_value_set_object (value, vala_for_statement_get_condition (self));
            break;
            case VALA_FOR_STATEMENT_BODY:
            g_value_set_object (value, vala_for_statement_get_body (self));
            break;
      }
}


static void vala_for_statement_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec)
{
      ValaForStatement * self;
      self = VALA_FOR_STATEMENT (object);
      switch (property_id) {
            case VALA_FOR_STATEMENT_CONDITION:
            vala_for_statement_set_condition (self, g_value_get_object (value));
            break;
            case VALA_FOR_STATEMENT_BODY:
            vala_for_statement_set_body (self, g_value_get_object (value));
            break;
      }
}


static void vala_for_statement_class_init (ValaForStatementClass * klass)
{
      vala_for_statement_parent_class = g_type_class_peek_parent (klass);
      g_type_class_add_private (klass, sizeof (ValaForStatementPrivate));
      G_OBJECT_CLASS (klass)->get_property = vala_for_statement_get_property;
      G_OBJECT_CLASS (klass)->set_property = vala_for_statement_set_property;
      G_OBJECT_CLASS (klass)->dispose = vala_for_statement_dispose;
      VALA_CODE_NODE_CLASS (klass)->accept = vala_for_statement_real_accept;
      VALA_CODE_NODE_CLASS (klass)->replace = vala_for_statement_real_replace;
      g_object_class_install_property (G_OBJECT_CLASS (klass), VALA_FOR_STATEMENT_CONDITION, g_param_spec_object ("condition", "foo", "bar", VALA_TYPE_EXPRESSION, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
      g_object_class_install_property (G_OBJECT_CLASS (klass), VALA_FOR_STATEMENT_BODY, g_param_spec_object ("body", "foo", "bar", VALA_TYPE_BLOCK, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void vala_for_statement_vala_statement_interface_init (ValaStatementIface * iface)
{
      vala_for_statement_vala_statement_parent_iface = g_type_interface_peek_parent (iface);
}


static void vala_for_statement_init (ValaForStatement * self)
{
      self->priv = VALA_FOR_STATEMENT_GET_PRIVATE (self);
      self->priv->initializer = GEE_LIST (gee_array_list_new (g_object_ref, g_object_unref, g_direct_equal));
      self->priv->iterator = GEE_LIST (gee_array_list_new (g_object_ref, g_object_unref, g_direct_equal));
}


static void vala_for_statement_dispose (GObject * obj)
{
      ValaForStatement * self;
      ValaForStatementClass * klass;
      GObjectClass * parent_class;
      self = VALA_FOR_STATEMENT (obj);
      (self->priv->initializer == NULL ? NULL : (self->priv->initializer = (g_object_unref (self->priv->initializer), NULL)));
      (self->priv->iterator == NULL ? NULL : (self->priv->iterator = (g_object_unref (self->priv->iterator), NULL)));
      (self->priv->_condition == NULL ? NULL : (self->priv->_condition = (g_object_unref (self->priv->_condition), NULL)));
      (self->priv->_body == NULL ? NULL : (self->priv->_body = (g_object_unref (self->priv->_body), NULL)));
      klass = VALA_FOR_STATEMENT_CLASS (g_type_class_peek (VALA_TYPE_FOR_STATEMENT));
      parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
      parent_class->dispose (obj);
}


GType vala_for_statement_get_type (void)
{
      static GType vala_for_statement_type_id = 0;
      if (G_UNLIKELY (vala_for_statement_type_id == 0)) {
            static const GTypeInfo g_define_type_info = { sizeof (ValaForStatementClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_for_statement_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaForStatement), 0, (GInstanceInitFunc) vala_for_statement_init };
            static const GInterfaceInfo vala_statement_info = { (GInterfaceInitFunc) vala_for_statement_vala_statement_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
            vala_for_statement_type_id = g_type_register_static (VALA_TYPE_CODE_NODE, "ValaForStatement", &g_define_type_info, 0);
            g_type_add_interface_static (vala_for_statement_type_id, VALA_TYPE_STATEMENT, &vala_statement_info);
      }
      return vala_for_statement_type_id;
}





Generated by  Doxygen 1.6.0   Back to index