#include <stdio.h>
#include "asn-incl.h"
#include "asn1module.h"
#include "mem.h"
#include "print.h"
#include "rules.h"
#include "type-info.h"
#include "util.h"
#include "gen-type.h"
static void PrintCType PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td, Type *parent, Type *t));
static void PrintCStructElmts PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td, Type *parent, Type *t));
static void PrintCChoiceIdEnum PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td, Type *parent, Type *t));
static void PrintCChoiceUnion PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td, Type *parent, Type *t));
static void PrintCChoiceTypeDef PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td));
static void PrintTypeComment PROTO ((FILE *f, TypeDef *head, Type *t));
static void PrintPreTypeDefStuff PROTO ((FILE *f, CRules *r, Module *m, TypeDef *td, Type *parent, Type *t));
void
PrintCTypeDef PARAMS ((f, r, m, td),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td)
{
CTRI *ctri;
CTDI *ctdi;
Type *t;
ctdi = td->cTypeDefInfo;
if ((ctdi == NULL) || (!ctdi->genTypeDef))
return;
t = td->type;
ctri = t->cTypeRefInfo;
PrintPreTypeDefStuff (f, r, m, td, NULL, t);
switch (ctri->cTypeId)
{
case C_TYPEREF:
case C_LIB:
case C_ANY:
case C_ANYDEFINEDBY:
case C_LIST:
fprintf (f, "typedef ");
PrintCType (f, r, m, td, NULL, t);
fprintf (f, " %s;", ctdi->cTypeName);
PrintTypeComment (f, td, t);
fprintf (f, "\n\n");
break;
case C_CHOICE:
PrintCChoiceTypeDef (f, r, m, td);
break;
case C_STRUCT:
fprintf (f, "typedef ");
fprintf (f,"%s %s", "struct", t->cTypeRefInfo->cTypeName);
PrintTypeComment (f, td, t);
fprintf (f,"\n{\n");
PrintCStructElmts (f, r, m, td, NULL, t);
fprintf (f, "} %s;", ctdi->cTypeName);
fprintf (f, "\n\n");
break;
default:
break;
}
}
static void
PrintCType PARAMS ((f, r, m, td, parent, t),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
Type *t)
{
CTRI *ctri;
CNamedElmt *n;
ctri = t->cTypeRefInfo;
if (ctri == NULL)
return;
switch (ctri->cTypeId)
{
case C_TYPEREF:
if ((t->basicType->a.localTypeRef->link->type->cTypeRefInfo->cTypeId == C_STRUCT)||
(t->basicType->a.localTypeRef->link->type->cTypeRefInfo->cTypeId == C_CHOICE))
{
fprintf (f,"struct ");
}
fprintf (f,"%s", ctri->cTypeName);
if (ctri->isPtr)
fprintf (f,"*");
break;
case C_ANY:
fprintf (f,"/* ANY- Fix Me ! */\n");
case C_ANYDEFINEDBY:
case C_LIST:
case C_LIB:
fprintf (f,"%s", ctri->cTypeName);
if ((ctri->cNamedElmts != NULL) &&
(t->basicType->choiceId == BASICTYPE_ENUMERATED))
{
fprintf (f, "\n {\n");
FOR_EACH_LIST_ELMT (n, ctri->cNamedElmts)
{
fprintf (f," %s = %d", n->name, n->value);
if (n != (CNamedElmt*)LAST_LIST_ELMT (ctri->cNamedElmts))
fprintf (f,",");
fprintf (f,"\n");
}
fprintf (f, " }");
}
if (ctri->isPtr)
fprintf (f,"*");
break;
default:
break;
}
}
static void
PrintCStructElmts PARAMS ((f, r, m, td, parent, t),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
Type *t)
{
CTRI *ctri;
NamedType *et;
NamedTypeList *elmts;
elmts = t->basicType->a.sequence;
if ((elmts == NULL) || (LIST_EMPTY (elmts)))
{
fprintf (f, " char unused; /* empty ASN1 SET/SEQ - not used */\n");
}
FOR_EACH_LIST_ELMT (et, elmts)
{
ctri = et->type->cTypeRefInfo;
fprintf (f," ");
PrintCType (f, r, m, td, t, et->type);
fprintf (f, " %s;", ctri->cFieldName);
PrintTypeComment (f, td, et->type);
fprintf (f, "\n");
}
}
static void
PrintCChoiceIdEnum PARAMS ((f, r, m, td, parent, t),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
Type *t)
{
NamedType *et;
NamedType *last;
CTRI *ctri;
ctri = t->cTypeRefInfo;
fprintf (f, " enum %s\n {\n", ctri->choiceIdEnumName);
if ((t->basicType->a.choice != NULL) &&
!(LIST_EMPTY (t->basicType->a.choice)))
last = (NamedType*)LAST_LIST_ELMT (t->basicType->a.choice);
FOR_EACH_LIST_ELMT (et, t->basicType->a.choice)
{
ctri = et->type->cTypeRefInfo;
fprintf (f," %s", ctri->choiceIdSymbol);
if (et == last)
fprintf (f, "\n");
else
fprintf (f, ",\n");
}
ctri = t->cTypeRefInfo;
fprintf (f, " } %s;", ctri->choiceIdEnumFieldName);
}
static void
PrintCChoiceUnion PARAMS ((f, r, m, td, parent, t),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
Type *t)
{
CTRI *ctri;
ctri = t->cTypeRefInfo;
fprintf (f," union %s\n {\n", ctri->cTypeName);
PrintCStructElmts (f, r, m, td, parent, t);
fprintf (f, " }");
}
static void
PrintCChoiceTypeDef PARAMS ((f, r, m, td),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td)
{
CTRI *ctri;
char *choiceName;
Type *t;
t = td->type;
ctri = t->cTypeRefInfo;
choiceName = td->cTypeDefInfo->cTypeName;
fprintf (f, "typedef ");
fprintf (f, "struct %s", choiceName);
PrintTypeComment (f, td, t);
fprintf (f,"\n{\n");
PrintCChoiceIdEnum (f, r, m, td, NULL, t);
fprintf (f,"\n");
PrintCChoiceUnion (f, r, m, td, NULL, t);
fprintf (f, " %s;", ctri->cFieldName);
fprintf (f,"\n} %s;\n\n", choiceName);
}
static void
PrintTypeComment PARAMS ((f, td, t),
FILE *f _AND_
TypeDef *td _AND_
Type *t)
{
fprintf (f," /* ");
SpecialPrintType (f, td, t);
fprintf (f," */");
}
static void
PrintPreTypeDefStuff PARAMS ((f, r, m, td, parent, t),
FILE *f _AND_
CRules *r _AND_
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
Type *t)
{
CTRI *ctri;
NamedType *et;
CNamedElmt *n;
ctri = td->type->cTypeRefInfo;
if ((ctri->cNamedElmts != NULL) &&
(t->basicType->choiceId != BASICTYPE_ENUMERATED))
{
FOR_EACH_LIST_ELMT (n, ctri->cNamedElmts)
{
fprintf(f, "\n#define %s %d", n->name, n->value);
}
fprintf (f, "\n\n");
}
else if ((t->basicType->choiceId == BASICTYPE_SET) ||
(t->basicType->choiceId == BASICTYPE_SEQUENCE) ||
(t->basicType->choiceId == BASICTYPE_CHOICE))
{
FOR_EACH_LIST_ELMT (et, t->basicType->a.set)
PrintPreTypeDefStuff (f, r, m, td, t, et->type);
}
else if ((t->basicType->choiceId == BASICTYPE_SETOF) ||
(t->basicType->choiceId == BASICTYPE_SEQUENCEOF))
{
PrintPreTypeDefStuff (f, r, m, td, t, t->basicType->a.setOf);
}
}