record.c 311 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Cserveni, Akos
 *   Delic, Adam
 *   Feher, Csaba
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Kremer, Peter
 *   Ormandi, Matyas
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <string.h>
#include "datatypes.h"
#include "../common/memory.h"
#include "record.h"
#include "encdec.h"

#include "main.hh"
#include "error.h"
#include "ttcn3/compiler.h"

static void defEmptyRecordClass(const struct_def *sdef,
                                output_struct *output);

static void defEmptyRecordTemplate(const char *name, const char *dispname,
  output_struct *output);

/** this is common code for both empty and non-empty cases, called from
 *  both functions for template generation */
static void defCommonRecordTemplate(const char *name,
  char **def, char **src);

/** code generation for original runtime */
static void defRecordClass1(const struct_def *sdef, output_struct *output);
static void defRecordTemplate1(const struct_def *sdef, output_struct *output);
/** code generation for alternative runtime (TITAN_RUNTIME_2) */
static void defRecordClass2(const struct_def *sdef, output_struct *output);
static void defRecordTemplate2(const struct_def *sdef, output_struct *output);

void defRecordClass(const struct_def *sdef, output_struct *output)
{
  if (use_runtime_2) defRecordClass2(sdef, output);
  else defRecordClass1(sdef, output);
}

void defRecordTemplate(const struct_def *sdef, output_struct *output)
{
  if (use_runtime_2) defRecordTemplate2(sdef, output);
  else defRecordTemplate1(sdef, output);
}

struct raw_option_struct {
  boolean lengthto; /* indicates whether this field contains a length */
  int lengthof; /* how many length indicators this field counted in */
  int *lengthoffield; /* the list of length indicator field indices */
  boolean pointerto;
  int pointerof;
  boolean ptrbase;
  int extbitgroup;
  int tag_type;
  boolean delayed_decode; /* indicates whether the field has to be decoded
			 out of order (later) */
  int nof_dependent_fields; /* list of fields that are to be decoded */
  int *dependent_fields; /* after this field */
};

static char *genRawFieldDecodeLimit(char *src, const struct_def *sdef,
  int i, const struct raw_option_struct *raw_options);

static char *genRawDecodeRecordField(char *src, const struct_def *sdef,
  int i, const struct raw_option_struct *raw_options, boolean delayed_decode,
  int *prev_ext_group);

static void set_raw_options(const struct_def *sdef,
  struct raw_option_struct *raw_options, boolean* haslengthto,
  boolean* haspointer, boolean* hascrosstag, boolean* has_ext_bit);

static char *generate_raw_coding(char *src,
  const struct_def *sdef, struct raw_option_struct *raw_options,
  boolean haspointer, boolean hascrosstag, boolean has_ext_bit);
static char *generate_raw_coding_negtest(char *src,
  const struct_def *sdef, struct raw_option_struct *raw_options);

void set_raw_options(const struct_def *sdef,
  struct raw_option_struct *raw_options, boolean *haslengthto,
  boolean *haspointer, boolean *hascrosstag, boolean *has_ext_bit)
{
Kristof Szabados's avatar
Kristof Szabados committed
103
    size_t i;
Elemer Lelik's avatar
Elemer Lelik committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    for (i = 0; i < sdef->nElements; i++) {
      raw_options[i].lengthto = FALSE;
      raw_options[i].lengthof = 0;
      raw_options[i].lengthoffield = NULL;
      raw_options[i].pointerto = FALSE;
      raw_options[i].pointerof = 0;
      raw_options[i].ptrbase = FALSE;
      raw_options[i].extbitgroup = 0;
      raw_options[i].tag_type = 0;
      raw_options[i].delayed_decode = FALSE;
      raw_options[i].nof_dependent_fields = 0;
      raw_options[i].dependent_fields = NULL;
    }
    *haslengthto = FALSE;
    *haspointer = FALSE;
    *hascrosstag = FALSE;
    *has_ext_bit = sdef->hasRaw && sdef->raw.extension_bit!=XDEFNO &&
                                  sdef->raw.extension_bit!=XDEFDEFAULT;
    for(i=0;i<sdef->nElements;i++){
      if(sdef->elements[i].hasRaw &&
                                 sdef->elements[i].raw.crosstaglist.nElements){
        *hascrosstag = TRUE;
        break;
      }
    }
    if(sdef->hasRaw){ /* fill tag_type. 0-No tag, >0 index of the tag + 1 */
      for(i=0;i<sdef->raw.taglist.nElements;i++){
        raw_options[sdef->raw.taglist.list[i].fieldnum].tag_type= i+1;
      }
      for(i=0;i<sdef->raw.ext_bit_goup_num;i++){
        int k;
        for(k=sdef->raw.ext_bit_groups[i].from;
                                    k<=sdef->raw.ext_bit_groups[i].to;k++)
            raw_options[k].extbitgroup=i+1;
      }
    }
    for(i=0;i<sdef->nElements;i++){
      if (sdef->elements[i].hasRaw && sdef->elements[i].raw.lengthto_num > 0) {
        int j;
        *haslengthto = TRUE;
        raw_options[i].lengthto = TRUE;
        for(j = 0; j < sdef->elements[i].raw.lengthto_num; j++) {
	  int field_index = sdef->elements[i].raw.lengthto[j];
	  raw_options[field_index].lengthoffield = (int*)
	    Realloc(raw_options[field_index].lengthoffield,
	      (raw_options[field_index].lengthof + 1) * sizeof(int));
	  raw_options[field_index].lengthoffield[
	    raw_options[field_index].lengthof] = i;
	  raw_options[field_index].lengthof++;
        }
      }
      if(sdef->elements[i].hasRaw && sdef->elements[i].raw.pointerto!=-1){
        raw_options[i].pointerto = TRUE;
        raw_options[sdef->elements[i].raw.pointerto].pointerof=i+1;
        *haspointer = TRUE;
        raw_options[sdef->elements[i].raw.pointerbase].ptrbase = TRUE;
      }
    }
    if (sdef->kind == RECORD && *hascrosstag) {
      /* looking for fields that require delayed decoding because of
	 forward references in CROSSTAG */
      for (i = 0; i < sdef->nElements; i++) {
        int j;
	/* we are looking for a field index that is greater than i */
	size_t max_index = i;
	if (!sdef->elements[i].hasRaw) continue;
	for (j = 0; j < sdef->elements[i].raw.crosstaglist.nElements; j++) {
	  int k;
	  rawAST_coding_taglist *crosstag =
	    sdef->elements[i].raw.crosstaglist.list + j;
	  for (k = 0; k < crosstag->nElements; k++) {
	    rawAST_coding_field_list *keyid = crosstag->fields + k;
	    if (keyid->nElements >= 1) {
	      int field_index = keyid->fields[0].nthfield;
	      if (field_index > max_index) max_index = field_index;
	    }
	  }
	}
	if (max_index > i) {
	  raw_options[i].delayed_decode = TRUE;
	  raw_options[max_index].nof_dependent_fields++;
	  raw_options[max_index].dependent_fields = (int*)
	    Realloc(raw_options[max_index].dependent_fields,
	      raw_options[max_index].nof_dependent_fields *
	      sizeof(*raw_options[max_index].dependent_fields));
	  raw_options[max_index].dependent_fields[
	    raw_options[max_index].nof_dependent_fields - 1] = i;
	}
      }
    }
}

