File: err-chk.c

package info (click to toggle)
esnacc 1.8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,452 kB
  • sloc: ansic: 42,340; cpp: 13,880; yacc: 2,682; tcl: 1,587; lex: 688; sh: 573; makefile: 111
file content (1058 lines) | stat: -rw-r--r-- 34,459 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
103
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
210
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
252
253
254
255
256
257
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
288
289
290
291
292
293
294
295
296
297
298
299
300
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
328
329
330
331
332
333
334
335
336
337
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
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
473
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
604
605
606
607
608
609
610
611
612
613
614
615
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
661
662
663
664
665
666
667
668
669
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
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
833
834
835
836
837
838
839
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
/*
 * compiler/core/err_chk.c - Check for semantic errors an ASN.1 module
 *
 * The following are checked:
 *
 * - Components of CHOICE and SET types must have distinct tags. x
 *
 * - CHOICE, ANY, and ANY DEFINED BY types cannot be implicitly tagged. x
 *
 * - Type and value names within the same scope must be unique. x
 *
 * - Field names in a SET, SEQUENCE or CHOICE must be distinct.  If
 *   a CHOICE with no field name is embedded in a SET, SEQUENCE or CHOICE,
 *   then the embedded CHOICE's field names must be distinct from its
 *   parents to avoid ambiguity in value notation. x
 *
 * - An APPLICATION tag can only be used once per module. x (done in asn1.yacc)
 *
 * - Each value in a named bit (BIT STRINGs) or named number x
 *   (INTEGERs and ENUMERATED) list must be different.
 *
 * - Each identifier in a named bit or named number list must be different. x
 *
 * - The tags on a series of one or more consecutive OPTIONAL or DEFAULT
 *   SEQUENCE elements and the following element must be distinct. x
 *
 * link_types.c does the following three checks
 * A COMPONENTS OF type in a SET must reference a SET
 * A COMPONENTS OF type in a SEQUENCE must reference a SEQUENCE
 * SELECTION types must reference a field of a CHOICE type.
 *
 * - gives a warning if an ANY DEFINED BY type appears in a SET or
 * if and ANY DEFINED BY appears in a SEQUENCE before its identifier.
 * these cases make decoding difficult.
 *
 *  ******* following are not done yet - need improved value proc. first*****
 *
 * - Each identifier in a BIT STRING value must from that BIT
 *   STRING's named bit list.
 *
 * - SET or SEQUENCE values can be empty {} only if the SET or
 *   SEQUENCE type was defined as empty or all of its elements are marked
 *   as OPTIONAL or DEFAULT.
 *
 * Mike Sample
 * 92/07/13
 *
 * Copyright (C) 1991, 1992 Michael Sample
 *            and the University of British Columbia
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * $Header: /baseline/SNACC/compiler/core/err-chk.c,v 1.11 2004/04/06 15:13:41 gronej Exp $
 * $Log: err-chk.c,v $
 * Revision 1.11  2004/04/06 15:13:41  gronej
 * *** empty log message ***
 *
 * Revision 1.10  2004/03/31 20:03:19  leonberp
 * resolved many gcc compile warnings
 *
 * Revision 1.9  2004/01/14 19:07:52  gronej
 * Updated Compiler to accept and process relative-oid's
 *
 * Revision 1.8  2003/07/28 11:11:23  colestor
 * Changes to complete handing of the "--snacc namespace" compiler directive.
 * Also, updates to handle ASN.1 constant integer tag designations for C++/C.
 *
 *
 * Revision 1.7  2003/07/07 14:50:13  nicholar
 * Eliminated headers and cleaned up include references
 *
 * Revision 1.6  2003/04/29 21:09:12  leonberp
 * integerated Deepak's changes for IOB support
 *
 * Revision 1.5  2002/09/16 16:49:22  mcphersc
 * Fixed warnings
 *
 * Revision 1.4  2002/09/04 18:58:39  leonberp
 * Enhanced ANY DEFINED BY check to handle int, oid, enum, or choice of them.
 *
 * Revision 1.3  2002/09/04 18:23:07  vracarl
 * got rid of c++ comments
 *
 * Revision 1.2  2000/10/24 14:54:51  rwc
 * Updated to remove high-level warnings (level 4 on MSVC++) for an easier build.
 * SOME warnings persist due to difficulty in modifying the SNACC compiler to
 * properly build clean source; also some files are built by Lex/Yacc.
 *
 * Revision 1.1.1.1  2000/08/21 20:35:59  leonberp
 * First CVS Version of SNACC.
 *
 * Revision 1.3.1.1  1997/08/20 23:14:40  povey
 *
 *
 * Revision 1.3  1995/07/25 19:41:25  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.2  1994/09/01  00:33:02  rj
 * snacc_config.h removed; err_chk.h includet.
 *
 * Revision 1.1  1994/08/28  09:49:05  rj
 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
 *
 */

