1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/* valaccodeblock.vala
*
* Copyright (C) 2006-2008 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.1 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 <ccode/valaccodeblock.h>
#include <gee/arraylist.h>
#include <gee/list.h>
#include <gee/collection.h>
#include <ccode/valaccodelabel.h>
#include <ccode/valaccodereturnstatement.h>
#include <ccode/valaccodegotostatement.h>
#include <ccode/valaccodecontinuestatement.h>
#include <ccode/valaccodebreakstatement.h>
struct _ValaCCodeBlockPrivate {
gboolean _suppress_newline;
GeeList* statements;
};
#define VALA_CCODE_BLOCK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALA_TYPE_CCODE_BLOCK, ValaCCodeBlockPrivate))
enum {
VALA_CCODE_BLOCK_DUMMY_PROPERTY,
VALA_CCODE_BLOCK_SUPPRESS_NEWLINE
};
static void vala_ccode_block_real_write (ValaCCodeNode* base, ValaCCodeWriter* writer);
static gpointer vala_ccode_block_parent_class = NULL;
static void vala_ccode_block_dispose (GObject * obj);
/**
* Prepend the specified statement to the list of statements.
*/
void vala_ccode_block_prepend_statement (ValaCCodeBlock* self, ValaCCodeNode* statement) {
g_return_if_fail (VALA_IS_CCODE_BLOCK (self));
g_return_if_fail (VALA_IS_CCODE_NODE (statement));
gee_list_insert (self->priv->statements, 0, statement);
}
/**
* Append the specified statement to the list of statements.
*/
void vala_ccode_block_add_statement (ValaCCodeBlock* self, ValaCCodeNode* statement) {
g_return_if_fail (VALA_IS_CCODE_BLOCK (self));
g_return_if_fail (VALA_IS_CCODE_NODE (statement));
/* allow generic nodes to include comments */
gee_collection_add (GEE_COLLECTION (self->priv->statements), statement);
}
static void vala_ccode_block_real_write (ValaCCodeNode* base, ValaCCodeWriter* writer) {
ValaCCodeBlock * self;
ValaCCodeNode* last_statement;
self = VALA_CCODE_BLOCK (base);
g_return_if_fail (VALA_IS_CCODE_WRITER (writer));
/* the last reachable statement*/
last_statement = NULL;
vala_ccode_writer_write_begin_block (writer);
{
GeeList* statement_collection;
int statement_it;
statement_collection = self->priv->statements;
for (statement_it = 0; statement_it < gee_collection_get_size (GEE_COLLECTION (statement_collection)); statement_it = statement_it + 1) {
ValaCCodeNode* statement;
statement = ((ValaCCodeNode*) (gee_list_get (GEE_LIST (statement_collection), statement_it)));
{
vala_ccode_node_write_declaration (statement, writer);
/* determine last reachable statement*/
if (VALA_IS_CCODE_LABEL (statement)) {
ValaCCodeNode* _tmp0;
_tmp0 = NULL;
last_statement = (_tmp0 = NULL, (last_statement == NULL ? NULL : (last_statement = (g_object_unref (last_statement), NULL))), _tmp0);
} else {
if (VALA_IS_CCODE_RETURN_STATEMENT (statement) || VALA_IS_CCODE_GOTO_STATEMENT (statement) || VALA_IS_CCODE_CONTINUE_STATEMENT (statement) || VALA_IS_CCODE_BREAK_STATEMENT (statement)) {
ValaCCodeNode* _tmp2;
ValaCCodeNode* _tmp1;
_tmp2 = NULL;
_tmp1 = NULL;
last_statement = (_tmp2 = (_tmp1 = statement, (_tmp1 == NULL ? NULL : g_object_ref (_tmp1))), (last_statement == NULL ? NULL : (last_statement = (g_object_unref (last_statement), NULL))), _tmp2);
}
}
(statement == NULL ? NULL : (statement = (g_object_unref (statement), NULL)));
}
}
}
{
GeeList* statement_collection;
int statement_it;
statement_collection = self->priv->statements;
for (statement_it = 0; statement_it < gee_collection_get_size (GEE_COLLECTION (statement_collection)); statement_it = statement_it + 1) {
ValaCCodeNode* statement;
statement = ((ValaCCodeNode*) (gee_list_get (GEE_LIST (statement_collection), statement_it)));
{
vala_ccode_node_write (statement, writer);
/* only output reachable code*/
if (statement == last_statement) {
(statement == NULL ? NULL : (statement = (g_object_unref (statement), NULL)));
break;
}
(statement == NULL ? NULL : (statement = (g_object_unref (statement), NULL)));
}
}
}
vala_ccode_writer_write_end_block (writer);
if (!self->priv->_suppress_newline) {
vala_ccode_writer_write_newline (writer);
}
(last_statement == NULL ? NULL : (last_statement = (g_object_unref (last_statement), NULL)));
}
/**
* Represents a C code block.
*/
ValaCCodeBlock* vala_ccode_block_new (void) {
ValaCCodeBlock * self;
self = g_object_newv (VALA_TYPE_CCODE_BLOCK, 0, NULL);
return self;
}
gboolean vala_ccode_block_get_suppress_newline (ValaCCodeBlock* self) {
g_return_val_if_fail (VALA_IS_CCODE_BLOCK (self), FALSE);
return self->priv->_suppress_newline;
}
void vala_ccode_block_set_suppress_newline (ValaCCodeBlock* self, gboolean value) {
g_return_if_fail (VALA_IS_CCODE_BLOCK (self));
self->priv->_suppress_newline = value;
}
static void vala_ccode_block_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
ValaCCodeBlock * self;
self = VALA_CCODE_BLOCK (object);
switch (property_id) {
case VALA_CCODE_BLOCK_SUPPRESS_NEWLINE:
g_value_set_boolean (value, vala_ccode_block_get_suppress_newline (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void vala_ccode_block_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
ValaCCodeBlock * self;
self = VALA_CCODE_BLOCK (object);
switch (property_id) {
case VALA_CCODE_BLOCK_SUPPRESS_NEWLINE:
vala_ccode_block_set_suppress_newline (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void vala_ccode_block_class_init (ValaCCodeBlockClass * klass) {
vala_ccode_block_parent_class = g_type_class_peek_parent (klass);
g_type_class_add_private (klass, sizeof (ValaCCodeBlockPrivate));
G_OBJECT_CLASS (klass)->get_property = vala_ccode_block_get_property;
G_OBJECT_CLASS (klass)->set_property = vala_ccode_block_set_property;
G_OBJECT_CLASS (klass)->dispose = vala_ccode_block_dispose;
VALA_CCODE_NODE_CLASS (klass)->write = vala_ccode_block_real_write;
g_object_class_install_property (G_OBJECT_CLASS (klass), VALA_CCODE_BLOCK_SUPPRESS_NEWLINE, g_param_spec_boolean ("suppress-newline", "suppress-newline", "suppress-newline", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}
static void vala_ccode_block_instance_init (ValaCCodeBlock * self) {
self->priv = VALA_CCODE_BLOCK_GET_PRIVATE (self);
self->priv->statements = GEE_LIST (gee_array_list_new (VALA_TYPE_CCODE_NODE, ((GBoxedCopyFunc) (g_object_ref)), g_object_unref, g_direct_equal));
}
static void vala_ccode_block_dispose (GObject * obj) {
ValaCCodeBlock * self;
self = VALA_CCODE_BLOCK (obj);
(self->priv->statements == NULL ? NULL : (self->priv->statements = (g_object_unref (self->priv->statements), NULL)));
G_OBJECT_CLASS (vala_ccode_block_parent_class)->dispose (obj);
}
GType vala_ccode_block_get_type (void) {
static GType vala_ccode_block_type_id = 0;
if (G_UNLIKELY (vala_ccode_block_type_id == 0)) {
static const GTypeInfo g_define_type_info = { sizeof (ValaCCodeBlockClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_ccode_block_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaCCodeBlock), 0, (GInstanceInitFunc) vala_ccode_block_instance_init };
vala_ccode_block_type_id = g_type_register_static (VALA_TYPE_CCODE_STATEMENT, "ValaCCodeBlock", &g_define_type_info, 0);
}
return vala_ccode_block_type_id;
}
|