Logo Search packages:      
Sourcecode: vala version File versions

valablock.c

/* valablock.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 "valablock.h"
#include <gee/arraylist.h>
#include <gee/list.h>
#include <gee/readonlycollection.h>
#include <vala/valasourcereference.h>
#include <vala/valablock.h>
#include <vala/valavariabledeclarator.h>
#include <vala/valacodevisitor.h>

struct _ValaBlockPrivate {
      gboolean _contains_jump_statement;
      GeeList* statement_list;
      GeeList* local_variables;
};
#define VALA_BLOCK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALA_TYPE_BLOCK, ValaBlockPrivate))
enum  {
      VALA_BLOCK_DUMMY_PROPERTY,
      VALA_BLOCK_CONTAINS_JUMP_STATEMENT
};
static void vala_block_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor);
static gpointer vala_block_parent_class = NULL;
static ValaStatementIface* vala_block_vala_statement_parent_iface = NULL;
static void vala_block_dispose (GObject * obj);


/**
 * Creates a new block.
 *
 * @param source reference to source code
 */
ValaBlock* vala_block_new (ValaSourceReference* source_reference)
{
      ValaBlock * self;
      g_return_val_if_fail (source_reference == NULL || VALA_IS_SOURCE_REFERENCE (source_reference), NULL);
      self = g_object_newv (VALA_TYPE_BLOCK, 0, NULL);
      vala_code_node_set_source_reference (VALA_CODE_NODE (self), source_reference);
      return self;
}


/**
 * Append a statement to this block.
 *
 * @param stmt a statement
 */
void vala_block_add_statement (ValaBlock* self, ValaStatement* stmt)
{
      g_return_if_fail (VALA_IS_BLOCK (self));
      g_return_if_fail (VALA_IS_STATEMENT (stmt));
      gee_collection_add (GEE_COLLECTION (self->priv->statement_list), stmt);
}


/**
 * Returns a copy of the list of statements.
 *
 * @return statement list
 */
GeeCollection* vala_block_get_statements (ValaBlock* self)
{
      g_return_val_if_fail (VALA_IS_BLOCK (self), NULL);
      return GEE_COLLECTION (gee_read_only_collection_new (g_object_ref, g_object_unref, GEE_COLLECTION (self->priv->statement_list)));
}


/**
 * Add a local variable to this block.
 *
 * @param decl a variable declarator
 */
void vala_block_add_local_variable (ValaBlock* self, ValaVariableDeclarator* decl)
{
      g_return_if_fail (VALA_IS_BLOCK (self));
      g_return_if_fail (VALA_IS_VARIABLE_DECLARATOR (decl));
      gee_collection_add (GEE_COLLECTION (self->priv->local_variables), decl);
}


/**
 * Returns a copy of the list of local variables.
 *
 * @return variable declarator list
 */
GeeCollection* vala_block_get_local_variables (ValaBlock* self)
{
      g_return_val_if_fail (VALA_IS_BLOCK (self), NULL);
      return GEE_COLLECTION (gee_read_only_collection_new (g_object_ref, g_object_unref, GEE_COLLECTION (self->priv->local_variables)));
}


static void vala_block_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor)
{
      ValaBlock * self;
      self = ((ValaBlock*) base);
      g_return_if_fail (VALA_IS_CODE_VISITOR (visitor));
      vala_code_visitor_visit_begin_block (visitor, self);
      {
            GeeList* stmt_collection;
            GeeIterator* stmt_it;
            stmt_collection = self->priv->statement_list;
            stmt_it = gee_iterable_iterator (GEE_ITERABLE (stmt_collection));
            while (gee_iterator_next (stmt_it)) {
                  ValaStatement* stmt;
                  stmt = gee_iterator_get (stmt_it);
                  {
                        vala_code_node_accept (VALA_CODE_NODE (stmt), visitor);
                        (stmt = (g_object_unref (stmt), NULL));
                  }
            }
            (stmt_it == NULL ? NULL : (stmt_it = (g_object_unref (stmt_it), NULL)));
      }
      vala_code_visitor_visit_end_block (visitor, self);
}