#include <string.h>
#include <ctype.h>
#include "asn-incl.h"
#include "asn1module.h"
#include "snacc-util.h"
#include "define.h"


typedef struct DefinedTag
{
    Tag *tag;
    struct DefinedTag *next;
} DefinedTag;


typedef struct DefinedName
{
    char *name;
    struct DefinedName *next;
} DefinedName;


static NamedType *badNamedType;
// static DefinedName *fieldNames = NULL;


int	 CountTags PROTO ((Type *t));
enum BasicTypeChoiceId ParanoidGetBuiltinType PARAMS ((t),Type *t);

void ErrChkTypeDef PROTO ((Module *m, TypeDef *td));
void ErrChkType PROTO ((Module *m, TypeDef *td, Type *parent, NamedType *nt, Type *t));
void ErrChkElmtTypes PROTO ((Module *m, TypeDef *td, Type *parent, NamedTypeList *e));
void ErrChkBasicType PROTO ((Module  *m, TypeDef *td, Type *parent, NamedType *nt, Type *type));
void ErrChkValueDef PROTO ((Module *m, ValueDef *vd));
void ErrChkValue PROTO ((Module *m, ValueDef *vd, Value *v));
int HasDistinctTags PROTO ((NamedTypeList *elmts));
int AddFirstTag PROTO ((DefinedObj **definedTags, Type *t));
void ChkFieldNames PROTO ((Module *m, TypeDef *td, Type *parent, NamedTypeList *elmts));
void ChkNamedNumbers PROTO ((Module *m, Type *t, NamedNumberList *n));
void ChkNamedBits PROTO ((Module *m, Type *t, NamedNumberList *n));
void ChkSeqTags PROTO ((Module *m, TypeDef *td, Type *t));
char *DetermineCode(Tag *tag, int *ptagLen, int bJustIntegerFlag);


extern FILE* errFileG;		/* Pointer to file for reporting errors */



/* return TRUE if the Tag *t1 and t2 are the same in class and code */
int
TagObjCmp (void *vt1, void *vt2)
{
    Tag *t1 = vt1;
    Tag *t2 = vt2;
    int iResult = 0;
    if (t1->valueRef == NULL && t2->valueRef == NULL)
    {
        iResult = (t1->tclass == t2->tclass && t1->code == t2->code);
    }
    else
    {           // RWC;THEN we need to check further, may be indirectly referenced.
                // THIS logic assumes similar types, only Integer value returned...
        char *p1 = DetermineCode(t1, NULL, 1);
        if (p1)
        {
            char *p1Tmp = strdup(p1);
                            // "static" memory, so 1 needs to be copied.
            char *p2 = DetermineCode(t2, NULL, 1);
            if (p2)
               if (strcmp(p1Tmp, p2) == 0) 
                  iResult = 1;
            free(p1Tmp);
        }       // END IF p1
    }
    return(iResult);
}


/*
 * Checks for errors listed above.
 * sets module status to MOD_ERROR if any errors occured
 */
