update for 0.3.3 release
[vala-lang.git] / gobject / valaccodetypesymbolbinding.vala
blob2b3d119effd1e1ad51d1423d41030c0b0bfb9986
1 /* valaccodetypesymbolbinding.vala
3 * Copyright (C) 2008 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
21 * Raffaele Sandrini <raffaele@sandrini.ch>
24 using GLib;
26 public abstract class Vala.CCodeTypeSymbolBinding : CCodeBinding {
27 public CCodeFunctionCall get_param_spec (Property prop) {
28 var cspec = new CCodeFunctionCall ();
29 cspec.add_argument (prop.get_canonical_cconstant ());
30 cspec.add_argument (new CCodeConstant ("\"%s\"".printf (prop.nick)));
31 cspec.add_argument (new CCodeConstant ("\"%s\"".printf (prop.blurb)));
32 if ((prop.property_type.data_type is Class && ((Class) prop.property_type.data_type).is_subtype_of (codegen.gobject_type)) || prop.property_type.data_type is Interface) {
33 cspec.call = new CCodeIdentifier ("g_param_spec_object");
34 cspec.add_argument (new CCodeIdentifier (prop.property_type.data_type.get_upper_case_cname ("TYPE_")));
35 } else if (prop.property_type.data_type == codegen.string_type.data_type) {
36 cspec.call = new CCodeIdentifier ("g_param_spec_string");
37 cspec.add_argument (new CCodeConstant ("NULL"));
38 } else if (prop.property_type.data_type is Enum) {
39 var e = prop.property_type.data_type as Enum;
40 if (e.has_type_id) {
41 if (e.is_flags) {
42 cspec.call = new CCodeIdentifier ("g_param_spec_flags");
43 } else {
44 cspec.call = new CCodeIdentifier ("g_param_spec_enum");
46 cspec.add_argument (new CCodeIdentifier (e.get_type_id ()));
47 } else {
48 if (e.is_flags) {
49 cspec.call = new CCodeIdentifier ("g_param_spec_uint");
50 cspec.add_argument (new CCodeConstant ("0"));
51 cspec.add_argument (new CCodeConstant ("G_MAXUINT"));
52 } else {
53 cspec.call = new CCodeIdentifier ("g_param_spec_int");
54 cspec.add_argument (new CCodeConstant ("G_MININT"));
55 cspec.add_argument (new CCodeConstant ("G_MAXINT"));
59 if (prop.default_expression != null) {
60 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
61 } else {
62 cspec.add_argument (new CCodeConstant (prop.property_type.data_type.get_default_value ()));
64 } else if (prop.property_type.data_type is Struct) {
65 var st = (Struct) prop.property_type.data_type;
66 if (st.get_type_id () == "G_TYPE_INT") {
67 cspec.call = new CCodeIdentifier ("g_param_spec_int");
68 cspec.add_argument (new CCodeConstant ("G_MININT"));
69 cspec.add_argument (new CCodeConstant ("G_MAXINT"));
70 if (prop.default_expression != null) {
71 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
72 } else {
73 cspec.add_argument (new CCodeConstant ("0"));
75 } else if (st.get_type_id () == "G_TYPE_UINT") {
76 cspec.call = new CCodeIdentifier ("g_param_spec_uint");
77 cspec.add_argument (new CCodeConstant ("0"));
78 cspec.add_argument (new CCodeConstant ("G_MAXUINT"));
79 if (prop.default_expression != null) {
80 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
81 } else {
82 cspec.add_argument (new CCodeConstant ("0U"));
84 } else if (st.get_type_id () == "G_TYPE_LONG") {
85 cspec.call = new CCodeIdentifier ("g_param_spec_long");
86 cspec.add_argument (new CCodeConstant ("G_MINLONG"));
87 cspec.add_argument (new CCodeConstant ("G_MAXLONG"));
88 if (prop.default_expression != null) {
89 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
90 } else {
91 cspec.add_argument (new CCodeConstant ("0L"));
93 } else if (st.get_type_id () == "G_TYPE_ULONG") {
94 cspec.call = new CCodeIdentifier ("g_param_spec_ulong");
95 cspec.add_argument (new CCodeConstant ("0"));
96 cspec.add_argument (new CCodeConstant ("G_MAXULONG"));
97 if (prop.default_expression != null) {
98 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
99 } else {
100 cspec.add_argument (new CCodeConstant ("0UL"));
102 } else if (st.get_type_id () == "G_TYPE_BOOLEAN") {
103 cspec.call = new CCodeIdentifier ("g_param_spec_boolean");
104 if (prop.default_expression != null) {
105 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
106 } else {
107 cspec.add_argument (new CCodeConstant ("FALSE"));
109 } else if (st.get_type_id () == "G_TYPE_CHAR") {
110 cspec.call = new CCodeIdentifier ("g_param_spec_char");
111 cspec.add_argument (new CCodeConstant ("G_MININT8"));
112 cspec.add_argument (new CCodeConstant ("G_MAXINT8"));
113 if (prop.default_expression != null) {
114 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
115 } else {
116 cspec.add_argument (new CCodeConstant ("0"));
118 } else if (st.get_type_id () == "G_TYPE_FLOAT") {
119 cspec.call = new CCodeIdentifier ("g_param_spec_float");
120 cspec.add_argument (new CCodeConstant ("-G_MAXFLOAT"));
121 cspec.add_argument (new CCodeConstant ("G_MAXFLOAT"));
122 if (prop.default_expression != null) {
123 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
124 } else {
125 cspec.add_argument (new CCodeConstant ("0.0F"));
127 } else if (st.get_type_id () == "G_TYPE_DOUBLE") {
128 cspec.call = new CCodeIdentifier ("g_param_spec_double");
129 cspec.add_argument (new CCodeConstant ("-G_MAXDOUBLE"));
130 cspec.add_argument (new CCodeConstant ("G_MAXDOUBLE"));
131 if (prop.default_expression != null) {
132 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
133 } else {
134 cspec.add_argument (new CCodeConstant ("0.0"));
136 } else if (st.get_type_id () == "G_TYPE_GTYPE") {
137 cspec.call = new CCodeIdentifier ("g_param_spec_gtype");
138 if (prop.default_expression != null) {
139 cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
140 } else {
141 cspec.add_argument (new CCodeConstant ("G_TYPE_NONE"));
143 } else {
144 cspec.call = new CCodeIdentifier ("g_param_spec_pointer");
146 } else {
147 cspec.call = new CCodeIdentifier ("g_param_spec_pointer");
150 var pflags = "G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB";
151 if (prop.get_accessor != null) {
152 pflags = "%s%s".printf (pflags, " | G_PARAM_READABLE");
154 if (prop.set_accessor != null) {
155 pflags = "%s%s".printf (pflags, " | G_PARAM_WRITABLE");
156 if (prop.set_accessor.construction) {
157 if (prop.set_accessor.writable) {
158 pflags = "%s%s".printf (pflags, " | G_PARAM_CONSTRUCT");
159 } else {
160 pflags = "%s%s".printf (pflags, " | G_PARAM_CONSTRUCT_ONLY");
164 cspec.add_argument (new CCodeConstant (pflags));
166 return cspec;
169 public CCodeFunctionCall get_signal_creation (Signal sig, TypeSymbol type) {
170 var csignew = new CCodeFunctionCall (new CCodeIdentifier ("g_signal_new"));
171 csignew.add_argument (new CCodeConstant ("\"%s\"".printf (sig.name)));
172 csignew.add_argument (new CCodeIdentifier (type.get_upper_case_cname ("TYPE_")));
173 csignew.add_argument (new CCodeConstant ("G_SIGNAL_RUN_LAST"));
174 csignew.add_argument (new CCodeConstant ("0"));
175 csignew.add_argument (new CCodeConstant ("NULL"));
176 csignew.add_argument (new CCodeConstant ("NULL"));
178 string marshaller = codegen.get_marshaller_function (sig.get_parameters (), sig.return_type);
180 var marshal_arg = new CCodeIdentifier (marshaller);
181 csignew.add_argument (marshal_arg);
183 var params = sig.get_parameters ();
184 var params_len = params.size;
185 if (sig.return_type is PointerType || sig.return_type.type_parameter != null) {
186 csignew.add_argument (new CCodeConstant ("G_TYPE_POINTER"));
187 } else if (sig.return_type is ErrorType) {
188 csignew.add_argument (new CCodeConstant ("G_TYPE_POINTER"));
189 } else if (sig.return_type.data_type == null) {
190 csignew.add_argument (new CCodeConstant ("G_TYPE_NONE"));
191 } else {
192 csignew.add_argument (new CCodeConstant (sig.return_type.data_type.get_type_id ()));
194 csignew.add_argument (new CCodeConstant ("%d".printf (params_len)));
195 foreach (FormalParameter param in params) {
196 if (param.parameter_type is PointerType || param.parameter_type.type_parameter != null || param.direction != ParameterDirection.IN) {
197 csignew.add_argument (new CCodeConstant ("G_TYPE_POINTER"));
198 } else if (param.parameter_type is ErrorType) {
199 csignew.add_argument (new CCodeConstant ("G_TYPE_POINTER"));
200 } else {
201 csignew.add_argument (new CCodeConstant (param.parameter_type.data_type.get_type_id ()));
205 marshal_arg.name = marshaller;
207 return csignew;