]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/idl-gen/gen-code.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * compiler/back_ends/idl_gen/gen_idl_code.c - routines for printing CORBA IDL code from type trees
22 * assumes that the type tree has already been run through the
23 * IDL type generator (idl_gen/types.c).
27 * Copyright (C) 1991, 1992 Michael Sample
28 * and the University of British Columbia
30 * Copyright © 1995 Robert Joop
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/compiler/back-ends/idl-gen/gen-code.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
38 * $Log: gen-code.c,v $
39 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
40 * Move from private repository to open source repository
42 * Revision 1.2 2001/05/05 00:59:28 rmurphy
43 * Adding darwin license headers
45 * Revision 1.1.1.1 1999/03/16 18:06:45 aram
46 * Originals from SMIME Free Library.
48 * Revision 1.2 1997/03/13 09:15:21 wan
49 * Improved dependency generation for stupid makedepends.
50 * Corrected PeekTag to peek into buffer only as far as necessary.
51 * Added installable error handler.
52 * Fixed small glitch in idl-code generator (Markku Savela <msa@msa.tte.vtt.fi>).
54 * Revision 1.1 1997/01/01 20:25:34 rj
61 #if STDC_HEADERS || HAVE_STRING_H
69 #include "asn1module.h"
72 #include "lib-types.h"
77 #include "snacc-util.h"
79 #include "tag-util.h" /* get GetTags/FreeTags/CountTags/TagByteLen */
85 static long int longJmpValG
= -100;
87 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
89 PrintComment
PARAMS ((idl
, m
),
96 fprintf (idl
, "// NOTE: this is a machine generated file -- editing not recommended\n");
97 fprintf (idl
, "//\n");
98 fprintf (idl
, "// %s -- IDL for ASN.1 module %s\n", m
->idlFileName
, m
->modId
->name
);
99 fprintf (idl
, "//\n");
100 fprintf (idl
, "// This file was generated by snacc on %s", ctime (&t
));
101 fprintf (idl
, "// UBC snacc written by Mike Sample\n");
102 fprintf (idl
, "// IDL generator written by Robert Joop\n");
107 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
109 PrintIncludes
PARAMS ((idl
, mods
, m
),
111 ModuleList
*mods _AND_
117 fprintf (idl
, "#include \"ASN1Types.idl\"\n");
118 fprintf (idl
, "#include \"BitString.idl\"\n");
120 tmp
= (void *)CURR_LIST_NODE (mods
); /* remember curr loc */
121 FOR_EACH_LIST_ELMT (currMod
, mods
)
122 fprintf (idl
, "#include \"%s\"\n", currMod
->idlFileName
);
123 SET_CURR_LIST_NODE (mods
, tmp
);
125 } /* PrintIncludes */
127 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
129 PrintTypeDecl
PARAMS ((f
, td
),
133 switch (td
->type
->basicType
->choiceId
)
135 case BASICTYPE_COMPONENTSOF
:
136 case BASICTYPE_SELECTION
:
137 case BASICTYPE_UNKNOWN
:
138 case BASICTYPE_MACRODEF
:
139 case BASICTYPE_MACROTYPE
:
140 return; /* do nothing */
142 case BASICTYPE_ENUMERATED
:
143 if (IsNewType (td
->type
))
144 fprintf (f
, " enum %s;\n", td
->idlTypeDefInfo
->typeName
);
148 if (IsNewType (td
->type
))
149 fprintf (f
, " struct %s;\n", td
->idlTypeDefInfo
->typeName
);
152 } /* PrintTypeDecl */
154 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
156 PrintIDLTypeAndName
PARAMS ((idl
, mods
, m
, r
, td
, parent
, t
),
158 ModuleList
*mods _AND_
166 fprintf (idl
, "union %sOptional switch (boolean) { case True: %s %s; };\n", t
->idlTypeRefInfo
->typeName
, t
->idlTypeRefInfo
->typeName
, t
->idlTypeRefInfo
->fieldName
);
168 fprintf (idl
, "%s %s;\n", t
->idlTypeRefInfo
->typeName
, t
->idlTypeRefInfo
->fieldName
);
171 if (t
->idlTypeRefInfo
->isPtr
)
175 } /* PrintIDLTypeAndName */
177 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
179 * prints typedef or new class given an ASN.1 type def of a primitive type
180 * or typeref. Uses inheritance to cover re-tagging and named elmts.
183 PrintIDLSimpleDef
PARAMS ((idl
, r
, td
),
191 fprintf (idl
, " /* ");
192 SpecialPrintType (idl
, td
, td
->type
);
193 fprintf (idl
, " */\n");
195 if (hasNamedElmts
= HasNamedElmts (td
->type
))
197 int tlen
= strlen (td
->idlTypeDefInfo
->typeName
) - strlen (r
->typeSuffix
);
198 switch (GetBuiltinType (td
->type
))
200 case BASICTYPE_INTEGER
:
201 fprintf (idl
, " typedef %s %s;\n", td
->type
->idlTypeRefInfo
->typeName
, td
->idlTypeDefInfo
->typeName
);
202 FOR_EACH_LIST_ELMT (n
, td
->type
->idlTypeRefInfo
->namedElmts
)
203 fprintf (idl
, " const %s %.*s_%s = %d;\n", td
->idlTypeDefInfo
->typeName
, tlen
, td
->idlTypeDefInfo
->typeName
, n
->name
, n
->value
);
205 case BASICTYPE_ENUMERATED
:
206 fprintf (idl
, " enum %s\n", td
->idlTypeDefInfo
->typeName
);
207 fprintf (idl
, " {\n");
208 FOR_EACH_LIST_ELMT (n
, td
->type
->idlTypeRefInfo
->namedElmts
)
210 char comma
= (n
!= (CNamedElmt
*)LAST_LIST_ELMT (td
->type
->idlTypeRefInfo
->namedElmts
)) ? ',' : ' ';
211 fprintf (idl
, " %s%c // (original value = %d)\n", n
->name
, comma
, n
->value
);
213 fprintf (idl
, " };\n");
215 case BASICTYPE_BITSTRING
:
216 fprintf (idl
, " typedef %s %s;\n", td
->type
->idlTypeRefInfo
->typeName
, td
->idlTypeDefInfo
->typeName
);
217 FOR_EACH_LIST_ELMT (n
, td
->type
->idlTypeRefInfo
->namedElmts
)
218 fprintf (idl
, " const unsigned long %.*s_%s = %d;\n", tlen
, td
->idlTypeDefInfo
->typeName
, n
->name
, n
->value
);
221 fprintf (idl
, " ???!\n");
225 fprintf (idl
, " typedef %s %s;\n\n", td
->type
->idlTypeRefInfo
->typeName
, td
->idlTypeDefInfo
->typeName
);
227 } /* PrintIDLSimpleDef */
229 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
231 PrintIDLChoiceDefCode
PARAMS ((idl
, mods
, m
, r
, td
, parent
, choice
),
233 ModuleList
*mods _AND_
242 /* put class spec in idl file */
244 /* write out choiceId enum type */
246 fprintf (idl
, " enum %s%s\n", td
->idlTypeDefInfo
->typeName
, r
->choiceEnumSuffix
);
247 fprintf (idl
, " {\n");
248 FOR_EACH_LIST_ELMT (e
, choice
->basicType
->a
.choice
)
250 fprintf (idl
, " %s", e
->type
->idlTypeRefInfo
->choiceIdSymbol
);
251 if (e
!= (NamedType
*)LAST_LIST_ELMT (choice
->basicType
->a
.choice
))
252 fprintf (idl
, ",\n");
256 fprintf (idl
, " };\n\n");
258 /* write out the choice element anonymous union */
259 fprintf (idl
, " union %s switch (%s%s)\n", td
->idlTypeDefInfo
->typeName
, td
->idlTypeDefInfo
->typeName
, r
->choiceEnumSuffix
);
260 fprintf (idl
, " {\n");
261 FOR_EACH_LIST_ELMT (e
, choice
->basicType
->a
.choice
)
264 fprintf (idl
, " case %s: %s %s;\n", e
->type
->idlTypeRefInfo
->choiceIdSymbol
, e
->type
->idlTypeRefInfo
->typeName
, e
->type
->idlTypeRefInfo
->fieldName
);
266 fprintf (idl
, " case %s: ", e
->type
->idlTypeRefInfo
->choiceIdSymbol
);
267 PrintIDLTypeAndName (idl
, mods
, m
, r
, td
, choice
, e
->type
);
270 fprintf (idl
, " };\n\n");
272 } /* PrintIDLChoiceDefCode */
275 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
277 PrintIDLSeqDefCode
PARAMS ((idl
, mods
, m
, r
, td
, parent
, seq
),
279 ModuleList
*mods _AND_
288 /* put class spec in idl file */
290 fprintf (idl
, " struct %s\n", td
->idlTypeDefInfo
->typeName
);
291 fprintf (idl
, " {\n");
293 /* write out the sequence elmts */
294 FOR_EACH_LIST_ELMT (e
, seq
->basicType
->a
.sequence
)
297 PrintIDLTypeAndName (idl
, mods
, m
, r
, td
, seq
, e
->type
);
300 /* close struct definition */
301 fprintf (idl
, " };\n\n\n");
303 } /* PrintIDLSeqDefCode */
305 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
307 PrintIDLSetDefCode
PARAMS ((idl
, mods
, m
, r
, td
, parent
, set
),
309 ModuleList
*mods _AND_
318 /* put class spec in idl file */
320 fprintf (idl
, " struct %s\n", td
->idlTypeDefInfo
->typeName
);
321 fprintf (idl
, " {\n");
323 /* write out the set elmts */
324 FOR_EACH_LIST_ELMT (e
, set
->basicType
->a
.set
)
327 PrintIDLTypeAndName (idl
, mods
, m
, r
, td
, set
, e
->type
);
330 fprintf (idl
, " };\n\n");
332 } /* PrintIDLSetDefCode */
334 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
336 PrintCxxSetOfDefCode
PARAMS ((idl
, mods
, m
, r
, td
, parent
, setOf
),
338 ModuleList
*mods _AND_
345 char *lcn
; /* list class name */
346 char *ecn
; /* (list) elmt class name */
348 lcn
= td
->idlTypeDefInfo
->typeName
;
349 ecn
= setOf
->basicType
->a
.setOf
->idlTypeRefInfo
->typeName
;
350 fprintf (idl
, " typedef sequence<%s> %s;\n", ecn
, lcn
);
352 } /* PrintCxxSetOfDefCode */
355 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
357 PrintCxxAnyDefCode
PARAMS ((idl
, mods
, m
, r
, td
, parent
, any
),
359 ModuleList
*mods _AND_
366 fprintf (idl
, " /* ");
367 SpecialPrintType (idl
, td
, td
->type
);
368 fprintf (idl
, " */\n");
369 fprintf (idl
, " typedef %s %s;\n\n", td
->type
->idlTypeRefInfo
->typeName
, td
->idlTypeDefInfo
->typeName
);
370 } /* PrintCxxAnyDefCode */
373 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
375 PrintIDLTypeDefCode
PARAMS ((idl
, mods
, m
, r
, td
),
377 ModuleList
*mods _AND_
382 switch (td
->type
->basicType
->choiceId
)
384 case BASICTYPE_BOOLEAN
: /* library type */
385 case BASICTYPE_REAL
: /* library type */
386 case BASICTYPE_OCTETSTRING
: /* library type */
387 case BASICTYPE_NULL
: /* library type */
388 case BASICTYPE_OID
: /* library type */
389 case BASICTYPE_INTEGER
: /* library type */
390 case BASICTYPE_BITSTRING
: /* library type */
391 case BASICTYPE_ENUMERATED
: /* library type */
392 PrintIDLSimpleDef (idl
, r
, td
);
395 case BASICTYPE_SEQUENCEOF
: /* list types */
396 case BASICTYPE_SETOF
:
397 PrintCxxSetOfDefCode (idl
, mods
, m
, r
, td
, NULL
, td
->type
);
400 case BASICTYPE_IMPORTTYPEREF
: /* type references */
401 case BASICTYPE_LOCALTYPEREF
:
403 * if this type has been re-tagged then
404 * must create new class instead of using a typedef
406 PrintIDLSimpleDef (idl
, r
, td
);
409 case BASICTYPE_ANYDEFINEDBY
: /* ANY types */
412 fprintf (stderr, " ANY types require modification. ");
413 fprintf (stderr, " The source files will have a \" ANY - Fix Me! \" comment before related code.\n\n");
415 PrintCxxAnyDefCode (idl
, mods
, m
, r
, td
, NULL
, td
->type
);
418 case BASICTYPE_CHOICE
:
419 PrintIDLChoiceDefCode (idl
, mods
, m
, r
, td
, NULL
, td
->type
);
423 PrintIDLSetDefCode (idl
, mods
, m
, r
, td
, NULL
, td
->type
);
426 case BASICTYPE_SEQUENCE
:
427 PrintIDLSeqDefCode (idl
, mods
, m
, r
, td
, NULL
, td
->type
);
430 case BASICTYPE_COMPONENTSOF
:
431 case BASICTYPE_SELECTION
:
432 case BASICTYPE_UNKNOWN
:
433 case BASICTYPE_MACRODEF
:
434 case BASICTYPE_MACROTYPE
:
438 } /* PrintIDLTypeDefCode */
440 /*\[sep]--------------------------------------------------------------------------------------------------------------------------*/
442 PrintIDLCode
PARAMS ((idl
, mods
, m
, r
, longJmpVal
),
444 ModuleList
*mods _AND_
447 long int longJmpVal _AND_
453 longJmpValG
= longJmpVal
;
455 PrintComment (idl
, m
);
457 PrintConditionalIncludeOpen (idl
, m
->idlFileName
);
459 PrintIncludes (idl
, mods
, m
);
462 fprintf (idl
, "module %s\n{\n\n", m
->idlname
);
464 fprintf (idl
, " //----------------------------------------------------------------------------\n");
465 fprintf (idl
, " // type declarations:\n\n");
466 FOR_EACH_LIST_ELMT (td
, m
->typeDefs
)
467 PrintTypeDecl (idl
, td
);
472 fprintf (idl
, " //----------------------------------------------------------------------------\n");
473 fprintf (idl
, " // value definitions:\n\n");
474 FOR_EACH_LIST_ELMT (vd
, m
->valueDefs
)
475 PrintIDLValueDef (idl
, r
, vd
);
479 fprintf (idl
, " //----------------------------------------------------------------------------\n");
480 fprintf (idl
, " // type definitions:\n\n");
483 PrintIDLAnyCode (idl
, r
, mods
, m
);
486 FOR_EACH_LIST_ELMT (td
, m
->typeDefs
)
488 PrintIDLTypeDefCode (idl
, mods
, m
, r
, td
);
492 fprintf (idl
, "}; // end of module %s\n", m
->idlname
);
494 PrintConditionalIncludeClose (idl
, m
->idlFileName
);
498 /*\[banner "EOF"]-----------------------------------------------------------------------------------------------------------------*/