New files for XPCOM interface generation

This commit is contained in:
vidur 1998-05-19 15:59:53 +00:00
Родитель 6b32a0861e
Коммит 2b92ea8444
4 изменённых файлов: 566 добавлений и 0 удалений

160
dom/tools/FileGen.cpp Normal file
Просмотреть файл

@ -0,0 +1,160 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <stdio.h>
#include <fstream.h>
#include <ctype.h>
#include "FileGen.h"
#include "IdlObject.h"
#include "IdlVariable.h"
#include "IdlParameter.h"
#include "IdlInterface.h"
static const char *kNPLStr = \
"/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-\n *\n * The contents of this file are subject to the Netscape Public License\n * Version 1.0 (the \"NPL\"); you may not use this file except in\n * compliance with the NPL. You may obtain a copy of the NPL at\n * http://www.mozilla.org/NPL/\n *\n * Software distributed under the NPL is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL\n * for the specific language governing rights and limitations under the\n * NPL.\n *\n * The Initial Developer of this code under the NPL is Netscape\n * Communications Corporation. Portions created by Netscape are\n * Copyright (C) 1998 Netscape Communications Corporation. All Rights\n * Reserved.\n */\n";
static const char *kDisclaimerStr = "/* AUTO-GENERATED. DO NOT EDIT!!! */\n";
static const char *kObjTypeStr = "nsIDOM%s*";
static const char *kUuidStr = "NS_IDOM%s_IID";
FileGen::FileGen()
{
mOutputFile = new ofstream();
}
FileGen::~FileGen()
{
delete mOutputFile;
}
int
FileGen::OpenFile(char *aFileName, char *aOutputDirName,
const char *aFilePrefix, const char *aFileSuffix)
{
char file_buf[512];
if (aOutputDirName) {
strcpy(file_buf, aOutputDirName);
strcat(file_buf, "/");
}
else {
file_buf[0] = '\0';
}
strcat(file_buf, aFilePrefix);
strcat(file_buf, strtok(aFileName, "."));
strcat(file_buf, ".");
strcat(file_buf, aFileSuffix);
mOutputFile->open(file_buf);
return mOutputFile->is_open();
}
void
FileGen::CloseFile()
{
mOutputFile->close();
mOutputFile->clear();
}
void
FileGen::GenerateNPL()
{
*mOutputFile << kNPLStr;
*mOutputFile << kDisclaimerStr;
}
void
FileGen::GetVariableType(char *aBuffer, IdlVariable &aVariable)
{
switch (aVariable.GetType()) {
case TYPE_BOOLEAN:
strcpy(aBuffer, "PRBool");
break;
case TYPE_LONG:
strcpy(aBuffer, "PRInt32");
break;
case TYPE_SHORT:
strcpy(aBuffer, "PRInt16");
break;
case TYPE_ULONG:
strcpy(aBuffer, "PRUint32");
break;
case TYPE_USHORT:
strcpy(aBuffer, "PRUint16");
break;
case TYPE_CHAR:
strcpy(aBuffer, "PRUint8");
break;
case TYPE_INT:
strcpy(aBuffer, "PRInt32");
break;
case TYPE_UINT:
strcpy(aBuffer, "PRUint32");
break;
case TYPE_STRING:
strcpy(aBuffer, "nsString&");
break;
case TYPE_OBJECT:
sprintf(aBuffer, kObjTypeStr, aVariable.GetTypeName());
break;
default:
// XXX Fail for other cases
break;
}
}
void
FileGen::GetParameterType(char *aBuffer, IdlParameter &aParameter)
{
GetVariableType(aBuffer, aParameter);
switch (aParameter.GetAttribute()) {
case IdlParameter::OUTPUT:
case IdlParameter::INOUT:
if (aParameter.GetType() != TYPE_STRING) {
strcat (aBuffer, "*");
}
break;
}
}
void
FileGen::GetInterfaceIID(char *aBuffer, IdlInterface &aInterface)
{
char buf[256];
char *cur_ptr = buf;
strcpy(buf, aInterface.GetName());
while (*cur_ptr != 0) {
if ((*cur_ptr >= 'a') && (*cur_ptr <= 'z')) {
*cur_ptr += ('A' - 'a');
}
cur_ptr++;
}
sprintf(aBuffer, kUuidStr, buf);
}
void
FileGen::GetCapitalizedName(char *aBuffer, IdlObject &aObject)
{
strcpy(aBuffer, aObject.GetName());
if ((aBuffer[0] >= 'a') && (aBuffer[0] <= 'z')) {
aBuffer[0] += ('A' - 'a');
}
}

