/* valalockstatement.c generated by valac, the Vala compiler
 * generated from valalockstatement.vala, do not modify */

/* valalockstatement.vala
 *
 * Copyright (C) 2009  Jiří Zárevúcky
 * Copyright (C) 2006-2010  Jürg Billeter
 * Copyright (C) 2006-2007  Raffaele Sandrini
 *
 * 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
 *
 * Authors:
 * 	Raffaele Sandrini <raffaele@sandrini.ch>
 * 	Jiří Zárevúcky <zarevucky.jiri@gmail.com>
 */

#include <glib.h>
#include <glib-object.h>
#include "vala.h"

#define _vala_code_node_unref0(var) ((var == NULL) ? NULL : (var = (vala_code_node_unref (var), NULL)))

struct _ValaLockStatementPrivate {
	ValaExpression* _resource;
	ValaBlock* _body;
};


static gpointer vala_lock_statement_parent_class = NULL;
static ValaStatementIface * vala_lock_statement_vala_statement_parent_iface = NULL;

#define VALA_LOCK_STATEMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALA_TYPE_LOCK_STATEMENT, ValaLockStatementPrivate))
static void vala_lock_statement_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor);
static gboolean vala_lock_statement_real_check (ValaCodeNode* base, ValaCodeContext* context);
static void vala_lock_statement_real_emit (ValaCodeNode* base, ValaCodeGenerator* codegen);
static void vala_lock_statement_finalize (ValaCodeNode * obj);


ValaLockStatement* vala_lock_statement_construct (GType object_type, ValaExpression* resource, ValaBlock* body, ValaSourceReference* source_reference) {
	ValaLockStatement* self = NULL;
	ValaBlock* _tmp0_;
	ValaSourceReference* _tmp1_;
	ValaExpression* _tmp2_;
	g_return_val_if_fail (resource != NULL, NULL);
	self = (ValaLockStatement*) vala_code_node_construct (object_type);
	_tmp0_ = body;
	vala_lock_statement_set_body (self, _tmp0_);
	_tmp1_ = source_reference;
	vala_code_node_set_source_reference ((ValaCodeNode*) self, _tmp1_);
	_tmp2_ = resource;
	vala_lock_statement_set_resource (self, _tmp2_);
	return self;
}


ValaLockStatement* vala_lock_statement_new (ValaExpression* resource, ValaBlock* body, ValaSourceReference* source_reference) {
	return vala_lock_statement_construct (VALA_TYPE_LOCK_STATEMENT, resource, body, source_reference);
}


static void vala_lock_statement_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor) {
	ValaLockStatement * self;
	ValaExpression* _tmp0_;
	ValaCodeVisitor* _tmp1_;
	ValaBlock* _tmp2_;
	ValaCodeVisitor* _tmp5_;
	self = (ValaLockStatement*) base;
	g_return_if_fail (visitor != NULL);
	_tmp0_ = self->priv->_resource;
	_tmp1_ = visitor;
	vala_code_node_accept ((ValaCodeNode*) _tmp0_, _tmp1_);
	_tmp2_ = self->priv->_body;
	if (_tmp2_ != NULL) {
		ValaBlock* _tmp3_;
		ValaCodeVisitor* _tmp4_;
		_tmp3_ = self->priv->_body;
		_tmp4_ = visitor;
		vala_code_node_accept ((ValaCodeNode*) _tmp3_, _tmp4_);
	}
	_tmp5_ = visitor;
	vala_code_visitor_visit_lock_statement (_tmp5_, self);
}


static gpointer _vala_code_node_ref0 (gpointer self) {
	return self ? vala_code_node_ref (self) : NULL;
}