char* generate_raw_coding(char* src,
  const struct_def *sdef, struct raw_option_struct *raw_options,
    boolean haspointer, boolean hascrosstag, boolean has_ext_bit)
{
  int i;
  const char *name = sdef->name;

  if (sdef->kind == SET) { /* set decoder start */
    size_t mand_num = 0;
    for (i = 0; i < sdef->nElements; i++) {
      if (!sdef->elements[i].isOptional) mand_num++;
    }
    src = mputprintf(src, "int %s::RAW_decode(const TTCN_Typedescriptor_t& "
      "p_td, TTCN_Buffer& p_buf, int limit, raw_order_t top_bit_ord, "
210
      "boolean, int, boolean, const RAW_Force_Omit* force_omit)\n"
Elemer Lelik's avatar
Elemer Lelik committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
      "{\n"
        "int prepaddlength = p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
        "limit -= prepaddlength;\n"
        "int decoded_length = 0;\n"
        "int field_map[%lu];\n"
        "memset(field_map, 0, sizeof(field_map));\n",
        name, (unsigned long) sdef->nElements);
    if (mand_num > 0)
      src = mputstr(src, "size_t nof_mand_fields = 0;\n");
    for (i = 0; i < sdef->nElements; i++) {
      if (sdef->elements[i].isOptional)
        src = mputprintf(src, "field_%s = OMIT_VALUE;\n",
                         sdef->elements[i].name);
    }
      src = mputstr(src,
	"raw_order_t local_top_order;\n"
      "if (p_td.raw->top_bit_order == TOP_BIT_INHERITED) "
	"local_top_order = top_bit_ord;\n"
      "else if (p_td.raw->top_bit_order == TOP_BIT_RIGHT) "
	"local_top_order = ORDER_MSB;\n"
      "else local_top_order = ORDER_LSB;\n"
      "while (limit > 0) {\n"
      "size_t fl_start_pos = p_buf.get_pos_bit();\n"
      );
      for (i = 0; i < sdef->nElements; i++) { /* decoding fields with tag */
	if (raw_options[i].tag_type &&
	    sdef->raw.taglist.list[raw_options[i].tag_type - 1].nElements > 0) {
	  rawAST_coding_taglist* cur_choice =
	    sdef->raw.taglist.list + raw_options[i].tag_type - 1;
	  size_t j;
	  boolean has_fixed = FALSE, has_variable = FALSE, flag_needed = FALSE;
	  for (j = 0; j < cur_choice->nElements; j++) {
	    if (cur_choice->fields[j].start_pos >= 0) {
	      if (has_fixed || has_variable) flag_needed = TRUE;
	      has_fixed = TRUE;
	    } else {
	      if (has_fixed) flag_needed = TRUE;
	      has_variable = TRUE;
	    }
	    if (has_fixed && has_variable) break;
	  }
252
	  src = mputprintf(src, "if (field_map[%lu] == 0",
Elemer Lelik's avatar
Elemer Lelik committed
253
            (unsigned long) i);
254
255
256
257
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src, " && (force_omit == NULL || !(*force_omit)(%d))", i);
    }
    src = mputstr(src, ") {\n");
Elemer Lelik's avatar
Elemer Lelik committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
	  if (flag_needed)
	    src = mputstr(src, "boolean already_failed = FALSE;\n");
	  if (has_fixed) {
	    /* first check the fields we can precode
	     * try to decode those key variables whose position we know
	     * this way we might be able to step over bad values faster
	     */
	    boolean first_fixed = TRUE;
	    src = mputstr(src, "raw_order_t temporal_top_order;\n"
	      "int temporal_decoded_length;\n");
	    for (j = 0; j < cur_choice->nElements; j++) {
	      size_t k;
	      rawAST_coding_field_list *cur_field_list = cur_choice->fields + j;
	      if (cur_field_list->start_pos < 0) continue;
	      if (!first_fixed) src = mputstr(src, "if (!already_failed) {\n");
	      for (k = cur_field_list->nElements - 1; k > 0; k--) {
		src = mputprintf(src, "if (%s_descr_.raw->top_bit_order == "
		  "TOP_BIT_RIGHT) temporal_top_order = ORDER_MSB;\n"
		"else if (%s_descr_.raw->top_bit_order == TOP_BIT_LEFT) "
		  "temporal_top_order = ORDER_LSB;\n"
		"else ", cur_field_list->fields[k - 1].typedescr,
		cur_field_list->fields[k - 1].typedescr);
	      }
	      src = mputprintf(src, "temporal_top_order = top_bit_ord;\n"
		"%s temporal_%lu;\n"
		"p_buf.set_pos_bit(fl_start_pos + %d);\n"
		"temporal_decoded_length = temporal_%lu.RAW_decode(%s_descr_, "
		  "p_buf, limit, temporal_top_order, TRUE);\n"
		"p_buf.set_pos_bit(fl_start_pos);\n"
		"if (temporal_decoded_length > 0 && temporal_%lu == %s) {\n"
288
289
    "RAW_Force_Omit field_%d_force_omit(%d, force_omit, "
    "%s_descr_.raw->forceomit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
290
		"int decoded_field_length = field_%s%s.RAW_decode(%s_descr_, "
291
		  "p_buf, limit, local_top_order, TRUE, -1, TRUE, &field_%d_force_omit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
292
293
294
295
296
		"if (decoded_field_length %s 0 && (",
		cur_field_list->fields[cur_field_list->nElements - 1].type,
		(unsigned long) j, cur_field_list->start_pos, (unsigned long) j,
		cur_field_list->fields[cur_field_list->nElements - 1].typedescr,
		(unsigned long) j, cur_field_list->value,
297
    i, i, sdef->elements[i].typedescrname,
Elemer Lelik's avatar
Elemer Lelik committed
298
299
                sdef->elements[i].name,
		sdef->elements[i].isOptional ? "()" : "",
300
		sdef->elements[i].typedescrname, i,
Elemer Lelik's avatar
Elemer Lelik committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		sdef->elements[i].isOptional ? ">" : ">=");
	      src = genRawFieldChecker(src, cur_choice, TRUE);
	      src = mputstr(src, ")) {\n"
		"decoded_length += decoded_field_length;\n"
		"limit -= decoded_field_length;\n");
	      if (!sdef->elements[i].isOptional)
		src = mputstr(src, "nof_mand_fields++;\n");
	      src = mputprintf(src, "field_map[%lu] = 1;\n"
		"continue;\n"
		"} else {\n"
		"p_buf.set_pos_bit(fl_start_pos);\n", (unsigned long) i);
	      if (sdef->elements[i].isOptional)
		src = mputprintf(src, "field_%s = OMIT_VALUE;\n",
		  sdef->elements[i].name);
	      if (flag_needed) src = mputstr(src, "already_failed = TRUE;\n");
	      src = mputstr(src, "}\n"
		"}\n");
	      if (first_fixed) first_fixed = FALSE;
	      else src = mputstr(src, "}\n");
	    }
	  }
	  if (has_variable) {
	    /* if there is one tag key whose position we don't know
	     * and we couldn't decide yet if element can be decoded or not
	     * than we have to decode it.
	     */
	    if (flag_needed) src = mputstr(src, "if (!already_failed) {\n");
328
329
330
331
	    src = mputprintf(src, 
    "RAW_Force_Omit field_%d_force_omit(%d, force_omit, "
    "%s_descr_.raw->forceomit);\n"
    "int decoded_field_length = "
Elemer Lelik's avatar
Elemer Lelik committed
332
		"field_%s%s.RAW_decode(%s_descr_, p_buf, limit, "
333
		"local_top_order, TRUE, -1, TRUE, &field_%d_force_omit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
334
	      "if (decoded_field_length %s 0 && (",
335
336
337
	      i, i, sdef->elements[i].typedescrname,
        sdef->elements[i].name, sdef->elements[i].isOptional ? "()" : "",
	      sdef->elements[i].typedescrname, i,
Elemer Lelik's avatar
Elemer Lelik committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	      sdef->elements[i].isOptional ? ">" : ">=");
	    src = genRawFieldChecker(src, cur_choice, TRUE);
	    src = mputstr(src, ")) {\n"
	      "decoded_length += decoded_field_length;\n"
	      "limit -= decoded_field_length;\n");
	    if (!sdef->elements[i].isOptional)
	      src = mputstr(src, "nof_mand_fields++;\n");
	    src = mputprintf(src, "field_map[%lu] = 1;\n"
	      "continue;\n"
	      "} else {\n"
	      "p_buf.set_pos_bit(fl_start_pos);\n", (unsigned long) i);
	    if (sdef->elements[i].isOptional)
	      src = mputprintf(src, "field_%s = OMIT_VALUE;\n",
		sdef->elements[i].name);
	    src = mputstr(src, "}\n");
	    if (flag_needed) src = mputstr(src, "}\n");
	  }
	  src = mputstr(src, "}\n");
	}
      }
      for (i = 0; i < sdef->nElements; i++) {
	/* decoding fields without TAG */
	if (!raw_options[i].tag_type) {
	  boolean repeatable;
	  if (sdef->elements[i].of_type && sdef->elements[i].hasRaw &&
363
364
365
366
367
368
	      sdef->elements[i].raw.repeatable == XDEFYES) {
      repeatable = TRUE;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, "if (force_omit == NULL || !(*force_omit)(%d)) ", i);
      }
    }
Elemer Lelik's avatar
Elemer Lelik committed
369
370
	  else {
	    repeatable = FALSE;
371
372
373
374
375
376
	    src = mputprintf(src, "if (field_map[%lu] == 0",
            (unsigned long) i);
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, " && (force_omit == NULL || !(*force_omit)(%d))", i);
      }
      src = mputstr(src, ") ");
Elemer Lelik's avatar
Elemer Lelik committed
377
378
	  }
	  src = mputprintf(src, "{\n"
379
380
      "RAW_Force_Omit field_%d_force_omit(%d, force_omit, "
      "%s_descr_.raw->forceomit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
381
	    "int decoded_field_length = field_%s%s.RAW_decode(%s_descr_, "
382
	      "p_buf, limit, local_top_order, TRUE, %s, ",
383
      i, i, sdef->elements[i].typedescrname,
Elemer Lelik's avatar
Elemer Lelik committed
384
	    sdef->elements[i].name, sdef->elements[i].isOptional ? "()" : "",
385
	    sdef->elements[i].typedescrname, repeatable?"1":"-1");
Elemer Lelik's avatar
Elemer Lelik committed
386
	  if (repeatable)
387
            src = mputprintf(src, "field_map[%lu] == 0",
Elemer Lelik's avatar
Elemer Lelik committed
388
              (unsigned long) i);
389
390
391
392
    else {
      src = mputstr(src, "TRUE");
    }
	  src = mputprintf(src, ", &field_%d_force_omit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
393
394
395
	    "if (decoded_field_length %s 0) {\n"
	    "decoded_length += decoded_field_length;\n"
	    "limit -= decoded_field_length;\n",
396
	    i, sdef->elements[i].isOptional ? ">" : ">=");
