Changeset 279


Ignore:
Timestamp:
Mar 28, 2007, 10:27:09 PM (18 years ago)
Author:
cinc
Message:

IH file emitter done so far.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/idl-compiler/ih-emitter_c/ih_file_emitter.c

    r278 r279  
    4646static void emitIHFileHeader(PPARSEINFO pLocalPI, PINTERFACE pif)
    4747{
    48   gchar *chrTemp;
    4948  FILE* fh=pLocalPI->outFile;
    5049
     
    5756
    5857  /* The *.h of this class contains some declarations we need */
    59   chrTemp=strlwr(g_strdup(pif->chrName));
    60   fprintf(fh, "#include \"%s.h\"\n\n", chrTemp);
    61   g_free(chrTemp);
     58  fprintf(fh, "#include \"%s.h\"\n\n", pif->chrFileStem);
     59
    6260}
    6361
     
    138136#endif
    139137#endif
     138
     139
     140/*
     141  \param pArray Pointer to the list of parameters.
     142 */
     143static void emitMethodParamStrings(PPARSEINFO pLocalPI, PINTERFACE pif, GPtrArray *pArray)
     144{
     145  FILE* fh=pLocalPI->outFile;
     146  int a;
     147
     148  for(a=0;a<pArray->len;a++)
     149    {
     150      int b;
     151      PMETHODPARAM pm=(PMETHODPARAM)g_ptr_array_index(pArray, a);
     152
     153      switch(pm->uiDirection)
     154        {
     155        case PARM_DIRECTION_IN:
     156          fprintf(fh, "  \"%s", pm->chrType);
     157          break;
     158        case PARM_DIRECTION_OUT:
     159          fprintf(fh, "  \"%s*", pm->chrType);
     160          break;
     161        case PARM_DIRECTION_INOUT:
     162
     163          break;
     164        default:
     165          fprintf(fh, "  \"%s*", pm->chrType);
     166          break;
     167        }
     168      for(b=0;b<pm->uiStar;b++)
     169        fprintf(fh, "*");
     170      fprintf(fh, "\",\n");     
     171    }
     172}
     173
     174static void emitNewMethods(PPARSEINFO pLocalPI, PINTERFACE pif)
     175{
     176  FILE* fh=pLocalPI->outFile;
     177  GPtrArray *pArray;
     178  int a;
     179
     180  pArray=pif->pMethodArray;
     181
     182  for(a=0;a<pArray->len;a++)
     183    {
     184      PMETHOD pm=(PMETHOD)g_ptr_array_index(pArray, a);
     185
     186      fprintf(fh, "/*\n * New method: %s\n */\n", pm->chrName);
     187      fprintf(fh, "#if !defined(_decl_impl_%s_%s_)\n", pif->chrName, pm->chrName);
     188      fprintf(fh, "#define _decl_impl_%s_%s_ 1\n", pif->chrName, pm->chrName);
     189      fprintf(fh, "NOM_Scope %s ", pm->mpReturn.chrType);
     190      fprintf(fh, "NOMLINK impl_%s_%s(%s *nomSelf,\n", pif->chrName, pm->chrName, pif->chrName);
     191      /* Do parameters */
     192      emitMethodParams(pLocalPI, pif, pm->pParamArray);
     193      fprintf(fh, " CORBA_Environment *ev);\n");
     194      fprintf(fh, "static char* nomIdString_%s_%s = nomMNDef_%s_%s;\n",
     195              pif->chrName, pm->chrName, pif->chrName, pm->chrName);
     196      fprintf(fh, "static char* nomFullIdString_%s_%s = nomMNFullDef_%s_%s;\n",
     197              pif->chrName, pm->chrName, pif->chrName, pm->chrName);
     198
     199      fprintf(fh, "static nomParmInfo nomParm_%s_%s = {\n", pif->chrName, pm->chrName);
     200      fprintf(fh, "  %d,  /* Number of parameters */\n", pm->pParamArray->len);
     201
     202      fprintf(fh, "  \"%s\", /* Return type */\n  {\n", pm->mpReturn.chrType);
     203      emitMethodParamStrings(pLocalPI, pif, pm->pParamArray);
     204      fprintf(fh, "  }\n}\n");
     205
     206      fprintf(fh, "#endif /* _decl_impl_%s_%s_ */\n\n", pif->chrName, pm->chrName);
     207    }
     208  fprintf(fh, "\n");
     209}
    140210
    141211static void emitOverridenMethods(PPARSEINFO pLocalPI, PINTERFACE pif)
     
    198268  int a;
    199269
    200   fprintf(fh, "/*** Overriden method table ***/\n");
     270  fprintf(fh, "/* Table of the overriden methods by this class */\n");
    201271  fprintf(fh, "static nomOverridenMethodDesc nomOverridenMethodsWPRootFolder[] = {\n");
    202272
     
    220290    }
    221291    fprintf(fh, "};\n\n");
    222 
     292}
     293
     294
     295static void emitStaticMethodTable(PPARSEINFO pLocalPI, PINTERFACE pif)
     296{
     297  FILE* fh=pLocalPI->outFile;
     298  GPtrArray *pArray;
     299  int a;
     300
     301  pArray=pif->pMethodArray;
     302
     303  fprintf(fh, "/* Description of the static methods introduced by this class */\n");
     304  fprintf(fh, "static nomStaticMethodDesc nomStaticMethods%s[] = {\n", pif->chrName);
     305
     306  for(a=0;a<pArray->len;a++)
     307    {
     308      PMETHOD pm=(PMETHOD)g_ptr_array_index(pArray, a);
     309
     310      fprintf(fh, "{\n");
     311      fprintf(fh, "  &%sClassData.%s,\n", pif->chrName,  pm->chrName);
     312      fprintf(fh, "  (nomID)&nomIdString_%s_%s,\n", pif->chrName,  pm->chrName);
     313      fprintf(fh, "  &nomFullIdString_%s_%s,  /* char *chrMethodDescriptor */\n", pif->chrName,  pm->chrName);
     314      fprintf(fh, "  (nomMethodProc*)  impl_%s_%s,\n", pif->chrName,  pm->chrName);
     315      fprintf(fh, "  &nomParm_%s_%s\n", pif->chrName,  pm->chrName);
     316      fprintf(fh, "},\n");
     317    }
     318  fprintf(fh, "};\n\n");
     319}
     320
     321
     322static void emitMetaClass(PPARSEINFO pLocalPI, PINTERFACE pif)
     323{
     324  FILE* fh=pLocalPI->outFile;
     325
     326  fprintf(fh, "/* The meta class for this class */\n");
     327  fprintf(fh, "static char * nomIdStringMetaClass_%s = \"%s\";\n\n", pif->chrName, pif->chrMetaClass);
     328}
     329
     330static void emitClassId(PPARSEINFO pLocalPI, PINTERFACE pif)
     331{
     332  FILE* fh=pLocalPI->outFile;
     333
     334  if(pif->chrMetaClass)
     335    {
     336      fprintf(fh, "/* Identify this class */\n");
     337      fprintf(fh, "static char * nomIdString_%s = \"%s\";\n\n", pif->chrName, pif->chrName);
     338    }
     339}
     340
     341
     342static void emitParentClasses(PPARSEINFO pLocalPI, PINTERFACE pif)
     343{
     344  FILE* fh=pLocalPI->outFile;
     345  GPtrArray *pArray=g_ptr_array_new();
     346  PINTERFACE pifParent=pif;
     347  int a;
     348
     349  fprintf(fh, "/* Array of parent names (chain of parents) */\n");
     350  fprintf(fh, "static char* nomParentClassNames%s[]=\n{\n", pif->chrName);
     351  /* Emit the parents. We have to output them sorted beginning from the
     352     leftmost parent. */
     353  while(pifParent->chrParent && (pifParent=findInterfaceFromName(pifParent->chrParent))!=NULLHANDLE)
     354    {
     355      g_ptr_array_add(pArray, (gpointer) pifParent);
     356    }
     357
     358  for(a=pArray->len-1; a>=0; a--)
     359    {
     360      pifParent=(PINTERFACE)g_ptr_array_index(pArray, a);
     361      fprintf(fh, "  \"%s\",\n", pifParent->chrName);
     362    }
     363  g_ptr_array_free(pArray, TRUE);
     364  fprintf(fh, "};\n\n");
     365
     366  fprintf(fh, "static char * nomIdString_Parent_%s = \"%s\";\n\n", pif->chrParent, pif->chrParent);
     367
     368  fprintf(fh, "/* Array of parent IDs (direct parents, for now NOM only support single inheritance) */\n");
     369  fprintf(fh, "static nomID nomParentClasses%s[]=\n", pif->chrName);
     370  fprintf(fh, "{\n");
     371  fprintf(fh, "  &nomIdString_Parent_%s,\n", pif->chrParent);
     372  fprintf(fh, "};\n\n");
     373}
     374
     375static gulong getNumberOfParentsInChain(PPARSEINFO pLocalPI, PINTERFACE pif)
     376{
     377  gulong ulRet=0;
     378  PINTERFACE pifParent=pif;
     379
     380  while(pifParent->chrParent && (pifParent=findInterfaceFromName(pifParent->chrParent))!=NULLHANDLE)
     381    ulRet++;
     382
     383  return ulRet;
     384}
     385
     386static gulong calculateInstanceDataSize(PPARSEINFO pLocalPI, PINTERFACE pif)
     387{
     388  int a;
     389  gulong ulRet=0;
     390  GPtrArray *pArray=pif->pInstanceVarArray;
     391 
     392  for(a=0;a<pArray->len;a++)
     393    {
     394      PMETHODPARAM piv=(PMETHODPARAM)g_ptr_array_index(pArray, a);
     395
     396      //g_printf("\t\tType:\t\t%s", piv->chrType);
     397
     398      if(piv->uiStar)
     399        ulRet+=sizeof(gpointer);
     400
     401      else if(!strcmp(piv->chrType, "gboolean"))
     402        ulRet+=sizeof(gboolean);
     403      else if(!strcmp(piv->chrType, "gpointer"))
     404        ulRet+=sizeof(gpointer);
     405      else if(!strcmp(piv->chrType, "gchar"))
     406        ulRet+=sizeof(gchar);
     407      else if(!strcmp(piv->chrType, "guchar"))
     408        ulRet+=sizeof(guchar);
     409      else if(!strcmp(piv->chrType, "gint"))
     410        ulRet+=sizeof(gint);
     411      else if(!strcmp(piv->chrType, "guint"))
     412        ulRet+=sizeof(guint);
     413      else if(!strcmp(piv->chrType, "gshort"))
     414        ulRet+=sizeof(gshort);
     415      else if(!strcmp(piv->chrType, "gushort"))
     416        ulRet+=sizeof(gushort);
     417      else if(!strcmp(piv->chrType, "glong"))
     418        ulRet+=sizeof(glong);
     419      else if(!strcmp(piv->chrType, "gulong"))
     420        ulRet+=sizeof(gulong);
     421      else if(!strcmp(piv->chrType, "gint8"))
     422        ulRet+=sizeof(gint8);
     423      else if(!strcmp(piv->chrType, "gfloat"))
     424        ulRet+=sizeof(gfloat);
     425      else if(!strcmp(piv->chrType, "gdouble"))
     426        ulRet+=sizeof(gdouble);
     427      else
     428        /* Check if it's an interface */
     429        if(findInterfaceFromName(piv->chrType))
     430          ulRet+=sizeof(gpointer);
     431        else{
     432          g_message("Warning: Unknown type \"%s\". Assuming sizeof(gpointer) for it (%s, %s).",
     433                    piv->chrType, __FILE__, __FUNCTION__);
     434          ulRet+=sizeof(gpointer);
     435        }
     436    }
     437
     438  return ulRet;
     439}
     440
     441static void emitStaticClassInfo(PPARSEINFO pLocalPI, PINTERFACE pif)
     442{
     443  FILE* fh=pLocalPI->outFile;
     444
     445  fprintf(fh, "static nomStaticClassInfo %sSCI = {\n", pif->chrName);
     446  fprintf(fh, "  0,               /* Version */\n");
     447  fprintf(fh, "  %d, /* Number of static methods introduced by this class */\n", pif->pMethodArray->len);
     448  fprintf(fh, "  %d,               /* Overrides */\n", pif->pOverrideArray->len);
     449  fprintf(fh, "  %s_MajorVersion,\n", pif->chrName);
     450  fprintf(fh, "  %s_MinorVersion,\n", pif->chrName);
     451  fprintf(fh, "  %ld,               /* Instance data size */\n",
     452          calculateInstanceDataSize(pLocalPI, pif));
     453  fprintf(fh, "  1,               /* Number of parents (multiple inheritance) */\n");
     454  fprintf(fh, "  &nomIdString_%s,\n", pif->chrName);
     455  if(pif->chrMetaClass)
     456    fprintf(fh, "  &nomIdStringMetaClass_%s,    /* Explicit meta id*/\n", pif->chrName);
     457  else
     458    fprintf(fh, "  NULL,    /* Explicit meta id*/\n");
     459  fprintf(fh, "  (nomClassDataStructure*)&%sClassData,\n", pif->chrName);
     460  fprintf(fh, "  (nomCClassDataStructure*)&%sCClassData,\n", pif->chrName);
     461  fprintf(fh, "  (nomStaticMethodDesc*)&nomStaticMethods%s,\n", pif->chrName);
     462  if(pif->chrParent)
     463    fprintf(fh, "  nomParentClasses%s,\n", pif->chrName);
     464  else
     465    fprintf(fh, "  NULL,\n");
     466  fprintf(fh, "  nomParentClassNames%s, /* Name of all the parent classes in chain */\n", pif->chrName);
     467  fprintf(fh, "  %ld,                /* Number of parents in the chain of classes */\n",
     468          getNumberOfParentsInChain( pLocalPI,  pif));
     469  fprintf(fh, "  nomOverridenMethods%s,\n", pif->chrName);
     470  fprintf(fh, "};\n\n");
     471};
     472
     473
     474
     475static void emitClassCreationFunc(PPARSEINFO pLocalPI, PINTERFACE pif)
     476{
     477  FILE* fh=pLocalPI->outFile;
     478
     479  fprintf(fh, "/*** Class creation function ***/\n\n");
     480
     481  if(pif->chrMetaClass)
     482    {
     483      /* Try to get the name of the metaclass include file. */
     484      PINTERFACE pifMeta=findInterfaceFromName(pif->chrMetaClass);
     485      if(pifMeta)
     486        fprintf(fh, "#include \"%s.h\\n", pifMeta->chrFileStem);
     487      else
     488        {
     489          fprintf(fh, "#ifndef %s\n", pif->chrMetaClass);
     490          fprintf(fh, "#error Your have to include the header file defining class \"%s\" first.\n", pif->chrMetaClass);
     491          fprintf(fh, "#endif\n\n");
     492        }
     493    }
     494  fprintf(fh, "#include \"nomgc.h\"\n");
     495  fprintf(fh, "NOMClass* NOMLINK %sNewClass(gulong ulMajor, gulong ulMinor)\n", pif->chrName);
     496  fprintf(fh, "{\n");
     497  fprintf(fh, "  NOMClass* result;\n");
     498
     499  fprintf(fh, "#ifdef __OS2__\n");
     500  fprintf(fh, "  gulong ulObj, ulOffset;\n");
     501  fprintf(fh, "  gchar thePath[CCHMAXPATH];\n");
     502  fprintf(fh, "  HMODULE hModule;\n");
     503
     504  fprintf(fh, "  g_assert(DosQueryModFromEIP( &hModule, &ulObj, CCHMAXPATH, ");
     505  fprintf(fh, "thePath, &ulOffset, (ULONG)%sNewClass)==0);\n", pif->chrName);
     506  fprintf(fh, "  g_strlcat(thePath, \".DLL\", sizeof(thePath));\n");
     507  fprintf(fh, "  if(!nomQueryUsingNameIsDLLRegistered(thePath))\n");
     508  fprintf(fh, "    {\n");
     509  fprintf(fh, "    HREGDLL hReg=nomBeginRegisterDLLWithGC();\n");
     510  fprintf(fh, "    g_assert(nomRegisterDLLByName(hReg, thePath));\n");
     511  fprintf(fh, "    nomEndRegisterDLLWithGC(hReg);\n");
     512  fprintf(fh, "    }\n");
     513  fprintf(fh, "#else\n");
     514  fprintf(fh, "#error DLL must be registered with the garbage collector!\n");
     515  fprintf(fh, "#endif\n\n");
     516
     517  if(pif->chrMetaClass)
     518    {
     519      fprintf(fh, "  /* Create the metaclass */\n");
     520      fprintf(fh, "  %sNewClass(%s_MajorVersion, %s_MinorVersion);\n",
     521              pif->chrMetaClass, pif->chrMetaClass, pif->chrMetaClass);
     522    }
     523  fprintf(fh, "  %sNewClass(%s_MajorVersion, %s_MinorVersion);\n",
     524          pif->chrParent, pif->chrParent, pif->chrParent);
     525  fprintf(fh, "  result = nomBuildClass(1, &%sSCI, ulMajor, ulMinor);\n\n", pif->chrName);
     526  fprintf(fh, "  return result;\n");
     527  fprintf(fh, "};\n\n");
    223528}
    224529
     
    252557              emitGetDataMacros(pLocalPI, pif);
    253558              emitIHClassDataStructs(pLocalPI, pif);
     559              emitNewMethods(pLocalPI, pif);
    254560              emitOverridenMethods(pLocalPI, pif);
    255561              emitOverridenMethodTable(pLocalPI, pif);
     562              emitStaticMethodTable(pLocalPI, pif);
     563              emitMetaClass(pLocalPI, pif);
     564              emitClassId(pLocalPI, pif);
     565              emitParentClasses(pLocalPI, pif);
     566              emitStaticClassInfo(pLocalPI, pif);
     567              emitClassCreationFunc(pLocalPI, pif);
    256568
    257569              emitIHFileFooter(pLocalPI, pif);
Note: See TracChangeset for help on using the changeset viewer.