static gboolean vala_lock_statement_real_check (ValaCodeNode* base, ValaCodeContext* context) {
	ValaLockStatement * self;
	gboolean result = FALSE;
	ValaBlock* _tmp0_;
	gboolean _tmp34_;
	gboolean _tmp35_;
	ValaExpression* _tmp38_;
	ValaCodeContext* _tmp39_;
	gboolean _tmp40_ = FALSE;
	ValaExpression* _tmp41_;
	ValaExpression* _tmp49_;
	ValaSymbol* _tmp50_;
	ValaSymbol* _tmp51_;
	ValaSymbol* _tmp52_;
	ValaSymbol* _tmp53_;
	ValaCodeContext* _tmp54_;
	ValaSemanticAnalyzer* _tmp55_;
	ValaSemanticAnalyzer* _tmp56_;
	ValaClass* _tmp57_;
	ValaClass* _tmp58_;
	ValaExpression* _tmp63_;
	ValaSymbol* _tmp64_;
	ValaSymbol* _tmp65_;
	gboolean _tmp66_;
	gboolean _tmp67_;
	self = (ValaLockStatement*) base;
	g_return_val_if_fail (context != NULL, FALSE);
	_tmp0_ = self->priv->_body;
	if (_tmp0_ != NULL) {
		ValaBlock* fin_body = NULL;
		ValaSourceReference* _tmp1_;
		ValaSourceReference* _tmp2_;
		ValaBlock* _tmp3_;
		ValaBlock* _tmp4_;
		ValaExpression* _tmp5_;
		ValaSourceReference* _tmp6_;
		ValaSourceReference* _tmp7_;
		ValaUnlockStatement* _tmp8_;
		ValaUnlockStatement* _tmp9_;
		ValaBlock* block = NULL;
		ValaSourceReference* _tmp10_;
		ValaSourceReference* _tmp11_;
		ValaBlock* _tmp12_;
		ValaBlock* _tmp13_;
		ValaExpression* _tmp14_;
		ValaSourceReference* _tmp15_;
		ValaSourceReference* _tmp16_;
		ValaLockStatement* _tmp17_;
		ValaLockStatement* _tmp18_;
		ValaBlock* _tmp19_;
		ValaBlock* _tmp20_;
		ValaBlock* _tmp21_;
		ValaSourceReference* _tmp22_;
		ValaSourceReference* _tmp23_;
		ValaTryStatement* _tmp24_;
		ValaTryStatement* _tmp25_;
		ValaBlock* parent_block = NULL;
		ValaCodeNode* _tmp26_;
		ValaCodeNode* _tmp27_;
		ValaBlock* _tmp28_;
		ValaBlock* _tmp29_;
		ValaBlock* _tmp30_;
		ValaBlock* _tmp31_;
		ValaCodeContext* _tmp32_;
		gboolean _tmp33_;
		_tmp1_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
		_tmp2_ = _tmp1_;
		_tmp3_ = vala_block_new (_tmp2_);
		fin_body = _tmp3_;
		_tmp4_ = fin_body;
		_tmp5_ = self->priv->_resource;
		_tmp6_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
		_tmp7_ = _tmp6_;
		_tmp8_ = vala_unlock_statement_new (_tmp5_, _tmp7_);
		_tmp9_ = _tmp8_;
		vala_block_add_statement (_tmp4_, (ValaStatement*) _tmp9_);
		_vala_code_node_unref0 (_tmp9_);
		_tmp10_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
		_tmp11_ = _tmp10_;
		_tmp12_ = vala_block_new (_tmp11_);
		block = _tmp12_;
		_tmp13_ = block;
		_tmp14_ = self->priv->_resource;
		_tmp15_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
		_tmp16_ = _tmp15_;
		_tmp17_ = vala_lock_statement_new (_tmp14_, NULL, _tmp16_);
		_tmp18_ = _tmp17_;
		vala_block_add_statement (_tmp13_, (ValaStatement*) _tmp18_);
		_vala_code_node_unref0 (_tmp18_);
		_tmp19_ = block;
		_tmp20_ = self->priv->_body;
		_tmp21_ = fin_body;
		_tmp22_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
		_tmp23_ = _tmp22_;
		_tmp24_ = vala_try_statement_new (_tmp20_, _tmp21_, _tmp23_);
		_tmp25_ = _tmp24_;
		vala_block_add_statement (_tmp19_, (ValaStatement*) _tmp25_);
		_vala_code_node_unref0 (_tmp25_);
		_tmp26_ = vala_code_node_get_parent_node ((ValaCodeNode*) self);
		_tmp27_ = _tmp26_;
		_tmp28_ = _vala_code_node_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp27_, VALA_TYPE_BLOCK, ValaBlock));
		parent_block = _tmp28_;
		_tmp29_ = parent_block;
		_tmp30_ = block;
		vala_block_replace_statement (_tmp29_, (ValaStatement*) self, (ValaStatement*) _tmp30_);
		_tmp31_ = block;
		_tmp32_ = context;
		_tmp33_ = vala_code_node_check ((ValaCodeNode*) _tmp31_, _tmp32_);
		result = _tmp33_;
		_vala_code_node_unref0 (parent_block);
		_vala_code_node_unref0 (block);
		_vala_code_node_unref0 (fin_body);
		return result;
	}
	_tmp34_ = vala_code_node_get_checked ((ValaCodeNode*) self);
	_tmp35_ = _tmp34_;
	if (_tmp35_) {
		gboolean _tmp36_;
		gboolean _tmp37_;
		_tmp36_ = vala_code_node_get_error ((ValaCodeNode*) self);
		_tmp37_ = _tmp36_;
		result = !_tmp37_;
		return result;
	}
	vala_code_node_set_checked ((ValaCodeNode*) self, TRUE);
	_tmp38_ = self->priv->_resource;
	_tmp39_ = context;
	vala_code_node_check ((ValaCodeNode*) _tmp38_, _tmp39_);
	_tmp41_ = self->priv->_resource;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp41_, VALA_TYPE_MEMBER_ACCESS)) {
		ValaExpression* _tmp42_;
		ValaSymbol* _tmp43_;
		ValaSymbol* _tmp44_;
		_tmp42_ = self->priv->_resource;
		_tmp43_ = vala_expression_get_symbol_reference (_tmp42_);
		_tmp44_ = _tmp43_;
		_tmp40_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp44_, VALA_TYPE_LOCKABLE);
	} else {
		_tmp40_ = FALSE;
	}
	if (!_tmp40_) {
		ValaExpression* _tmp45_;
		ValaExpression* _tmp46_;
		ValaSourceReference* _tmp47_;
		ValaSourceReference* _tmp48_;
		vala_code_node_set_error ((ValaCodeNode*) self, TRUE);
		_tmp45_ = self->priv->_resource;
		vala_code_node_set_error ((ValaCodeNode*) _tmp45_, TRUE);
		_tmp46_ = self->priv->_resource;
		_tmp47_ = vala_code_node_get_source_reference ((ValaCodeNode*) _tmp46_);
		_tmp48_ = _tmp47_;
		vala_report_error (_tmp48_, "Expression is either not a member access or does not denote a lockable" \
" member");
		result = FALSE;
		return result;
	}
	_tmp49_ = self->priv->_resource;
	_tmp50_ = vala_expression_get_symbol_reference (_tmp49_);
	_tmp51_ = _tmp50_;
	_tmp52_ = vala_symbol_get_parent_symbol (_tmp51_);
	_tmp53_ = _tmp52_;
	_tmp54_ = context;
	_tmp55_ = vala_code_context_get_analyzer (_tmp54_);
	_tmp56_ = _tmp55_;
	_tmp57_ = vala_semantic_analyzer_get_current_class (_tmp56_);
	_tmp58_ = _tmp57_;
	if (_tmp53_ != G_TYPE_CHECK_INSTANCE_CAST (_tmp58_, VALA_TYPE_SYMBOL, ValaSymbol)) {
		ValaExpression* _tmp59_;
		ValaExpression* _tmp60_;
		ValaSourceReference* _tmp61_;
		ValaSourceReference* _tmp62_;
		vala_code_node_set_error ((ValaCodeNode*) self, TRUE);
		_tmp59_ = self->priv->_resource;
		vala_code_node_set_error ((ValaCodeNode*) _tmp59_, TRUE);
		_tmp60_ = self->priv->_resource;
		_tmp61_ = vala_code_node_get_source_reference ((ValaCodeNode*) _tmp60_);
		_tmp62_ = _tmp61_;
		vala_report_error (_tmp62_, "Only members of the current class are lockable");
	}
	_tmp63_ = self->priv->_resource;
	_tmp64_ = vala_expression_get_symbol_reference (_tmp63_);
	_tmp65_ = _tmp64_;
	vala_lockable_set_lock_used (G_TYPE_CHECK_INSTANCE_CAST (_tmp65_, VALA_TYPE_LOCKABLE, ValaLockable), TRUE);
	_tmp66_ = vala_code_node_get_error ((ValaCodeNode*) self);
	_tmp67_ = _tmp66_;
	result = !_tmp67_;
	return result;
}