Elemer Lelik's avatar
Elemer Lelik committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	  if (repeatable) {
	    if (!sdef->elements[i].isOptional) src = mputprintf(src,
	      "if (field_map[%lu] == 0) nof_mand_fields++;\n",
              (unsigned long) i);
	    src = mputprintf(src, "field_map[%lu]++;\n", (unsigned long) i);
	  } else {
	    if (!sdef->elements[i].isOptional)
	      src = mputstr(src, "nof_mand_fields++;\n");
	    src = mputprintf(src, "field_map[%lu] = 1;\n", (unsigned long) i);
	  }
	  src = mputstr(src, "continue;\n"
	    "} else {\n"
	    "p_buf.set_pos_bit(fl_start_pos);\n");
	  if (sdef->elements[i].isOptional) {
	    if (repeatable)
	      src = mputprintf(src, "if (field_map[%lu] == 0) ",
                (unsigned long) i);
	    src = mputprintf(src, "field_%s = OMIT_VALUE;\n",
	      sdef->elements[i].name);
	  }
	  src = mputstr(src, "}\n"
	    "}\n");
	}
      }
      for (i = 0; i < sdef->nElements; i++){
	/* decoding fields with tag OTHERWISE */
	if (raw_options[i].tag_type &&
	    sdef->raw.taglist.list[raw_options[i].tag_type-1].nElements == 0) {
425
426
427
428
429
430
431
	  src = mputprintf(src, "if (field_map[%lu] == 0", (unsigned long) i);
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src, " && (force_omit == NULL || !(*force_omit)(%d))", i);
    }
    src = mputprintf(src, ") {\n"
      "RAW_Force_Omit field_%d_force_omit(%d, force_omit, "
      "%s_descr_.raw->forceomit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
432
	    "int decoded_field_length = field_%s%s.RAW_decode(%s_descr_, "
433
	      "p_buf, limit, local_top_order, TRUE, -1, TRUE, &field_%d_force_omit);\n"
Elemer Lelik's avatar
Elemer Lelik committed
434
435
	    "if (decoded_field_length %s 0) {\n"
	    "decoded_length += decoded_field_length;\n"
436
437
	    "limit -= decoded_field_length;\n",
      i, i, sdef->elements[i].typedescrname,
Elemer Lelik's avatar
Elemer Lelik committed
438
	    sdef->elements[i].name, sdef->elements[i].isOptional ? "()" : "",
439
	    sdef->elements[i].typedescrname, i,
Elemer Lelik's avatar
Elemer Lelik committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
	    sdef->elements[i].isOptional ? ">" : ">=");
	  if (!sdef->elements[i].isOptional)
	    src = mputstr(src, "nof_mand_fields++;\n");
	  src = mputprintf(src, "field_map[%lu] = 1;\n"
	    "continue;\n"
	    "} else {\n"
	    "p_buf.set_pos_bit(fl_start_pos);\n", (unsigned long) i);
	  if (sdef->elements[i].isOptional)
	    src = mputprintf(src, "field_%s = OMIT_VALUE;\n",
	      sdef->elements[i].name);
	  src = mputstr(src, "}\n"
	    "}\n");
	}
      }
      src = mputstr(src, "break;\n"  /* no field decoded successfully, quit */
        "}\n");
      if (mand_num > 0) src = mputprintf(src,
        "if (nof_mand_fields != %lu) return limit ? -1 : -TTCN_EncDec::ET_INCOMPL_MSG;\n",
        (unsigned long) mand_num);
      /* If not all required fields were decoded and there are no bits left,
       * that means that the last field was decoded successfully but used up
       * the buffer. Signal "incomplete". If there were bits left, that means that
       * no field could be decoded from them; signal an error.
       */
      src = mputstr(src, "return decoded_length + prepaddlength + "
	"p_buf.increase_pos_padd(p_td.raw->padding);\n"
      "}\n\n");
  } else {
    /* set decoder end, record decoder start */
    int prev_ext_group = 0;
    src = mputprintf(src,
      "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, "
      "TTCN_Buffer& p_buf, int limit, raw_order_t top_bit_ord, boolean no_err, "
473
      "int, boolean, const RAW_Force_Omit* force_omit)\n"
Elemer Lelik's avatar
Elemer Lelik committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
      "{ (void)no_err;\n"
	"  int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
	"  limit-=prepaddlength;\n"
	"  size_t last_decoded_pos = p_buf.get_pos_bit();\n"
	"  int decoded_length = 0;\n"
	"  int decoded_field_length = 0;\n"
	"  raw_order_t local_top_order;\n"
	, name);
      if (hascrosstag) {
	src = mputstr(src, "  int selected_field = -1;\n");
      }
      if (sdef->raw.ext_bit_goup_num) {
        src=mputstr(src, "  int group_limit = 0;\n");
      }
      src=mputstr(src,
      "  if(p_td.raw->top_bit_order==TOP_BIT_INHERITED)"
      "local_top_order=top_bit_ord;\n"
      "  else if(p_td.raw->top_bit_order==TOP_BIT_RIGHT)local_top_order"
      "=ORDER_MSB;\n"
      "  else local_top_order=ORDER_LSB;\n"
      );
      if(has_ext_bit){
        src=mputstr(src,
          "  {\n"
          "  cbyte* data=p_buf.get_read_data();\n"
          "  int count=1;\n"
          "  unsigned mask = 1 << (local_top_order==ORDER_LSB ? 0 : 7);\n"
          "  if(p_td.raw->extension_bit==EXT_BIT_YES){\n"
          "    while((data[count-1] & mask)==0 && count*8<(int)limit) count++;\n"
          "  }\n"
          "  else{\n"
          "    while((data[count-1] & mask)!=0 && count*8<(int)limit) count++;\n"
          "  }\n"
          "  if(limit) limit=count*8;\n"
          "  }\n"
        );
      }
      if(haspointer)
       src=mputstr(src,
         "  int end_of_available_data=last_decoded_pos+limit;\n");
      for(i=0;i<sdef->nElements;i++){
        if(raw_options[i].pointerof)
          src=mputprintf(src,
          "  int start_of_field%lu=-1;\n"
          ,(unsigned long) i
          );
        if(raw_options[i].ptrbase)
          src=mputprintf(src,
          "  int start_pos_of_field%lu=-1;\n"
          ,(unsigned long) i
          );
        if(raw_options[i].lengthto)
          src=mputprintf(src,
          "  int value_of_length_field%lu = 0;\n"
          ,(unsigned long) i
          );
      }
      for(i=0;i<sdef->nElements;i++){ /* decoding fields */
	if (raw_options[i].delayed_decode) {
	  int j;
	  /* checking whether there are enough bits in the buffer to decode
	     the field */
	  src = mputstr(src, "  if (");
	  src = genRawFieldDecodeLimit(src, sdef, i, raw_options);
	  src = mputprintf(src, " < %d) return -TTCN_EncDec::ET_LEN_ERR;\n",
	    sdef->elements[i].raw.length);
	  /* skipping over the field that has to be decoded later because of
	     forward referencing in CROSSTAG */
	  src = mputprintf(src,
	    "  size_t start_of_field%lu = p_buf.get_pos_bit();\n"
	    "  p_buf.set_pos_bit(start_of_field%lu + %d);\n"
	    "  decoded_length += %d;\n"
	    "  last_decoded_pos += %d;\n"
	    "  limit -= %d;\n",
	    (unsigned long) i, (unsigned long) i, sdef->elements[i].raw.length,
            sdef->elements[i].raw.length,
	    sdef->elements[i].raw.length, sdef->elements[i].raw.length);
	  for (j = 0; j < raw_options[i].lengthof; j++) {
	    src = mputprintf(src,
	      "  value_of_length_field%d -= %d;\n",
	      raw_options[i].lengthoffield[j], sdef->elements[i].raw.length);
	  }
	} else {
	  src = genRawDecodeRecordField(src, sdef, i, raw_options, FALSE,
	   &prev_ext_group);
	  if (raw_options[i].nof_dependent_fields > 0) {
	    int j;
	    for (j = 0; j < raw_options[i].nof_dependent_fields; j++) {
	      int dependent_field_index = raw_options[i].dependent_fields[j];
	      /* seek to the beginning of the dependent field */
	      src = mputprintf(src,
		"  p_buf.set_pos_bit(start_of_field%d);\n",
		dependent_field_index);
	      /* decode the dependent field */
	      src = genRawDecodeRecordField(src, sdef, dependent_field_index,
		raw_options, TRUE, &prev_ext_group);
	    }
	    if (i < sdef->nElements - 1) {
	      /* seek back if there are more regular fields to decode */
	      src = mputstr(src,
		"  p_buf.set_pos_bit(last_decoded_pos);\n");
	    }
	  }
	}
      } /* decoding fields*/

      if(sdef->hasRaw && sdef->raw.presence.nElements > 0)
      {
	src = mputstr(src, "  if (");
	src = genRawFieldChecker(src, &sdef->raw.presence, FALSE);
	src = mputstr(src, ") return -1;\n");
      }

      src=mputstr(src,
      "  p_buf.set_pos_bit(last_decoded_pos);\n"
      "  return decoded_length+prepaddlength+"
      "p_buf.increase_pos_padd(p_td.raw->padding);\n}\n\n");
  } /* record decoder end */

  src = mputprintf(src, /* encoder */
    "int %s::RAW_encode(const TTCN_Typedescriptor_t&%s, "
    "RAW_enc_tree& myleaf) const {\n", name,
    use_runtime_2 ? " p_td" : "");
  if (use_runtime_2) {
    src = mputstr(src, "  if (err_descr) return RAW_encode_negtest(err_descr, p_td, myleaf);\n");
  }
  src = mputprintf(src,
    "  if (!is_bound()) TTCN_EncDec_ErrorContext::error"
    "(TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
    "  int encoded_length = 0;\n"
604
    "  myleaf.isleaf = FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
605
606
607
608
609
610
611
612
613
614
    "  myleaf.body.node.num_of_nodes = %lu;\n"
    "  myleaf.body.node.nodes = init_nodes_of_enc_tree(%lu);\n",
    (unsigned long)sdef->nElements, (unsigned long)sdef->nElements);
  /* init nodes */
  for (i = 0; i < sdef->nElements; i++) {
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src,
        "  if (field_%s.ispresent()) {\n", sdef->elements[i].name);
    }
    src = mputprintf(src,
615
      "  myleaf.body.node.nodes[%lu] = new RAW_enc_tree(TRUE, &myleaf, "
Elemer Lelik's avatar
Elemer Lelik committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
      "&(myleaf.curr_pos), %lu, %s_descr_.raw);\n",
      (unsigned long)i, (unsigned long)i, sdef->elements[i].typedescrname);
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src,
        "  }\n"
        "  else myleaf.body.node.nodes[%lu] = NULL;\n", (unsigned long)i);
    }
  }
  for (i = 0; i < sdef->raw.ext_bit_goup_num; i++) {
    if (sdef->raw.ext_bit_groups[i].ext_bit != XDEFNO) {
      src = mputprintf(src,
        "  {\n"
        "   int node_idx = %d;\n"
        "   while (node_idx <= %d && myleaf.body.node.nodes[node_idx] == NULL) node_idx++;\n"
        "   if (myleaf.body.node.nodes[node_idx]) {\n"
        "     myleaf.body.node.nodes[node_idx]->ext_bit_handling = 1;\n"
        "     myleaf.body.node.nodes[node_idx]->ext_bit = %s;\n  }\n"
        "   node_idx = %d;\n"
        "   while (node_idx >= %d && myleaf.body.node.nodes[node_idx] == NULL) node_idx--;\n"
        "   if (myleaf.body.node.nodes[node_idx]) myleaf.body.node.nodes[node_idx]"
        "->ext_bit_handling += 2;\n"
        "  }\n",
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].ext_bit == XDEFYES ? "EXT_BIT_YES" : "EXT_BIT_REVERSE",
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].from);
    }
  }
  for (i = 0; i < sdef->nElements; i++) {
    /* encoding fields */
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src,
        "  if (field_%s.ispresent()) {\n", sdef->elements[i].name);
    }
    if (raw_options[i].lengthto && sdef->elements[i].raw.lengthindex == NULL
        && sdef->elements[i].raw.union_member_num == 0) {
      /* encoding of lenghto fields */
      int a;
      src = mputprintf(src,
        "  encoded_length += %d;\n"
        "  myleaf.body.node.nodes[%lu]->calc = CALC_LENGTH;\n"
        "  myleaf.body.node.nodes[%lu]->coding_descr = &%s_descr_;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.lengthto.num_of_fields = %d;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.lengthto.unit = %d;\n"
661
        "  myleaf.body.node.nodes[%lu]->calcof.lengthto.offset = %d;\n"
Elemer Lelik's avatar
Elemer Lelik committed
662
663
664
665
666
667
668
        "  myleaf.body.node.nodes[%lu]->calcof.lengthto.fields = "
        "init_lengthto_fields_list(%d);\n"
        "  myleaf.body.node.nodes[%lu]->length = %d;\n",
        sdef->elements[i].raw.fieldlength, (unsigned long)i,
        (unsigned long)i, sdef->elements[i].typedescrname,
        (unsigned long)i, sdef->elements[i].raw.lengthto_num,
        (unsigned long)i, sdef->elements[i].raw.unit,
669
        (unsigned long)i, sdef->elements[i].raw.lengthto_offset,
Elemer Lelik's avatar
Elemer Lelik committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
        (unsigned long)i, sdef->elements[i].raw.lengthto_num,
        (unsigned long)i, sdef->elements[i].raw.fieldlength);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "  if (field_%s.ispresent()) {\n",
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
          "  myleaf.body.node.nodes[%lu]->calcof.lengthto.fields[%d].level = "
          "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
          "  myleaf.body.node.nodes[%lu]->calcof.lengthto.fields[%d].pos = "
          "myleaf.body.node.nodes[%d]->curr_pos.pos;\n",
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a],
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a]);
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "  } else {\n"
            "  myleaf.body.node.nodes[%lu]->calcof.lengthto.fields[%d].level = 0;\n"
            "  myleaf.body.node.nodes[%lu]->calcof.lengthto.fields[%d].pos = 0;\n"
            "  }\n", (unsigned long)i, a, (unsigned long)i, a);
        }
      }
    } else if (raw_options[i].pointerto) {
      /* encoding of pointerto fields */
      if (sdef->elements[sdef->elements[i].raw.pointerto].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[sdef->elements[i].raw.pointerto].name);
      }
      src = mputprintf(src,
        "  encoded_length += %d;\n"
        "  myleaf.body.node.nodes[%lu]->calc = CALC_POINTER;\n"
        "  myleaf.body.node.nodes[%lu]->coding_descr = &%s_descr_;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.pointerto.unit = %d;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.pointerto.ptr_offset = %d;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.pointerto.ptr_base = %d;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.pointerto.target.level = "
        "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
        "  myleaf.body.node.nodes[%lu]->calcof.pointerto.target.pos = "
        "myleaf.body.node.nodes[%d]->curr_pos.pos;\n"
        "  myleaf.body.node.nodes[%lu]->length = %d;\n",
        sdef->elements[i].raw.fieldlength,(unsigned long)i,
        (unsigned long)i, sdef->elements[i].typedescrname,
        (unsigned long)i, sdef->elements[i].raw.unit,
        (unsigned long)i, sdef->elements[i].raw.ptroffset,
        (unsigned long)i, sdef->elements[i].raw.pointerbase,
        (unsigned long)i, sdef->elements[i].raw.pointerto,
        (unsigned long)i, sdef->elements[i].raw.pointerto,
        (unsigned long)i, sdef->elements[i].raw.fieldlength);
      if (sdef->elements[sdef->elements[i].raw.pointerto].isOptional) {
        src = mputprintf(src,
          "  } else {\n"
          "    INTEGER atm;\n"
          "    atm = 0;\n"
          "    encoded_length += atm.RAW_encode(%s_descr_"
          ", *myleaf.body.node.nodes[%lu]);\n"
          "  }\n",
          sdef->elements[i].typedescrname, (unsigned long)i);
      }
    } else {
      /* encoding of normal fields */
      src = mputprintf(src,
        "  encoded_length += field_%s%s.RAW_encode(%s_descr_"
        ", *myleaf.body.node.nodes[%lu]);\n",
        sdef->elements[i].name, sdef->elements[i].isOptional ? "()" : "",
        sdef->elements[i].typedescrname, (unsigned long)i);
    }
    if (sdef->elements[i].isOptional) {
      src = mputstr(src, "  }\n");
    }
  }
  for (i = 0; i < sdef->nElements; i++) {
    /* fill presence, tag and crosstag */
    if (raw_options[i].lengthto && sdef->elements[i].raw.lengthindex) {
      /* encoding of lenghto fields */
      int a;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[i].name);
      }
      src = mputprintf(src,
        "  if (myleaf.body.node.nodes[%lu]->body.node.nodes[%d]) {\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "calc = CALC_LENGTH;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "coding_descr = &%s_descr_;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "calcof.lengthto.num_of_fields = %d;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "calcof.lengthto.unit = %d;\n"
762
763
         "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "calcof.lengthto.offset = %d;\n"
Elemer Lelik's avatar
Elemer Lelik committed
764
765
766
767
768
769
770
771
772
773
774
775
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
        "calcof.lengthto.fields = "
        "init_lengthto_fields_list(%d);\n",
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.lengthindex->typedescr,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.lengthto_num,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.unit,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
776
777
        sdef->elements[i].raw.lengthto_offset,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
Elemer Lelik's avatar
Elemer Lelik committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
        sdef->elements[i].raw.lengthto_num);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "  if (field_%s.ispresent()) {\n",
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
          "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]"
          "->calcof.lengthto.fields[%d].level = "
          "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
          "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]"
          "->calcof.lengthto.fields[%d].pos = "
          "myleaf.body.node.nodes[%d]->curr_pos.pos;\n",
          (unsigned long)i,sdef->elements[i].raw.lengthindex->nthfield,
          a, sdef->elements[i].raw.lengthto[a],
          (unsigned long)i,sdef->elements[i].raw.lengthindex->nthfield,
          a, sdef->elements[i].raw.lengthto[a]);
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
           src = mputprintf(src,
           "  } else {\n"
           "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
           "calcof.lengthto.fields[%d].level = 0;\n"
           "  myleaf.body.node.nodes[%lu]->body.node.nodes[%d]->"
           "calcof.lengthto.fields[%d].pos = 0;\n"
           "  }\n",
           (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield, a,
           (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield, a);
        }
      }
      src = mputstr(src, "  }\n");
      if (sdef->elements[i].isOptional) {
        src = mputstr(src, "  }\n");
      }
    }
    if (raw_options[i].lengthto && sdef->elements[i].raw.union_member_num) {
      /* encoding of lenghto fields */
      int a;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) ", sdef->elements[i].name);
      }

      src = mputprintf(src,
        "  {\n"
        "  int sel_field = 0;\n"
        "  while (myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field] == NULL) "
        "{ sel_field++; }\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]->"
        "calc = CALC_LENGTH;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]->"
        "calcof.lengthto.num_of_fields = %d;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]->"
        "calcof.lengthto.unit = %d;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]->"
833
834
        "calcof.lengthto.offset = %d;\n"
        "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]->"
Elemer Lelik's avatar
Elemer Lelik committed
835
836
837
838
        "calcof.lengthto.fields = init_lengthto_fields_list(%d);\n",
        (unsigned long)i,(unsigned long)i,
        (unsigned long)i,sdef->elements[i].raw.lengthto_num,
        (unsigned long)i,sdef->elements[i].raw.unit,
839
        (unsigned long)i,sdef->elements[i].raw.lengthto_offset,
Elemer Lelik's avatar
Elemer Lelik committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
        (unsigned long)i,sdef->elements[i].raw.lengthto_num);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "  if (field_%s.ispresent()) {\n",
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
          "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]"
          "->calcof.lengthto.fields[%d].level = "
          "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
          "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]"
          "->calcof.lengthto.fields[%d].pos = "
          "myleaf.body.node.nodes[%d]->curr_pos.pos;\n",
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a],
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a]);
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "  }else{\n"
            "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]"
            "->calcof.lengthto.fields[%d].level = 0;\n"
            "  myleaf.body.node.nodes[%lu]->body.node.nodes[sel_field]"
            "->calcof.lengthto.fields[%d].pos = 0;\n"
            "  }\n",
            (unsigned long)i, a,
            (unsigned long)i, a);
        }
      }
      src = mputstr(src,
        "  }\n");
    }
    if (raw_options[i].tag_type && sdef->raw.taglist.list[raw_options[i].tag_type - 1].nElements) {
      /* tag */
      rawAST_coding_taglist *cur_choice =
        sdef->raw.taglist.list + raw_options[i].tag_type - 1;
      src = mputstr(src, "  if (");
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, "field_%s.ispresent() && (", sdef->elements[i].name);
      }
      src = genRawFieldChecker(src, cur_choice, FALSE);
      if (sdef->elements[i].isOptional) src = mputstr(src, ")");
      src = mputstr(src,") {\n");
      src = genRawTagChecker(src, cur_choice);
      src=mputstr(src,"  }\n");
    }
    if (sdef->elements[i].hasRaw && sdef->elements[i].raw.presence.nElements) {
      /* presence */
      src = mputstr(src, "  if (");
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, "field_%s.ispresent() && (", sdef->elements[i].name);
      }
      src = genRawFieldChecker(src, &sdef->elements[i].raw.presence, FALSE);
      if (sdef->elements[i].isOptional) src = mputstr(src, ")");
      src = mputstr(src, ") {\n");
      src = genRawTagChecker(src, &sdef->elements[i].raw.presence);
      src = mputstr(src,"  }\n");
    }
    if (sdef->elements[i].hasRaw &&
        sdef->elements[i].raw.crosstaglist.nElements) {
      /* crosstag */
      int a;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[i].name);
      }
      src = mputprintf(src,
        "  switch (field_%s%s.get_selection()) {\n",
        sdef->elements[i].name,sdef->elements[i].isOptional ? "()" : "");
      for (a = 0; a < sdef->elements[i].raw.crosstaglist.nElements; a++) {
        rawAST_coding_taglist *cur_choice =
	  sdef->elements[i].raw.crosstaglist.list + a;
        if (cur_choice->nElements > 0) {
	    src = mputprintf(src, "  case %s%s%s:\n"
	      "  if (", sdef->elements[i].type,
	      "::ALT_", cur_choice->fieldName);
	    src = genRawFieldChecker(src, cur_choice, FALSE);
	    src = mputstr(src, ") {\n");
	    if (!strcmp(cur_choice->fields[0].value, "OMIT_VALUE")) {
	      if (cur_choice->fields[0].nElements != 1)
		NOTSUPP("omit value with multiple fields in CROSSTAG");
	      /* eliminating the corresponding encoded leaf, which will have
	         the same effect as if the referred field had been omit */
	      src = mputprintf(src,
		"  encoded_length -= myleaf.body.node.nodes[%d]->length;\n"
		"  delete myleaf.body.node.nodes[%d];\n"
		"  myleaf.body.node.nodes[%d] = NULL;\n",
		cur_choice->fields[0].fields[0].nthfield,
		cur_choice->fields[0].fields[0].nthfield,
		cur_choice->fields[0].fields[0].nthfield);
	    } else {
	      int ll;
	      src = mputprintf(src,
		"  RAW_enc_tr_pos pr_pos;\n"
		"  pr_pos.level = myleaf.curr_pos.level + %d;\n"
		"  int new_pos[] = { ",
		cur_choice->fields[0].nElements);
              for (ll = 0; ll < cur_choice->fields[0].nElements; ll++) {
		if (ll > 0) src = mputstr(src, ", ");
		src = mputprintf(src, "%d",
		  cur_choice->fields[0].fields[ll].nthfield);
              }
	      src = mputprintf(src, " };\n"
		"  pr_pos.pos = init_new_tree_pos(myleaf.curr_pos, %d, "
		  "new_pos);\n",
		cur_choice->fields[0].nElements);
              if (cur_choice->fields[0].value[0] == ' ') {
	        /* the value is a string literal (the encoder can be called
		   on that object directly */
		src = mputprintf(src,
		  "  RAW_enc_tree* temp_leaf = myleaf.get_node(pr_pos);\n"
                  "  if (temp_leaf != NULL)\n"
                  "    %s.RAW_encode(%s_descr_,*temp_leaf);\n"
                  "  else\n"
                  "    TTCN_EncDec_ErrorContext::error\n"
                  "      (TTCN_EncDec::ET_OMITTED_TAG, \"Encoding a tagged,"
                  " but omitted value.\");\n",
		  cur_choice->fields[0].value,
		  cur_choice->fields[0].fields[
		    cur_choice->fields[0].nElements - 1].typedescr);
	      } else {
	        /* a temporary object needs to be created for encoding */
		src = mputprintf(src,
		  "  %s new_val(%s);\n"
                  "  RAW_enc_tree* temp_leaf = myleaf.get_node(pr_pos);\n"
                  "  if (temp_leaf != NULL)\n"
                  "    new_val.RAW_encode(%s_descr_,*temp_leaf);\n"
                  "  else\n"
                  "    TTCN_EncDec_ErrorContext::error\n"
                  "      (TTCN_EncDec::ET_OMITTED_TAG, \"Encoding a tagged,"
                  " but omitted value.\");\n",
		  cur_choice->fields[0].fields[
		    cur_choice->fields[0].nElements - 1].type,
		  cur_choice->fields[0].value,
		  cur_choice->fields[0].fields[
		    cur_choice->fields[0].nElements - 1].typedescr);
              }
              src = mputstr(src, "  free_tree_pos(pr_pos.pos);\n");
	    }
	    src = mputstr(src, "  }\n"
	      "  break;\n");
          }
        }
        src = mputstr(src, "  default:;\n"
          "  }\n");
        if (sdef->elements[i].isOptional) src = mputstr(src, "  }\n");
      }
    }
  /* presence */
  if (sdef->hasRaw && sdef->raw.presence.nElements > 0) {
    src = mputstr(src, "  if (");
    src = genRawFieldChecker(src, &sdef->raw.presence, FALSE);
    src = mputstr(src,") {\n");
    src = genRawTagChecker(src, &sdef->raw.presence);
    src = mputstr(src,"  }\n");
  }
  src = mputstr(src, "  return myleaf.length = encoded_length;\n}\n\n");

  if (use_runtime_2)
    src = generate_raw_coding_negtest(src, sdef, raw_options);
  return src;
}