void
ErrChkModule PARAMS ((m),
    Module *m)
{
    TypeDef *td;
    ValueDef *vd=NULL;
    DefinedObj *typeNames;
    DefinedObj *valueNames;
    ImportModule *impList;
    ImportElmt *impElmt;

    /*
     * go through each type in typeList
     */
    typeNames = NewObjList();
    FOR_EACH_LIST_ELMT (td, m->typeDefs)
    {
        /* first check for name conflicts */
        if (ObjIsDefined (typeNames, td->definedName, StrObjCmp))
        {
            PrintErrLoc (m->asn1SrcFileName, (long)td->type->lineNo);
            fprintf (errFileG, "ERROR - type \"%s\" is multiply defined.\n",
				td->definedName);
            m->status = MOD_ERROR;
        }
        else
            DefineObj (&typeNames, td->definedName);

        /* now check type def internals */
        ErrChkTypeDef (m, td);
    }

    /*  now check for name conflicts with imported types */
    FOR_EACH_LIST_ELMT (impList, m->imports)
    {
        FOR_EACH_LIST_ELMT (impElmt, impList->importElmts)
        {
            if ((!impElmt->privateScope) && (isupper (impElmt->name[0])))
            {
                if (ObjIsDefined (typeNames, impElmt->name, StrObjCmp))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)impElmt->lineNo);
                    fprintf (errFileG, "ERROR - type \"%s\" is multiply defined.\n",
						impElmt->name);
                    m->status = MOD_ERROR;
                }
                else
                    DefineObj (&typeNames, impElmt->name);
            }
        }
    }
    FreeDefinedObjs (&typeNames);


    /*
     *  go through each value for types
     */
    valueNames = NewObjList();
    FOR_EACH_LIST_ELMT (vd, m->valueDefs)
    {
        /* check for name conflict */
        if (ObjIsDefined (valueNames, vd->definedName, StrObjCmp))
        {
            PrintErrLoc (m->asn1SrcFileName, (long)vd->value->lineNo);
            fprintf (errFileG, "ERROR - value \"%s\" is multiply defined.\n",
				vd->definedName);
            m->status = MOD_ERROR;
        }
        else
            DefineObj (&valueNames, vd->definedName);

        /* check value internal info */
        ErrChkValueDef (m, vd);
    }
    /*  now check for name conflicts with imported values */
    FOR_EACH_LIST_ELMT (impList, m->imports)
    {
        FOR_EACH_LIST_ELMT (impElmt, impList->importElmts)
        {
            if ((!impElmt->privateScope) && (islower (impElmt->name[0])))
            {
                if (ObjIsDefined (valueNames, impElmt->name, StrObjCmp))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)impElmt->lineNo);
                    fprintf (errFileG, "ERROR - value \"%s\" is multiply defined.\n",
						vd->definedName);
                    m->status = MOD_ERROR;
                }
                else
                    DefineObj (&valueNames, impElmt->name);
            }
        }
    }


    FreeDefinedObjs (&valueNames);

}   /* ErrChkModule */



void
ErrChkTypeDef PARAMS ((m, td),
    Module *m _AND_
    TypeDef *td)
{
   if (td == NULL)
       return;

   ErrChkType (m, td, NULL, NULL, td->type);

} /* ErrChkTypeDef */



void
ErrChkType PARAMS ((m, td, parent, nt, t),
    Module *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedType *nt _AND_
    Type *t)
{
    if (t == NULL)
        return;

    ErrChkBasicType (m, td, parent, nt, t);

} /* ErrChkType */



void
ErrChkElmtTypes PARAMS ((m, td, parent, e),
    Module *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedTypeList *e)
{
    NamedType *nt;

    /*
     * if starting new type aggregate type,
     * check that the field names are distinct
     * (goes 'through' un-named elements that are CHOICEs)
     */
    if (td->type == parent)
    {
        ChkFieldNames (m, td, parent, e);
    }


    FOR_EACH_LIST_ELMT (nt, e)
    {
        ErrChkType (m, td, parent, nt, nt->type);
    }
}  /* ErrChkElmtTypes */