static void vala_lock_statement_real_emit (ValaCodeNode* base, ValaCodeGenerator* codegen) {
	ValaLockStatement * self;
	ValaExpression* _tmp0_;
	ValaCodeGenerator* _tmp1_;
	ValaCodeGenerator* _tmp2_;
	self = (ValaLockStatement*) base;
	g_return_if_fail (codegen != NULL);
	_tmp0_ = self->priv->_resource;
	_tmp1_ = codegen;
	vala_code_node_emit ((ValaCodeNode*) _tmp0_, _tmp1_);
	_tmp2_ = codegen;
	vala_code_visitor_visit_lock_statement ((ValaCodeVisitor*) _tmp2_, self);
}


ValaExpression* vala_lock_statement_get_resource (ValaLockStatement* self) {
	ValaExpression* result;
	ValaExpression* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_resource;
	result = _tmp0_;
	return result;
}


void vala_lock_statement_set_resource (ValaLockStatement* self, ValaExpression* value) {
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _vala_code_node_ref0 (_tmp0_);
	_vala_code_node_unref0 (self->priv->_resource);
	self->priv->_resource = _tmp1_;
}


ValaBlock* vala_lock_statement_get_body (ValaLockStatement* self) {
	ValaBlock* result;
	ValaBlock* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_body;
	result = _tmp0_;
	return result;
}