char *generate_raw_coding_negtest(char *src, const struct_def *sdef,
  struct raw_option_struct *raw_options)
{
  int i;
  const char *name = sdef->name;
  src = mputprintf(src,
    /* Count the nodes and discover the new node order only.  No node creation
       or encoding.  */
    "int %s::RAW_encode_negtest(const Erroneous_descriptor_t *p_err_descr, "
    "const TTCN_Typedescriptor_t& /*p_td*/, RAW_enc_tree& myleaf) const\n"
    "{\n"
    "  if (!is_bound()) TTCN_EncDec_ErrorContext::error"
    "(TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
    "  int idx_map[%lu];\n"
1017
1018
1019
1020
    "  int idx_map_before[%lu];\n"
    "  int idx_map_after[%lu];\n"
    "  bool altered_[%lu];\n"
    "  for (int idx_map_idx = 0; idx_map_idx < %lu; ++idx_map_idx) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1021
    "    idx_map[idx_map_idx] = -1;\n"
1022
1023
1024
1025
    "    idx_map_before[idx_map_idx] = -1;\n"
    "    idx_map_after[idx_map_idx] = -1;\n"
    "    altered_[idx_map_idx] = false;\n"
    "  };\n"
Elemer Lelik's avatar
Elemer Lelik committed
1026
    "  (void)idx_map;\n"
1027
1028
1029
    "  (void)idx_map_before;\n"
    "  (void)idx_map_after;\n"
    "  (void)altered_;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1030
1031
    "  int encoded_length = 0;\n"
    "  int num_fields = get_count();\n"
1032
    "  myleaf.isleaf = FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1033
1034
1035
    "  myleaf.body.node.num_of_nodes = 0;\n"
    "  for (int field_idx = 0; field_idx < num_fields; ++field_idx) {\n"
    "    if ((p_err_descr->omit_before != -1) &&\n"
1036
1037
    "        (field_idx < p_err_descr->omit_before)) {\n"
    "      altered_[field_idx] = true;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1038
    "      continue;\n"
1039
    "    }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1040
1041
    "    const Erroneous_values_t *err_vals = p_err_descr->get_field_err_values(field_idx);\n"
    "    const Erroneous_descriptor_t *emb_descr = p_err_descr->get_field_emb_descr(field_idx);\n"
1042
1043
    "    if (err_vals && err_vals->before) {\n"
    "      idx_map_before[field_idx] = myleaf.body.node.num_of_nodes;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1044
    "      ++myleaf.body.node.num_of_nodes;\n"
1045
    "    }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1046
1047
    "    if (err_vals && err_vals->value) {\n"
    "      if (err_vals->value->errval) {\n"
1048
    "        idx_map[field_idx] = myleaf.body.node.num_of_nodes;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1049
1050
    "        ++myleaf.body.node.num_of_nodes;\n"
    "      }\n"
1051
    "      altered_[field_idx] = true;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1052
    "    } else {\n"
1053
1054
    "      if (emb_descr) altered_[field_idx] = true;\n"
    "      idx_map[field_idx] = myleaf.body.node.num_of_nodes;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1055
1056
    "      ++myleaf.body.node.num_of_nodes;\n"
    "    }\n"
1057
1058
    "    if (err_vals && err_vals->after) {\n"
    "      idx_map_after[field_idx] = myleaf.body.node.num_of_nodes;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1059
    "      ++myleaf.body.node.num_of_nodes;\n"
1060
    "    }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1061
    "    if ((p_err_descr->omit_after != -1) &&\n"
1062
1063
1064
1065
1066
1067
    "        (field_idx >= p_err_descr->omit_after)) {\n"
    "      ++field_idx;\n"
    "      while (field_idx < num_fields) {\n"
    "        altered_[field_idx] = true;\n"
    "        ++field_idx;\n"
    "      }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1068
    "      break;\n"
1069
    "    }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1070
    "  }\n",
1071
1072
1073
    name, (unsigned long)sdef->nElements, (unsigned long)sdef->nElements,
    (unsigned long)sdef->nElements, (unsigned long)sdef->nElements,
    (unsigned long)sdef->nElements);
Elemer Lelik's avatar
Elemer Lelik committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  /* Init nodes.  */
  src = mputprintf(src,
    "  myleaf.body.node.nodes =\n"
    "    init_nodes_of_enc_tree(myleaf.body.node.num_of_nodes);\n"
    "  TTCN_EncDec_ErrorContext e_c;\n"
    "  int node_pos = 0;\n"
    "  int next_optional_idx = 0;\n"
    "  const int *my_optional_indexes = get_optional_indexes();\n"
    "  for (int field_idx = 0; field_idx < num_fields; ++field_idx) {\n"
    "    boolean is_optional_field = my_optional_indexes &&\n"
    "      (my_optional_indexes[next_optional_idx] == field_idx);\n"
    "    if ((p_err_descr->omit_before != -1) &&\n"
    "        (field_idx < p_err_descr->omit_before)) {\n"
    "      if (is_optional_field) ++next_optional_idx;\n"
    "      continue;\n"
    "    }\n"
    "    const Erroneous_values_t *err_vals =\n"
    "      p_err_descr->get_field_err_values(field_idx);\n"
    "    if (err_vals && err_vals->before) {\n"
    "      if (err_vals->before->errval == NULL)\n"
    "        TTCN_error(\"internal error: erroneous before value missing\");\n"
    "      if (err_vals->before->raw) {\n"
    "        myleaf.body.node.nodes[node_pos] =\n"
1097
    "          new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1098
1099
1100
1101
1102
1103
1104
    "                           node_pos, err_vals->before->errval"
    "->get_descriptor()->raw);\n"
    "      } else {\n"
    "        if (err_vals->before->type_descr == NULL)\n"
    "          TTCN_error(\"internal error: erroneous before typedescriptor "
    "missing\");\n"
    "        myleaf.body.node.nodes[node_pos] =\n"
1105
    "          new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
    "            node_pos, err_vals->before->type_descr->raw);\n"
    "      }\n"
    "      ++node_pos;\n"
    "    }\n"
    "    if (err_vals && err_vals->value) {\n"
    "      if (err_vals->value->errval) {\n"
    "        e_c.set_msg(\"'%%s'(erroneous value): \", fld_name(field_idx));\n"
    "        if (err_vals->value->raw) {\n"
    "          myleaf.body.node.nodes[node_pos] =\n"
1115
    "            new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1116
1117
1118
1119
1120
1121
1122
    "                             node_pos, err_vals->value->errval"
    "->get_descriptor()->raw);\n"
    "        } else {\n"
    "          if (err_vals->value->type_descr == NULL)\n"
    "            TTCN_error(\"internal error: erroneous value typedescriptor "
    "missing\");\n"
    "          myleaf.body.node.nodes[node_pos] =\n"
1123
    "            new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1124
1125
1126
1127
1128
1129
1130
1131
    "              node_pos, err_vals->value->type_descr->raw);\n"
    "        }\n"
    "        ++node_pos;\n"
    "      }\n"
    "    } else {\n"
    "      e_c.set_msg(\"'%%s': \", fld_name(field_idx));\n"
    "      if (!is_optional_field || get_at(field_idx)->ispresent()) {\n"
    "        myleaf.body.node.nodes[node_pos] =\n"
1132
    "          new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    "                           node_pos, fld_descr(field_idx)->raw);\n"
    "      } else {\n"
    "        // `omitted' field.\n"
    "        myleaf.body.node.nodes[node_pos] = NULL;\n"
    "      }\n"
    "      ++node_pos;\n"
    "    }\n"
    "    if (err_vals && err_vals->after) {\n"
    "      if (err_vals->after->errval == NULL)\n"
    "        TTCN_error(\"internal error: erroneous before value missing\");\n"
    "      if (err_vals->after->raw) {\n"
    "        myleaf.body.node.nodes[node_pos] =\n"
1145
    "          new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1146
1147
1148
1149
1150
1151
1152
    "                           node_pos, err_vals->after->errval"
    "->get_descriptor()->raw);\n"
    "      } else {\n"
    "        if (err_vals->after->type_descr == NULL)\n"
    "          TTCN_error(\"internal error: erroneous after typedescriptor "
    "missing\");\n"
    "        myleaf.body.node.nodes[node_pos] =\n"
1153
    "          new RAW_enc_tree(TRUE, &myleaf, &(myleaf.curr_pos),\n"
Elemer Lelik's avatar
Elemer Lelik committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
    "            node_pos, err_vals->after->type_descr->raw);\n"
    "      }\n"
    "      ++node_pos;\n"
    "    }\n"
    "    if (is_optional_field) ++next_optional_idx;\n"
    "    if ((p_err_descr->omit_after != -1) &&\n"
    "        (field_idx >= p_err_descr->omit_after))\n"
    "      break;\n"
    "  }\n");
  /* Handling of the tricky attributes.  A simple integer array will be used
     to track changes (field positions) in the record.
     int idx_map[n_fields] = {
       -1 (= field 0. was omitted),
        1 (= field 1. can be found at index 1.),
       -2 (= field 2. is still at index 2., but its value has changed) };  */
  for (i = 0; i < sdef->raw.ext_bit_goup_num; i++) {
    if (sdef->raw.ext_bit_groups[i].ext_bit != XDEFNO) {
      src = mputprintf(src,
        "  {\n"
1173
        "    boolean in_between_modified = FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1174
1175
        "    for (int idx_map_idx = %d; idx_map_idx <= %d; ++idx_map_idx)\n"
        "      if (idx_map[idx_map_idx] < 0) {\n"
1176
        "        in_between_modified = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1177
1178
        "        break;\n"
        "      }\n"
1179
        "    if (altered_[%d] || altered_[%d] ||\n"
Elemer Lelik's avatar
Elemer Lelik committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
        "        %d != idx_map[%d] - idx_map[%d] || in_between_modified) {\n"
        "      e_c.set_msg(\"Field #%d and/or #%d: \");\n"
        "      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "        \"Conflicting negative testing attributes, extension bit "
        "group #%d will be ignored\");\n"
        "    } else {\n"
        "      int node_idx = idx_map[%d];\n"
        "      while (node_idx <= idx_map[%d] &&\n"
        "             myleaf.body.node.nodes[node_idx] == NULL)\n"
        "        node_idx++;\n"
        "      if (myleaf.body.node.nodes[node_idx]) {\n"
        "        myleaf.body.node.nodes[node_idx]->ext_bit_handling = 1;\n"
        "        myleaf.body.node.nodes[node_idx]->ext_bit = %s;\n"
        "      }\n"
        "      node_idx = idx_map[%d];\n"
        "      while (node_idx >= idx_map[%d] &&\n"
        "             myleaf.body.node.nodes[node_idx] == NULL)\n"
        "        node_idx--;\n"
        "      if (myleaf.body.node.nodes[node_idx])\n"
        "        myleaf.body.node.nodes[node_idx]->ext_bit_handling += 2;\n"
        "    }\n"
        "  }\n",
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].to - sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to, i,
        sdef->raw.ext_bit_groups[i].from,
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].ext_bit == XDEFYES ? "EXT_BIT_YES" : "EXT_BIT_REVERSE",
        sdef->raw.ext_bit_groups[i].to,
        sdef->raw.ext_bit_groups[i].from);
    }
  }
  for (i = 0; i < sdef->nElements; i++) {
    if (sdef->elements[i].isOptional) {
      src = mputprintf(src,
        "  if (field_%s.ispresent()) {\n",
        sdef->elements[i].name);
    }
    if (raw_options[i].lengthto && sdef->elements[i].raw.lengthindex == NULL
        && sdef->elements[i].raw.union_member_num == 0) {
      /* Encoding of lenghto fields.  */
      int a;
      src = mputprintf(src,
1229
1230
1231
        "  if (idx_map[%lu] >= 0 && !altered_[%lu]) {\n"
        "    size_t nof_fields = %d",
        (unsigned long)i, (unsigned long)i, sdef->elements[i].raw.lengthto_num);
Elemer Lelik's avatar
Elemer Lelik committed
1232
1233
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        src = mputprintf(src,
1234
          " + (idx_map_before[%lu] >= 0) + (idx_map_after[%lu] >= 0)",
Elemer Lelik's avatar
Elemer Lelik committed
1235
          (unsigned long)sdef->elements[i].raw.lengthto[a],
1236
          (unsigned long)sdef->elements[i].raw.lengthto[a]);
Elemer Lelik's avatar
Elemer Lelik committed
1237
1238
      }
      src = mputprintf(src,
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
        ";\n"
        "    encoded_length += %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calc = CALC_LENGTH;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->coding_descr = &%s_descr_;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.num_of_fields = nof_fields;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.unit = %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.offset = %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields = "
        "init_lengthto_fields_list(nof_fields);\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->length = %d;\n"
        "    nof_fields = 0;\n",
Elemer Lelik's avatar
Elemer Lelik committed
1250
1251
        sdef->elements[i].raw.fieldlength, (unsigned long)i,
        (unsigned long)i, sdef->elements[i].typedescrname,
1252
        (unsigned long)i, (unsigned long)i, sdef->elements[i].raw.unit,
1253
        (unsigned long)i, sdef->elements[i].raw.lengthto_offset,
1254
        (unsigned long)i, (unsigned long)i, sdef->elements[i].raw.fieldlength);
Elemer Lelik's avatar
Elemer Lelik committed
1255
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
1256
1257
1258
        src = mputprintf(src,
          "    if (idx_map[%lu] >= 0",
          (unsigned long)sdef->elements[i].raw.lengthto[a]);
Elemer Lelik's avatar
Elemer Lelik committed
1259
1260
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
1261
            " && field_%s.ispresent()",
Elemer Lelik's avatar
Elemer Lelik committed
1262
1263
1264
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
1265
1266
          ") {\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields].level = "
Elemer Lelik's avatar
Elemer Lelik committed
1267
          "myleaf.body.node.nodes[idx_map[%lu]]->curr_pos.level;\n"
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields++].pos = "
          "myleaf.body.node.nodes[idx_map[%lu]]->curr_pos.pos;\n"
          "    } else {\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->"
          "calcof.lengthto.fields[nof_fields].level = 0;\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->"
          "calcof.lengthto.fields[nof_fields++].pos = 0;\n"
          "    }\n"
          "    if (idx_map_before[%lu] >= 0) {\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields].level = "
          "myleaf.body.node.nodes[idx_map_before[%lu]]->curr_pos.level;\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields++].pos = "
          "myleaf.body.node.nodes[idx_map_before[%lu]]->curr_pos.pos;\n"
          "    }\n"
          "    if (idx_map_after[%lu] >= 0) {\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields].level = "
          "myleaf.body.node.nodes[idx_map_after[%lu]]->curr_pos.level;\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->calcof.lengthto.fields[nof_fields++].pos = "
          "myleaf.body.node.nodes[idx_map_after[%lu]]->curr_pos.pos;\n"
          "    }\n",
          (unsigned long)i,
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)i,
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)i, (unsigned long)i,
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)i,
Elemer Lelik's avatar
Elemer Lelik committed
1295
          (unsigned long)sdef->elements[i].raw.lengthto[a],
