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

/* valaaddressofexpression.vala
 *
 * Copyright (C) 2007-2010  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 <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 _ValaAddressofExpressionPrivate {
	ValaExpression* _inner;
};


static gpointer vala_addressof_expression_parent_class = NULL;

#define VALA_ADDRESSOF_EXPRESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALA_TYPE_ADDRESSOF_EXPRESSION, ValaAddressofExpressionPrivate))
static void vala_addressof_expression_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor);
static void vala_addressof_expression_real_accept_children (ValaCodeNode* base, ValaCodeVisitor* visitor);
static void vala_addressof_expression_real_replace_expression (ValaCodeNode* base, ValaExpression* old_node, ValaExpression* new_node);
static gboolean vala_addressof_expression_real_is_pure (ValaExpression* base);
static gboolean vala_addressof_expression_real_is_accessible (ValaExpression* base, ValaSymbol* sym);
static gboolean vala_addressof_expression_real_check (ValaCodeNode* base, ValaCodeContext* context);
static void vala_addressof_expression_real_emit (ValaCodeNode* base, ValaCodeGenerator* codegen);
static void vala_addressof_expression_finalize (ValaCodeNode * obj);


/**
 * Creates a new address-of expression.
 *
 * @param inner variable whose address is to be computed
 * @return      newly created address-of expression
 */
ValaAddressofExpression* vala_addressof_expression_construct (GType object_type, ValaExpression* inner, ValaSourceReference* source_reference) {
	ValaAddressofExpression* self = NULL;
	ValaSourceReference* _tmp0_;
	ValaExpression* _tmp1_;
	g_return_val_if_fail (inner != NULL, NULL);
	self = (ValaAddressofExpression*) vala_expression_construct (object_type);
	_tmp0_ = source_reference;
	vala_code_node_set_source_reference ((ValaCodeNode*) self, _tmp0_);
	_tmp1_ = inner;
	vala_addressof_expression_set_inner (self, _tmp1_);
	return self;
}


ValaAddressofExpression* vala_addressof_expression_new (ValaExpression* inner, ValaSourceReference* source_reference) {
	return vala_addressof_expression_construct (VALA_TYPE_ADDRESSOF_EXPRESSION, inner, source_reference);
}


static void vala_addressof_expression_real_accept (ValaCodeNode* base, ValaCodeVisitor* visitor) {
	ValaAddressofExpression * self;
	ValaCodeVisitor* _tmp0_;
	ValaCodeVisitor* _tmp1_;
	self = (ValaAddressofExpression*) base;
	g_return_if_fail (visitor != NULL);
	_tmp0_ = visitor;
	vala_code_visitor_visit_addressof_expression (_tmp0_, self);
	_tmp1_ = visitor;
	vala_code_visitor_visit_expression (_tmp1_, (ValaExpression*) self);
}