void vala_lock_statement_set_body (ValaLockStatement* self, ValaBlock* value) {
	ValaBlock* _tmp0_;
	ValaBlock* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _vala_code_node_ref0 (_tmp0_);
	_vala_code_node_unref0 (self->priv->_body);
	self->priv->_body = _tmp1_;
}


static void vala_lock_statement_class_init (ValaLockStatementClass * klass) {
	vala_lock_statement_parent_class = g_type_class_peek_parent (klass);
	((ValaCodeNodeClass *) klass)->finalize = vala_lock_statement_finalize;
	g_type_class_add_private (klass, sizeof (ValaLockStatementPrivate));
	((ValaCodeNodeClass *) klass)->accept = (void (*) (ValaCodeNode *, ValaCodeVisitor*)) vala_lock_statement_real_accept;
	((ValaCodeNodeClass *) klass)->check = (gboolean (*) (ValaCodeNode *, ValaCodeContext*)) vala_lock_statement_real_check;
	((ValaCodeNodeClass *) klass)->emit = (void (*) (ValaCodeNode *, ValaCodeGenerator*)) vala_lock_statement_real_emit;
}


static void vala_lock_statement_vala_statement_interface_init (ValaStatementIface * iface) {
	vala_lock_statement_vala_statement_parent_iface = g_type_interface_peek_parent (iface);
}


static void vala_lock_statement_instance_init (ValaLockStatement * self) {
	self->priv = VALA_LOCK_STATEMENT_GET_PRIVATE (self);
}


static void vala_lock_statement_finalize (ValaCodeNode * obj) {
	ValaLockStatement * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, VALA_TYPE_LOCK_STATEMENT, ValaLockStatement);
	_vala_code_node_unref0 (self->priv->_resource);
	_vala_code_node_unref0 (self->priv->_body);
	VALA_CODE_NODE_CLASS (vala_lock_statement_parent_class)->finalize (obj);
}


/**
 * Represents a lock statement e.g. {{{ lock (a); }}} or {{{ lock (a) { f(a); } }}}.
 *
 * If the statement is empty, the mutex remains locked until a corresponding UnlockStatement
 * occurs. Otherwise it's translated into a try/finally statement which unlocks the mutex
 * after the block is finished.
 */
GType vala_lock_statement_get_type (void) {
	static volatile gsize vala_lock_statement_type_id__volatile = 0;
	if (g_once_init_enter (&vala_lock_statement_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ValaLockStatementClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_lock_statement_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaLockStatement), 0, (GInstanceInitFunc) vala_lock_statement_instance_init, NULL };
		static const GInterfaceInfo vala_statement_info = { (GInterfaceInitFunc) vala_lock_statement_vala_statement_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType vala_lock_statement_type_id;
		vala_lock_statement_type_id = g_type_register_static (VALA_TYPE_CODE_NODE, "ValaLockStatement", &g_define_type_info, 0);
		g_type_add_interface_static (vala_lock_statement_type_id, VALA_TYPE_STATEMENT, &vala_statement_info);
		g_once_init_leave (&vala_lock_statement_type_id__volatile, vala_lock_statement_type_id);
	}
	return vala_lock_statement_type_id__volatile;
}