void
ErrChkBasicType PARAMS ((m, td, parent, tnt, type),
    Module  *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedType *tnt _AND_
    Type *type)
{
    NamedType *nt=NULL;
    Type *refdType;
    enum BasicTypeChoiceId refdTypeId;

    if ((type == NULL) || (type->basicType == NULL))
        return;

    switch (type->basicType->choiceId)
    {
        case BASICTYPE_LOCALTYPEREF:
        case BASICTYPE_IMPORTTYPEREF:
            /*
             * make sure that untagged CHOICE and ANY types
             * are not implicitly tagged
             */
            refdTypeId = ParanoidGetBuiltinType (type);
            if ((type->implicit) &&
                 ((refdTypeId == BASICTYPE_CHOICE) ||
                  (refdTypeId == BASICTYPE_ANY) ||
                  (refdTypeId == BASICTYPE_ANYDEFINEDBY)) &&
                (CountTags (type->basicType->a.localTypeRef->link->type) == 0))
            {
                m->status = MOD_ERROR;
                PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                fprintf (errFileG, "ERROR - IMPLICITLY tagged CHOICE, ANY or ANY DEFINED BY type.\n");
            }

            if ((parent != NULL) &&
                ((refdTypeId == BASICTYPE_ANY) ||
                 (refdTypeId == BASICTYPE_ANYDEFINEDBY)))
            {

                /*
                 * give a warning.  It is stupid to have an ANY DEFINED
                 * BY type in a SET since they are not ordered and hence
                 * the ANY DEFINED BY type may need to be decoded before
                 * its identifer which is very difficult
                 */
                if ((refdTypeId == BASICTYPE_ANYDEFINEDBY) &&
                    (parent->basicType->choiceId == BASICTYPE_SET))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - ANY DEFINED BY in a SET needs to be decoded before its identifier. This is not guaranteed since SETs are not ordered.  Use a SEQUENCE instead, if possible.\n");
                }

                /*
                 * give a warning.  It is stupid to have an ANY DEFINED
                 * BY type in a SEQUENCE before its identifier.
                 * The ANY DEFINED BY type will need to be decoded before
                 * its identifer which is very difficult.
                 * tnt is the NamedType holding "type"
                 */
                if ((refdTypeId == BASICTYPE_ANYDEFINEDBY) && (tnt != NULL) &&
                    (parent->basicType->choiceId == BASICTYPE_SEQUENCE) &&
                    (GetAsnListElmtIndex (tnt, parent->basicType->a.sequence) <
                     GetAsnListElmtIndex (type->basicType->a.anyDefinedBy->link, parent->basicType->a.sequence)))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - ANY DEFINED BY in SEQUENCE should appear before its identifier since the identifier must be decoded before the ANY DEFINED BY type.\n");
                }


                if (parent->basicType->choiceId == BASICTYPE_SEQUENCE)
                    nt = LAST_LIST_ELMT (parent->basicType->a.sequence);

                /*
                 * untagged, optional ANYs are strange and will cause faulty
                 * decoding code to be generated unless they are the last
                 * elmt in a SEQUENCE.
                 * (if they are the last elmt it is easy to check
                 *  for the presence of the ANY if definite lengths are used)
                 * (must peek ahead for EOC otherwise)
                 */
                if (!((parent->basicType->choiceId == BASICTYPE_SEQUENCE) &&
                      (type == nt->type)) &&
                    (type->optional) && (CountTags (type) == 0))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - untagged optional ANY encountered, the produced code will be wrong.\n");
                }

            }

            break;


        case BASICTYPE_INTEGER:
        case BASICTYPE_ENUMERATED:
            ChkNamedNumbers (m, type, type->basicType->a.integer);
            break;

        case BASICTYPE_BITSTRING:
            ChkNamedBits (m, type, type->basicType->a.bitString);
            break;


        case BASICTYPE_SEQUENCEOF:
        case BASICTYPE_SETOF:
            ErrChkType (m, td, type, NULL, type->basicType->a.setOf);
            break;

        case BASICTYPE_SEQUENCE:
            ErrChkElmtTypes (m, td, type, type->basicType->a.sequence);

            /*
             * check that tags on one or more consecutive optional elmts
             * and following (if any) non-optional elmt are distinct
             */
            ChkSeqTags (m, td, type);
            break;

        case BASICTYPE_OBJECTCLASS:		// Deepak: 14/Mar/2003
            ErrChkElmtTypes (m, td, type, type->basicType->a.objectclass->classdef);

            /*
             * check that tags on one or more consecutive optional elmts
             * and following (if any) non-optional elmt are distinct
             */
            ChkSeqTags (m, td, type);	// Deepak: ????? chk for Class Tags???
            break;

        case BASICTYPE_CHOICE:
            /* CHOICE elements must have distinct tags */
            if (!HasDistinctTags (type->basicType->a.choice))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                fprintf (errFileG, "ERROR - tag conflict among the CHOICE elements.\n");
                m->status = MOD_ERROR;
            }

            /*
             * untagged choices cannot be implicitily tagged
             * (this would make it impossible/difficult to figure out which
             * elmt of the choice was present when decoding)
             */
            if (((type->tags == NULL) || LIST_EMPTY (type->tags)) &&
                 (type->implicit))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                fprintf (errFileG, "ERROR - IMPLICITLy tagged CHOICE type.\n");
                m->status = MOD_ERROR;
            }

            /* Check out each of the components */
            ErrChkElmtTypes (m, td, type, type->basicType->a.choice);


            break;

            case BASICTYPE_ANYDEFINEDBY:
                /* for ANY DEFINED BY make sure id field is int or oid */
                refdType = GetType (type->basicType->a.anyDefinedBy->link->type);
                if ((refdType->basicType->choiceId != BASICTYPE_INTEGER) &&
                    (refdType->basicType->choiceId != BASICTYPE_ENUMERATED) &&
                    (refdType->basicType->choiceId != BASICTYPE_OID) &&
                    (refdType->basicType->choiceId != BASICTYPE_RELATIVE_OID))
                {
                    if (refdType->basicType->choiceId == BASICTYPE_CHOICE)
 		            {
 		                NamedType* nt;
 		                
 		                FOR_EACH_LIST_ELMT(nt, refdType->basicType->a.choice)
 		                {
							enum BasicTypeChoiceId choiceId = nt->type->basicType->choiceId;

							if (choiceId != BASICTYPE_INTEGER &&
								choiceId != BASICTYPE_ENUMERATED &&
								choiceId != BASICTYPE_OID &&
                                choiceId != BASICTYPE_RELATIVE_OID)
							{
 								  PrintErrLoc(m->asn1SrcFileName, (long)type->lineNo);
 								  fprintf(errFileG, "ERROR - Field referenced by ANY DEFINED BY type must be of INTEGER or OBJECT IDENTIFIER type.\n");
 								  m->status = MOD_ERROR;
 							}
						}
					}
 		            else
 		            {

                       PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                       fprintf (errFileG, "ERROR - Field referenced by ANY DEFINED BY type must be of INTEGER or OBJECT IDENTIFIER type.\n");
                       m->status = MOD_ERROR;
                    }
                }
                /* make sure id field is not optional */
                if (type->basicType->a.anyDefinedBy->link->type->optional)
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "ERROR - Field referenced by ANY DEFINED BY cannot be optional.\n");
                    m->status = MOD_ERROR;
                }

                /*
                 * give a warning.  It is stupid to have an ANY DEFINED
                 * BY type in a SET since they are not ordered and hence
                 * the ANY DEFINED BY type may need to be decoded before
                 * its identifer which is very difficult
                 */
                if ((parent != NULL) &&
                    (parent->basicType->choiceId == BASICTYPE_SET))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - ANY DEFINED BY in a SET needs to be decoded before its identifier. This is not guaranteed since SETs are not ordered.  Use a SEQUENCE instead, if possible.\n");
                }

                /*
                 * give a warning.  It is stupid to have an ANY DEFINED
                 * BY type in a SEQUENCE before its identifier.
                 * The ANY DEFINED BY type will need to be decoded before
                 * its identifer which is very difficult.
                 * tnt is the NamedType holding "type"
                 */
                if ((parent != NULL) && (tnt != NULL) &&
                    (parent->basicType->choiceId == BASICTYPE_SEQUENCE) &&
                    (GetAsnListElmtIndex (tnt, parent->basicType->a.sequence) <
                     GetAsnListElmtIndex (type->basicType->a.anyDefinedBy->link, parent->basicType->a.sequence)))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - ANY DEFINED BY in SEQUENCE should appear before its identifier since the identifier must be decoded before the ANY DEFINED BY type.\n");
                }


            /* fall through - arrrrrg! */


            case BASICTYPE_ANY:
            /* ANY cannot be implicitily tagged */
            if (((type->tags == NULL) || LIST_EMPTY (type->tags)) &&
                 (type->implicit))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                fprintf (errFileG, "ERROR - IMPLICITLy tagged ANY type.\n");
                m->status = MOD_ERROR;
            }


            if (parent != NULL)
            {
                if (parent->basicType->choiceId == BASICTYPE_SEQUENCE)
                    nt = LAST_LIST_ELMT (parent->basicType->a.sequence);

                /*
                 * untagged, optional ANYs are strange and will cause faulty
                 * decoding code to be generated unless they are the last
                 * elmt in a SEQUENCE
                 */
                if (!((parent->basicType->choiceId == BASICTYPE_SEQUENCE) &&
                      (type == nt->type)) &&
                    (type->optional) && (CountTags (type) == 0))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - untagged optional ANY encountered, the produced code will be wrong.\n");
                }

                /*
                 *  if parent is SET or CHOICE then ANY or ANY DEFINED BY
                 *  should be tagged to help determine its presence
                 *
                 * NOTE: there are also probs with untagged ANYs in SEQs
                 * where the ANY is preceeded by optional elmts
                 * (err msg written in produced code)
                 */
                if (((parent->basicType->choiceId == BASICTYPE_SET) ||
                     (parent->basicType->choiceId == BASICTYPE_CHOICE)) &&
                    (CountTags (type) == 0))
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                    fprintf (errFileG, "WARNING - untagged ANY in a SET or CHOICE, the produced code will be wrong.\n");
                }
            }


            break;



        case BASICTYPE_SET:
            /* SET elements must have distinct tags */
            if (!HasDistinctTags (type->basicType->a.set))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)type->lineNo);
                fprintf (errFileG, "ERROR - tag conflict among the SET elements.\n");
                m->status = MOD_ERROR;
            }

            /* Check out each of the components */
            ErrChkElmtTypes (m, td, type, type->basicType->a.set);
            break;


        default:
          /* the rest do not need checking */
        break;
    }
}  /* ErrChkBasicType */