gboolean vala_block_get_contains_jump_statement (ValaBlock* self)
{
      g_return_val_if_fail (VALA_IS_BLOCK (self), FALSE);
      return self->priv->_contains_jump_statement;
}


void vala_block_set_contains_jump_statement (ValaBlock* self, gboolean value)
{
      g_return_if_fail (VALA_IS_BLOCK (self));
      self->priv->_contains_jump_statement = value;
}


static void vala_block_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec)
{
      ValaBlock * self;
      self = VALA_BLOCK (object);
      switch (property_id) {
            case VALA_BLOCK_CONTAINS_JUMP_STATEMENT:
            g_value_set_boolean (value, vala_block_get_contains_jump_statement (self));
            break;
      }
}


static void vala_block_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec)
{
      ValaBlock * self;
      self = VALA_BLOCK (object);
      switch (property_id) {
            case VALA_BLOCK_CONTAINS_JUMP_STATEMENT:
            vala_block_set_contains_jump_statement (self, g_value_get_boolean (value));
            break;
      }
}


static void vala_block_class_init (ValaBlockClass * klass)
{
      vala_block_parent_class = g_type_class_peek_parent (klass);
      g_type_class_add_private (klass, sizeof (ValaBlockPrivate));
      G_OBJECT_CLASS (klass)->get_property = vala_block_get_property;
      G_OBJECT_CLASS (klass)->set_property = vala_block_set_property;
      G_OBJECT_CLASS (klass)->dispose = vala_block_dispose;
      VALA_CODE_NODE_CLASS (klass)->accept = vala_block_real_accept;
      g_object_class_install_property (G_OBJECT_CLASS (klass), VALA_BLOCK_CONTAINS_JUMP_STATEMENT, g_param_spec_boolean ("contains-jump-statement", "foo", "bar", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void vala_block_vala_statement_interface_init (ValaStatementIface * iface)
{
      vala_block_vala_statement_parent_iface = g_type_interface_peek_parent (iface);
}


static void vala_block_init (ValaBlock * self)
{
      self->priv = VALA_BLOCK_GET_PRIVATE (self);
      self->priv->statement_list = GEE_LIST (gee_array_list_new (g_object_ref, g_object_unref, g_direct_equal));
      self->priv->local_variables = GEE_LIST (gee_array_list_new (g_object_ref, g_object_unref, g_direct_equal));
}


static void vala_block_dispose (GObject * obj)
{
      ValaBlock * self;
      ValaBlockClass * klass;
      GObjectClass * parent_class;
      self = VALA_BLOCK (obj);
      (self->priv->statement_list == NULL ? NULL : (self->priv->statement_list = (g_object_unref (self->priv->statement_list), NULL)));
      (self->priv->local_variables == NULL ? NULL : (self->priv->local_variables = (g_object_unref (self->priv->local_variables), NULL)));
      klass = VALA_BLOCK_CLASS (g_type_class_peek (VALA_TYPE_BLOCK));
      parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
      parent_class->dispose (obj);
}


GType vala_block_get_type (void)
{
      static GType vala_block_type_id = 0;
      if (G_UNLIKELY (vala_block_type_id == 0)) {
            static const GTypeInfo g_define_type_info = { sizeof (ValaBlockClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_block_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaBlock), 0, (GInstanceInitFunc) vala_block_init };
            static const GInterfaceInfo vala_statement_info = { (GInterfaceInitFunc) vala_block_vala_statement_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
            vala_block_type_id = g_type_register_static (VALA_TYPE_SYMBOL, "ValaBlock", &g_define_type_info, 0);
            g_type_add_interface_static (vala_block_type_id, VALA_TYPE_STATEMENT, &vala_statement_info);
      }
      return vala_block_type_id;
}





Generated by  Doxygen 1.6.0   Back to index