static void vala_addressof_expression_real_accept_children (ValaCodeNode* base, ValaCodeVisitor* visitor) {
	ValaAddressofExpression * self;
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	ValaCodeVisitor* _tmp2_;
	self = (ValaAddressofExpression*) base;
	g_return_if_fail (visitor != NULL);
	_tmp0_ = vala_addressof_expression_get_inner (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = visitor;
	vala_code_node_accept ((ValaCodeNode*) _tmp1_, _tmp2_);
}


static void vala_addressof_expression_real_replace_expression (ValaCodeNode* base, ValaExpression* old_node, ValaExpression* new_node) {
	ValaAddressofExpression * self;
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	ValaExpression* _tmp2_;
	self = (ValaAddressofExpression*) base;
	g_return_if_fail (old_node != NULL);
	g_return_if_fail (new_node != NULL);
	_tmp0_ = vala_addressof_expression_get_inner (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = old_node;
	if (_tmp1_ == _tmp2_) {
		ValaExpression* _tmp3_;
		_tmp3_ = new_node;
		vala_addressof_expression_set_inner (self, _tmp3_);
	}
}


static gboolean vala_addressof_expression_real_is_pure (ValaExpression* base) {
	ValaAddressofExpression * self;
	gboolean result = FALSE;
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	gboolean _tmp2_;
	self = (ValaAddressofExpression*) base;
	_tmp0_ = vala_addressof_expression_get_inner (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = vala_expression_is_pure (_tmp1_);
	result = _tmp2_;
	return result;
}


static gboolean vala_addressof_expression_real_is_accessible (ValaExpression* base, ValaSymbol* sym) {
	ValaAddressofExpression * self;
	gboolean result = FALSE;
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	ValaSymbol* _tmp2_;
	gboolean _tmp3_;
	self = (ValaAddressofExpression*) base;
	g_return_val_if_fail (sym != NULL, FALSE);
	_tmp0_ = vala_addressof_expression_get_inner (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = sym;
	_tmp3_ = vala_expression_is_accessible (_tmp1_, _tmp2_);
	result = _tmp3_;
	return result;
}


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


static gboolean vala_addressof_expression_real_check (ValaCodeNode* base, ValaCodeContext* context) {
	ValaAddressofExpression * self;
	gboolean result = FALSE;
	gboolean _tmp0_;
	gboolean _tmp1_;
	ValaExpression* _tmp4_;
	ValaExpression* _tmp5_;
	ValaExpression* _tmp6_;
	ValaExpression* _tmp7_;
	ValaCodeContext* _tmp8_;
	gboolean _tmp9_;
	ValaElementAccess* ea = NULL;
	ValaExpression* _tmp10_;
	ValaExpression* _tmp11_;
	ValaElementAccess* _tmp12_;
	gboolean _tmp13_ = FALSE;
	ValaExpression* _tmp14_;
	ValaExpression* _tmp15_;
	ValaExpression* _tmp35_;
	ValaExpression* _tmp36_;
	ValaDataType* _tmp37_;
	ValaDataType* _tmp38_;
	gboolean _tmp39_;
	gboolean _tmp54_;
	gboolean _tmp55_;
	self = (ValaAddressofExpression*) base;
	g_return_val_if_fail (context != NULL, FALSE);
	_tmp0_ = vala_code_node_get_checked ((ValaCodeNode*) self);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		gboolean _tmp2_;
		gboolean _tmp3_;
		_tmp2_ = vala_code_node_get_error ((ValaCodeNode*) self);
		_tmp3_ = _tmp2_;
		result = !_tmp3_;
		return result;
	}
	vala_code_node_set_checked ((ValaCodeNode*) self, TRUE);
	_tmp4_ = vala_addressof_expression_get_inner (self);
	_tmp5_ = _tmp4_;
	vala_expression_set_lvalue (_tmp5_, TRUE);
	_tmp6_ = vala_addressof_expression_get_inner (self);
	_tmp7_ = _tmp6_;
	_tmp8_ = context;
	_tmp9_ = vala_code_node_check ((ValaCodeNode*) _tmp7_, _tmp8_);
	if (!_tmp9_) {
		vala_code_node_set_error ((ValaCodeNode*) self, TRUE);
		result = FALSE;
		return result;
	}
	_tmp10_ = vala_addressof_expression_get_inner (self);
	_tmp11_ = _tmp10_;
	_tmp12_ = _vala_code_node_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (_tmp11_, VALA_TYPE_ELEMENT_ACCESS) ? ((ValaElementAccess*) _tmp11_) : NULL);
	ea = _tmp12_;
	_tmp14_ = vala_addressof_expression_get_inner (self);
	_tmp15_ = _tmp14_;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp15_, VALA_TYPE_MEMBER_ACCESS)) {
		ValaExpression* _tmp16_;
		ValaExpression* _tmp17_;
		ValaSymbol* _tmp18_;
		ValaSymbol* _tmp19_;
		_tmp16_ = vala_addressof_expression_get_inner (self);
		_tmp17_ = _tmp16_;
		_tmp18_ = vala_expression_get_symbol_reference (_tmp17_);
		_tmp19_ = _tmp18_;
		_tmp13_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp19_, VALA_TYPE_VARIABLE);
	} else {
		_tmp13_ = FALSE;
	}
	if (_tmp13_) {
	} else {
		gboolean _tmp20_ = FALSE;
		ValaElementAccess* _tmp21_;
		_tmp21_ = ea;
		if (_tmp21_ != NULL) {
			gboolean _tmp22_ = FALSE;
			ValaElementAccess* _tmp23_;
			ValaExpression* _tmp24_;
			ValaExpression* _tmp25_;
			ValaDataType* _tmp26_;
			ValaDataType* _tmp27_;
			_tmp23_ = ea;
			_tmp24_ = vala_element_access_get_container (_tmp23_);
			_tmp25_ = _tmp24_;
			_tmp26_ = vala_expression_get_value_type (_tmp25_);
			_tmp27_ = _tmp26_;
			if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp27_, VALA_TYPE_ARRAY_TYPE)) {
				_tmp22_ = TRUE;
			} else {
				ValaElementAccess* _tmp28_;
				ValaExpression* _tmp29_;
				ValaExpression* _tmp30_;
				ValaDataType* _tmp31_;
				ValaDataType* _tmp32_;
				_tmp28_ = ea;
				_tmp29_ = vala_element_access_get_container (_tmp28_);
				_tmp30_ = _tmp29_;
				_tmp31_ = vala_expression_get_value_type (_tmp30_);
				_tmp32_ = _tmp31_;
				_tmp22_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp32_, VALA_TYPE_POINTER_TYPE);
			}
			_tmp20_ = _tmp22_;
		} else {
			_tmp20_ = FALSE;
		}
		if (_tmp20_) {
		} else {
			ValaSourceReference* _tmp33_;
			ValaSourceReference* _tmp34_;
			vala_code_node_set_error ((ValaCodeNode*) self, TRUE);
			_tmp33_ = vala_code_node_get_source_reference ((ValaCodeNode*) self);
			_tmp34_ = _tmp33_;
			vala_report_error (_tmp34_, "Address-of operator not supported for this expression");
			result = FALSE;
			_vala_code_node_unref0 (ea);
			return result;
		}
	}
	_tmp35_ = vala_addressof_expression_get_inner (self);
	_tmp36_ = _tmp35_;
	_tmp37_ = vala_expression_get_value_type (_tmp36_);
	_tmp38_ = _tmp37_;
	_tmp39_ = vala_data_type_is_reference_type_or_type_parameter (_tmp38_);
	if (_tmp39_) {
		ValaExpression* _tmp40_;
		ValaExpression* _tmp41_;
		ValaDataType* _tmp42_;
		ValaDataType* _tmp43_;
		ValaPointerType* _tmp44_;
		ValaPointerType* _tmp45_;
		ValaPointerType* _tmp46_;
		ValaPointerType* _tmp47_;
		_tmp40_ = vala_addressof_expression_get_inner (self);
		_tmp41_ = _tmp40_;
		_tmp42_ = vala_expression_get_value_type (_tmp41_);
		_tmp43_ = _tmp42_;
		_tmp44_ = vala_pointer_type_new (_tmp43_, NULL);
		_tmp45_ = _tmp44_;
		_tmp46_ = vala_pointer_type_new ((ValaDataType*) _tmp45_, NULL);
		_tmp47_ = _tmp46_;
		vala_expression_set_value_type ((ValaExpression*) self, (ValaDataType*) _tmp47_);
		_vala_code_node_unref0 (_tmp47_);
		_vala_code_node_unref0 (_tmp45_);
	} else {
		ValaExpression* _tmp48_;
		ValaExpression* _tmp49_;
		ValaDataType* _tmp50_;
		ValaDataType* _tmp51_;
		ValaPointerType* _tmp52_;
		ValaPointerType* _tmp53_;
		_tmp48_ = vala_addressof_expression_get_inner (self);
		_tmp49_ = _tmp48_;
		_tmp50_ = vala_expression_get_value_type (_tmp49_);
		_tmp51_ = _tmp50_;
		_tmp52_ = vala_pointer_type_new (_tmp51_, NULL);
		_tmp53_ = _tmp52_;
		vala_expression_set_value_type ((ValaExpression*) self, (ValaDataType*) _tmp53_);
		_vala_code_node_unref0 (_tmp53_);
	}
	_tmp54_ = vala_code_node_get_error ((ValaCodeNode*) self);
	_tmp55_ = _tmp54_;
	result = !_tmp55_;
	_vala_code_node_unref0 (ea);
	return result;
}


