| 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 "symbol.h"
|
|---|
| 36 | #include "tdata.h"
|
|---|
| 37 |
|
|---|
| 38 | #include "run_texpr.h"
|
|---|
| 39 |
|
|---|
| 40 | static void run_taccess(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 41 | stree_taccess_t *taccess, tdata_item_t **res);
|
|---|
| 42 | static void run_tindex(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 43 | stree_tindex_t *tindex, tdata_item_t **res);
|
|---|
| 44 | static void run_tliteral(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 45 | stree_tliteral_t *tliteral, tdata_item_t **res);
|
|---|
| 46 | static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 47 | stree_tnameref_t *tnameref, tdata_item_t **res);
|
|---|
| 48 |
|
|---|
| 49 | void run_texpr(stree_program_t *prog, stree_csi_t *ctx, stree_texpr_t *texpr,
|
|---|
| 50 | tdata_item_t **res)
|
|---|
| 51 | {
|
|---|
| 52 | switch (texpr->tc) {
|
|---|
| 53 | case tc_taccess:
|
|---|
| 54 | run_taccess(prog, ctx, texpr->u.taccess, res);
|
|---|
| 55 | break;
|
|---|
| 56 | case tc_tindex:
|
|---|
| 57 | run_tindex(prog, ctx, texpr->u.tindex, res);
|
|---|
| 58 | break;
|
|---|
| 59 | case tc_tliteral:
|
|---|
| 60 | run_tliteral(prog, ctx, texpr->u.tliteral, res);
|
|---|
| 61 | break;
|
|---|
| 62 | case tc_tnameref:
|
|---|
| 63 | run_tnameref(prog, ctx, texpr->u.tnameref, res);
|
|---|
| 64 | break;
|
|---|
| 65 | case tc_tapply:
|
|---|
| 66 | printf("Unimplemented: Evaluate type expression type %d.\n",
|
|---|
| 67 | texpr->tc);
|
|---|
| 68 | exit(1);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | static void run_taccess(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 73 | stree_taccess_t *taccess, tdata_item_t **res)
|
|---|
| 74 | {
|
|---|
| 75 | stree_symbol_t *sym;
|
|---|
| 76 | tdata_item_t *targ_i;
|
|---|
| 77 | tdata_item_t *titem;
|
|---|
| 78 | tdata_object_t *tobject;
|
|---|
| 79 | stree_csi_t *base_csi;
|
|---|
| 80 |
|
|---|
| 81 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 82 | printf("Evaluating type access operation.\n");
|
|---|
| 83 | #endif
|
|---|
| 84 | /* Evaluate base type. */
|
|---|
| 85 | run_texpr(prog, ctx, taccess->arg, &targ_i);
|
|---|
| 86 |
|
|---|
| 87 | if (targ_i->tic != tic_tobject) {
|
|---|
| 88 | printf("Error: Using '.' with type which is not an object.\n");
|
|---|
| 89 | exit(1);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* Get base CSI. */
|
|---|
| 93 | base_csi = targ_i->u.tobject->csi;
|
|---|
| 94 |
|
|---|
| 95 | sym = symbol_lookup_in_csi(prog, base_csi, taccess->member_name);
|
|---|
| 96 |
|
|---|
| 97 | /* Existence should have been verified in type checking phase. */
|
|---|
| 98 | assert(sym != NULL);
|
|---|
| 99 |
|
|---|
| 100 | if (sym->sc != sc_csi) {
|
|---|
| 101 | printf("Error: Symbol '");
|
|---|
| 102 | symbol_print_fqn(sym);
|
|---|
| 103 | printf("' is not a CSI.\n");
|
|---|
| 104 | exit(1);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /* Construct type item. */
|
|---|
| 108 | titem = tdata_item_new(tic_tobject);
|
|---|
| 109 | tobject = tdata_object_new();
|
|---|
| 110 | titem->u.tobject = tobject;
|
|---|
| 111 |
|
|---|
| 112 | tobject->static_ref = b_false;
|
|---|
| 113 | tobject->csi = sym->u.csi;
|
|---|
| 114 |
|
|---|
| 115 | *res = titem;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | static void run_tindex(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 119 | stree_tindex_t *tindex, tdata_item_t **res)
|
|---|
| 120 | {
|
|---|
| 121 | tdata_item_t *base_ti;
|
|---|
| 122 | tdata_item_t *titem;
|
|---|
| 123 | tdata_array_t *tarray;
|
|---|
| 124 | stree_expr_t *arg_expr;
|
|---|
| 125 | list_node_t *arg_node;
|
|---|
| 126 |
|
|---|
| 127 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 128 | printf("Evaluating type index operation.\n");
|
|---|
| 129 | #endif
|
|---|
| 130 | /* Evaluate base type. */
|
|---|
| 131 | run_texpr(prog, ctx, tindex->base_type, &base_ti);
|
|---|
| 132 |
|
|---|
| 133 | /* Construct type item. */
|
|---|
| 134 | titem = tdata_item_new(tic_tarray);
|
|---|
| 135 | tarray = tdata_array_new();
|
|---|
| 136 | titem->u.tarray = tarray;
|
|---|
| 137 |
|
|---|
| 138 | tarray->base_ti = base_ti;
|
|---|
| 139 | tarray->rank = tindex->n_args;
|
|---|
| 140 |
|
|---|
| 141 | /* Copy extents. */
|
|---|
| 142 | list_init(&tarray->extents);
|
|---|
| 143 | arg_node = list_first(&tindex->args);
|
|---|
| 144 |
|
|---|
| 145 | while (arg_node != NULL) {
|
|---|
| 146 | arg_expr = list_node_data(arg_node, stree_expr_t *);
|
|---|
| 147 | list_append(&tarray->extents, arg_expr);
|
|---|
| 148 | arg_node = list_next(&tindex->args, arg_node);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | *res = titem;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static void run_tliteral(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 155 | stree_tliteral_t *tliteral, tdata_item_t **res)
|
|---|
| 156 | {
|
|---|
| 157 | tdata_item_t *titem;
|
|---|
| 158 | tdata_primitive_t *tprimitive;
|
|---|
| 159 | tprimitive_class_t tpc;
|
|---|
| 160 |
|
|---|
| 161 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 162 | printf("Evaluating type literal.\n");
|
|---|
| 163 | #endif
|
|---|
| 164 | (void) prog;
|
|---|
| 165 | (void) ctx;
|
|---|
| 166 | (void) tliteral;
|
|---|
| 167 |
|
|---|
| 168 | switch (tliteral->tlc) {
|
|---|
| 169 | case tlc_int: tpc = tpc_int; break;
|
|---|
| 170 | case tlc_string: tpc = tpc_string; break;
|
|---|
| 171 | case tlc_resource: tpc = tpc_resource; break;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /* Construct type item. */
|
|---|
| 175 | titem = tdata_item_new(tic_tprimitive);
|
|---|
| 176 | tprimitive = tdata_primitive_new(tpc);
|
|---|
| 177 | titem->u.tprimitive = tprimitive;
|
|---|
| 178 |
|
|---|
| 179 | *res = titem;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | static void run_tnameref(stree_program_t *prog, stree_csi_t *ctx,
|
|---|
| 183 | stree_tnameref_t *tnameref, tdata_item_t **res)
|
|---|
| 184 | {
|
|---|
| 185 | stree_symbol_t *sym;
|
|---|
| 186 | tdata_item_t *titem;
|
|---|
| 187 | tdata_object_t *tobject;
|
|---|
| 188 |
|
|---|
| 189 | #ifdef DEBUG_RUN_TRACE
|
|---|
| 190 | printf("Evaluating type name reference.\n");
|
|---|
| 191 | #endif
|
|---|
| 192 | sym = symbol_lookup_in_csi(prog, ctx, tnameref->name);
|
|---|
| 193 |
|
|---|
| 194 | /* Existence should have been verified in type-checking phase. */
|
|---|
| 195 | assert(sym);
|
|---|
| 196 |
|
|---|
| 197 | if (sym->sc != sc_csi) {
|
|---|
| 198 | printf("Error: Symbol '");
|
|---|
| 199 | symbol_print_fqn(sym);
|
|---|
| 200 | printf("' is not a CSI.\n");
|
|---|
| 201 | exit(1);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /* Construct type item. */
|
|---|
| 205 | titem = tdata_item_new(tic_tobject);
|
|---|
| 206 | tobject = tdata_object_new();
|
|---|
| 207 | titem->u.tobject = tobject;
|
|---|
| 208 |
|
|---|
| 209 | tobject->static_ref = b_false;
|
|---|
| 210 | tobject->csi = sym->u.csi;
|
|---|
| 211 |
|
|---|
| 212 | *res = titem;
|
|---|
| 213 | }
|
|---|