53
dom/tools/FileGen.h Normal file
Просмотреть файл

@ -0,0 +1,53 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef _FileGen_h__
#define _FileGen_h__
#include <string.h>
class ofstream;
class IdlObject;
class IdlSpecification;
class IdlInterface;
class IdlVariable;
class IdlParameter;
class FileGen {
public:
FileGen();
~FileGen();
virtual void Generate(char *aFileName, char *aOutputDirName,
IdlSpecification &aSpec)=0;
protected:
void GenerateNPL();
int OpenFile(char *aFileName, char *aOutputDirName,
const char *aFilePrefix, const char *aFileSuffix);
void CloseFile();
ofstream* GetFile() { return mOutputFile; }
void GetVariableType(char *aBuffer, IdlVariable &aVariable);
void GetParameterType(char *aBuffer, IdlParameter &aParameter);
void GetInterfaceIID(char *aBuffer, IdlInterface &aInterface);
void GetCapitalizedName(char *aBuffer, IdlObject &aObject);
private:
ofstream *mOutputFile;
};
#endif // _FileGen_h__

305
dom/tools/XPCOMGen.cpp Normal file
Просмотреть файл

@ -0,0 +1,305 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>
#include <fstream.h>
#include <ctype.h>
#include "XPComGen.h"
#include "Exceptions.h"
#include "plhash.h"
#include "IdlSpecification.h"
#include "IdlInterface.h"
#include "IdlVariable.h"
#include "IdlAttribute.h"
#include "IdlFunction.h"
#include "IdlParameter.h"
static const char *kFilePrefix = "nsIDOM";
static const char *kFileSuffix = "h";
static const char *kIfdefStr = "#ifndef nsIDOM%s_h__\n#define nsIDOM%s_h__\n\n";
static const char *kIncludeISupportsStr = "#include \"nsISupports.h\"\n";
static const char *kIncludeStr = "#include \"nsIDOM%s.h\"\n";
static const char *kForwardClassStr = "class nsIDOM%s;\n";
static const char *kUuidStr = "#define %s \\\n { 0x6f7652e0, 0xee43, 0x11d1,\\\n { x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }\n\n";
static const char *kClassDeclStr = "class nsIDOM%s : ";
static const char *kBaseClassStr = "public nsIDOM%s";
static const char *kNoBaseClassStr = "public nsISupports";
static const char *kClassPrologStr = " {\npublic:\n";
static const char *kConstDeclStr = " const %s %s = %l;\n";
static const char *kGetterMethodDeclStr = "\n virtual nsresult Get%s(%s%s a%s)=0;\n";
static const char *kSetterMethodDeclStr = " virtual nsresult Set%s(%s a%s)=0;\n";
static const char *kMethodDeclStr = "\n virtual nsresult %s(%s)=0;\n";
static const char *kParamStr = "%s a%s, ";
static const char *kReturnStr = "%s%s aReturn";
static const char *kClassEpilogStr = "};\n";
static const char *kEndifStr = "endif // nsIDOM%s_h__\n";
XPCOMGen::XPCOMGen()
{
}
XPCOMGen::~XPCOMGen()
{
}
void
XPCOMGen::Generate(char *aFileName,
char *aOutputDirName,
IdlSpecification &aSpec)
{
if (!OpenFile(aFileName, aOutputDirName, kFilePrefix, kFileSuffix)) {
throw new CantOpenFileException(aFileName);
}
GenerateNPL();
GenerateIfdef(aSpec);
GenerateIncludes(aSpec);
GenerateForwardDecls(aSpec);
int i, icount = aSpec.InterfaceCount();
for (i = 0; i < icount; i++) {
IdlInterface *iface = aSpec.GetInterfaceAt(i);
if (iface) {
GenerateGuid(*iface);
GenerateClassDecl(*iface);
GenerateMethods(*iface);
GenerateEndClassDecl();
}
}
GenerateEpilog(aSpec);
CloseFile();
}
void
XPCOMGen::GenerateIfdef(IdlSpecification &aSpec)
{
char buf[512];
IdlInterface *iface = aSpec.GetInterfaceAt(0);
ofstream *file = GetFile();
if (iface) {
sprintf(buf, kIfdefStr, iface->GetName(), iface->GetName());
*file << buf;
}
}
void
XPCOMGen::GenerateIncludes(IdlSpecification &aSpec)
{
char buf[512];
ofstream *file = GetFile();
*file << kIncludeISupportsStr;
int i, icount = aSpec.InterfaceCount();
for (i = 0; i < icount; i++) {
IdlInterface *iface = aSpec.GetInterfaceAt(i);
if (iface) {
int b, bcount = iface->BaseClassCount();
for (b = 0; b < bcount; b++) {
sprintf(buf, kIncludeStr, iface->GetBaseClassAt(b));
*file << buf;
}
}
}
*file << "\n";
}
static PRIntn
ForwardDeclEnumerator(PLHashEntry *he, PRIntn i, void *arg)
{
char buf[512];
ofstream *file = (ofstream *)arg;
sprintf(buf, kForwardClassStr, (char *)he->key);
*file << buf;
return HT_ENUMERATE_NEXT;
}
void
XPCOMGen::GenerateForwardDecls(IdlSpecification &aSpec)
{
ofstream *file = GetFile();
PLHashTable *htable = PL_NewHashTable(10, PL_HashString,
PL_CompareStrings,
PL_CompareValues,
(PLHashAllocOps *)NULL, NULL);
int i, icount = aSpec.InterfaceCount();
for (i = 0; i < icount; i++) {
IdlInterface *iface = aSpec.GetInterfaceAt(i);
int a, acount = iface->AttributeCount();
for (a = 0; a < acount; a++) {
IdlAttribute *attr = iface->GetAttributeAt(a);
if ((attr->GetType() == TYPE_OBJECT) &&
!PL_HashTableLookup(htable, attr->GetTypeName())) {
PL_HashTableAdd(htable, attr->GetTypeName(), (void *)1);
}
}
int m, mcount = iface->FunctionCount();
for (m = 0; m < mcount; m++) {
IdlFunction *func = iface->GetFunctionAt(m);
IdlVariable *rval = func->GetReturnValue();
if ((rval->GetType() == TYPE_OBJECT) &&
!PL_HashTableLookup(htable, rval->GetTypeName())) {
PL_HashTableAdd(htable, rval->GetTypeName(), (void *)1);
}
int p, pcount = func->ParameterCount();
for (p = 0; p < pcount; p++) {
IdlParameter *param = func->GetParameterAt(p);
if ((param->GetType() == TYPE_OBJECT) &&
!PL_HashTableLookup(htable, param->GetTypeName())) {
PL_HashTableAdd(htable, param->GetTypeName(), (void *)1);
}
}
}
}
PL_HashTableEnumerateEntries(htable,
(PLHashEnumerator)ForwardDeclEnumerator,
file);
PL_HashTableDestroy(htable);
*file << "\n";
}
void
XPCOMGen::GenerateGuid(IdlInterface &aInterface)
{
char buf[512];
char uuid_buf[256];
ofstream *file = GetFile();
// XXX Need to generate unique guids
GetInterfaceIID(uuid_buf, aInterface);
sprintf(buf, kUuidStr, uuid_buf);
*file << buf;
}
void
XPCOMGen::GenerateClassDecl(IdlInterface &aInterface)
{
char buf[512];
ofstream *file = GetFile();
sprintf(buf, kClassDeclStr, aInterface.GetName());
*file << buf;
if (aInterface.BaseClassCount() > 0) {
int b, bcount = aInterface.BaseClassCount();
for (b = 0; b < bcount; b++) {
if (b > 0) {
*file << ", ";
}
sprintf(buf, kBaseClassStr, aInterface.GetBaseClassAt(b));
*file << buf;
}
}
else {
*file << kNoBaseClassStr;
}
*file << kClassPrologStr;
}
void
XPCOMGen::GenerateMethods(IdlInterface &aInterface)
{
char buf[512];
char name_buf[128];
char type_buf[128];
ofstream *file = GetFile();
int a, acount = aInterface.AttributeCount();
for (a = 0; a < acount; a++) {
IdlAttribute *attr = aInterface.GetAttributeAt(a);
GetVariableType(type_buf, *attr);
GetCapitalizedName(name_buf, *attr);
sprintf(buf, kGetterMethodDeclStr, name_buf, type_buf,
attr->GetType() == TYPE_STRING ? "" : "*", name_buf);
*file << buf;
if (!attr->GetReadOnly()) {
sprintf(buf, kSetterMethodDeclStr, name_buf, type_buf,
name_buf);
*file << buf;
}
}
int m, mcount = aInterface.FunctionCount();
for (m = 0; m < mcount; m++) {
char param_buf[256];
char *cur_param = param_buf;
IdlFunction *func = aInterface.GetFunctionAt(m);
int p, pcount = func->ParameterCount();
for (p = 0; p < pcount; p++) {
IdlParameter *param = func->GetParameterAt(p);
GetParameterType(type_buf, *param);
GetCapitalizedName(name_buf, *param);
sprintf(cur_param, kParamStr, type_buf, name_buf);
cur_param += strlen(cur_param);
}
IdlVariable *rval = func->GetReturnValue();
GetVariableType(type_buf, *rval);
sprintf(cur_param, kReturnStr, type_buf,
rval->GetType() == TYPE_STRING ? "" : "*");
GetCapitalizedName(name_buf, *func);
sprintf(buf, kMethodDeclStr, name_buf, param_buf);
*file << buf;
}
}
void
XPCOMGen::GenerateEndClassDecl()
{
ofstream *file = GetFile();
*file << kClassEpilogStr;
}
void
XPCOMGen::GenerateEpilog(IdlSpecification &aSpec)
{
char buf[512];
IdlInterface *iface = aSpec.GetInterfaceAt(0);
ofstream *file = GetFile();
if (iface) {
sprintf(buf, kEndifStr, iface->GetName());
*file << buf;
}
}

48
dom/tools/XPCOMGen.h Normal file
Просмотреть файл

@ -0,0 +1,48 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef _XPCOMGen_h__
#define _XPCOMGen_h__
#include <string.h>
#include "FileGen.h"
class ofstream;
class IdlSpecification;
class IdlInterface;
class XPCOMGen : public FileGen {
public:
XPCOMGen();
~XPCOMGen();
virtual void Generate(char *aFileName, char *aOutputDirName,
IdlSpecification &aSpec);
protected:
void GenerateIfdef(IdlSpecification &aSpec);
void GenerateIncludes(IdlSpecification &aSpec);
void GenerateForwardDecls(IdlSpecification &aSpec);
void GenerateGuid(IdlInterface &aInterface);
void GenerateClassDecl(IdlInterface &aInterface);
void GenerateMethods(IdlInterface &aInterface);
void GenerateEndClassDecl();
void GenerateEpilog(IdlSpecification &aSpec);
};
#endif // _XPCOMGen_h__