| 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 Evaluates type expressions. */
|
|---|
| 30 |
|
|---|
| 31 | #include <assert.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include "list.h"
|
|---|
| 34 | #include "mytypes.h"
|
|---|
| 35 | #include "strtab.h"
|
|---|
| 36 | #include "symbol.h"
|
|---|
| 37 | #include "tdata.h"
|
|---|
| 38 |
|
|---|
| 39 | #include "run_texpr.h"
|
|---|
| 40 |
|
|---|
| 41 | static void run_taccess(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 42 | stree_taccess_t *taccess, tdata_item_t **res);
|
|---|
| 43 | static void run_tindex(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 44 | stree_tindex_t *tindex, tdata_item_t **res);
|
|---|
| 45 | static void run_tliteral(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 46 | stree_tliteral_t *tliteral, tdata_item_t **res);
|
|---|
| 47 | static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 48 | stree_tnameref_t *tnameref, tdata_item_t **res);
|
|---|
| 49 | static void run_tapply(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 50 | stree_tapply_t *tapply, tdata_item_t **res);
|
|---|
| 51 |
|
|---|
| 52 | void run_texpr(stree_program_t *prog, stree_csi_t *ctx, stree_texpr_t *texpr,
|
|---|
| 53 | tdata_item_t **res)
|
|---|
| 54 | {
|
|---|
| 55 | switch (texpr->tc) {
|
|---|
| 56 | case tc_taccess:
|
|---|
| 57 | run_taccess(prog, ctx, texpr->u.taccess, res);
|
|---|
| 58 | break;
|
|---|
| 59 | case tc_tindex:
|
|---|
| 60 | run_tindex(prog, ctx, texpr->u.tindex, res);
|
|---|
| 61 | break;
|
|---|
| 62 | case tc_tliteral:
|
|---|
| 63 | run_tliteral(prog, ctx, texpr->u.tliteral, res);
|
|---|
| 64 | break;
|
|---|
| 65 | case tc_tnameref:
|
|---|
| 66 | run_tnameref(prog, ctx, texpr->u.tnameref, res);
|
|---|
| 67 | break;
|
|---|
| 68 | case tc_tapply:
|
|---|
| 69 | run_tapply(prog, ctx, texpr->u.tapply, res);
|
|---|
| 70 | break;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | static void run_taccess(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 75 | stree_taccess_t *taccess, tdata_item_t **res)
|
|---|
| 76 | {
|
|---|
| 77 | stree_symbol_t *sym;
|
|---|
| 78 | tdata_item_t *targ_i;
|
|---|
| 79 | tdata_item_t *titem;
|
|---|
| 80 | tdata_object_t *tobject;
|
|---|
| 81 | stree_csi_t *base_csi;
|
|---|
| 82 |
|
|---|
| 83 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 84 | printf("Evaluating type access operation.\n");
|
|---|
| 85 | #endif
|
|---|
| 86 | /* Evaluate base type. */
|
|---|
| 87 | run_texpr(prog, ctx, taccess->arg, &targ_i);
|
|---|
| 88 |
|
|---|
| 89 | if (targ_i->tic == tic_ignore) {
|
|---|
| 90 | *res = tdata_item_new(tic_ignore);
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (targ_i->tic != tic_tobject) {
|
|---|
| 95 | printf("Error: Using '.' with type which is not an object.\n");
|
|---|
| 96 | *res = tdata_item_new(tic_ignore);
|
|---|
| 97 | return;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /* Get base CSI. */
|
|---|
| 101 | base_csi = targ_i->u.tobject->csi;
|
|---|
| 102 |
|
|---|
| 103 | sym = symbol_lookup_in_csi(prog, base_csi, taccess->member_name);
|
|---|
| 104 | if (sym == NULL) {
|
|---|
| 105 | printf("Error: CSI '");
|
|---|
| 106 | symbol_print_fqn(csi_to_symbol(base_csi));
|
|---|
| 107 | printf("' has no member named '%s'.\n",
|
|---|
| 108 | strtab_get_str(taccess->member_name->sid));
|
|---|
| 109 | *res = tdata_item_new(tic_ignore);
|
|---|
| 110 | return;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (sym->sc != sc_csi) {
|
|---|
| 114 | printf("Error: Symbol '");
|
|---|
| 115 | symbol_print_fqn(sym);
|
|---|
| 116 | printf("' is not a CSI.\n");
|
|---|
| 117 | *res = tdata_item_new(tic_ignore);
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /* Construct type item. */
|
|---|
| 122 | titem = tdata_item_new(tic_tobject);
|
|---|
| 123 | tobject = tdata_object_new();
|
|---|
| 124 | titem->u.tobject = tobject;
|
|---|
| 125 |
|
|---|
| 126 | tobject->static_ref = b_false;
|
|---|
| 127 | tobject->csi = sym->u.csi;
|
|---|
| 128 |
|
|---|
| 129 | *res = titem;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static void run_tindex(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 133 | stree_tindex_t *tindex, tdata_item_t **res)
|
|---|
| 134 | {
|
|---|
| 135 | tdata_item_t *base_ti;
|
|---|
| 136 | tdata_item_t *titem;
|
|---|
| 137 | tdata_array_t *tarray;
|
|---|
| 138 | stree_expr_t *arg_expr;
|
|---|
| 139 | list_node_t *arg_node;
|
|---|
| 140 |
|
|---|
| 141 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 142 | printf("Evaluating type index operation.\n");
|
|---|
| 143 | #endif
|
|---|
| 144 | /* Evaluate base type. */
|
|---|
| 145 | run_texpr(prog, ctx, tindex->base_type, &base_ti);
|
|---|
| 146 |
|
|---|
| 147 | if (base_ti->tic == tic_ignore) {
|
|---|
| 148 | *res = tdata_item_new(tic_ignore);
|
|---|
| 149 | return;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* Construct type item. */
|
|---|
| 153 | titem = tdata_item_new(tic_tarray);
|
|---|
| 154 | tarray = tdata_array_new();
|
|---|
| 155 | titem->u.tarray = tarray;
|
|---|
| 156 |
|
|---|
| 157 | tarray->base_ti = base_ti;
|
|---|
| 158 | tarray->rank = tindex->n_args;
|
|---|
| 159 |
|
|---|
| 160 | /* Copy extents. */
|
|---|
| 161 | list_init(&tarray->extents);
|
|---|
| 162 | arg_node = list_first(&tindex->args);
|
|---|
| 163 |
|
|---|
| 164 | while (arg_node != NULL) {
|
|---|
| 165 | arg_expr = list_node_data(arg_node, stree_expr_t *);
|
|---|
| 166 | list_append(&tarray->extents, arg_expr);
|
|---|
| 167 | arg_node = list_next(&tindex->args, arg_node);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | *res = titem;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | static void run_tliteral(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 174 | stree_tliteral_t *tliteral, tdata_item_t **res)
|
|---|
| 175 | {
|
|---|
| 176 | tdata_item_t *titem;
|
|---|
| 177 | tdata_primitive_t *tprimitive;
|
|---|
| 178 | tprimitive_class_t tpc;
|
|---|
| 179 |
|
|---|
| 180 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 181 | printf("Evaluating type literal.\n");
|
|---|
| 182 | #endif
|
|---|
| 183 | (void) prog;
|
|---|
| 184 | (void) ctx;
|
|---|
| 185 | (void) tliteral;
|
|---|
| 186 |
|
|---|
| 187 | switch (tliteral->tlc) {
|
|---|
| 188 | case tlc_bool: tpc = tpc_bool; break;
|
|---|
| 189 | case tlc_char: tpc = tpc_char; break;
|
|---|
| 190 | case tlc_int: tpc = tpc_int; break;
|
|---|
| 191 | case tlc_string: tpc = tpc_string; break;
|
|---|
| 192 | case tlc_resource: tpc = tpc_resource; break;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /* Construct type item. */
|
|---|
| 196 | titem = tdata_item_new(tic_tprimitive);
|
|---|
| 197 | tprimitive = tdata_primitive_new(tpc);
|
|---|
| 198 | titem->u.tprimitive = tprimitive;
|
|---|
| 199 |
|
|---|
| 200 | *res = titem;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 204 | stree_tnameref_t *tnameref, tdata_item_t **res)
|
|---|
| 205 | {
|
|---|
| 206 | stree_symbol_t *sym;
|
|---|
| 207 | tdata_item_t *titem;
|
|---|
| 208 | tdata_object_t *tobject;
|
|---|
| 209 |
|
|---|
| 210 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 211 | printf("Evaluating type name reference.\n");
|
|---|
| 212 | #endif
|
|---|
| 213 | sym = symbol_lookup_in_csi(prog, ctx, tnameref->name);
|
|---|
| 214 | if (sym == NULL) {
|
|---|
| 215 | printf("Error: Symbol '%s' not found.\n",
|
|---|
| 216 | strtab_get_str(tnameref->name->sid));
|
|---|
| 217 | *res = tdata_item_new(tic_ignore);
|
|---|
| 218 | return;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if (sym->sc != sc_csi) {
|
|---|
| 222 | printf("Error: Symbol '");
|
|---|
| 223 | symbol_print_fqn(sym);
|
|---|
| 224 | printf("' is not a CSI.\n");
|
|---|
| 225 | *res = tdata_item_new(tic_ignore);
|
|---|
| 226 | return;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /* Construct type item. */
|
|---|
| 230 | titem = tdata_item_new(tic_tobject);
|
|---|
| 231 | tobject = tdata_object_new();
|
|---|
| 232 | titem->u.tobject = tobject;
|
|---|
| 233 |
|
|---|
| 234 | tobject->static_ref = b_false;
|
|---|
| 235 | tobject->csi = sym->u.csi;
|
|---|
| 236 |
|
|---|
| 237 | *res = titem;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | static void run_tapply(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 241 | stree_tapply_t *tapply, tdata_item_t **res)
|
|---|
| 242 | {
|
|---|
| 243 | tdata_item_t *base_ti;
|
|---|
| 244 | tdata_item_t *arg_ti;
|
|---|
| 245 | tdata_item_t *titem;
|
|---|
| 246 | tdata_object_t *tobject;
|
|---|
| 247 |
|
|---|
| 248 | list_node_t *arg_n;
|
|---|
| 249 | stree_texpr_t *arg;
|
|---|
| 250 |
|
|---|
| 251 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 252 | printf("Evaluating type apply operation.\n");
|
|---|
| 253 | #endif
|
|---|
| 254 | /* Construct type item. */
|
|---|
| 255 | titem = tdata_item_new(tic_tobject);
|
|---|
| 256 | tobject = tdata_object_new();
|
|---|
| 257 | titem->u.tobject = tobject;
|
|---|
| 258 |
|
|---|
| 259 | /* Evaluate base (generic) type. */
|
|---|
| 260 | run_texpr(prog, ctx, tapply->gtype, &base_ti);
|
|---|
| 261 |
|
|---|
| 262 | if (base_ti->tic != tic_tobject) {
|
|---|
| 263 | printf("Error: Base type of generic application is not "
|
|---|
| 264 | "a CSI.\n");
|
|---|
| 265 | *res = tdata_item_new(tic_ignore);
|
|---|
| 266 | return;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | tobject->static_ref = b_false;
|
|---|
| 270 | tobject->csi = base_ti->u.tobject->csi;
|
|---|
| 271 | list_init(&tobject->targs);
|
|---|
| 272 |
|
|---|
| 273 | /* Evaluate type arguments. */
|
|---|
| 274 | arg_n = list_first(&tapply->targs);
|
|---|
| 275 | while (arg_n != NULL) {
|
|---|
| 276 | arg = list_node_data(arg_n, stree_texpr_t *);
|
|---|
| 277 | run_texpr(prog, ctx, arg, &arg_ti);
|
|---|
| 278 |
|
|---|
| 279 | if (arg_ti->tic == tic_ignore) {
|
|---|
| 280 | *res = tdata_item_new(tic_ignore);
|
|---|
| 281 | return;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | list_append(&tobject->targs, arg_ti);
|
|---|
| 285 |
|
|---|
| 286 | arg_n = list_next(&tapply->targs, arg_n);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | *res = titem;
|
|---|
| 290 | }
|
|---|