| 1 | /* | 
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @file Run-time data representation. */ | 
|---|
| 30 |  | 
|---|
| 31 | #include <stdlib.h> | 
|---|
| 32 | #include <assert.h> | 
|---|
| 33 | #include "intmap.h" | 
|---|
| 34 | #include "list.h" | 
|---|
| 35 | #include "mytypes.h" | 
|---|
| 36 | #include "stree.h" | 
|---|
| 37 | #include "strtab.h" | 
|---|
| 38 | #include "symbol.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "tdata.h" | 
|---|
| 41 |  | 
|---|
| 42 | static void tdata_item_subst_tprimitive(tdata_primitive_t *torig, | 
|---|
| 43 | tdata_tvv_t *tvv, tdata_item_t **res); | 
|---|
| 44 | static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv, | 
|---|
| 45 | tdata_item_t **res); | 
|---|
| 46 | static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv, | 
|---|
| 47 | tdata_item_t **res); | 
|---|
| 48 | static void tdata_item_subst_tdeleg(tdata_deleg_t *torig, | 
|---|
| 49 | tdata_tvv_t *tvv, tdata_item_t **res); | 
|---|
| 50 | static void tdata_item_subst_tebase(tdata_ebase_t *tebase, | 
|---|
| 51 | tdata_tvv_t *tvv, tdata_item_t **res); | 
|---|
| 52 | static void tdata_item_subst_tenum(tdata_enum_t *tenum, | 
|---|
| 53 | tdata_tvv_t *tvv, tdata_item_t **res); | 
|---|
| 54 | static void tdata_item_subst_tfun(tdata_fun_t *torig, | 
|---|
| 55 | tdata_tvv_t *tvv, tdata_item_t **res); | 
|---|
| 56 | static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv, | 
|---|
| 57 | tdata_item_t **res); | 
|---|
| 58 |  | 
|---|
| 59 | static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv, | 
|---|
| 60 | tdata_fun_sig_t **res); | 
|---|
| 61 |  | 
|---|
| 62 | static void tdata_tprimitive_print(tdata_primitive_t *tprimitive); | 
|---|
| 63 | static void tdata_tobject_print(tdata_object_t *tobject); | 
|---|
| 64 | static void tdata_tarray_print(tdata_array_t *tarray); | 
|---|
| 65 | static void tdata_tdeleg_print(tdata_deleg_t *tdeleg); | 
|---|
| 66 | static void tdata_tebase_print(tdata_ebase_t *tebase); | 
|---|
| 67 | static void tdata_tenum_print(tdata_enum_t *tenum); | 
|---|
| 68 | static void tdata_tfun_print(tdata_fun_t *tfun); | 
|---|
| 69 | static void tdata_tvref_print(tdata_vref_t *tvref); | 
|---|
| 70 |  | 
|---|
| 71 | /** Determine if CSI @a a is derived from CSI described by type item @a tb. | 
|---|
| 72 | * | 
|---|
| 73 | * XXX This won't work with generics. | 
|---|
| 74 | * | 
|---|
| 75 | * @param a     Potential derived CSI. | 
|---|
| 76 | * @param tb    Type of potentail base CSI. | 
|---|
| 77 | */ | 
|---|
| 78 | bool_t tdata_is_csi_derived_from_ti(stree_csi_t *a, tdata_item_t *tb) | 
|---|
| 79 | { | 
|---|
| 80 | bool_t res; | 
|---|
| 81 |  | 
|---|
| 82 | switch (tb->tic) { | 
|---|
| 83 | case tic_tobject: | 
|---|
| 84 | res = stree_is_csi_derived_from_csi(a, tb->u.tobject->csi); | 
|---|
| 85 | break; | 
|---|
| 86 | default: | 
|---|
| 87 | printf("Error: Base type is not a CSI.\n"); | 
|---|
| 88 | exit(1); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | return res; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 | * Determine if CSI described by type item @a a is derived from CSI described | 
|---|
| 96 | * by type item @a tb. | 
|---|
| 97 | * | 
|---|
| 98 | * XXX This is somewhat complementary to stype_convert(). It is used for | 
|---|
| 99 | * the explicit @c as conversion. It should only work for objects and only | 
|---|
| 100 | * allow conversion from base to derived types. We might want to scrap this | 
|---|
| 101 | * for a version specific to @c as. The current code does not work with | 
|---|
| 102 | * generics. | 
|---|
| 103 | * | 
|---|
| 104 | * @param a     Potential derived CSI. | 
|---|
| 105 | * @param tb    Type of potentail base CSI. | 
|---|
| 106 | */ | 
|---|
| 107 | bool_t tdata_is_ti_derived_from_ti(tdata_item_t *ta, tdata_item_t *tb) | 
|---|
| 108 | { | 
|---|
| 109 | bool_t res; | 
|---|
| 110 |  | 
|---|
| 111 | switch (ta->tic) { | 
|---|
| 112 | case tic_tobject: | 
|---|
| 113 | res = tdata_is_csi_derived_from_ti(ta->u.tobject->csi, tb); | 
|---|
| 114 | break; | 
|---|
| 115 | default: | 
|---|
| 116 | printf("Error: Derived type is not a CSI.\n"); | 
|---|
| 117 | exit(1); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | return res; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /** Determine if two type items are equal (i.e. describe the same type). | 
|---|
| 124 | * | 
|---|
| 125 | * Needed to check compatibility of type arguments in which a parametrized | 
|---|
| 126 | * type is not monotonous. | 
|---|
| 127 | * | 
|---|
| 128 | * @param a     Type item | 
|---|
| 129 | * @param b     Type item | 
|---|
| 130 | * @return      @c b_true if equal, @c b_false if not. | 
|---|
| 131 | */ | 
|---|
| 132 | bool_t tdata_item_equal(tdata_item_t *a, tdata_item_t *b) | 
|---|
| 133 | { | 
|---|
| 134 | /* | 
|---|
| 135 | * Special case: Nil vs. object | 
|---|
| 136 | * | 
|---|
| 137 | * XXX Type of @c Nil should probably be @c object to avoid this | 
|---|
| 138 | * madness. | 
|---|
| 139 | */ | 
|---|
| 140 | if (a->tic == tic_tprimitive && a->u.tprimitive->tpc == tpc_nil) { | 
|---|
| 141 | if (b->tic == tic_tobject) | 
|---|
| 142 | return b_true; | 
|---|
| 143 | } else if (b->tic == tic_tprimitive && b->u.tprimitive->tpc == tpc_nil) { | 
|---|
| 144 | if (a->tic == tic_tobject) | 
|---|
| 145 | return b_true; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | if (a->tic != b->tic) | 
|---|
| 149 | return b_false; | 
|---|
| 150 |  | 
|---|
| 151 | switch (a->tic) { | 
|---|
| 152 | case tic_tprimitive: | 
|---|
| 153 | /* Check if both have the same tprimitive class. */ | 
|---|
| 154 | return (a->u.tprimitive->tpc == b->u.tprimitive->tpc); | 
|---|
| 155 | case tic_tobject: | 
|---|
| 156 | /* Check if both use the same CSI definition. */ | 
|---|
| 157 | return (a->u.tobject->csi == b->u.tobject->csi); | 
|---|
| 158 | case tic_tarray: | 
|---|
| 159 | /* Compare rank and base type. */ | 
|---|
| 160 | if (a->u.tarray->rank != b->u.tarray->rank) | 
|---|
| 161 | return b_false; | 
|---|
| 162 |  | 
|---|
| 163 | return tdata_item_equal(a->u.tarray->base_ti, | 
|---|
| 164 | b->u.tarray->base_ti); | 
|---|
| 165 | case tic_tenum: | 
|---|
| 166 | /* Check if both use the same enum definition. */ | 
|---|
| 167 | return (a->u.tenum->enum_d == b->u.tenum->enum_d); | 
|---|
| 168 | case tic_tvref: | 
|---|
| 169 | /* Check if both refer to the same type argument. */ | 
|---|
| 170 | return (a->u.tvref->targ == b->u.tvref->targ); | 
|---|
| 171 | default: | 
|---|
| 172 | printf("Warning: Unimplemented: Compare types '"); | 
|---|
| 173 | tdata_item_print(a); | 
|---|
| 174 | printf("' and '"); | 
|---|
| 175 | tdata_item_print(b); | 
|---|
| 176 | printf("'.\n"); | 
|---|
| 177 | return b_true; | 
|---|
| 178 | } | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /** Substitute type variables in a type item. | 
|---|
| 182 | * | 
|---|
| 183 | * This is the second part of generic type application. In the first part | 
|---|
| 184 | * obtained a TVV using stype_titem_to_tvv() and in this second part we | 
|---|
| 185 | * actually substitute type variables in a type item for their values. | 
|---|
| 186 | * @a tvv must contain all variables referenced in @a ti. | 
|---|
| 187 | * | 
|---|
| 188 | * @param ti    Type item to substitute into. | 
|---|
| 189 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 190 | * @param res   Place to store pointer to new type item. | 
|---|
| 191 | */ | 
|---|
| 192 | void tdata_item_subst(tdata_item_t *ti, tdata_tvv_t *tvv, tdata_item_t **res) | 
|---|
| 193 | { | 
|---|
| 194 | switch (ti->tic) { | 
|---|
| 195 | case tic_tprimitive: | 
|---|
| 196 | tdata_item_subst_tprimitive(ti->u.tprimitive, tvv, res); | 
|---|
| 197 | break; | 
|---|
| 198 | case tic_tobject: | 
|---|
| 199 | tdata_item_subst_tobject(ti->u.tobject, tvv, res); | 
|---|
| 200 | break; | 
|---|
| 201 | case tic_tarray: | 
|---|
| 202 | tdata_item_subst_tarray(ti->u.tarray, tvv, res); | 
|---|
| 203 | break; | 
|---|
| 204 | case tic_tdeleg: | 
|---|
| 205 | tdata_item_subst_tdeleg(ti->u.tdeleg, tvv, res); | 
|---|
| 206 | break; | 
|---|
| 207 | case tic_tebase: | 
|---|
| 208 | tdata_item_subst_tebase(ti->u.tebase, tvv, res); | 
|---|
| 209 | break; | 
|---|
| 210 | case tic_tenum: | 
|---|
| 211 | tdata_item_subst_tenum(ti->u.tenum, tvv, res); | 
|---|
| 212 | break; | 
|---|
| 213 | case tic_tfun: | 
|---|
| 214 | tdata_item_subst_tfun(ti->u.tfun, tvv, res); | 
|---|
| 215 | break; | 
|---|
| 216 | case tic_tvref: | 
|---|
| 217 | tdata_item_subst_tvref(ti->u.tvref, tvv, res); | 
|---|
| 218 | break; | 
|---|
| 219 | case tic_ignore: | 
|---|
| 220 | *res = tdata_item_new(tic_ignore); | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | /** Substitute type variables in a primitive type item. | 
|---|
| 225 | * | 
|---|
| 226 | * @param torig Type item to substitute into. | 
|---|
| 227 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 228 | * @param res   Place to store pointer to new type item. | 
|---|
| 229 | */ | 
|---|
| 230 | static void tdata_item_subst_tprimitive(tdata_primitive_t *torig, | 
|---|
| 231 | tdata_tvv_t *tvv, tdata_item_t **res) | 
|---|
| 232 | { | 
|---|
| 233 | tdata_primitive_t *tnew; | 
|---|
| 234 |  | 
|---|
| 235 | (void) tvv; | 
|---|
| 236 |  | 
|---|
| 237 | /* Plain copy */ | 
|---|
| 238 | tnew = tdata_primitive_new(torig->tpc); | 
|---|
| 239 | *res = tdata_item_new(tic_tprimitive); | 
|---|
| 240 | (*res)->u.tprimitive = tnew; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | /** Substitute type variables in an object type item. | 
|---|
| 244 | * | 
|---|
| 245 | * @param torig Type item to substitute into. | 
|---|
| 246 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 247 | * @param res   Place to store pointer to new type item. | 
|---|
| 248 | */ | 
|---|
| 249 | static void tdata_item_subst_tobject(tdata_object_t *torig, tdata_tvv_t *tvv, | 
|---|
| 250 | tdata_item_t **res) | 
|---|
| 251 | { | 
|---|
| 252 | tdata_object_t *tnew; | 
|---|
| 253 | list_node_t *targ_n; | 
|---|
| 254 | tdata_item_t *targ; | 
|---|
| 255 | tdata_item_t *new_targ; | 
|---|
| 256 |  | 
|---|
| 257 | /* Copy static ref flag and base CSI. */ | 
|---|
| 258 | tnew = tdata_object_new(); | 
|---|
| 259 | tnew->static_ref = torig->static_ref; | 
|---|
| 260 | tnew->csi = torig->csi; | 
|---|
| 261 | list_init(&tnew->targs); | 
|---|
| 262 |  | 
|---|
| 263 | /* Substitute arguments */ | 
|---|
| 264 | targ_n = list_first(&torig->targs); | 
|---|
| 265 | while (targ_n != NULL) { | 
|---|
| 266 | targ = list_node_data(targ_n, tdata_item_t *); | 
|---|
| 267 | tdata_item_subst(targ, tvv, &new_targ); | 
|---|
| 268 | list_append(&tnew->targs, new_targ); | 
|---|
| 269 |  | 
|---|
| 270 | targ_n = list_next(&torig->targs, targ_n); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | *res = tdata_item_new(tic_tobject); | 
|---|
| 274 | (*res)->u.tobject = tnew; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | /** Substitute type variables in an array type item. | 
|---|
| 278 | * | 
|---|
| 279 | * @param torig Type item to substitute into. | 
|---|
| 280 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 281 | * @param res   Place to store pointer to new type item. | 
|---|
| 282 | */ | 
|---|
| 283 | static void tdata_item_subst_tarray(tdata_array_t *torig, tdata_tvv_t *tvv, | 
|---|
| 284 | tdata_item_t **res) | 
|---|
| 285 | { | 
|---|
| 286 | tdata_array_t *tnew; | 
|---|
| 287 | list_node_t *ext_n; | 
|---|
| 288 | stree_expr_t *extent; | 
|---|
| 289 |  | 
|---|
| 290 | tnew = tdata_array_new(); | 
|---|
| 291 |  | 
|---|
| 292 | /* Substitute base type */ | 
|---|
| 293 | tdata_item_subst(torig->base_ti, tvv, &tnew->base_ti); | 
|---|
| 294 |  | 
|---|
| 295 | /* Copy rank and extents */ | 
|---|
| 296 | tnew->rank = torig->rank; | 
|---|
| 297 | list_init(&tnew->extents); | 
|---|
| 298 |  | 
|---|
| 299 | ext_n = list_first(&torig->extents); | 
|---|
| 300 | while (ext_n != NULL) { | 
|---|
| 301 | extent = list_node_data(ext_n, stree_expr_t *); | 
|---|
| 302 | list_append(&tnew->extents, extent); | 
|---|
| 303 |  | 
|---|
| 304 | ext_n = list_next(&tnew->extents, ext_n); | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | *res = tdata_item_new(tic_tarray); | 
|---|
| 308 | (*res)->u.tarray = tnew; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | /** Substitute type variables in a delegate type item. | 
|---|
| 312 | * | 
|---|
| 313 | * @param torig Type item to substitute into. | 
|---|
| 314 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 315 | * @param res   Place to store pointer to new type item. | 
|---|
| 316 | */ | 
|---|
| 317 | static void tdata_item_subst_tdeleg(tdata_deleg_t *torig, tdata_tvv_t *tvv, | 
|---|
| 318 | tdata_item_t **res) | 
|---|
| 319 | { | 
|---|
| 320 | tdata_deleg_t *tnew; | 
|---|
| 321 |  | 
|---|
| 322 | tnew = tdata_deleg_new(); | 
|---|
| 323 | tnew->deleg = torig->deleg; | 
|---|
| 324 | tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig); | 
|---|
| 325 |  | 
|---|
| 326 | *res = tdata_item_new(tic_tdeleg); | 
|---|
| 327 | (*res)->u.tdeleg = tnew; | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | /** Substitute type variables in a enum-base type item. | 
|---|
| 331 | * | 
|---|
| 332 | * @param torig Type item to substitute into. | 
|---|
| 333 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 334 | * @param res   Place to store pointer to new type item. | 
|---|
| 335 | */ | 
|---|
| 336 | static void tdata_item_subst_tebase(tdata_ebase_t *tebase, | 
|---|
| 337 | tdata_tvv_t *tvv, tdata_item_t **res) | 
|---|
| 338 | { | 
|---|
| 339 | tdata_ebase_t *tnew; | 
|---|
| 340 |  | 
|---|
| 341 | (void) tvv; | 
|---|
| 342 |  | 
|---|
| 343 | /* Plain copy */ | 
|---|
| 344 | tnew = tdata_ebase_new(); | 
|---|
| 345 | tnew->enum_d = tebase->enum_d; | 
|---|
| 346 |  | 
|---|
| 347 | *res = tdata_item_new(tic_tebase); | 
|---|
| 348 | (*res)->u.tebase = tnew; | 
|---|
| 349 | } | 
|---|
| 350 |  | 
|---|
| 351 | /** Substitute type variables in a enum type item. | 
|---|
| 352 | * | 
|---|
| 353 | * @param torig Type item to substitute into. | 
|---|
| 354 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 355 | * @param res   Place to store pointer to new type item. | 
|---|
| 356 | */ | 
|---|
| 357 | static void tdata_item_subst_tenum(tdata_enum_t *tenum, | 
|---|
| 358 | tdata_tvv_t *tvv, tdata_item_t **res) | 
|---|
| 359 | { | 
|---|
| 360 | tdata_enum_t *tnew; | 
|---|
| 361 |  | 
|---|
| 362 | (void) tvv; | 
|---|
| 363 |  | 
|---|
| 364 | /* Plain copy */ | 
|---|
| 365 | tnew = tdata_enum_new(); | 
|---|
| 366 | tnew->enum_d = tenum->enum_d; | 
|---|
| 367 |  | 
|---|
| 368 | *res = tdata_item_new(tic_tenum); | 
|---|
| 369 | (*res)->u.tenum = tnew; | 
|---|
| 370 | } | 
|---|
| 371 |  | 
|---|
| 372 | /** Substitute type variables in a functional type item. | 
|---|
| 373 | * | 
|---|
| 374 | * @param torig Type item to substitute into. | 
|---|
| 375 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 376 | * @param res   Place to store pointer to new type item. | 
|---|
| 377 | */ | 
|---|
| 378 | static void tdata_item_subst_tfun(tdata_fun_t *torig, tdata_tvv_t *tvv, | 
|---|
| 379 | tdata_item_t **res) | 
|---|
| 380 | { | 
|---|
| 381 | tdata_fun_t *tnew; | 
|---|
| 382 |  | 
|---|
| 383 | tnew = tdata_fun_new(); | 
|---|
| 384 | tdata_item_subst_fun_sig(torig->tsig, tvv, &tnew->tsig); | 
|---|
| 385 |  | 
|---|
| 386 | *res = tdata_item_new(tic_tfun); | 
|---|
| 387 | (*res)->u.tfun = tnew; | 
|---|
| 388 | } | 
|---|
| 389 |  | 
|---|
| 390 | /** Substitute type variables in a type-variable reference item. | 
|---|
| 391 | * | 
|---|
| 392 | * @param torig Type item to substitute into. | 
|---|
| 393 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 394 | * @param res   Place to store pointer to new type item. | 
|---|
| 395 | */ | 
|---|
| 396 | static void tdata_item_subst_tvref(tdata_vref_t *tvref, tdata_tvv_t *tvv, | 
|---|
| 397 | tdata_item_t **res) | 
|---|
| 398 | { | 
|---|
| 399 | tdata_item_t *ti_new; | 
|---|
| 400 |  | 
|---|
| 401 | ti_new = tdata_tvv_get_val(tvv, tvref->targ->name->sid); | 
|---|
| 402 | assert(ti_new != NULL); | 
|---|
| 403 |  | 
|---|
| 404 | /* XXX Might be better to clone here. */ | 
|---|
| 405 | *res = ti_new; | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | /** Substitute type variables in a function signature type fragment. | 
|---|
| 409 | * | 
|---|
| 410 | * @param torig Type item to substitute into. | 
|---|
| 411 | * @param tvv   Type variable valuation (values of type variables). | 
|---|
| 412 | * @param res   Place to store pointer to new type item. | 
|---|
| 413 | */ | 
|---|
| 414 | static void tdata_item_subst_fun_sig(tdata_fun_sig_t *torig, tdata_tvv_t *tvv, | 
|---|
| 415 | tdata_fun_sig_t **res) | 
|---|
| 416 | { | 
|---|
| 417 | tdata_fun_sig_t *tnew; | 
|---|
| 418 | list_node_t *arg_n; | 
|---|
| 419 | tdata_item_t *arg_ti; | 
|---|
| 420 | tdata_item_t *narg_ti; | 
|---|
| 421 |  | 
|---|
| 422 | tnew = tdata_fun_sig_new(); | 
|---|
| 423 |  | 
|---|
| 424 | /* Substitute type of each argument */ | 
|---|
| 425 | list_init(&tnew->arg_ti); | 
|---|
| 426 | arg_n = list_first(&torig->arg_ti); | 
|---|
| 427 | while (arg_n != NULL) { | 
|---|
| 428 | arg_ti = list_node_data(arg_n, tdata_item_t *); | 
|---|
| 429 |  | 
|---|
| 430 | /* XXX Because of overloaded Builtin.WriteLine */ | 
|---|
| 431 | if (arg_ti == NULL) | 
|---|
| 432 | narg_ti = NULL; | 
|---|
| 433 | else | 
|---|
| 434 | tdata_item_subst(arg_ti, tvv, &narg_ti); | 
|---|
| 435 |  | 
|---|
| 436 | list_append(&tnew->arg_ti, narg_ti); | 
|---|
| 437 |  | 
|---|
| 438 | arg_n = list_next(&torig->arg_ti, arg_n); | 
|---|
| 439 | } | 
|---|
| 440 |  | 
|---|
| 441 | /* Substitute type of variadic argument */ | 
|---|
| 442 | if (torig->varg_ti != NULL) | 
|---|
| 443 | tdata_item_subst(torig->varg_ti, tvv, &tnew->varg_ti); | 
|---|
| 444 |  | 
|---|
| 445 | /* Substitute return type */ | 
|---|
| 446 | if (torig->rtype != NULL) | 
|---|
| 447 | tdata_item_subst(torig->rtype, tvv, &tnew->rtype); | 
|---|
| 448 |  | 
|---|
| 449 | *res = tnew; | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 |  | 
|---|
| 453 | /** Print type item. | 
|---|
| 454 | * | 
|---|
| 455 | * @param titem Type item | 
|---|
| 456 | */ | 
|---|
| 457 | void tdata_item_print(tdata_item_t *titem) | 
|---|
| 458 | { | 
|---|
| 459 | if (titem == NULL) { | 
|---|
| 460 | printf("none"); | 
|---|
| 461 | return; | 
|---|
| 462 | } | 
|---|
| 463 |  | 
|---|
| 464 | switch (titem->tic) { | 
|---|
| 465 | case tic_tprimitive: | 
|---|
| 466 | tdata_tprimitive_print(titem->u.tprimitive); | 
|---|
| 467 | break; | 
|---|
| 468 | case tic_tobject: | 
|---|
| 469 | tdata_tobject_print(titem->u.tobject); | 
|---|
| 470 | break; | 
|---|
| 471 | case tic_tarray: | 
|---|
| 472 | tdata_tarray_print(titem->u.tarray); | 
|---|
| 473 | break; | 
|---|
| 474 | case tic_tdeleg: | 
|---|
| 475 | tdata_tdeleg_print(titem->u.tdeleg); | 
|---|
| 476 | break; | 
|---|
| 477 | case tic_tebase: | 
|---|
| 478 | tdata_tebase_print(titem->u.tebase); | 
|---|
| 479 | break; | 
|---|
| 480 | case tic_tenum: | 
|---|
| 481 | tdata_tenum_print(titem->u.tenum); | 
|---|
| 482 | break; | 
|---|
| 483 | case tic_tfun: | 
|---|
| 484 | tdata_tfun_print(titem->u.tfun); | 
|---|
| 485 | break; | 
|---|
| 486 | case tic_tvref: | 
|---|
| 487 | tdata_tvref_print(titem->u.tvref); | 
|---|
| 488 | break; | 
|---|
| 489 | case tic_ignore: | 
|---|
| 490 | printf("ignore"); | 
|---|
| 491 | break; | 
|---|
| 492 | } | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | /** Print primitive type item. | 
|---|
| 496 | * | 
|---|
| 497 | * @param tprimitive    Primitive type item | 
|---|
| 498 | */ | 
|---|
| 499 | static void tdata_tprimitive_print(tdata_primitive_t *tprimitive) | 
|---|
| 500 | { | 
|---|
| 501 | switch (tprimitive->tpc) { | 
|---|
| 502 | case tpc_bool: printf("bool"); break; | 
|---|
| 503 | case tpc_char: printf("char"); break; | 
|---|
| 504 | case tpc_int: printf("int"); break; | 
|---|
| 505 | case tpc_nil: printf("nil"); break; | 
|---|
| 506 | case tpc_string: printf("string"); break; | 
|---|
| 507 | case tpc_resource: printf("resource"); break; | 
|---|
| 508 | } | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | /** Print object type item. | 
|---|
| 512 | * | 
|---|
| 513 | * @param tobject       Object type item | 
|---|
| 514 | */ | 
|---|
| 515 | static void tdata_tobject_print(tdata_object_t *tobject) | 
|---|
| 516 | { | 
|---|
| 517 | stree_symbol_t *csi_sym; | 
|---|
| 518 | list_node_t *arg_n; | 
|---|
| 519 | tdata_item_t *arg; | 
|---|
| 520 |  | 
|---|
| 521 | csi_sym = csi_to_symbol(tobject->csi); | 
|---|
| 522 | assert(csi_sym != NULL); | 
|---|
| 523 | symbol_print_fqn(csi_sym); | 
|---|
| 524 |  | 
|---|
| 525 | arg_n = list_first(&tobject->targs); | 
|---|
| 526 | while (arg_n != NULL) { | 
|---|
| 527 | arg = list_node_data(arg_n, tdata_item_t *); | 
|---|
| 528 | putchar('/'); | 
|---|
| 529 | tdata_item_print(arg); | 
|---|
| 530 | arg_n = list_next(&tobject->targs, arg_n); | 
|---|
| 531 | } | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 | /** Print array type item. | 
|---|
| 535 | * | 
|---|
| 536 | * @param tarray        Array type item | 
|---|
| 537 | */ | 
|---|
| 538 | static void tdata_tarray_print(tdata_array_t *tarray) | 
|---|
| 539 | { | 
|---|
| 540 | int i; | 
|---|
| 541 |  | 
|---|
| 542 | tdata_item_print(tarray->base_ti); | 
|---|
| 543 |  | 
|---|
| 544 | printf("["); | 
|---|
| 545 | for (i = 0; i < tarray->rank - 1; ++i) | 
|---|
| 546 | printf(","); | 
|---|
| 547 | printf("]"); | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 | /** Print delegate type item. | 
|---|
| 551 | * | 
|---|
| 552 | * @param tdeleg        Delegate type item | 
|---|
| 553 | */ | 
|---|
| 554 | static void tdata_tdeleg_print(tdata_deleg_t *tdeleg) | 
|---|
| 555 | { | 
|---|
| 556 | stree_symbol_t *deleg_sym; | 
|---|
| 557 |  | 
|---|
| 558 | deleg_sym = deleg_to_symbol(tdeleg->deleg); | 
|---|
| 559 | symbol_print_fqn(deleg_sym); | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | /** Print enum-base type item. | 
|---|
| 563 | * | 
|---|
| 564 | * @param tebase                Enum-base type item | 
|---|
| 565 | */ | 
|---|
| 566 | static void tdata_tebase_print(tdata_ebase_t *tebase) | 
|---|
| 567 | { | 
|---|
| 568 | stree_symbol_t *enum_sym; | 
|---|
| 569 |  | 
|---|
| 570 | enum_sym = enum_to_symbol(tebase->enum_d); | 
|---|
| 571 |  | 
|---|
| 572 | printf("typeref("); | 
|---|
| 573 | symbol_print_fqn(enum_sym); | 
|---|
| 574 | printf(")"); | 
|---|
| 575 | } | 
|---|
| 576 |  | 
|---|
| 577 | /** Print enum type item. | 
|---|
| 578 | * | 
|---|
| 579 | * @param tenum         Enum type item | 
|---|
| 580 | */ | 
|---|
| 581 | static void tdata_tenum_print(tdata_enum_t *tenum) | 
|---|
| 582 | { | 
|---|
| 583 | stree_symbol_t *enum_sym; | 
|---|
| 584 |  | 
|---|
| 585 | enum_sym = enum_to_symbol(tenum->enum_d); | 
|---|
| 586 | symbol_print_fqn(enum_sym); | 
|---|
| 587 | } | 
|---|
| 588 |  | 
|---|
| 589 | /** Print function type item. | 
|---|
| 590 | * | 
|---|
| 591 | * @param tfun          Function type item | 
|---|
| 592 | */ | 
|---|
| 593 | static void tdata_tfun_print(tdata_fun_t *tfun) | 
|---|
| 594 | { | 
|---|
| 595 | list_node_t *arg_n; | 
|---|
| 596 | tdata_item_t *arg_ti; | 
|---|
| 597 | bool_t first; | 
|---|
| 598 |  | 
|---|
| 599 | printf("fun("); | 
|---|
| 600 |  | 
|---|
| 601 | arg_n = list_first(&tfun->tsig->arg_ti); | 
|---|
| 602 | first = b_true; | 
|---|
| 603 | while (arg_n != NULL) { | 
|---|
| 604 | if (first == b_false) | 
|---|
| 605 | printf("; "); | 
|---|
| 606 | else | 
|---|
| 607 | first = b_false; | 
|---|
| 608 |  | 
|---|
| 609 | arg_ti = list_node_data(arg_n, tdata_item_t *); | 
|---|
| 610 | tdata_item_print(arg_ti); | 
|---|
| 611 |  | 
|---|
| 612 | arg_n = list_next(&tfun->tsig->arg_ti, arg_n); | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | printf(") : "); | 
|---|
| 616 | tdata_item_print(tfun->tsig->rtype); | 
|---|
| 617 | } | 
|---|
| 618 |  | 
|---|
| 619 | /** Print type variable reference type item. | 
|---|
| 620 | * | 
|---|
| 621 | * @param tvref         Type variable reference type item | 
|---|
| 622 | */ | 
|---|
| 623 | static void tdata_tvref_print(tdata_vref_t *tvref) | 
|---|
| 624 | { | 
|---|
| 625 | printf("%s", strtab_get_str(tvref->targ->name->sid)); | 
|---|
| 626 | } | 
|---|
| 627 |  | 
|---|
| 628 | /** Allocate new type item. | 
|---|
| 629 | * | 
|---|
| 630 | * @param tic   Type item class | 
|---|
| 631 | * @return      New type item | 
|---|
| 632 | */ | 
|---|
| 633 | tdata_item_t *tdata_item_new(titem_class_t tic) | 
|---|
| 634 | { | 
|---|
| 635 | tdata_item_t *titem; | 
|---|
| 636 |  | 
|---|
| 637 | titem = calloc(1, sizeof(tdata_item_t)); | 
|---|
| 638 | if (titem == NULL) { | 
|---|
| 639 | printf("Memory allocation failed.\n"); | 
|---|
| 640 | exit(1); | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | titem->tic = tic; | 
|---|
| 644 | return titem; | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | /** Allocate new array type item. | 
|---|
| 648 | * | 
|---|
| 649 | * @return      New array type item | 
|---|
| 650 | */ | 
|---|
| 651 | tdata_array_t *tdata_array_new(void) | 
|---|
| 652 | { | 
|---|
| 653 | tdata_array_t *tarray; | 
|---|
| 654 |  | 
|---|
| 655 | tarray = calloc(1, sizeof(tdata_array_t)); | 
|---|
| 656 | if (tarray == NULL) { | 
|---|
| 657 | printf("Memory allocation failed.\n"); | 
|---|
| 658 | exit(1); | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 | return tarray; | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | /** Allocate new object type item. | 
|---|
| 665 | * | 
|---|
| 666 | * @return      New object type item | 
|---|
| 667 | */ | 
|---|
| 668 | tdata_object_t *tdata_object_new(void) | 
|---|
| 669 | { | 
|---|
| 670 | tdata_object_t *tobject; | 
|---|
| 671 |  | 
|---|
| 672 | tobject = calloc(1, sizeof(tdata_object_t)); | 
|---|
| 673 | if (tobject == NULL) { | 
|---|
| 674 | printf("Memory allocation failed.\n"); | 
|---|
| 675 | exit(1); | 
|---|
| 676 | } | 
|---|
| 677 |  | 
|---|
| 678 | return tobject; | 
|---|
| 679 | } | 
|---|
| 680 |  | 
|---|
| 681 | /** Allocate new primitive type item. | 
|---|
| 682 | * | 
|---|
| 683 | * @return      New primitive type item | 
|---|
| 684 | */ | 
|---|
| 685 | tdata_primitive_t *tdata_primitive_new(tprimitive_class_t tpc) | 
|---|
| 686 | { | 
|---|
| 687 | tdata_primitive_t *tprimitive; | 
|---|
| 688 |  | 
|---|
| 689 | tprimitive = calloc(1, sizeof(tdata_primitive_t)); | 
|---|
| 690 | if (tprimitive == NULL) { | 
|---|
| 691 | printf("Memory allocation failed.\n"); | 
|---|
| 692 | exit(1); | 
|---|
| 693 | } | 
|---|
| 694 |  | 
|---|
| 695 | tprimitive->tpc = tpc; | 
|---|
| 696 | return tprimitive; | 
|---|
| 697 | } | 
|---|
| 698 |  | 
|---|
| 699 | /** Allocate new delegate type item. | 
|---|
| 700 | * | 
|---|
| 701 | * @return      New function type item | 
|---|
| 702 | */ | 
|---|
| 703 | tdata_deleg_t *tdata_deleg_new(void) | 
|---|
| 704 | { | 
|---|
| 705 | tdata_deleg_t *tdeleg; | 
|---|
| 706 |  | 
|---|
| 707 | tdeleg = calloc(1, sizeof(tdata_deleg_t)); | 
|---|
| 708 | if (tdeleg == NULL) { | 
|---|
| 709 | printf("Memory allocation failed.\n"); | 
|---|
| 710 | exit(1); | 
|---|
| 711 | } | 
|---|
| 712 |  | 
|---|
| 713 | return tdeleg; | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | /** Allocate new enum-base type item. | 
|---|
| 717 | * | 
|---|
| 718 | * @return      New enum type item | 
|---|
| 719 | */ | 
|---|
| 720 | tdata_ebase_t *tdata_ebase_new(void) | 
|---|
| 721 | { | 
|---|
| 722 | tdata_ebase_t *tebase; | 
|---|
| 723 |  | 
|---|
| 724 | tebase = calloc(1, sizeof(tdata_ebase_t)); | 
|---|
| 725 | if (tebase == NULL) { | 
|---|
| 726 | printf("Memory allocation failed.\n"); | 
|---|
| 727 | exit(1); | 
|---|
| 728 | } | 
|---|
| 729 |  | 
|---|
| 730 | return tebase; | 
|---|
| 731 | } | 
|---|
| 732 |  | 
|---|
| 733 | /** Allocate new enum type item. | 
|---|
| 734 | * | 
|---|
| 735 | * @return      New enum type item | 
|---|
| 736 | */ | 
|---|
| 737 | tdata_enum_t *tdata_enum_new(void) | 
|---|
| 738 | { | 
|---|
| 739 | tdata_enum_t *tenum; | 
|---|
| 740 |  | 
|---|
| 741 | tenum = calloc(1, sizeof(tdata_enum_t)); | 
|---|
| 742 | if (tenum == NULL) { | 
|---|
| 743 | printf("Memory allocation failed.\n"); | 
|---|
| 744 | exit(1); | 
|---|
| 745 | } | 
|---|
| 746 |  | 
|---|
| 747 | return tenum; | 
|---|
| 748 | } | 
|---|
| 749 |  | 
|---|
| 750 | /** Allocate new functional type item. | 
|---|
| 751 | * | 
|---|
| 752 | * @return      New function type item | 
|---|
| 753 | */ | 
|---|
| 754 | tdata_fun_t *tdata_fun_new(void) | 
|---|
| 755 | { | 
|---|
| 756 | tdata_fun_t *tfun; | 
|---|
| 757 |  | 
|---|
| 758 | tfun = calloc(1, sizeof(tdata_fun_t)); | 
|---|
| 759 | if (tfun == NULL) { | 
|---|
| 760 | printf("Memory allocation failed.\n"); | 
|---|
| 761 | exit(1); | 
|---|
| 762 | } | 
|---|
| 763 |  | 
|---|
| 764 | return tfun; | 
|---|
| 765 | } | 
|---|
| 766 |  | 
|---|
| 767 | /** Allocate new type variable reference type item. | 
|---|
| 768 | * | 
|---|
| 769 | * @return      New type variable reference type item | 
|---|
| 770 | */ | 
|---|
| 771 | tdata_vref_t *tdata_vref_new(void) | 
|---|
| 772 | { | 
|---|
| 773 | tdata_vref_t *tvref; | 
|---|
| 774 |  | 
|---|
| 775 | tvref = calloc(1, sizeof(tdata_vref_t)); | 
|---|
| 776 | if (tvref == NULL) { | 
|---|
| 777 | printf("Memory allocation failed.\n"); | 
|---|
| 778 | exit(1); | 
|---|
| 779 | } | 
|---|
| 780 |  | 
|---|
| 781 | return tvref; | 
|---|
| 782 | } | 
|---|
| 783 |  | 
|---|
| 784 | /** Allocate new function signature type fragment. | 
|---|
| 785 | * | 
|---|
| 786 | * @return      New function signature type fragment | 
|---|
| 787 | */ | 
|---|
| 788 | tdata_fun_sig_t *tdata_fun_sig_new(void) | 
|---|
| 789 | { | 
|---|
| 790 | tdata_fun_sig_t *tfun_sig; | 
|---|
| 791 |  | 
|---|
| 792 | tfun_sig = calloc(1, sizeof(tdata_fun_sig_t)); | 
|---|
| 793 | if (tfun_sig == NULL) { | 
|---|
| 794 | printf("Memory allocation failed.\n"); | 
|---|
| 795 | exit(1); | 
|---|
| 796 | } | 
|---|
| 797 |  | 
|---|
| 798 | return tfun_sig; | 
|---|
| 799 | } | 
|---|
| 800 |  | 
|---|
| 801 | /** Create a new type variable valuation. | 
|---|
| 802 | * | 
|---|
| 803 | * @retrun      New type variable valuation | 
|---|
| 804 | */ | 
|---|
| 805 | tdata_tvv_t *tdata_tvv_new(void) | 
|---|
| 806 | { | 
|---|
| 807 | tdata_tvv_t *tvv; | 
|---|
| 808 |  | 
|---|
| 809 | tvv = calloc(1, sizeof(tdata_tvv_t)); | 
|---|
| 810 | if (tvv == NULL) { | 
|---|
| 811 | printf("Memory allocation failed.\n"); | 
|---|
| 812 | exit(1); | 
|---|
| 813 | } | 
|---|
| 814 |  | 
|---|
| 815 | return tvv; | 
|---|
| 816 | } | 
|---|
| 817 |  | 
|---|
| 818 | /** Get type variable value. | 
|---|
| 819 | * | 
|---|
| 820 | * Looks up value of the variable with name SID @a name in type | 
|---|
| 821 | * variable valuation @a tvv. | 
|---|
| 822 | * | 
|---|
| 823 | * @param tvv           Type variable valuation | 
|---|
| 824 | * @param name          Name of the variable (SID) | 
|---|
| 825 | * @return              Value of the type variable (type item) or @c NULL | 
|---|
| 826 | *                      if not defined in @a tvv | 
|---|
| 827 | */ | 
|---|
| 828 | tdata_item_t *tdata_tvv_get_val(tdata_tvv_t *tvv, sid_t name) | 
|---|
| 829 | { | 
|---|
| 830 | return (tdata_item_t *)intmap_get(&tvv->tvv, name); | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | /** Set type variable value. | 
|---|
| 834 | * | 
|---|
| 835 | * Sets the value of variable with name SID @a name in type variable | 
|---|
| 836 | * valuation @a tvv to the value @a tvalue. | 
|---|
| 837 | * | 
|---|
| 838 | * @param tvv           Type variable valuation | 
|---|
| 839 | * @param name          Name of the variable (SID) | 
|---|
| 840 | * @param tvalue        Value to set (type item) or @c NULL to unset | 
|---|
| 841 | */ | 
|---|
| 842 | void tdata_tvv_set_val(tdata_tvv_t *tvv, sid_t name, tdata_item_t *tvalue) | 
|---|
| 843 | { | 
|---|
| 844 | intmap_set(&tvv->tvv, name, tvalue); | 
|---|
| 845 | } | 
|---|