void
ErrChkValueDef PARAMS ((m, vd),
    Module *m _AND_
    ValueDef *vd)
{
    ErrChkValue (m, vd, vd->value);
}

void
ErrChkValue PARAMS ((m, vd, v),
    Module *m _AND_
    ValueDef *vd _AND_
    Value *v)
{

}


/*
 * returns non-zero if the first tags on the elements
 * are all different.  Otherwise 0 is returned
 *
 *  algorithm: add each tag to a list, adding only if
 *             not already in list. if there, free list
 *             and return FALSE. if finished adding tags
 *             and no duplicates occurred then return TRUE;
 */
int
HasDistinctTags PARAMS ((elmts),
    NamedTypeList *elmts)
{
    DefinedObj *tL;
    NamedType *e;

    tL = NewObjList();
    FOR_EACH_LIST_ELMT (e, elmts)
    {
        if (!AddFirstTag (&tL, e->type))
        {
            FreeDefinedObjs (&tL);
            badNamedType = e;
            return FALSE;
        }
    }
    FreeDefinedObjs (&tL);
    badNamedType = NULL;
    return TRUE;
} /* HasDistinctTags */


/*
 * puts first tag of the given type into the defined tags list
 * returns FALSE if the tag was already in the defined tags list.
 * return TRUE otherwise
 */