1296
1297
1298
1299
1300
1301
          (unsigned long)i,
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)i,
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          (unsigned long)i,
Elemer Lelik's avatar
Elemer Lelik committed
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
          (unsigned long)sdef->elements[i].raw.lengthto[a]);
      }
      /* Closing outer index check.  */
      src = mputstr(src,
        "  }\n");
    } else if (raw_options[i].pointerto) {
      /* Encoding of pointerto fields.  */
      if (sdef->elements[sdef->elements[i].raw.pointerto].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[sdef->elements[i].raw.pointerto].name);
      }
      src = mputprintf(src,
1315
        "  boolean in_between_modified_pointerto_%s = FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1316
1317
        "  for (int idx_map_idx = %d; idx_map_idx <= %d; ++idx_map_idx) {\n"
        "    if (idx_map[idx_map_idx] < 0) {\n"
1318
        "      in_between_modified_pointerto_%s = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1319
1320
1321
        "      break;\n"
        "    }\n"
        "  }\n"
1322
        "  if (altered_[%lu] || altered_[%lu] ||\n"
Elemer Lelik's avatar
Elemer Lelik committed
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
        "      %lu != idx_map[%lu] - idx_map[%lu] || in_between_modified_pointerto_%s) {\n"
        "    e_c.set_msg(\"Field '%s' and/or '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, POINTERTO "
        "attribute will be ignored\");\n"
        "  } else {\n"
        "    encoded_length += %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calc = CALC_POINTER;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->coding_descr = &%s_descr_;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.pointerto.unit = %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.pointerto.ptr_offset = %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.pointerto.ptr_base = %d;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.pointerto.target.level = "
        "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->calcof.pointerto.target.pos = "
        "myleaf.body.node.nodes[%d]->curr_pos.pos;\n"
        "    myleaf.body.node.nodes[idx_map[%lu]]->length = %d;\n"
        "  }\n",
        sdef->elements[i].name,
        i, sdef->elements[i].raw.pointerto,
        sdef->elements[i].name, (unsigned long)i,
        (unsigned long)sdef->elements[i].raw.pointerto,
        (unsigned long)sdef->elements[i].raw.pointerto - (unsigned long)i,
        (unsigned long)sdef->elements[i].raw.pointerto, (unsigned long)i,
        sdef->elements[i].name, sdef->elements[i].name,
        sdef->elements[sdef->elements[i].raw.pointerto].name,
        sdef->elements[i].raw.fieldlength, (unsigned long)i,
        (unsigned long)i, sdef->elements[i].typedescrname,
        (unsigned long)i, sdef->elements[i].raw.unit,
        (unsigned long)i, sdef->elements[i].raw.ptroffset,
        (unsigned long)i, sdef->elements[i].raw.pointerbase,
        (unsigned long)i, sdef->elements[i].raw.pointerto,
        (unsigned long)i, sdef->elements[i].raw.pointerto,
        (unsigned long)i, sdef->elements[i].raw.fieldlength);
      if (sdef->elements[sdef->elements[i].raw.pointerto].isOptional) {
        src = mputprintf(src,
        "  } else {\n"
        "    INTEGER atm;\n"
        "    atm = 0;\n"
        "    encoded_length += atm.RAW_encode(%s_descr_, "
        "*myleaf.body.node.nodes[idx_map[%lu]]);\n"
        "  }\n",
        sdef->elements[i].typedescrname, (unsigned long)i);
      }
    } else {
      /* Encoding of normal fields with no negative testing.  Needed for the
         later phase.  */
      src = mputprintf(src,
1371
        "  if (!altered_[%lu] && idx_map[%lu] >= 0) "
Elemer Lelik's avatar
Elemer Lelik committed
1372
1373
        "encoded_length += field_%s%s.RAW_encode(%s_descr_, "
        "*myleaf.body.node.nodes[idx_map[%lu]]);\n",
1374
        (unsigned long)i, (unsigned long)i, sdef->elements[i].name,
Elemer Lelik's avatar
Elemer Lelik committed
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
        sdef->elements[i].isOptional ? "()" : "",
        sdef->elements[i].typedescrname, (unsigned long)i);
    }
    if (sdef->elements[i].isOptional) {
      src = mputstr(src,
        "  }\n");
    }
  }
  for (i = 0; i < sdef->nElements; i++) {
    /* Fill presence, tag and crosstag.  */
    if (raw_options[i].lengthto && sdef->elements[i].raw.lengthindex) {
      /* Encoding of lenghto fields with lengthindex.  */
      int a;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[i].name);
      }
      src = mputprintf(src,
1394
        "  if (altered_[%lu]) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1395
1396
1397
1398
1399
1400
        "    e_c.set_msg(\"Field '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, LENGTHTO/LENGTHINDEX "
        "attribute will be ignored\");\n"
        "  } else {\n"
        "    if (myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]) {\n"
1401
        "      boolean negtest_confl_lengthto = FALSE;\n",
Elemer Lelik's avatar
Elemer Lelik committed
1402
1403
1404
1405
        (unsigned long)i, sdef->elements[i].name, (unsigned long)i,
        sdef->elements[i].raw.lengthindex->nthfield);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        src = mputprintf(src,
1406
          "      if (altered_[%lu]) {\n"
1407
          "        negtest_confl_lengthto = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
          "        e_c.set_msg(\"Field '%s': \");\n"
          "        TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
          "          \"Conflicting negative testing attributes, LENGTHTO/LENGTHINDEX "
          "attribute will be ignored\");\n"
          "      }\n",
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
      }
      src = mputprintf(src,
        "      if (!negtest_confl_lengthto) {\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
        "calc = CALC_LENGTH;\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
        "coding_descr = &%s_descr_;\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
        "calcof.lengthto.num_of_fields = %d;\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
        "calcof.lengthto.unit = %d;\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
1427
1428
        "calcof.lengthto.offset = %d;\n"
        "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
Elemer Lelik's avatar
Elemer Lelik committed
1429
1430
1431
1432
1433
1434
1435
1436
1437
        "calcof.lengthto.fields = init_lengthto_fields_list(%d);\n",
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.lengthindex->typedescr,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.lengthto_num,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
        sdef->elements[i].raw.unit,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
1438
1439
        sdef->elements[i].raw.lengthto_offset,
        (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield,
Elemer Lelik's avatar
Elemer Lelik committed
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
        sdef->elements[i].raw.lengthto_num);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "        if (field_%s.ispresent()) {\n",
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
          "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]"
          "->calcof.lengthto.fields[%d].level = "
          "myleaf.body.node.nodes[idx_map[%d]]->curr_pos.level;\n"
          "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]"
          "->calcof.lengthto.fields[%d].pos = "
          "myleaf.body.node.nodes[idx_map[%d]]->curr_pos.pos;\n",
          (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield, a,
          sdef->elements[i].raw.lengthto[a], (unsigned long)i,
          sdef->elements[i].raw.lengthindex->nthfield, a,
          sdef->elements[i].raw.lengthto[a]);
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "        } else {\n"
            "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
            "calcof.lengthto.fields[%d].level = 0;\n"
            "        myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[%d]->"
            "calcof.lengthto.fields[%d].pos = 0;\n"
            "        }\n",
           (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield, a,
           (unsigned long)i, sdef->elements[i].raw.lengthindex->nthfield, a);
        }
      }
      /* Closing index check.  */
      src = mputstr(src,
        "      }\n");
      src = mputstr(src, "    }\n  }\n");
      if (sdef->elements[i].isOptional) {
        src = mputstr(src, "  }\n");
      }
    }
    if (raw_options[i].lengthto && sdef->elements[i].raw.union_member_num) {
      /* Encoding of lenghto fields.  */
      int a;
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) ", sdef->elements[i].name);
      }
      src = mputprintf(src,
        "  {\n"
1487
        "  if (altered_[%lu]) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1488
1489
1490
1491
1492
        "    e_c.set_msg(\"Field '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, LENGTHTO/LENGTHINDEX "
        "attribute will be ignored\");\n"
        "  } else {\n"
1493
        "    boolean negtest_confl_lengthto = FALSE;\n",
Elemer Lelik's avatar
Elemer Lelik committed
1494
1495
1496
        (unsigned long)i, sdef->elements[i].name);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        src = mputprintf(src,
1497
          "  if (altered_[%lu]) {\n"
1498
          "    negtest_confl_lengthto = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
          "    e_c.set_msg(\"Field '%s': \");\n"
          "      TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
          "        \"Conflicting negative testing attributes, LENGTHTO/LENGTHINDEX "
          "attribute will be ignored\");\n"
          "  }\n",
          (unsigned long)sdef->elements[i].raw.lengthto[a],
          sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
      }
      src = mputprintf(src,
        "    if (!negtest_confl_lengthto) {\n"
        "      int sel_field = 0;\n"
        "      while (myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field] == NULL) "
        "{ sel_field++; }\n"
        "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]->"
        "calc = CALC_LENGTH;\n"
        "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]->"
        "calcof.lengthto.num_of_fields = %d;\n"
        "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]->"
        "calcof.lengthto.unit = %d;\n"
        "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]->"
1519
1520
        "calcof.lengthto.offset = %d;\n"
        "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]->"
Elemer Lelik's avatar
Elemer Lelik committed
1521
1522
1523
1524
        "calcof.lengthto.fields = init_lengthto_fields_list(%d);\n",
        (unsigned long)i, (unsigned long)i,
        (unsigned long)i, sdef->elements[i].raw.lengthto_num,
        (unsigned long)i, sdef->elements[i].raw.unit,
1525
        (unsigned long)i, sdef->elements[i].raw.lengthto_offset,
Elemer Lelik's avatar
Elemer Lelik committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
        (unsigned long)i, sdef->elements[i].raw.lengthto_num);
      for (a = 0; a < sdef->elements[i].raw.lengthto_num; a++) {
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "      if (field_%s.ispresent()) {\n",
            sdef->elements[sdef->elements[i].raw.lengthto[a]].name);
        }
        src = mputprintf(src,
          "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]"
          "->calcof.lengthto.fields[%d].level = "
          "myleaf.body.node.nodes[%d]->curr_pos.level;\n"
          "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]"
          "->calcof.lengthto.fields[%d].pos = "
          "myleaf.body.node.nodes[%d]->curr_pos.pos;\n",
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a],
          (unsigned long)i, a, sdef->elements[i].raw.lengthto[a]);
        if (sdef->elements[sdef->elements[i].raw.lengthto[a]].isOptional) {
          src = mputprintf(src,
            "      } else {\n"
            "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]"
            "->calcof.lengthto.fields[%d].level = 0;\n"
            "      myleaf.body.node.nodes[idx_map[%lu]]->body.node.nodes[sel_field]"
            "->calcof.lengthto.fields[%d].pos = 0;\n"
            "      }\n",
            (unsigned long)i, a,
            (unsigned long)i, a);
        }
      }
      /* Closing inner index check.  */
      src = mputstr(src,
        "    }\n");
      /* Closing outer index check.  */
      src = mputstr(src,
        "  }\n");
      /* Closing ispresent or local block.  */
      src = mputstr(src,
        "  }\n");
    }
    if (raw_options[i].tag_type &&
        sdef->raw.taglist.list[raw_options[i].tag_type - 1].nElements) {
      /* Plain, old tag.  */
      rawAST_coding_taglist *cur_choice =
        sdef->raw.taglist.list + raw_options[i].tag_type - 1;
      src = mputprintf(src,
1570
        "  boolean negtest_confl_tag_%s = FALSE;\n"
1571
        "  if (altered_[%lu]) {\n"
1572
        "    negtest_confl_tag_%s = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
        "    e_c.set_msg(\"Field '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, TAG attribute will "
        "be ignored\");\n"
        "  }\n",
        sdef->elements[i].name, (unsigned long)i, sdef->elements[i].name,
        sdef->elements[i].name);
      src = mputprintf(src, "  if (!negtest_confl_tag_%s && (",
                       sdef->elements[i].name);
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, "field_%s.ispresent() && (", sdef->elements[i].name);
      }
      src = genRawFieldChecker(src, cur_choice, FALSE);
      if (sdef->elements[i].isOptional) src = mputstr(src, ")");
      src = mputstr(src,")) {\n");
      src = genRawTagChecker(src, cur_choice);
      src = mputstr(src,"  }\n");
    }
    if (sdef->elements[i].hasRaw && sdef->elements[i].raw.presence.nElements) {
      /* Presence attribute for fields.  */
      int taglist_idx, field_idx;
      /* The optional field itself.  */
      src = mputprintf(src,
1596
        "  boolean negtest_confl_presence_%s = FALSE;\n"
1597
        "  if (altered_[%lu]) {\n"
1598
        "    negtest_confl_presence_%s = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
        "    e_c.set_msg(\"Field '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, PRESENCE attribute "
        "will be ignored\");\n"
        "  }\n",
        sdef->elements[i].name, (unsigned long)i, sdef->elements[i].name,
        sdef->elements[i].name);
      /* Check the referenced fields.  */
      for (taglist_idx = 0;
           taglist_idx < sdef->elements[i].raw.presence.nElements; ++taglist_idx) {
        rawAST_coding_field_list *fields =
          sdef->elements[i].raw.presence.fields + taglist_idx;
        for (field_idx = 0; field_idx < fields->nElements; ++field_idx) {
          /* The top level field is enough.  >0 index is for subrefs.  */
          if (field_idx == 0) {
            rawAST_coding_fields *field = fields->fields + field_idx;
            src = mputprintf(src,
1616
              "  if (altered_[%d]) {\n"
1617
              "    negtest_confl_presence_%s = TRUE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
              "    e_c.set_msg(\"Field '%s': \");\n"
              "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
              "      \"Conflicting negative testing attributes, PRESENCE attribute "
              "will be ignored\");\n"
              "  }\n",
              field->nthfield, sdef->elements[i].name, field->nthfieldname);
          } else {
            /* Subrefs.  */
            break;
          }
        }
      }
      src = mputprintf(src, "  if (!negtest_confl_presence_%s && (",
                       sdef->elements[i].name);
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src, "field_%s.ispresent() && (", sdef->elements[i].name);
      }
      src = genRawFieldChecker(src, &sdef->elements[i].raw.presence, FALSE);
      if (sdef->elements[i].isOptional) src = mputstr(src, ")");
      src = mputstr(src, ")) {\n");
      src = genRawTagChecker(src, &sdef->elements[i].raw.presence);
      src = mputstr(src, "  }\n");
    }
    if (sdef->elements[i].hasRaw &&
        sdef->elements[i].raw.crosstaglist.nElements) {
      /* crosstag */
      int a;
      /* The union field itself.  */
      src = mputprintf(src,
1647
        "  if (altered_[%lu]) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
        "    e_c.set_msg(\"Field '%s': \");\n"
        "    TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_NEGTEST_CONFL,\n"
        "      \"Conflicting negative testing attributes, CROSSTAG attribute "
        "will be ignored\");\n"
        "  } else {\n",
        (unsigned long)i, sdef->elements[i].name);
      if (sdef->elements[i].isOptional) {
        src = mputprintf(src,
          "  if (field_%s.ispresent()) {\n",
          sdef->elements[i].name);
      }
      src = mputprintf(src,
        "  switch (field_%s%s.get_selection()) {\n",
        sdef->elements[i].name,sdef->elements[i].isOptional ? "()" : "");
      for (a = 0; a < sdef->elements[i].raw.crosstaglist.nElements; a++) {
        rawAST_coding_taglist *cur_choice =
          sdef->elements[i].raw.crosstaglist.list + a;
        if (cur_choice->nElements > 0) {
          int taglist_idx, field_idx;
          src = mputprintf(src,
            "  case %s%s%s: {\n"
1669
            "  boolean negtest_confl_crosstag = FALSE;\n",
Elemer Lelik's avatar
Elemer Lelik committed
1670
1671
1672
1673
1674
1675
1676
            sdef->elements[i].type, "::ALT_",
            cur_choice->fieldName);
          for (taglist_idx = 0;
               taglist_idx < cur_choice->nElements; ++taglist_idx) {
            rawAST_coding_field_list *fields = cur_choice->fields + taglist_idx;
            for (field_idx = 0; field_idx < fields->nElements; ++field_idx) {
              if (field_idx == 0) {
1677
                rawAST_coding_fields *field = fields->fields + field_idx;
Elemer Lelik's avatar
Elemer Lelik committed
1678
                src = mputprintf(src,
1679
                  "  if (altered_[%d]) {\n"
1680
                  "    negtest_confl_crosstag = TRUE;\n"