static void vala_addressof_expression_real_emit (ValaCodeNode* base, ValaCodeGenerator* codegen) {
	ValaAddressofExpression * self;
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	ValaCodeGenerator* _tmp2_;
	ValaCodeGenerator* _tmp3_;
	ValaCodeGenerator* _tmp4_;
	self = (ValaAddressofExpression*) base;
	g_return_if_fail (codegen != NULL);
	_tmp0_ = vala_addressof_expression_get_inner (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = codegen;
	vala_code_node_emit ((ValaCodeNode*) _tmp1_, _tmp2_);
	_tmp3_ = codegen;
	vala_code_visitor_visit_addressof_expression ((ValaCodeVisitor*) _tmp3_, self);
	_tmp4_ = codegen;
	vala_code_visitor_visit_expression ((ValaCodeVisitor*) _tmp4_, (ValaExpression*) self);
}


ValaExpression* vala_addressof_expression_get_inner (ValaAddressofExpression* self) {
	ValaExpression* result;
	ValaExpression* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_inner;
	result = _tmp0_;
	return result;
}


void vala_addressof_expression_set_inner (ValaAddressofExpression* self, ValaExpression* value) {
	ValaExpression* _tmp0_;
	ValaExpression* _tmp1_;
	ValaExpression* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp1_ = _vala_code_node_ref0 (_tmp0_);
	_vala_code_node_unref0 (self->priv->_inner);
	self->priv->_inner = _tmp1_;
	_tmp2_ = self->priv->_inner;
	vala_code_node_set_parent_node ((ValaCodeNode*) _tmp2_, (ValaCodeNode*) self);
}


static void vala_addressof_expression_class_init (ValaAddressofExpressionClass * klass) {
	vala_addressof_expression_parent_class = g_type_class_peek_parent (klass);
	((ValaCodeNodeClass *) klass)->finalize = vala_addressof_expression_finalize;
	g_type_class_add_private (klass, sizeof (ValaAddressofExpressionPrivate));
	((ValaCodeNodeClass *) klass)->accept = (void (*) (ValaCodeNode *, ValaCodeVisitor*)) vala_addressof_expression_real_accept;
	((ValaCodeNodeClass *) klass)->accept_children = (void (*) (ValaCodeNode *, ValaCodeVisitor*)) vala_addressof_expression_real_accept_children;
	((ValaCodeNodeClass *) klass)->replace_expression = (void (*) (ValaCodeNode *, ValaExpression*, ValaExpression*)) vala_addressof_expression_real_replace_expression;
	((ValaExpressionClass *) klass)->is_pure = (gboolean (*) (ValaExpression *)) vala_addressof_expression_real_is_pure;
	((ValaExpressionClass *) klass)->is_accessible = (gboolean (*) (ValaExpression *, ValaSymbol*)) vala_addressof_expression_real_is_accessible;
	((ValaCodeNodeClass *) klass)->check = (gboolean (*) (ValaCodeNode *, ValaCodeContext*)) vala_addressof_expression_real_check;
	((ValaCodeNodeClass *) klass)->emit = (void (*) (ValaCodeNode *, ValaCodeGenerator*)) vala_addressof_expression_real_emit;
}


static void vala_addressof_expression_instance_init (ValaAddressofExpression * self) {
	self->priv = VALA_ADDRESSOF_EXPRESSION_GET_PRIVATE (self);
}


static void vala_addressof_expression_finalize (ValaCodeNode * obj) {
	ValaAddressofExpression * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, VALA_TYPE_ADDRESSOF_EXPRESSION, ValaAddressofExpression);
	_vala_code_node_unref0 (self->priv->_inner);
	VALA_CODE_NODE_CLASS (vala_addressof_expression_parent_class)->finalize (obj);
}


/**
 * Represents an address-of expression in the source code, e.g. `&foo`.
 */
GType vala_addressof_expression_get_type (void) {
	static volatile gsize vala_addressof_expression_type_id__volatile = 0;
	if (g_once_init_enter (&vala_addressof_expression_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ValaAddressofExpressionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_addressof_expression_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaAddressofExpression), 0, (GInstanceInitFunc) vala_addressof_expression_instance_init, NULL };
		GType vala_addressof_expression_type_id;
		vala_addressof_expression_type_id = g_type_register_static (VALA_TYPE_EXPRESSION, "ValaAddressofExpression", &g_define_type_info, 0);
		g_once_init_leave (&vala_addressof_expression_type_id__volatile, vala_addressof_expression_type_id);
	}
	return vala_addressof_expression_type_id__volatile;
}