int
AddFirstTag PARAMS ((definedTags, t),
    DefinedObj **definedTags _AND_
    Type *t)
{
    Tag *tag;
    TagList *tl;
    int implicitRef;
    NamedType *e;

    tl = t->tags;
    if (tl != NULL)
        AsnListFirst (tl);

    implicitRef = FALSE;

    for (;;)
    {
         /*
          * get first tag from tag list local to this type if any
          */

        if ((tl != NULL) && (CURR_LIST_NODE (tl) != NULL) &&
            (CURR_LIST_ELMT (tl) != NULL))
        {
            tag = (Tag*) CURR_LIST_ELMT (tl);

            if (ObjIsDefined (*definedTags, tag, TagObjCmp))
                return FALSE;
            else
            {
                DefineObj (definedTags, tag);
                return TRUE;
            }
        }

        /*
         * follow tags of referenced types if no tags on this type
         */

        if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
             (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
        {
            if (!implicitRef)
                implicitRef = t->implicit;


            if (t->basicType->a.localTypeRef->link == NULL)
            {
                /* this should be found in the type link stage */
                fprintf (errFileG, "ERROR - unresolved type ref, cannot get tags for decoding\n");
                break;
            }
            t = t->basicType->a.localTypeRef->link->type;
            tl = t->tags;

            if (tl != NULL)
            {
                AsnListFirst (tl); /* set curr ptr to first node */
                if ((!LIST_EMPTY (tl)) && implicitRef)
                {
                    AsnListNext (tl);
                    implicitRef = FALSE;
                }
            }

        }

        /*
         * if untagged choice and no tags found yet
         */
        else if ((t->basicType->choiceId == BASICTYPE_CHOICE))
        {
            /*
             * add top level tags from each choice elmt
             */
            if (implicitRef)
            {
                fprintf (errFileG, "ERROR - IMPLICITLY Tagged CHOICE\n");
            }


            FOR_EACH_LIST_ELMT (e, t->basicType->a.choice)
            {
                if (!AddFirstTag (definedTags, e->type))
                    return FALSE;
            }

            return TRUE;
        }

        else /* could be ANY type - assume correct tagging */
            return TRUE;

    }
    return TRUE;
}  /* AddFirstTag */




/*
 *  Prints Errors if the field names of the elements are
 *  not distinct.
 *  currently an endless recursion problem here
 *  for recursive types involving CHOICEs - Fixed MS
 */
void
ChkFieldNamesRec PARAMS ((m, td, parent, elmts, fieldNames, followedTypeRefs),
    Module *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedTypeList *elmts _AND_
    DefinedObj **fieldNames _AND_
    DefinedObj **followedTypeRefs)
{
    NamedType *e;
    Type *definingType;

    FOR_EACH_LIST_ELMT (e, elmts)
    {
        definingType = ParanoidGetType (e->type);
        if (e->fieldName != NULL)
        {
            if (ObjIsDefined (*fieldNames, e->fieldName, StrObjCmp))
            {
                if (parent->basicType->a.choice == elmts)
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)e->type->lineNo);
                    fprintf (errFileG, "WARNING - field name \"%s\" is used more than once in same value notation scope.\n",
						e->fieldName);
                }
                else
                {
                    PrintErrLoc (m->asn1SrcFileName, (long)parent->lineNo);
                    fprintf (errFileG, "WARNING - field name \"%s\" in embedded CHOICE conflicts with field name in type \"%s\".",
						e->fieldName, td->definedName);
                    fprintf (errFileG, " This may lead to ambiguous value notation.\n");
                }
                /* m->status = MOD_ERROR; */
            }
            else
                DefineObj (fieldNames, e->fieldName);
        }

        /*
         * must include embedded CHOICE's field names
         * if it has no field name (this case is a reference to
         * a CHOICE) (fieldName is NULL)
         */
        else if (((e->type->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
                  (e->type->basicType->choiceId == BASICTYPE_IMPORTTYPEREF)) &&
                 (definingType->basicType->choiceId == BASICTYPE_CHOICE))
        {
            /* stop if this is a recursive ref we have already checked */
            if (!ObjIsDefined (*followedTypeRefs, e->type->basicType->a.localTypeRef->typeName, StrObjCmp))
            {
                /* push this type name so we don't go through it again */
                DefineObj (followedTypeRefs,  e->type->basicType->a.localTypeRef->typeName);
                /* pass in field type not defining type as parent for line no*/
                ChkFieldNamesRec (m, td, e->type, definingType->basicType->a.choice, fieldNames, followedTypeRefs);

                /* pop this type name since we're done checking it */
                UndefineObj (followedTypeRefs,  e->type->basicType->a.localTypeRef->typeName, StrObjCmp);
            }
        }

        /* this is an embedded CHOICE definition (fieldName is NULL) */
        else if (e->type->basicType->choiceId == BASICTYPE_CHOICE)
        {
            ChkFieldNamesRec (m, td, e->type, /* pass in field type for line */
                             definingType->basicType->a.choice, fieldNames, followedTypeRefs);
        }

    }
}  /* ChkFieldNamesRec */



/*
 * wrapper for ChkFieldNamesRec
 * Checks that the field names of an aggregate type (CHOICE/SET/SEQ)
 * are distinct.  Violations are printed to errFileG.
 */
void
ChkFieldNames PARAMS ((m, td, parent, elmts),
    Module *m _AND_
    TypeDef *td _AND_
    Type *parent _AND_
    NamedTypeList *elmts)
{
    DefinedObj *fieldNames;
    DefinedObj *followedTypeRefs;

    fieldNames = NewObjList();
    followedTypeRefs = NewObjList();

    /*
     * first define the type itself as followed to prevent
     * infinintely checking it
     */
    DefineObj (&followedTypeRefs, td->definedName);

    ChkFieldNamesRec (m, td, parent, elmts, &fieldNames, &followedTypeRefs);

    FreeDefinedObjs (&fieldNames);
    FreeDefinedObjs (&followedTypeRefs);

} /* ChkFieldNames */



/*
 * make sure that the identifiers of the named numbers are unique
 * among themselves.
 *
 * also check that the values of the named numbers are unique
 * among themselves.
 */
void
ChkNamedNumbers PARAMS ((m, t, n),
    Module *m _AND_
    Type *t _AND_
    NamedNumberList *n)
{
    DefinedObj *ids;
    DefinedObj *nums;
    ValueDef *nn;
    Value *baseVal;


    if (n == NULL)
        return;

    ids = NewObjList();
    nums = NewObjList();
    FOR_EACH_LIST_ELMT (nn, n)
    {
        if (ObjIsDefined (ids, nn->definedName, StrObjCmp))
        {
            PrintErrLoc (m->asn1SrcFileName, (long)t->lineNo);
            fprintf (errFileG, "ERROR - named numbers (%s) must have unique identifiers.\n",
				nn->definedName);
        }
        else
            DefineObj (&ids, nn->definedName);

        baseVal = GetValue (nn->value);
        if (baseVal->basicValue->choiceId != BASICVALUE_INTEGER)
        {
            PrintErrLoc (m->asn1SrcFileName, (long)t->lineNo);
            fprintf (errFileG, "ERROR - value format problem (%s)- named numbers must be integers.\n",
				nn->definedName);
        }
        else if (ObjIsDefined (nums, &baseVal->basicValue->a.integer, IntObjCmp))
        {
            PrintErrLoc (m->asn1SrcFileName, (long)t->lineNo);
            fprintf (errFileG, "ERROR - named numbers (%s) must have unique values.\n",
				nn->definedName);
        }
        else
            DefineObj (&nums, &baseVal->basicValue->a.integer);

    }

    FreeDefinedObjs (&ids);
    FreeDefinedObjs (&nums);

}  /* ChkNamedNumbers */



/*
 * The same as ChkNamedNumbers except that the elmt values must be
 * > 0 (needed for BIT STRINGs)
 */
void
ChkNamedBits PARAMS ((m, t, n),
    Module *m _AND_
    Type *t _AND_
    NamedNumberList *n)
{
    ValueDef *vd;
    Value *baseVal;

    ChkNamedNumbers (m, t, n);

    FOR_EACH_LIST_ELMT (vd, n)
    {
        baseVal = GetValue (vd->value);
        if ((baseVal->basicValue->choiceId == BASICVALUE_INTEGER) &&
            (baseVal->basicValue->a.integer < 0))
        {
            PrintErrLoc (m->asn1SrcFileName, (long)t->lineNo);
            fprintf (errFileG, "ERROR - named bits (%s) must have positive values.\n",
				vd->definedName);
        }
    }

} /* ChkNamedBits */



/*
 * check that tags on one or more consecutive optional elmts
 * and following (if any) non-optional elmt are distinct
 */
void
ChkSeqTags PARAMS ((m, td, t),
    Module *m _AND_
    TypeDef *td _AND_
    Type *t)
{
    DefinedObj *dO;
    NamedType *e;

    if (t->basicType->choiceId != BASICTYPE_SEQUENCE)
        return;

    dO = NewObjList();
    FOR_EACH_LIST_ELMT (e, t->basicType->a.sequence)
    {
        /* if optional add tag */
        if (e->type->optional || (e->type->defaultVal != NULL))
        {
            if (!AddFirstTag (&dO, e->type))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)e->type->lineNo);
                fprintf (errFileG, "ERROR - one or more consecutive optional SEQUENCE elmements and the the following non-optional elmt (if any) must have distinct tags.\n");
                m->status = MOD_ERROR;
            }
        }
        else if (dO != NULL)   /* first non-opt after opt elmts */
        {
            if (!AddFirstTag (&dO, e->type))
            {
                PrintErrLoc (m->asn1SrcFileName, (long)e->type->lineNo);
                fprintf (errFileG, "ERROR - one or more consecutive optional SEQUENCE elmements and the the following non-optional elmt (if any) must have distinct tags.\n");
                m->status = MOD_ERROR;
            }
            FreeDefinedObjs (&dO);
            dO = NewObjList();
        }
    }
    FreeDefinedObjs (&dO);
    td = td;     /* AVOIDS Compiler warnings.*/

}  /* ChkSeqTags */