diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 90825e2392b2d70e43c7a25b8a3752299a933894 (patch) | |
tree | e33aa27f02b74604afbfd0ea4f1cfca8833d882a /python/sip/sipgen | |
download | tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.tar.gz tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebindings@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'python/sip/sipgen')
-rw-r--r-- | python/sip/sipgen/export.c | 998 | ||||
-rw-r--r-- | python/sip/sipgen/gencode.c | 10820 | ||||
-rw-r--r-- | python/sip/sipgen/heap.c | 115 | ||||
-rw-r--r-- | python/sip/sipgen/lexer.c | 3266 | ||||
-rw-r--r-- | python/sip/sipgen/lexer.l | 567 | ||||
-rw-r--r-- | python/sip/sipgen/main.c | 484 | ||||
-rw-r--r-- | python/sip/sipgen/parser.c | 7526 | ||||
-rw-r--r-- | python/sip/sipgen/parser.h | 268 | ||||
-rw-r--r-- | python/sip/sipgen/parser.y | 4945 | ||||
-rw-r--r-- | python/sip/sipgen/sip.h | 1027 | ||||
-rw-r--r-- | python/sip/sipgen/sipgen.sbf | 16 | ||||
-rw-r--r-- | python/sip/sipgen/transform.c | 2856 |
12 files changed, 32888 insertions, 0 deletions
diff --git a/python/sip/sipgen/export.c b/python/sip/sipgen/export.c new file mode 100644 index 00000000..a6e62523 --- /dev/null +++ b/python/sip/sipgen/export.c @@ -0,0 +1,998 @@ +/* + * The XML and API file generator module for SIP. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#include <stdio.h> + +#include "sip.h" + + +#define XML_VERSION_NR 0 /* The schema version number. */ + + +static void apiEnums(sipSpec *pt, classDef *scope, FILE *fp); +static void apiVars(sipSpec *pt, classDef *scope, FILE *fp); +static int apiCtor(sipSpec *pt, classDef *scope, ctorDef *ct, int sec, + FILE *fp); +static int apiOverload(sipSpec *pt, classDef *scope, overDef *od, int sec, + FILE *fp); +static int apiArgument(argDef *ad, int out, int need_comma, int sec, FILE *fp); +static void xmlClass(sipSpec *pt, classDef *cd, FILE *fp); +static void xmlEnums(sipSpec *pt, classDef *scope, int indent, FILE *fp); +static void xmlVars(sipSpec *pt, classDef *scope, int indent, FILE *fp); +static void xmlFunction(classDef *scope, memberDef *md, overDef *oloads, + int indent, FILE *fp); +static int xmlCtor(classDef *scope, ctorDef *ct, int sec, int indent, FILE *fp); +static int xmlOverload(classDef *scope, memberDef *md, overDef *od, + classDef *xtnds, int stat, int sec, int indent, FILE *fp); +static void xmlCppSignature(FILE *fp, overDef *od); +static void xmlArgument(argDef *ad, const char *dir, int res_xfer, int sec, + int indent, FILE *fp); +static void xmlType(argDef *ad, int sec, FILE *fp); +static void xmlIndent(int indent, FILE *fp); +static const char *dirAttribute(argDef *ad); +static void exportDefaultValue(argDef *ad, FILE *fp); +static const char *pyType(argDef *ad, int sec, classDef **scope); + + +/* + * Generate the API file. + */ +void generateAPI(sipSpec *pt, const char *apiFile) +{ + overDef *od; + classDef *cd; + FILE *fp; + + /* Generate the file. */ + if ((fp = fopen(apiFile, "w")) == NULL) + fatal("Unable to create file \"%s\"\n", apiFile); + + apiEnums(pt, NULL, fp); + apiVars(pt, NULL, fp); + + for (od = pt->overs; od != NULL; od = od->next) + { + if (od->common->module != pt->module) + continue; + + if (od->common->slot != no_slot) + continue; + + if (apiOverload(pt, NULL, od, FALSE, fp)) + apiOverload(pt, NULL, od, TRUE, fp); + } + + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + ctorDef *ct; + + if (cd->iff->module != pt->module) + continue; + + if (isExternal(cd)) + continue; + + apiEnums(pt, cd, fp); + apiVars(pt, cd, fp); + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + if (isPrivateCtor(ct)) + continue; + + if (apiCtor(pt, cd, ct, FALSE, fp)) + apiCtor(pt, cd, ct, TRUE, fp); + } + + for (od = cd->overs; od != NULL; od = od->next) + { + if (isPrivate(od)) + continue; + + if (od->common->slot != no_slot) + continue; + + if (apiOverload(pt, cd, od, FALSE, fp)) + apiOverload(pt, cd, od, TRUE, fp); + } + } + + fclose(fp); +} + + +/* + * Generate an API ctor. + */ +static int apiCtor(sipSpec *pt, classDef *scope, ctorDef *ct, int sec, + FILE *fp) +{ + int need_sec = FALSE, need_comma, a; + + /* Do the callable type form. */ + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, scope->ecd, scope->pyname); + fprintf(fp, "("); + + need_comma = FALSE; + + for (a = 0; a < ct->pysig.nrArgs; ++a) + { + argDef *ad = &ct->pysig.args[a]; + + need_comma = apiArgument(ad, FALSE, need_comma, sec, fp); + + if (ad->atype == rxcon_type || ad->atype == rxdis_type) + need_sec = TRUE; + } + + fprintf(fp, ")\n"); + + /* Do the call __init__ form. */ + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, scope->ecd, scope->pyname); + fprintf(fp, ".__init__(self"); + + for (a = 0; a < ct->pysig.nrArgs; ++a) + apiArgument(&ct->pysig.args[a], FALSE, TRUE, sec, fp); + + fprintf(fp, ")\n"); + + return need_sec; +} + + +/* + * Generate the APIs for all the enums in a scope. + */ +static void apiEnums(sipSpec *pt, classDef *scope, FILE *fp) +{ + enumDef *ed; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + enumMemberDef *emd; + + if (ed->module != pt->module) + continue; + + if (ed->ecd != scope) + continue; + + if (ed->pyname != NULL) + { + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, ed->ecd, ed->pyname->text); + fprintf(fp, "\n"); + } + + for (emd = ed->members; emd != NULL; emd = emd->next) + { + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, ed->ecd, emd->pyname->text); + fprintf(fp, "\n"); + } + } +} + + +/* + * Generate the APIs for all the variables in a scope. + */ +static void apiVars(sipSpec *pt, classDef *scope, FILE *fp) +{ + varDef *vd; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->module != pt->module) + continue; + + if (vd->ecd != scope) + continue; + + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, vd->ecd, vd->pyname->text); + fprintf(fp, "\n"); + } +} + + +/* + * Generate a single API overload. + */ +static int apiOverload(sipSpec *pt, classDef *scope, overDef *od, int sec, + FILE *fp) +{ + int need_sec = FALSE, need_comma = FALSE, is_res, nr_out, a; + + fprintf(fp, "%s.", pt->module->name); + prScopedPythonName(fp, scope, od->common->pyname->text); + fprintf(fp, "("); + + if (scope != NULL && scope->iff->type != namespace_iface && !isStatic(od)) + { + fprintf(fp, "self"); + need_comma = TRUE; + } + + nr_out = 0; + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + if (isOutArg(ad)) + ++nr_out; + + if (!isInArg(ad)) + continue; + + need_comma = apiArgument(ad, FALSE, need_comma, sec, fp); + + if (ad->atype == rxcon_type || ad->atype == rxdis_type) + need_sec = TRUE; + } + + fprintf(fp, ")"); + + is_res = (od->pysig.result.atype != void_type || od->pysig.result.nrderefs != 0); + + if (is_res || nr_out > 0) + { + fprintf(fp, " -> "); + + if ((is_res && nr_out > 0) || nr_out > 1) + fprintf(fp, "("); + + if (is_res) + need_comma = apiArgument(&od->pysig.result, TRUE, FALSE, sec, fp); + else + need_comma = FALSE; + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + if (!isOutArg(ad)) + continue; + + need_comma = apiArgument(ad, TRUE, need_comma, sec, fp); + } + + if ((is_res && nr_out > 0) || nr_out > 1) + fprintf(fp, ")"); + } + + fprintf(fp, "\n"); + + return need_sec; +} + + +/* + * Generate the API for an argument. + */ +static int apiArgument(argDef *ad, int out, int need_comma, int sec, FILE *fp) +{ + const char *tname; + classDef *tscope; + + if (isArraySize(ad)) + return need_comma; + + if (sec && (ad->atype == slotcon_type || ad->atype == slotdis_type)) + return need_comma; + + if ((tname = pyType(ad, sec, &tscope)) == NULL) + return need_comma; + + if (need_comma) + fprintf(fp, ", "); + + prScopedPythonName(fp, tscope, tname); + + if (ad->name != NULL) + fprintf(fp, " %s", ad->name); + + /* + * Handle the default value, but ignore it if it is an output only + * argument. + */ + if (ad->defval && !out) + { + fprintf(fp, "="); + prcode(fp, "%M"); + exportDefaultValue(ad, fp); + prcode(fp, "%M"); + } + + return TRUE; +} + + +/* + * Generate the XML export file. + */ +void generateXML(sipSpec *pt, const char *xmlFile) +{ + FILE *fp; + classDef *cd; + memberDef *md; + + if ((fp = fopen(xmlFile, "w")) == NULL) + fatal("Unable to create file \"%s\"\n", xmlFile); + + fprintf(fp, "<?xml version=\"1.0\"?>\n"); + fprintf(fp, "<Module version=\"%u\" name=\"%s\">\n", + XML_VERSION_NR, pt->module->name); + + /* + * Note that we don't yet handle mapped types, templates or exceptions. + */ + + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + if (cd->iff->module != pt->module) + continue; + + if (isExternal(cd)) + continue; + + xmlClass(pt, cd, fp); + } + + for (cd = pt->proxies; cd != NULL; cd = cd->next) + xmlClass(pt, cd, fp); + + xmlEnums(pt, NULL, 1, fp); + xmlVars(pt, NULL, 1, fp); + + for (md = pt->othfuncs; md != NULL; md = md->next) + { + if (md->module != pt->module) + continue; + + xmlFunction(NULL, md, pt->overs, 1, fp); + } + + fprintf(fp, "</Module>\n"); + + fclose(fp); +} + + +/* + * Generate the XML for a class. + */ +static void xmlClass(sipSpec *pt, classDef *cd, FILE *fp) +{ + int indent = 1; + ctorDef *ct; + memberDef *md; + + if (isOpaque(cd)) + { + xmlIndent(indent, fp); + fprintf(fp, "<OpaqueClass name=\"", cd->pyname); + prScopedPythonName(fp, cd->ecd, cd->pyname); + fprintf(fp, "\"/>\n"); + + return; + } + + xmlIndent(indent++, fp); + fprintf(fp, "<Class name=\"", cd->pyname); + prScopedPythonName(fp, cd->ecd, cd->pyname); + fprintf(fp, "\""); + + if (cd->convtocode != NULL) + fprintf(fp, " convert=\"1\""); + + if (cd->real != NULL) + fprintf(fp, " extends=\"%s\"", cd->real->iff->module->name); + + if (cd->supers != NULL) + { + classList *cl; + + fprintf(fp, " inherits=\""); + + for (cl = cd->supers; cl != NULL; cl = cl->next) + { + if (cl != cd->supers) + fprintf(fp, " "); + + prScopedPythonName(fp, cl->cd->ecd, cl->cd->pyname); + } + + fprintf(fp, "\""); + } + + fprintf(fp, ">\n"); + + xmlEnums(pt, cd, indent, fp); + xmlVars(pt, cd, indent, fp); + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + if (isPrivateCtor(ct)) + continue; + + if (xmlCtor(cd, ct, FALSE, indent, fp)) + xmlCtor(cd, ct, TRUE, indent, fp); + } + + for (md = cd->members; md != NULL; md = md->next) + xmlFunction(cd, md, cd->overs, indent, fp); + + xmlIndent(--indent, fp); + fprintf(fp, "</Class>\n"); +} + + +/* + * Generate the XML for all the enums in a scope. + */ +static void xmlEnums(sipSpec *pt, classDef *scope, int indent, FILE *fp) +{ + enumDef *ed; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + if (ed->module != pt->module) + continue; + + if (ed->ecd != scope) + continue; + + if (ed->pyname != NULL) + { + enumMemberDef *emd; + + xmlIndent(indent++, fp); + fprintf(fp, "<Enum name=\""); + prScopedPythonName(fp, ed->ecd, ed->pyname->text); + fprintf(fp, "\">\n"); + + for (emd = ed->members; emd != NULL; emd = emd->next) + { + xmlIndent(indent, fp); + fprintf(fp, "<EnumMember name=\""); + prScopedPythonName(fp, ed->ecd, emd->pyname->text); + fprintf(fp, "\"/>\n"); + } + + xmlIndent(--indent, fp); + fprintf(fp, "</Enum>\n"); + } + else + { + enumMemberDef *emd; + + for (emd = ed->members; emd != NULL; emd = emd->next) + { + xmlIndent(indent, fp); + fprintf(fp, "<Member name=\""); + prScopedPythonName(fp, ed->ecd, emd->pyname->text); + fprintf(fp, "\" const=\"1\" typename=\"int\"/>\n"); + } + } + } +} + + +/* + * Generate the XML for all the variables in a scope. + */ +static void xmlVars(sipSpec *pt, classDef *scope, int indent, FILE *fp) +{ + varDef *vd; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->module != pt->module) + continue; + + if (vd->ecd != scope) + continue; + + xmlIndent(indent, fp); + fprintf(fp, "<Member name=\""); + prScopedPythonName(fp, vd->ecd, vd->pyname->text); + fprintf(fp, "\""); + + if (isConstArg(&vd->type) || scope == NULL) + fprintf(fp, " const=\"1\""); + + if (isStaticVar(vd)) + fprintf(fp, " static=\"1\""); + + xmlType(&vd->type, FALSE, fp); + fprintf(fp, "/>\n"); + } +} + + +/* + * Generate the XML for a ctor. + */ +static int xmlCtor(classDef *scope, ctorDef *ct, int sec, int indent, FILE *fp) +{ + int a, need_sec; + + xmlIndent(indent++, fp); + fprintf(fp, "<Function name=\""); + prScopedPythonName(fp, scope, "__init__"); + fprintf(fp, "\""); + + /* Handle the trivial case. */ + if (ct->pysig.nrArgs == 0) + { + fprintf(fp, "/>\n"); + return FALSE; + } + + fprintf(fp, ">\n"); + + need_sec = FALSE; + + for (a = 0; a < ct->pysig.nrArgs; ++a) + { + argDef *ad = &ct->pysig.args[a]; + + xmlArgument(ad, dirAttribute(ad), FALSE, sec, indent, fp); + + if (ad->atype == rxcon_type || ad->atype == rxdis_type) + need_sec = TRUE; + } + + xmlIndent(--indent, fp); + fprintf(fp, "</Function>\n"); + + return need_sec; +} + + +/* + * Generate the XML for a function. + */ +static void xmlFunction(classDef *scope, memberDef *md, overDef *oloads, + int indent, FILE *fp) +{ + overDef *od; + + for (od = oloads; od != NULL; od = od->next) + { + int isstat; + classDef *xtnds; + + if (od->common != md) + continue; + + if (isPrivate(od)) + continue; + + if (isSignal(od)) + { + xmlIndent(indent, fp); + fprintf(fp, "<Signal name=\""); + prScopedPythonName(fp, scope, md->pyname->text); + fprintf(fp, "\" sig=\""); + xmlCppSignature(fp, od); + fprintf(fp, "\"/>\n", md->pyname->text); + + continue; + } + + xtnds = NULL; + isstat = (scope == NULL || scope->iff->type == namespace_iface || isStatic(od)); + + if (scope == NULL && md->slot != no_slot && od->pysig.args[0].atype == class_type) + { + xtnds = od->pysig.args[0].u.cd; + isstat = FALSE; + } + + if (xmlOverload(scope, md, od, xtnds, isstat, FALSE, indent, fp)) + xmlOverload(scope, md, od, xtnds, isstat, TRUE, indent, fp); + } +} + + +/* + * Generate the XML for an overload. + */ +static int xmlOverload(classDef *scope, memberDef *md, overDef *od, + classDef *xtnds, int stat, int sec, int indent, FILE *fp) +{ + int a, need_sec, no_res; + + xmlIndent(indent++, fp); + fprintf(fp, "<Function name=\""); + prScopedPythonName(fp, scope, md->pyname->text); + fprintf(fp, "\""); + + if (isAbstract(od)) + fprintf(fp, " abstract=\"1\""); + + if (stat) + fprintf(fp, " static=\"1\""); + + if (isSlot(od)) + { + fprintf(fp, " slot=\""); + xmlCppSignature(fp, od); + fprintf(fp, "\""); + } + + if (xtnds != NULL) + { + fprintf(fp, " extends=\""); + prScopedPythonName(fp, xtnds->ecd, xtnds->pyname); + fprintf(fp, "\""); + } + + no_res = (od->pysig.result.atype == void_type && od->pysig.result.nrderefs == 0); + + /* Handle the trivial case. */ + if (no_res && od->pysig.nrArgs == 0) + { + fprintf(fp, "/>\n"); + return FALSE; + } + + fprintf(fp, ">\n"); + + if (!no_res) + xmlArgument(&od->pysig.result, "out", isResultTransferredBack(od), FALSE, indent, fp); + + need_sec = FALSE; + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + /* Ignore the first argument of number slots. */ + if (isNumberSlot(md) && a == 0 && od->pysig.nrArgs == 2) + continue; + + xmlArgument(ad, dirAttribute(ad), FALSE, sec, indent, fp); + + if (ad->atype == rxcon_type || ad->atype == rxdis_type) + need_sec = TRUE; + } + + xmlIndent(--indent, fp); + fprintf(fp, "</Function>\n"); + + return need_sec; +} + + +/* + * Generate the XML for a C++ signature. + */ +static void xmlCppSignature(FILE *fp, overDef *od) +{ + prcode(fp, "%M"); + prOverloadDecl(fp, od, TRUE); + prcode(fp, "%M"); +} + + +/* + * Convert an arguments direction to an XML attribute value. + */ +static const char *dirAttribute(argDef *ad) +{ + if (isInArg(ad)) + { + if (isOutArg(ad)) + return "inout"; + + return NULL; + } + + return "out"; +} + + +/* + * Generate the XML for an argument. + */ +static void xmlArgument(argDef *ad, const char *dir, int res_xfer, int sec, + int indent, FILE *fp) +{ + if (isArraySize(ad)) + return; + + if (sec && (ad->atype == slotcon_type || ad->atype == slotdis_type)) + return; + + xmlIndent(indent, fp); + fprintf(fp, "<Argument"); + xmlType(ad, sec, fp); + + if (dir != NULL) + fprintf(fp, " dir=\"%s\"", dir); + + if (isAllowNone(ad)) + fprintf(fp, " allownone=\"1\""); + + if (isTransferred(ad)) + fprintf(fp, " transfer=\"to\""); + else if (isThisTransferred(ad)) + fprintf(fp, " transfer=\"this\""); + else if (res_xfer || isTransferredBack(ad)) + fprintf(fp, " transfer=\"back\""); + + /* + * Handle the default value, but ignore it if it is an output only + * argument. + */ + if (ad->defval && (dir == NULL || strcmp(dir, "out") != 0)) + { + prcode(fp, " default=\"%M"); + exportDefaultValue(ad, fp); + prcode(fp, "%M\""); + } + + fprintf(fp, "/>\n"); +} + + +/* + * Generate the XML for a type. + */ +static void xmlType(argDef *ad, int sec, FILE *fp) +{ + const char *type_type = NULL, *type_name; + classDef *type_scope; + + fprintf(fp, " typename=\""); + + switch (ad->atype) + { + case class_type: + type_type = (isOpaque(ad->u.cd) ? "opaque" : "class"); + break; + + case enum_type: + if (ad->u.ed->pyname != NULL) + type_type = "enum"; + break; + + case rxcon_type: + case rxdis_type: + if (!sec) + type_type = "class"; + break; + + case qobject_type: + type_type = "class"; + break; + + case slotcon_type: + case slotdis_type: + { + int a; + + prcode(fp, "SLOT("); + + for (a = 0; a < ad->u.sa->nrArgs; ++a) + { + if (a > 0) + prcode(fp, ", "); + + prcode(fp, "%M%B%M", &ad->u.sa->args[a]); + } + + prcode(fp, ")"); + } + + break; + + case mapped_type: + prcode(fp, "%M%B%M", &ad->u.mtd->type); + type_type = "mappedtype"; + break; + } + + if ((type_name = pyType(ad, sec, &type_scope)) != NULL) + prScopedPythonName(fp, type_scope, type_name); + + fprintf(fp, "\""); + + if (type_type != NULL) + fprintf(fp, " typetype=\"%s\"", type_type); + + if (ad->name != NULL) + fprintf(fp, " name=\"%s\"", ad->name); +} + + +/* + * Generate the indentation for a line. + */ +static void xmlIndent(int indent, FILE *fp) +{ + while (indent-- > 0) + fprintf(fp, " "); +} + + +/* + * Export the default value of an argument. + */ +static void exportDefaultValue(argDef *ad, FILE *fp) +{ + /* Translate some special cases. */ + if (ad->defval->next == NULL && ad->defval->vtype == numeric_value) + { + if (ad->nrderefs > 0 && ad->defval->u.vnum == 0) + { + prcode(fp, "None"); + return; + } + + if (ad->atype == bool_type || ad->atype == cbool_type) + { + prcode(fp, ad->defval->u.vnum ? "True" : "False"); + return; + } + } + + generateExpression(ad->defval, fp); +} + + +/* + * Get the Python representation of a type. + */ +static const char *pyType(argDef *ad, int sec, classDef **scope) +{ + const char *type_name; + + *scope = NULL; + + switch (ad->atype) + { + case class_type: + type_name = ad->u.cd->pyname; + *scope = ad->u.cd->ecd; + break; + + case struct_type: + case void_type: + type_name = "sip.voidptr"; + break; + + case enum_type: + if (ad->u.ed->pyname != NULL) + { + type_name = ad->u.ed->pyname->text; + *scope = ad->u.ed->ecd; + } + else + type_name = "int"; + break; + + case signal_type: + type_name = "SIGNAL()"; + break; + + case slot_type: + type_name = "SLOT()"; + break; + + case rxcon_type: + case rxdis_type: + if (sec) + type_name = "callable"; + else + type_name = "QObject"; + + break; + + case qobject_type: + type_name = "QObject"; + break; + + case ustring_type: + case string_type: + case sstring_type: + case wstring_type: + if (ad->nrderefs > 0) + type_name = "str"; + else + type_name = "char"; + + break; + + case short_type: + case int_type: + case cint_type: + type_name = "int"; + break; + + case ushort_type: + case uint_type: + case ulong_type: + case ulonglong_type: + type_name = "unsigned long"; + break; + + case long_type: + case longlong_type: + type_name = "long"; + break; + + case float_type: + case cfloat_type: + case double_type: + case cdouble_type: + type_name = "float"; + break; + + case bool_type: + case cbool_type: + type_name = "bool"; + break; + + case pyobject_type: + type_name = "object"; + break; + + case pytuple_type: + type_name = "tuple"; + break; + + case pylist_type: + type_name = "list"; + break; + + case pydict_type: + type_name = "dict"; + break; + + case pycallable_type: + type_name = "callable"; + break; + + case pyslice_type: + type_name = "slice"; + break; + + case pytype_type: + type_name = "type"; + break; + + case ellipsis_type: + type_name = "..."; + break; + + case anyslot_type: + /* Need to check if this is enough. */ + type_name = "SLOT()"; + break; + + default: + type_name = NULL; + } + + return type_name; +} diff --git a/python/sip/sipgen/gencode.c b/python/sip/sipgen/gencode.c new file mode 100644 index 00000000..b23ee2b1 --- /dev/null +++ b/python/sip/sipgen/gencode.c @@ -0,0 +1,10820 @@ +/* + * The code generator module for SIP. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#include <stdio.h> +#include <time.h> +#include <errno.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> + +#include "sip.h" + + +/* + * These must match the values of SIP_TYPE_FLAGS_SHIFT and SIP_TYPE_FLAGS_MASK + * in siplib/sip.h. + */ +#define TYPE_FLAGS_SHIFT 8 +#define TYPE_FLAGS_MASK 0x0f00 + + +/* Control what generateSingleArg() actually generates. */ +typedef enum { + Call, + Declaration, + Definition +} funcArgType; + + +/* An entry in the sorted array of methods. */ +typedef struct { + memberDef *md; /* The method. */ + int is_static; /* Set if all overloads are static. */ +} sortedMethTab; + + +static int currentLineNr; /* Current output line number. */ +static char *currentFileName; /* Current output file name. */ +static int previousLineNr; /* Previous output line number. */ +static char *previousFileName; /* Previous output file name. */ +static int exceptions; /* Set if exceptions are enabled. */ +static int tracing; /* Set if tracing is enabled. */ +static int generating_c; /* Set if generating C. */ +static int release_gil; /* Set if always releasing the GIL. */ +static const char *prcode_last = NULL; /* The last prcode format string. */ +static int prcode_xml = FALSE; /* Set if prcode is XML aware. */ + + +static void generateDocumentation(sipSpec *, char *); +static void generateBuildFile(sipSpec *, char *, char *, int); +static void generateInternalAPIHeader(sipSpec *, char *, stringList *); +static void generateCpp(sipSpec *, char *, char *, int *); +static void generateIfaceCpp(sipSpec *, ifaceFileDef *, char *, char *, + FILE *); +static void generateMappedTypeCpp(mappedTypeDef *, FILE *); +static void generateImportedMappedTypeHeader(mappedTypeDef *mtd, sipSpec *pt, + FILE *fp); +static void generateMappedTypeHeader(mappedTypeDef *, int, FILE *); +static void generateClassCpp(classDef *cd, sipSpec *pt, FILE *fp); +static void generateImportedClassHeader(classDef *cd, sipSpec *pt, FILE *fp); +static void generateClassTableEntries(sipSpec *pt, nodeDef *nd, FILE *fp); +static void generateClassHeader(classDef *, int, sipSpec *, FILE *); +static void generateClassFunctions(sipSpec *, classDef *, FILE *); +static void generateShadowCode(sipSpec *, classDef *, FILE *); +static void generateFunction(sipSpec *, memberDef *, overDef *, classDef *, + classDef *, FILE *); +static void generateFunctionBody(sipSpec *, overDef *, classDef *, classDef *, + int deref, FILE *); +static void generateTypeDefinition(sipSpec *pt, classDef *cd, FILE *fp); +static void generateTypeInit(sipSpec *, classDef *, FILE *); +static void generateCppCodeBlock(codeBlock *, FILE *); +static void generateUsedIncludes(ifaceFileList *, int, FILE *); +static void generateIfaceHeader(sipSpec *, ifaceFileDef *, char *); +static void generateShadowClassDeclaration(sipSpec *, classDef *, FILE *); +static int hasConvertToCode(argDef *ad); +static void deleteTemps(signatureDef *sd, FILE *fp); +static void gc_ellipsis(signatureDef *sd, FILE *fp); +static void generateArgs(signatureDef *, funcArgType, FILE *); +static void generateVariable(argDef *, int, FILE *); +static void generateNamedValueType(argDef *, char *, FILE *); +static void generateSingleArg(argDef *, int, funcArgType, FILE *); +static void generateBaseType(argDef *, FILE *); +static void generateNamedBaseType(argDef *, char *, FILE *); +static void generateTupleBuilder(signatureDef *, FILE *); +static void generateEmitters(sipSpec *pt, classDef *cd, FILE *fp); +static void generateEmitter(sipSpec *, classDef *, visibleList *, FILE *); +static void generateVirtualHandler(sipSpec *, virtHandlerDef *, FILE *); +static void generateVirtHandlerErrorReturn(argDef *res, FILE *fp); +static void generateVirtualCatcher(sipSpec *, classDef *, int, virtOverDef *, + FILE *); +static void generateUnambiguousClass(classDef *cd, classDef *scope, FILE *fp); +static void generateProtectedEnums(sipSpec *, classDef *, FILE *); +static void generateProtectedDeclarations(classDef *, FILE *); +static void generateProtectedDefinitions(classDef *, FILE *); +static void generateProtectedCallArgs(overDef *od, FILE *fp); +static void generateConstructorCall(classDef *, ctorDef *, int, FILE *); +static void generateHandleResult(overDef *, int, char *, FILE *); +static void generateOrdinaryFunction(sipSpec *, classDef *, memberDef *, + FILE *); +static void generateSimpleFunctionCall(fcallDef *, FILE *); +static void generateFunctionCall(classDef *cd, classDef *ocd, overDef *od, + int deref, FILE *fp); +static void generateCppFunctionCall(classDef *cd, classDef *ocd, overDef *od, + FILE *fp); +static void generateSlotArg(signatureDef *sd, int argnr, FILE *fp); +static void generateBinarySlotCall(overDef *od, char *op, int deref, FILE *fp); +static void generateNumberSlotCall(overDef *od, char *op, FILE *fp); +static void generateVariableHandler(varDef *, FILE *); +static int generateObjToCppConversion(argDef *, FILE *); +static void generateVarClassConversion(varDef *, FILE *); +static void generateVarMember(varDef *vd, FILE *fp); +static int generateVoidPointers(sipSpec *, classDef *, FILE *); +static int generateChars(sipSpec *, classDef *, FILE *); +static int generateStrings(sipSpec *, classDef *, FILE *); +static sortedMethTab *createFunctionTable(classDef *, int *); +static sortedMethTab *createMethodTable(classDef *, int *); +static int generateMethodTable(classDef *, FILE *); +static void generateEnumMacros(sipSpec *pt, classDef *cd, FILE *fp); +static int generateEnumMemberTable(sipSpec *, classDef *, FILE *); +static int generateInts(sipSpec *, classDef *, FILE *); +static int generateLongs(sipSpec *, classDef *, FILE *); +static int generateUnsignedLongs(sipSpec *, classDef *, FILE *); +static int generateLongLongs(sipSpec *, classDef *, FILE *); +static int generateUnsignedLongLongs(sipSpec *, classDef *, FILE *); +static int generateVariableType(sipSpec *pt, classDef *cd, argType atype, + const char *eng, const char *s1, const char *s2, FILE *fp); +static int generateDoubles(sipSpec *, classDef *, FILE *); +static int generateEnums(sipSpec *, classDef *, FILE *); +static int generateClasses(sipSpec *, classDef *, FILE *); +static void generateEnumsInline(sipSpec *, FILE *); +static void generateClassesInline(sipSpec *, FILE *); +static void generateAccessFunctions(sipSpec *, classDef *, FILE *); +static void generateConvertToDefinitions(mappedTypeDef *, classDef *, FILE *); +static void generateEncodedClass(sipSpec *, classDef *, int, FILE *); +static int generateArgParser(sipSpec *, signatureDef *, classDef *, ctorDef *, + overDef *, int, FILE *); +static void generateTry(throwArgs *, FILE *); +static void generateCatch(throwArgs *ta, signatureDef *sd, FILE *fp); +static void generateThrowSpecifier(throwArgs *, FILE *); +static void generateSlot(sipSpec *pt, classDef *cd, enumDef *ed, memberDef *md, + FILE *fp); +static void generateCastZero(argDef *ad, FILE *fp); +static void generateCallDefaultCtor(ctorDef *ct, FILE *fp); +static int countVirtuals(classDef *); +static int skipOverload(overDef *, memberDef *, classDef *, classDef *, int); +static int compareMethTab(const void *, const void *); +static int compareEnumMembers(const void *, const void *); +static char *getSubFormatChar(char, argDef *); +static char *createIfaceFileName(char *, ifaceFileDef *, char *); +static FILE *createCompilationUnit(sipSpec *pt, char *fname, + char *description); +static FILE *createFile(sipSpec *, char *, char *); +static void closeFile(FILE *); +static void prScopedName(FILE *fp, scopedNameDef *snd, char *sep); +static void prTypeName(FILE *, argDef *, int); +static void prScopedClassName(FILE *, classDef *, char *); +static int isZeroArgSlot(memberDef *md); +static int isMultiArgSlot(memberDef *md); +static int isIntArgSlot(memberDef *md); +static int isInplaceNumberSlot(memberDef *md); +static int isInplaceSequenceSlot(memberDef *md); +static int needErrorFlag(codeBlock *cb); +static int needNewInstance(argDef *ad); +static int needDealloc(classDef *cd); +static char getBuildResultFormat(argDef *ad); +static const char *getParseResultFormat(argDef *ad, int isres, int xfervh); +static void generateParseResultExtraArgs(argDef *ad, int isres, FILE *fp); +static char *makePartName(char *codeDir, char *mname, int part, + char *srcSuffix); +static void normaliseArgs(signatureDef *); +static void restoreArgs(signatureDef *); +static const char *slotName(slotType st); +static void ints_intro(classDef *cd, FILE *fp); +static const char *argName(const char *name, codeBlock *cb); +static int usedInCode(codeBlock *code, const char *str); +static void generateDefaultValue(argDef *ad, int argnr, FILE *fp); +static void generateClassFromVoid(classDef *cd, const char *cname, + const char *vname, FILE *fp); +static void generateMappedTypeFromVoid(mappedTypeDef *mtd, const char *cname, + const char *vname, FILE *fp); +static int generateSubClassConvertors(sipSpec *pt, FILE *fp); +static void generateRegisterMetaType(classDef *cd, FILE *fp); + + +/* + * Generate the code from a specification. + */ +void generateCode(sipSpec *pt, char *codeDir, char *buildfile, char *docFile, + char *srcSuffix, int except, int trace, int releaseGIL, int parts, + stringList *xsl) +{ + exceptions = except; + tracing = trace; + release_gil = releaseGIL; + generating_c = pt->genc; + + if (srcSuffix == NULL) + srcSuffix = (generating_c ? ".c" : ".cpp"); + + /* Generate the documentation. */ + if (docFile != NULL) + generateDocumentation(pt,docFile); + + /* Generate the code. */ + if (codeDir != NULL) + { + generateCpp(pt,codeDir,srcSuffix,&parts); + generateInternalAPIHeader(pt,codeDir,xsl); + } + + /* Generate the build file. */ + if (buildfile != NULL) + generateBuildFile(pt,buildfile,srcSuffix,parts); +} + + +/* + * Generate the documentation. + */ +static void generateDocumentation(sipSpec *pt, char *docFile) +{ + FILE *fp; + codeBlock *cb; + + fp = createFile(pt, docFile, NULL); + + for (cb = pt->docs; cb != NULL; cb = cb->next) + fputs(cb->frag, fp); + + closeFile(fp); +} + + +/* + * Generate the build file. + */ +static void generateBuildFile(sipSpec *pt, char *buildFile, char *srcSuffix, + int parts) +{ + char *mname = pt->module->name; + ifaceFileDef *iff; + FILE *fp; + + fp = createFile(pt, buildFile, NULL); + + prcode(fp, "target = %s\nsources = ", mname); + + if (parts) + { + int p; + + for (p = 0; p < parts; ++p) + { + if (p > 0) + prcode(fp, " "); + + prcode(fp, "sip%spart%d%s", mname, p, srcSuffix); + } + } + else + { + prcode(fp, "sip%scmodule%s", mname, srcSuffix); + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + { + if (iff->module != pt->module) + continue; + + if (iff->type == exception_iface) + continue; + + prcode(fp, " sip%s%F%s", mname, iff->fqcname, srcSuffix); + } + } + + prcode(fp, "\nheaders = sipAPI%s.h", mname); + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + { + char *imname; + + imname = (iff->module == pt->module ? mname : iff->module->name); + + prcode(fp, " sip%s%F.h", imname, iff->fqcname); + } + + prcode(fp, "\n"); + + closeFile(fp); +} + + +/* + * Generate an expression in C++. + */ +void generateExpression(valueDef *vd, FILE *fp) +{ + while (vd != NULL) + { + if (vd->vunop != '\0') + prcode(fp,"%c",vd->vunop); + + switch (vd->vtype) + { + case qchar_value: + prcode(fp,"'%c'",vd->u.vqchar); + break; + + case string_value: + prcode(fp,"\"%s\"",vd->u.vstr); + break; + + case numeric_value: + prcode(fp,"%l",vd->u.vnum); + break; + + case real_value: + prcode(fp,"%g",vd->u.vreal); + break; + + case scoped_value: + if (prcode_xml) + prScopedName(fp, vd->u.vscp, "."); + else + prcode(fp, "%S", vd->u.vscp); + + break; + + case fcall_value: + generateSimpleFunctionCall(vd->u.fcd,fp); + break; + } + + if (vd->vbinop != '\0') + prcode(fp," %c ",vd->vbinop); + + vd = vd->next; + } +} + + +/* + * Generate the C++ internal module API header file. + */ +static void generateInternalAPIHeader(sipSpec *pt,char *codeDir,stringList *xsl) +{ + char *hfile, *mname = pt->module->name; + int noIntro; + FILE *fp; + nameDef *nd; + moduleDef *mod; + moduleListDef *mld; + + hfile = concat(codeDir,"/sipAPI",mname,".h",NULL); + fp = createFile(pt,hfile,"Internal module API header file."); + + /* Include files. */ + + prcode(fp, +"\n" +"#ifndef _%sAPI_H\n" +"#define _%sAPI_H\n" +"\n" +"\n" +"#include <sip.h>\n" + ,mname + ,mname); + + if (optRegisterTypes(pt)) + prcode(fp, +"\n" +"#include <QMetaType>\n" + ); + + /* Define the enabled features. */ + noIntro = TRUE; + + for (mod = pt->modules; mod != NULL; mod = mod->next) + { + qualDef *qd; + + for (qd = mod->qualifiers; qd != NULL; qd = qd->next) + if (qd->qtype == feature_qualifier && !excludedFeature(xsl,qd)) + { + if (noIntro) + { + prcode(fp, +"\n" +"\n" +"/* These are the features that are enabled. */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +"#define SIP_FEATURE_%s\n" + ,qd->name); + } + } + + generateCppCodeBlock(pt->exphdrcode,fp); + generateCppCodeBlock(pt->hdrcode,fp); + + /* Shortcuts that hide the messy detail of the APIs. */ + noIntro = TRUE; + + for (nd = pt->namecache; nd != NULL; nd = nd->next) + { + if (!isClassName(nd)) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +"\n" +"/*\n" +" * Convenient names to refer to the names of classes defined in this module.\n" +" * These are part of the public API.\n" +" */\n" +"\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +"#define sipName_%s %N\n" + ,nd->text,nd); + } + + prcode(fp, +"\n" +"\n" +"/* Convenient names to call the SIP API. */\n" +"#define sipConvertFromSliceObject(o,len,start,stop,step,slen) PySlice_GetIndicesEx((PySliceObject *)(o),(len),(start),(stop),(step),(slen))\n" +"#define sipIsSubClassInstance(o,wt) PyObject_TypeCheck((o),(PyTypeObject *)(wt))\n" +"\n" +"#define sipMapStringToClass sipAPI_%s->api_map_string_to_class\n" +"#define sipMapIntToClass sipAPI_%s->api_map_int_to_class\n" +"#define sipMalloc sipAPI_%s->api_malloc\n" +"#define sipFree sipAPI_%s->api_free\n" +"#define sipBuildResult sipAPI_%s->api_build_result\n" +"#define sipCallMethod sipAPI_%s->api_call_method\n" +"#define sipParseResult sipAPI_%s->api_parse_result\n" +"#define sipParseArgs sipAPI_%s->api_parse_args\n" +"#define sipParsePair sipAPI_%s->api_parse_pair\n" +"#define sipCommonCtor sipAPI_%s->api_common_ctor\n" +"#define sipCommonDtor sipAPI_%s->api_common_dtor\n" +"#define sipConvertFromSequenceIndex sipAPI_%s->api_convert_from_sequence_index\n" +"#define sipConvertFromVoidPtr sipAPI_%s->api_convert_from_void_ptr\n" +"#define sipConvertToCpp sipAPI_%s->api_convert_to_cpp\n" +"#define sipConvertToVoidPtr sipAPI_%s->api_convert_to_void_ptr\n" +"#define sipNoFunction sipAPI_%s->api_no_function\n" +"#define sipNoMethod sipAPI_%s->api_no_method\n" +"#define sipAbstractMethod sipAPI_%s->api_abstract_method\n" +"#define sipBadClass sipAPI_%s->api_bad_class\n" +"#define sipBadSetType sipAPI_%s->api_bad_set_type\n" +"#define sipBadCatcherResult sipAPI_%s->api_bad_catcher_result\n" +"#define sipBadOperatorArg sipAPI_%s->api_bad_operator_arg\n" +"#define sipTrace sipAPI_%s->api_trace\n" +"#define sipTransfer sipAPI_%s->api_transfer\n" +"#define sipTransferBack sipAPI_%s->api_transfer_back\n" +"#define sipTransferTo sipAPI_%s->api_transfer_to\n" +"#define sipWrapper_Check sipAPI_%s->api_wrapper_check\n" +"#define sipGetWrapper sipAPI_%s->api_get_wrapper\n" +"#define sipGetCppPtr sipAPI_%s->api_get_cpp_ptr\n" +"#define sipGetComplexCppPtr sipAPI_%s->api_get_complex_cpp_ptr\n" +"#define sipIsPyMethod sipAPI_%s->api_is_py_method\n" +"#define sipCallHook sipAPI_%s->api_call_hook\n" +"#define sipStartThread sipAPI_%s->api_start_thread\n" +"#define sipEndThread sipAPI_%s->api_end_thread\n" +"#define sipEmitSignal sipAPI_%s->api_emit_signal\n" +"#define sipConnectRx sipAPI_%s->api_connect_rx\n" +"#define sipDisconnectRx sipAPI_%s->api_disconnect_rx\n" +"#define sipGetSender sipAPI_%s->api_get_sender\n" +"#define sipRaiseUnknownException sipAPI_%s->api_raise_unknown_exception\n" +"#define sipRaiseClassException sipAPI_%s->api_raise_class_exception\n" +"#define sipRaiseSubClassException sipAPI_%s->api_raise_sub_class_exception\n" +"#define sipBadLengthForSlice sipAPI_%s->api_bad_length_for_slice\n" +"#define sipClassName sipAPI_%s->api_class_name\n" +"#define sipAddClassInstance sipAPI_%s->api_add_class_instance\n" +"#define sipAddMappedTypeInstance sipAPI_%s->api_add_mapped_type_instance\n" +"#define sipAddEnumInstance sipAPI_%s->api_add_enum_instance\n" +"#define sipConvertFromNamedEnum sipAPI_%s->api_convert_from_named_enum\n" +"#define sipGetAddress sipAPI_%s->api_get_address\n" +"#define sipFreeConnection sipAPI_%s->api_free_connection\n" +"#define sipEmitToSlot sipAPI_%s->api_emit_to_slot\n" +"#define sipSameConnection sipAPI_%s->api_same_connection\n" +"#define sipPySlotExtend sipAPI_%s->api_pyslot_extend\n" +"#define sipConvertRx sipAPI_%s->api_convert_rx\n" +"#define sipAddDelayedDtor sipAPI_%s->api_add_delayed_dtor\n" +"#define sipCanConvertToInstance sipAPI_%s->api_can_convert_to_instance\n" +"#define sipCanConvertToMappedType sipAPI_%s->api_can_convert_to_mapped_type\n" +"#define sipConvertToInstance sipAPI_%s->api_convert_to_instance\n" +"#define sipConvertToMappedType sipAPI_%s->api_convert_to_mapped_type\n" +"#define sipForceConvertToInstance sipAPI_%s->api_force_convert_to_instance\n" +"#define sipForceConvertToMappedType sipAPI_%s->api_force_convert_to_mapped_type\n" +"#define sipReleaseInstance sipAPI_%s->api_release_instance\n" +"#define sipReleaseMappedType sipAPI_%s->api_release_mapped_type\n" +"#define sipConvertFromInstance sipAPI_%s->api_convert_from_instance\n" +"#define sipConvertFromNewInstance sipAPI_%s->api_convert_from_new_instance\n" +"#define sipConvertFromMappedType sipAPI_%s->api_convert_from_mapped_type\n" +"#define sipGetState sipAPI_%s->api_get_state\n" +"#define sipFindMappedType sipAPI_%s->api_find_mapped_type\n" +"#define sipLong_AsUnsignedLong sipAPI_%s->api_long_as_unsigned_long\n" +"#define sipExportSymbol sipAPI_%s->api_export_symbol\n" +"#define sipImportSymbol sipAPI_%s->api_import_symbol\n" +"#define sipRegisterIntTypes sipAPI_%s->api_register_int_types\n" +"#define sipParseSignature sipAPI_%s->api_parse_signature\n" +"#define sipFindClass sipAPI_%s->api_find_class\n" +"#define sipFindNamedEnum sipAPI_%s->api_find_named_enum\n" +"#define sipString_AsChar sipAPI_%s->api_string_as_char\n" +"#define sipUnicode_AsWChar sipAPI_%s->api_unicode_as_wchar\n" +"#define sipUnicode_AsWString sipAPI_%s->api_unicode_as_wstring\n" + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname + ,mname); + + /* The name strings. */ + noIntro = TRUE; + + for (nd = pt->namecache; nd != NULL; nd = nd->next) + { + if (!isUsedName(nd)) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +"\n" +"/* The strings used by this module. */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +"extern char %N[];\n" + ,nd); + } + + /* The unscoped enum macros. */ + generateEnumMacros(pt, NULL, fp); + + prcode(fp, +"\n" +"\n" +"/* The SIP API, this module's API and the APIs of any imported modules. */\n" +"extern const sipAPIDef *sipAPI_%s;\n" +"extern sipExportedModuleDef sipModuleAPI_%s;\n" + ,mname + ,mname,mname); + + for (mld = pt->module->allimports; mld != NULL; mld = mld->next) + prcode(fp, +"extern const sipExportedModuleDef *sipModuleAPI_%s_%s;\n" + ,mname,mld->module->name); + + if (optQ_OBJECT4(pt)) + prcode(fp, +"\n" +"typedef const QMetaObject *(*sip_qt_metaobject_func)(sipWrapper *,sipWrapperType *,const QMetaObject *);\n" +"extern sip_qt_metaobject_func sip_%s_qt_metaobject;\n" +"\n" +"typedef int (*sip_qt_metacall_func)(sipWrapper *,sipWrapperType *,QMetaObject::Call,int,void **);\n" +"extern sip_qt_metacall_func sip_%s_qt_metacall;\n" + , mname + , mname); + + /* + * Note that we don't forward declare the virtual handlers. This is + * because we would need to #include everything needed for their + * argument types. + */ + + prcode(fp, +"\n" +"#endif\n" + ); + + closeFile(fp); + free(hfile); +} + + +/* + * Return the filename of a source code part on the heap. + */ +static char *makePartName(char *codeDir,char *mname,int part,char *srcSuffix) +{ + char buf[20]; + + sprintf(buf,"part%d",part); + + return concat(codeDir,"/sip",mname,buf,srcSuffix,NULL); +} + + +/* + * Generate the C/C++ code. + */ +static void generateCpp(sipSpec *pt, char *codeDir, char *srcSuffix, int *parts) +{ + char *cppfile, *mname = pt->module->name; + int noIntro, nrSccs = 0, files_in_part, max_per_part, this_part; + int is_inst_class, is_inst_voidp, is_inst_char, is_inst_string; + int is_inst_int, is_inst_long, is_inst_ulong, is_inst_longlong; + int is_inst_ulonglong, is_inst_double, is_inst_enum, nr_enummembers; + int hasexternal = FALSE, slot_extenders = FALSE, ctor_extenders = FALSE; + FILE *fp; + moduleListDef *mld; + classDef *cd; + memberDef *md; + ifaceFileDef *iff; + virtHandlerDef *vhd; + nameDef *nd; + exceptionDef *xd; + + /* Calculate the number of files in each part. */ + if (*parts) + { + int nr_files = 1; + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + if (iff->module == pt->module) + ++nr_files; + + max_per_part = (nr_files + *parts - 1) / *parts; + files_in_part = 1; + this_part = 0; + + cppfile = makePartName(codeDir,mname,0,srcSuffix); + } + else + cppfile = concat(codeDir,"/sip",mname,"cmodule",srcSuffix,NULL); + + fp = createCompilationUnit(pt, cppfile, "Module code."); + + prcode(fp, +"\n" +"#include \"sipAPI%s.h\"\n" +"\n" + ,mname); + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + if (iff->module == pt->module && iff->type != exception_iface) + prcode(fp, +"#include \"sip%s%F.h\"\n" + ,iff->module->name,iff->fqcname); + + generateUsedIncludes(pt->used, FALSE, fp); + + /* + * If there should be a Qt support API then generate stubs values for the + * optional parts. These should be undefined in %ModuleCode if a C++ + * implementation is provided. + */ + if (pt->qobjclass >= 0) + prcode(fp, +"\n" +"#define sipQtIsQtSignal 0\n" +"#define sipQtCreateUniversalSignalShortcut 0\n" +"#define sipQtCreateUniversalSignal 0\n" +"#define sipQtFindUniversalSignalShortcut 0\n" +"#define sipQtFindUniversalSignal 0\n" +"#define sipQtEmitSignalShortcut 0\n" +"#define sipQtEmitSignal 0\n" + ); + + /* Define the names. */ + noIntro = TRUE; + + for (nd = pt->namecache; nd != NULL; nd = nd->next) + { + if (!isUsedName(nd)) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +"\n" +"/* Define the strings used by this module. */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +"char %N[] = \"%s\";\n" + ,nd,nd->text); + } + + /* Generate the C++ code blocks. */ + generateCppCodeBlock(pt->cppcode,fp); + + /* Generate any virtual handler declarations. */ + for (vhd = pt->module->virthandlers; vhd != NULL; vhd = vhd->next) + if (!isDuplicateVH(vhd)) + generateVirtualHandler(pt,vhd,fp); + + /* Generate the global functions. */ + for (md = pt->othfuncs; md != NULL; md = md->next) + { + if (md->module != pt->module) + continue; + + if (md->slot == no_slot) + generateOrdinaryFunction(pt,NULL,md,fp); + else + { + overDef *od; + + /* + * Make sure that there is still an overload and we + * haven't moved them all to classes. + */ + for (od = pt->overs; od != NULL; od = od->next) + if (od->common == md) + { + generateSlot(pt, NULL, NULL, md, fp); + slot_extenders = TRUE; + break; + } + } + } + + /* Generate any class specific ctor or slot extenders. */ + for (cd = pt->proxies; cd != NULL; cd = cd->next) + { + if (cd->ctors != NULL) + { + generateTypeInit(pt, cd, fp); + ctor_extenders = TRUE; + } + + for (md = cd->members; md != NULL; md = md->next) + { + generateSlot(pt, cd, NULL, md, fp); + slot_extenders = TRUE; + } + } + + /* Generate any ctor extender table. */ + if (ctor_extenders) + { + prcode(fp, +"\n" +"static sipInitExtenderDef initExtenders[] = {\n" + ); + + for (cd = pt->proxies; cd != NULL; cd = cd->next) + if (cd->ctors != NULL) + { + prcode(fp, +" {init_%C, ", classFQCName(cd)); + + generateEncodedClass(pt, cd, 0, fp); + + prcode(fp, ", NULL},\n" + ); + } + + prcode(fp, +" {NULL, {0, 0, 0}, NULL}\n" +"};\n" + ); + } + + /* Generate any slot extender table. */ + if (slot_extenders) + { + prcode(fp, +"\n" +"static sipPySlotExtenderDef slotExtenders[] = {\n" + ); + + for (md = pt->othfuncs; md != NULL; md = md->next) + { + overDef *od; + + if (md->module != pt->module) + continue; + + if (md->slot == no_slot) + continue; + + for (od = pt->overs; od != NULL; od = od->next) + if (od->common == md) + { + prcode(fp, +" {(void *)slot_%s, %s, {0, 0, 0}},\n" + , md->pyname->text, slotName(md->slot)); + + break; + } + } + + for (cd = pt->proxies; cd != NULL; cd = cd->next) + for (md = cd->members; md != NULL; md = md->next) + { + prcode(fp, +" {(void *)slot_%C_%s, %s, ", classFQCName(cd), md->pyname->text, slotName(md->slot)); + + generateEncodedClass(pt, cd, 0, fp); + + prcode(fp, "},\n" + ); + } + + prcode(fp, +" {NULL, (sipPySlotType)0, {0, 0, 0}}\n" +"};\n" + ); + } + + /* Generate the global access functions. */ + generateAccessFunctions(pt,NULL,fp); + + /* Generate the module data structures. */ + if (pt->module->nrclasses > 0) + { + nrSccs = generateSubClassConvertors(pt, fp); + + prcode(fp, +"\n" +"\n" +"/*\n" +" * This defines each class in this module. The values are replaced by the\n" +" * proper Python types during the export process.\n" +" */\n" +"static sipWrapperType *typesTable[] = {\n" + ); + + generateClassTableEntries(pt, &pt->module->root, fp); + + prcode(fp, +"};\n" + ); + + /* Generate the external classes table if needed. */ + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + if (!isExternal(cd)) + continue; + + if (cd->iff->module != pt->module) + continue; + + if (!hasexternal) + { + prcode(fp, +"\n" +"\n" +"/* This defines each external type declared in this module, */\n" +"static sipExternalTypeDef externalTypesTable[] = {\n" + ); + + hasexternal = TRUE; + } + + prcode(fp, +" {%d, \"", cd->classnr); + prScopedName(fp, classFQCName(cd), "."); + prcode(fp,"\"},\n" + ); + } + + if (hasexternal) + prcode(fp, +" {-1, NULL}\n" +"};\n" + ); + } + + if (pt->module->nrmappedtypes > 0) + { + mappedTypeDef *mtd; + argDef type; + + type.argflags = 0; + type.name = NULL; + type.nrderefs = 0; + type.defval = NULL; + + prcode(fp, +"\n" +"\n" +"/* This defines each mapped type in this module. */\n" +"static sipMappedType *mappedTypesTable[] = {\n" + ); + + for (mtd = pt->mappedtypes; mtd != NULL; mtd = mtd->next) + { + if (mtd->iff->module != pt->module) + continue; + + type.atype = mapped_type; + type.u.mtd = mtd; + + prcode(fp, +" &sipMappedTypeDef_%T,\n" + ,&type); + } + + prcode(fp, +" 0\n" +"};\n" + ); + } + + if (pt->module->nrenums > 0) + { + enumDef *ed; + + prcode(fp, +"\n" +"\n" +"/* Define each named enum in this module. */\n" + ); + + /* Generate any slot tables. */ + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + memberDef *slot; + + if (ed->module != pt->module || ed->fqcname == NULL) + continue; + + if (ed->slots == NULL) + continue; + + for (slot = ed->slots; slot != NULL; slot = slot->next) + generateSlot(pt, NULL, ed, slot, fp); + + prcode(fp, +"\n" +"static sipPySlotDef slots_%C[] = {\n" + , ed->fqcname); + + for (slot = ed->slots; slot != NULL; slot = slot->next) + { + const char *stype; + + if ((stype = slotName(slot->slot)) != NULL) + prcode(fp, +" {(void *)slot_%C_%s, %s},\n" + , ed->fqcname, slot->pyname->text, stype); + } + + prcode(fp, +" {0, (sipPySlotType)0}\n" +"};\n" +"\n" + ); + } + + prcode(fp, +"static sipEnumDef enumTypesTable[] = {\n" + ); + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + char *emname; + + if (ed->module != pt->module || ed->fqcname == NULL) + continue; + + if (ed->ecd == NULL) + emname = mname; + else if (ed->ecd->real == NULL) + emname = ed->module->name; + else + emname = ed->ecd->real->iff->module->name; + + prcode(fp, +" {\"%s.%P\", ", emname, ed->ecd, ed->pyname->text); + + if (isRenamedEnum(ed) || (ed->ecd != NULL && isRenamedClass(ed->ecd))) + prcode(fp, "\"%S\", ", ed->fqcname); + else + prcode(fp, "NULL, "); + + if (ed->ecd == NULL) + prcode(fp, "-1"); + else + prcode(fp, "%d", ed->ecd->classnr); + + if (ed->slots != NULL) + prcode(fp, ", slots_%C", ed->fqcname); + else + prcode(fp, ", NULL"); + + prcode(fp, "},\n" + ); + } + + prcode(fp, +"};\n" + ); + + nr_enummembers = generateEnumMemberTable(pt,NULL,fp); + } + else + nr_enummembers = 0; + + if (pt->module->nrtypedefs > 0) + { + typedefDef *td; + + prcode(fp, +"\n" +"\n" +"/*\n" +" * These define each typedef in this module. They are only needed in case\n" +" * they are used as arguments to Qt signals.\n" +" */\n" +"\n" +"static sipTypedefDef typedefsTable[] = {\n" + ); + + for (td = pt->typedefs; td != NULL; td = td->next) + { + char *tdmname, *sat; + scopedNameDef *fqname; + + if (td->module != pt->module) + continue; + + fqname = NULL; + tdmname = NULL; + sat = "unknown"; + + switch (td->type.atype) + { + case string_type: + sat = (td->type.nrderefs == 0 ? "char" : "string"); + break; + + case sstring_type: + sat = (td->type.nrderefs == 0 ? "schar" : "sstring"); + break; + + case ustring_type: + sat = (td->type.nrderefs == 0 ? "uchar" : "ustring"); + break; + + case wstring_type: + sat = (td->type.nrderefs == 0 ? "wchar" : "wstring"); + break; + + case short_type: + sat = "short"; + break; + + case ushort_type: + sat = "ushort"; + break; + + case cint_type: + case int_type: + sat = "int"; + break; + + case uint_type: + sat = "uint"; + break; + + case long_type: + sat = "long"; + break; + + case ulong_type: + sat = "ulong"; + break; + + case longlong_type: + sat = "longlong"; + break; + + case ulonglong_type: + sat = "ulonglong"; + break; + + case cfloat_type: + case float_type: + sat = "float"; + break; + + case cdouble_type: + case double_type: + sat = "double"; + break; + + case bool_type: + case cbool_type: + sat = "bool"; + break; + + case void_type: + if (td->type.nrderefs != 0) + sat = "void"; + break; + + case enum_type: + if ((fqname = td->type.u.ed->fqcname) != NULL) + { + sat = "enum"; + + if (td->type.u.ed->module != pt->module) + tdmname = td->type.u.ed->module->fullname; + } + break; + + case class_type: + sat = "class"; + fqname = classFQCName(td->type.u.cd); + + if (td->type.u.cd->iff->module != pt->module) + tdmname = td->type.u.cd->iff->module->fullname; + break; + + case mapped_type: + sat = "mtype"; + fqname = td->type.u.mtd->iff->fqcname; + + if (td->type.u.mtd->iff->module != pt->module) + tdmname = td->type.u.mtd->iff->module->fullname; + break; + } + + prcode(fp, +" {\"%S\", %s_sat", td->fqname, sat); + + if (fqname != NULL) + prcode(fp, ", \"%S\"", fqname); + else + prcode(fp, ", NULL"); + + if (tdmname != NULL) + prcode(fp, ", \"%s\"", tdmname); + else + prcode(fp, ", NULL"); + + prcode(fp, "},\n" + ); + } + + prcode(fp, +" {NULL, unknown_sat, NULL, NULL}\n" +"};\n" + ); + } + + if (pt->module->nrvirthandlers > 0) + { + prcode(fp, +"\n" +"\n" +"/*\n" +" * This defines the virtual handlers that this module implements and can be\n" +" * used by other modules.\n" +" */\n" +"static sipVirtHandlerFunc virtHandlersTable[] = {\n" + ); + + for (vhd = pt->module->virthandlers; vhd != NULL; vhd = vhd->next) + if (!isDuplicateVH(vhd)) + prcode(fp, +" (sipVirtHandlerFunc)sipVH_%s_%d,\n" + ,mname,vhd->virthandlernr); + + prcode(fp, +"};\n" + ); + } + + if (pt->module->allimports != NULL) + { + prcode(fp, +"\n" +"\n" +"/* This defines the modules that this module needs to import. */\n" +"static sipImportedModuleDef importsTable[] = {\n" + ); + + for (mld = pt->module->allimports; mld != NULL; mld = mld->next) + prcode(fp, +" {\"%s\", %d, NULL},\n" + , mld->module->fullname, mld->module->version); + + prcode(fp, +" {NULL, -1, NULL}\n" +"};\n" + ); + } + + if (nrSccs > 0) + { + prcode(fp, +"\n" +"\n" +"/* This defines the class sub-convertors that this module defines. */\n" +"static sipSubClassConvertorDef convertorsTable[] = {\n" + ); + + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + if (cd->iff->module != pt->module) + continue; + + if (cd->convtosubcode == NULL) + continue; + + prcode(fp, +" {sipSubClass_%C, ",classFQCName(cd)); + + generateEncodedClass(pt,cd->subbase,0,fp); + + prcode(fp,", NULL},\n"); + } + + prcode(fp, +" {NULL, {0, 0, 0}, NULL}\n" +"};\n" + ); + } + + /* Generate any license information. */ + if (pt->module->license != NULL) + { + licenseDef *ld = pt->module->license; + + prcode(fp, +"\n" +"\n" +"/* Define the module's license. */\n" +"static sipLicenseDef module_license = {\n" + ); + + prcode(fp, +" \"%s\",\n" + ,ld->type); + + if (ld->licensee != NULL) + prcode(fp, +" \"%s\",\n" + ,ld->licensee); + else + prcode(fp, +" NULL,\n" + ); + + if (ld->timestamp != NULL) + prcode(fp, +" \"%s\",\n" + ,ld->timestamp); + else + prcode(fp, +" NULL,\n" + ); + + if (ld->sig != NULL) + prcode(fp, +" \"%s\"\n" + ,ld->sig); + else + prcode(fp, +" NULL\n" + ); + + prcode(fp, +"};\n" + ); + } + + /* Generate each instance table. */ + is_inst_class = generateClasses(pt,NULL,fp); + is_inst_voidp = generateVoidPointers(pt,NULL,fp); + is_inst_char = generateChars(pt,NULL,fp); + is_inst_string = generateStrings(pt,NULL,fp); + is_inst_int = generateInts(pt,NULL,fp); + is_inst_long = generateLongs(pt,NULL,fp); + is_inst_ulong = generateUnsignedLongs(pt,NULL,fp); + is_inst_longlong = generateLongLongs(pt,NULL,fp); + is_inst_ulonglong = generateUnsignedLongLongs(pt,NULL,fp); + is_inst_double = generateDoubles(pt,NULL,fp); + is_inst_enum = generateEnums(pt,NULL,fp); + + /* Generate any exceptions table. */ + if (pt->module->nrexceptions > 0) + prcode(fp, +"\n" +"\n" +"static PyObject *exceptionsTable[%d];\n" + ,pt->module->nrexceptions); + + /* Generate any Qt support API. */ + if (pt->qobjclass >= 0) + prcode(fp, +"\n" +"\n" +"/* This defines the Qt support API. */\n" +"\n" +"static sipQtAPI qtAPI = {\n" +" &typesTable[%d],\n" +" sipQtIsQtSignal,\n" +" sipQtCreateUniversalSignalShortcut,\n" +" sipQtCreateUniversalSignal,\n" +" sipQtFindUniversalSignalShortcut,\n" +" sipQtFindUniversalSignal,\n" +" sipQtEmitSignalShortcut,\n" +" sipQtEmitSignal,\n" +" sipQtCreateUniversalSlot,\n" +" sipQtDestroyUniversalSlot,\n" +" sipQtFindSlot,\n" +" sipQtConnect,\n" +" sipQtDisconnect,\n" +" sipQtSignalsBlocked,\n" +" sipQtGetSender,\n" +" sipQtForgetSender,\n" +" sipQtSameSignalSlotName,\n" +" sipQtFindConnection\n" +"};\n" + ,pt->qobjclass); + + prcode(fp, +"\n" +"\n" +"/* This defines this module. */\n" +"sipExportedModuleDef sipModuleAPI_%s = {\n" +" NULL,\n" +" SIP_API_MINOR_NR,\n" +" \"%s\",\n" +" NULL,\n" +" %d,\n" +" %s,\n" +" %s,\n" +" %d,\n" +" %s,\n" +" %s,\n" +" %s,\n" +" %d,\n" +" NULL,\n" +" %s,\n" +" %d,\n" +" %s,\n" +" %s,\n" +" %s,\n" +" %s,\n" +" {%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s},\n" +" %s,\n" +" %s,\n" +" %s,\n" +" %s,\n" +" %s,\n" +" NULL\n" +"};\n" + , mname + , pt->module->fullname + , pt->module->version + , pt->module->allimports != NULL ? "importsTable" : "NULL" + , pt->qobjclass >= 0 ? "&qtAPI" : "NULL" + , pt->module->nrclasses + , pt->module->nrclasses > 0 ? "typesTable" : "NULL" + , hasexternal ? "externalTypesTable" : "NULL" + , pt->module->nrmappedtypes > 0 ? "mappedTypesTable" : "NULL" + , pt->module->nrenums + , pt->module->nrenums > 0 ? "enumTypesTable" : "NULL" + , nr_enummembers + , nr_enummembers > 0 ? "enummembers" : "NULL" + , pt->module->nrtypedefs > 0 ? "typedefsTable" : "NULL" + , pt->module->nrvirthandlers > 0 ? "virtHandlersTable" : "NULL" + , nrSccs > 0 ? "convertorsTable" : "NULL" + , is_inst_class ? "classInstances" : "NULL" + , is_inst_voidp ? "voidPtrInstances" : "NULL" + , is_inst_char ? "charInstances" : "NULL" + , is_inst_string ? "stringInstances" : "NULL" + , is_inst_int ? "intInstances" : "NULL" + , is_inst_long ? "longInstances" : "NULL" + , is_inst_ulong ? "unsignedLongInstances" : "NULL" + , is_inst_longlong ? "longLongInstances" : "NULL" + , is_inst_ulonglong ? "unsignedLongLongInstances" : "NULL" + , is_inst_double ? "doubleInstances" : "NULL" + , is_inst_enum ? "enumInstances" : "NULL" + , pt->module->license != NULL ? "&module_license" : "NULL" + , pt->module->nrexceptions > 0 ? "exceptionsTable" : "NULL" + , slot_extenders ? "slotExtenders" : "NULL" + , ctor_extenders ? "initExtenders" : "NULL" + , hasDelayedDtors(pt->module) ? "sipDelayedDtors" : "NULL"); + + /* Generate the storage for the external API pointers. */ + prcode(fp, +"\n" +"\n" +"/* The SIP API and the APIs of any imported modules. */\n" +"const sipAPIDef *sipAPI_%s;\n" + ,mname); + + for (mld = pt->module->allimports; mld != NULL; mld = mld->next) + prcode(fp, +"const sipExportedModuleDef *sipModuleAPI_%s_%s;\n" + ,mname,mld->module->name); + + if (optQ_OBJECT4(pt)) + prcode(fp, +"\n" +"sip_qt_metaobject_func sip_%s_qt_metaobject;\n" +"sip_qt_metacall_func sip_%s_qt_metacall;\n" + , mname + , mname); + + /* Generate the Python module initialisation function. */ + prcode(fp, +"\n" +"\n" +"/* The Python module initialisation function. */\n" +"#if defined(SIP_STATIC_MODULE)\n" +"%svoid init%s()\n" +"#else\n" +"PyMODINIT_FUNC init%s()\n" +"#endif\n" +"{\n" +" static PyMethodDef sip_methods[] = {\n" + ,(generating_c ? "" : "extern \"C\" "), mname + ,mname); + + /* Generate the global functions. */ + + for (md = pt->othfuncs; md != NULL; md = md->next) + if (md->module == pt->module && md->slot == no_slot) + prcode(fp, +" {%N, func_%s, METH_VARARGS, NULL},\n" + ,md->pyname,md->pyname->text); + + prcode(fp, +" {0, 0, 0, 0}\n" +" };\n" +"\n" +" PyObject *sipModule, *sipModuleDict, *sip_sipmod, *sip_capiobj;\n" +"\n" + ); + + /* Generate the pre-initialisation code. */ + generateCppCodeBlock(pt->preinitcode,fp); + + prcode(fp, +" /* Initialise the module and get it's dictionary. */\n" +" sipModule = Py_InitModule((char *)sipModuleAPI_%s.em_name,sip_methods);\n" +" sipModuleDict = PyModule_GetDict(sipModule);\n" +"\n" +" /* Import the SIP module and get it's API. */\n" +" sip_sipmod = PyImport_ImportModule((char *)\"sip\");\n" +"\n" +" if (sip_sipmod == NULL)\n" +" return;\n" +"\n" +" sip_capiobj = PyDict_GetItemString(PyModule_GetDict(sip_sipmod),\"_C_API\");\n" +"\n" +" if (sip_capiobj == NULL || !PyCObject_Check(sip_capiobj))\n" +" return;\n" +"\n" + ,mname); + + if (generating_c) + prcode(fp, +" sipAPI_%s = (const sipAPIDef *)PyCObject_AsVoidPtr(sip_capiobj);\n" + ,mname); + else + prcode(fp, +" sipAPI_%s = reinterpret_cast<const sipAPIDef *>(PyCObject_AsVoidPtr(sip_capiobj));\n" + ,mname); + + prcode(fp, +"\n" +" /* Export the module and publish it's API. */\n" +" if (sipAPI_%s->api_export_module(&sipModuleAPI_%s,SIP_API_MAJOR_NR,SIP_API_MINOR_NR,sipModuleDict) < 0)\n" +" return;\n" + ,mname + ,mname); + + noIntro = TRUE; + + for (mld = pt->module->allimports; mld != NULL; mld = mld->next) + { + if (noIntro) + { + prcode(fp, +"\n" +" /* Get the APIs of the modules that this one is dependent on. */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" sipModuleAPI_%s_%s = sipModuleAPI_%s.em_imports[%d].im_module;\n" + ,mname,mld->module->name,mname,mld->module->modulenr); + } + + generateClassesInline(pt,fp); + generateEnumsInline(pt, fp); + + /* Create any exceptions. */ + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + { + if (xd->iff->module != pt->module) + continue; + + if (xd->iff->type != exception_iface) + continue; + + if (xd->exceptionnr < 0) + continue; + + prcode(fp, +"\n" +" if ((exceptionsTable[%d] = PyErr_NewException((char *)\"%s.%s\",", xd->exceptionnr, xd->iff->module->name, xd->pyname); + + if (xd->bibase != NULL) + prcode(fp, "PyExc_%s", xd->bibase); + else if (xd->base->iff->module == pt->module) + prcode(fp, "exceptionsTable[%d]", xd->base->exceptionnr); + else + prcode(fp, "sipException_%C", xd->base->iff->fqcname); + + prcode(fp, ",NULL)) == NULL || PyDict_SetItemString(sipModuleDict,\"%s\",exceptionsTable[%d]) < 0)\n" +" return;\n" + , xd->pyname, xd->exceptionnr); + } + + /* Generate any Qt metatype registration calls. */ + if (optRegisterTypes(pt)) + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + if (cd->iff->module != pt->module) + continue; + + generateRegisterMetaType(cd, fp); + } + + /* Generate the post-initialisation code. */ + generateCppCodeBlock(pt->postinitcode,fp); + + /* + * This has to be done after the post-initialisation code in case this + * module is exporting the symbol. + */ + if (optQ_OBJECT4(pt)) + prcode(fp, +"\n" +" sip_%s_qt_metaobject = (sip_qt_metaobject_func)sipImportSymbol(\"qtcore_qt_metaobject\");\n" +" sip_%s_qt_metacall = (sip_qt_metacall_func)sipImportSymbol(\"qtcore_qt_metacall\");\n" + , mname + , mname); + + prcode(fp, +"}\n" + ); + + /* Generate the interface source and header files. */ + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + { + if (iff->module == pt->module && iff->type != exception_iface) + { + if (*parts && files_in_part++ == max_per_part) + { + /* Close the old part. */ + closeFile(fp); + free(cppfile); + + /* Create a new one. */ + files_in_part = 1; + ++this_part; + + cppfile = makePartName(codeDir,mname,this_part,srcSuffix); + fp = createCompilationUnit(pt, cppfile, "Module code."); + } + + generateIfaceCpp(pt,iff,codeDir,srcSuffix,(*parts ? fp : NULL)); + } + + generateIfaceHeader(pt,iff,codeDir); + } + + closeFile(fp); + free(cppfile); + + /* How many parts we actually generated. */ + if (*parts) + *parts = this_part + 1; +} + + +/* + * Generate all the sub-class convertors for a module. + */ +static int generateSubClassConvertors(sipSpec *pt, FILE *fp) +{ + int nrSccs = 0; + classDef *cd; + + for (cd = pt->classes; cd != NULL; cd = cd->next) + { + if (cd->iff->module != pt->module) + continue; + + if (cd->convtosubcode == NULL) + continue; + + prcode(fp, +"\n" +"\n" +"/* Convert to a sub-class if possible. */\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static sipWrapperType *sipSubClass_%C(void **);}\n" + , classFQCName(cd)); + + prcode(fp, +"static sipWrapperType *sipSubClass_%C(void **sipCppRet)\n" +"{\n" +" %S *sipCpp = reinterpret_cast<%S *>(*sipCppRet);\n" +" sipWrapperType *sipClass;\n" +"\n" + , classFQCName(cd) + , classFQCName(cd->subbase), classFQCName(cd->subbase)); + + generateCppCodeBlock(cd->convtosubcode, fp); + + prcode(fp, +"\n" +" return sipClass;\n" +"}\n" + ); + + ++nrSccs; + } + + return nrSccs; +} + + +/* + * Generate an entry for a class in the classes table and all its children. + */ +static void generateClassTableEntries(sipSpec *pt, nodeDef *nd, FILE *fp) +{ + nodeDef *cnd; + + /* Generate the entry if it's not the root. */ + if (nd->cd != NULL) + if (isExternal(nd->cd)) + prcode(fp, +" 0,\n" + ); + else + prcode(fp, +" (sipWrapperType *)(void *)&sipType_%s_%C,\n" + , pt->module->name, classFQCName(nd->cd)); + + /* Generate all it's children. */ + for (cnd = nd->child; cnd != NULL; cnd = cnd->next) + generateClassTableEntries(pt, cnd, fp); +} + + +/* + * Generate the structure representing an encoded class. + */ +static void generateEncodedClass(sipSpec *pt,classDef *cd,int last,FILE *fp) +{ + moduleDef *mod = cd->iff->module; + + prcode(fp,"{%u, ",cd->classnr); + + if (mod == pt->module) + prcode(fp,"255"); + else + prcode(fp,"%u",mod->modulenr); + + prcode(fp,", %u}",last); +} + + +/* + * Generate an ordinary function (ie. not a class method). + */ +static void generateOrdinaryFunction(sipSpec *pt,classDef *cd,memberDef *md, + FILE *fp) +{ + overDef *od; + + prcode(fp, +"\n" +"\n" + ); + + if (cd != NULL) + { + if (!generating_c) + prcode(fp, +"extern \"C\" {static PyObject *meth_%C_%s(PyObject *,PyObject *);}\n" + , classFQCName(cd), md->pyname->text); + + prcode(fp, +"static PyObject *meth_%C_%s(PyObject *,PyObject *sipArgs)\n" + ,classFQCName(cd),md->pyname->text); + + od = cd->overs; + } + else + { + if (!generating_c) + prcode(fp, +"extern \"C\" {static PyObject *func_%s(PyObject *,PyObject *);}\n" + , md->pyname->text); + + prcode(fp, +"static PyObject *func_%s(PyObject *%s,PyObject *sipArgs)\n" + ,md->pyname->text,(generating_c ? "sipSelf" : "")); + + od = pt->overs; + } + + prcode(fp, +"{\n" +" int sipArgsParsed = 0;\n" + ); + + while (od != NULL) + { + if (od->common == md) + generateFunctionBody(pt,od,cd,cd,TRUE,fp); + + od = od->next; + } + + prcode(fp, +"\n" +" /* Raise an exception if the arguments couldn't be parsed. */\n" +" sipNoFunction(sipArgsParsed,%N);\n" +"\n" +" return NULL;\n" +"}\n" + ,md->pyname); +} + + +/* + * Generate the table of enum members for a scope. Return the number of them. + */ +static int generateEnumMemberTable(sipSpec *pt,classDef *cd,FILE *fp) +{ + int i, nr_members; + enumDef *ed; + enumMemberDef **etab, **et; + + /* First we count how many. */ + + nr_members = 0; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + enumMemberDef *emd; + + if (ed->ecd != cd || ed->module != pt->module) + continue; + + if (cd == NULL && ed->fqcname == NULL) + continue; + + for (emd = ed->members; emd != NULL; emd = emd->next) + ++nr_members; + } + + if (nr_members == 0) + return 0; + + /* Create a table so they can be sorted. */ + + etab = sipMalloc(sizeof (enumMemberDef *) * nr_members); + + et = etab; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + enumMemberDef *emd; + + if (ed->ecd != cd || ed->module != pt->module) + continue; + + if (cd == NULL && ed->fqcname == NULL) + continue; + + for (emd = ed->members; emd != NULL; emd = emd->next) + *et++ = emd; + } + + qsort(etab,nr_members,sizeof (enumMemberDef *),compareEnumMembers); + + /* Now generate the table. */ + + if (cd != NULL) + prcode(fp, +"\n" +"static sipEnumMemberDef enummembers_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"/* These are the enum members of all global enums. */\n" +"static sipEnumMemberDef enummembers[] = {\n" + ); + + for (i = 0; i < nr_members; ++i) + { + enumMemberDef *emd; + + emd = etab[i]; + + prcode(fp, +" {%N, ",emd->pyname); + + if (cd != NULL) + { + if (isProtectedEnum(emd->ed)) + prcode(fp,"sip"); + + prcode(fp,"%S::%s",classFQCName(cd),emd->cname); + } + else + prcode(fp,"%s" + ,emd->cname); + + prcode(fp, ", %d},\n", emd->ed->enumnr); + } + + prcode(fp, +"};\n" + ); + + return nr_members; +} + + +/* + * The qsort helper to compare two enumMemberDef structures based on the name + * of the enum member. + */ + +static int compareEnumMembers(const void *m1,const void *m2) +{ + return strcmp((*(enumMemberDef **)m1)->pyname->text, + (*(enumMemberDef **)m2)->pyname->text); +} + + +/* + * Generate the access functions for the variables. + */ +static void generateAccessFunctions(sipSpec *pt,classDef *cd,FILE *fp) +{ + varDef *vd; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->accessfunc == NULL) + continue; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + prcode(fp, +"\n" +"\n" +"/* Access function. */\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void *access_%C();}\n" + , vd->fqcname); + + prcode(fp, +"static void *access_%C()\n" +"{\n" + ,vd->fqcname); + + generateCppCodeBlock(vd->accessfunc,fp); + + prcode(fp, +"}\n" + ); + } +} + + +/* + * Generate the inline code to add a set of enum instances to a dictionary. + */ +static void generateEnumsInline(sipSpec *pt, FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->module != pt->module) + continue; + + if (vd->type.atype != enum_type) + continue; + + if (needsHandler(vd)) + continue; + + /* Skip enums that don't need inline code. */ + if (generating_c || vd->accessfunc != NULL || vd->type.nrderefs != 0) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +" /* Define the enum instances that have to be added inline. */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" sipAddEnumInstance("); + + if (vd->ecd == NULL) + prcode(fp,"sipModuleDict"); + else + prcode(fp,"(PyObject *)sipClass_%C",classFQCName(vd->ecd)); + + prcode(fp,",%N,(int)%S,sipEnum_%C);\n" + , vd->pyname, vd->fqcname, vd->type.u.ed->fqcname); + } +} + + +/* + * Generate the inline code to add a set of class instances to a dictionary. + */ +static void generateClassesInline(sipSpec *pt,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->module != pt->module) + continue; + + if (vd->type.atype != class_type && vd->type.atype != mapped_type) + continue; + + if (needsHandler(vd)) + continue; + + /* Skip classes that don't need inline code. */ + if (generating_c || vd->accessfunc != NULL || vd->type.nrderefs != 0) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +" /*\n" +" * Define the class and mapped type instances that have to be added\n" +" * inline.\n" +" */\n" + ); + + noIntro = FALSE; + } + + if (vd->type.atype == class_type) + prcode(fp, +" sipAddClassInstance("); + else + prcode(fp, +" sipAddMappedTypeInstance("); + + if (vd->ecd == NULL) + prcode(fp,"sipModuleDict"); + else + prcode(fp,"(PyObject *)sipClass_%C",classFQCName(vd->ecd)); + + prcode(fp,",%N,",vd->pyname); + + if (isConstArg(&vd->type)) + prcode(fp,"const_cast<%b *>(&%S)",&vd->type,vd->fqcname); + else + prcode(fp,"&%S",vd->fqcname); + + if (vd->type.atype == class_type) + prcode(fp, ",sipClass_%C);\n" + , classFQCName(vd->type.u.cd)); + else + prcode(fp, ",sipMappedType_%T);\n" + , &vd->type); + } +} + + +/* + * Generate the code to add a set of class instances to a dictionary. Return + * TRUE if there was at least one. + */ +static int generateClasses(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + scopedNameDef *vcname; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (vd->type.atype != class_type) + continue; + + if (needsHandler(vd)) + continue; + + /* + * Skip ordinary C++ class instances which need to be done with + * inline code rather than through a static table. This is + * because C++ does not guarantee the order in which the table + * and the instance will be created. So far this has only been + * seen to be a problem when statically linking SIP generated + * modules on Windows. + */ + if (!generating_c && vd->accessfunc == NULL && vd->type.nrderefs == 0) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the class instances to be added to this type dictionary. */\n" +"static sipClassInstanceDef classInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the class instances to be added to this module dictionary. */\n" +"static sipClassInstanceDef classInstances[] = {\n" + ); + + noIntro = FALSE; + } + + vcname = classFQCName(vd->type.u.cd); + + if (vd->accessfunc != NULL) + { + prcode(fp, +" {%N, (void *)access_%C, &sipClass_%C, SIP_ACCFUNC},\n" + ,vd->pyname,vd->fqcname,vcname); + } + else if (vd->type.nrderefs != 0) + { + prcode(fp, +" {%N, &%S, &sipClass_%C, SIP_INDIRECT},\n" + ,vd->pyname,vd->fqcname,vcname); + } + else if (isConstArg(&vd->type)) + { + prcode(fp, +" {%N, const_cast<%b *>(&%S), &sipClass_%C, 0},\n" + ,vd->pyname,&vd->type,vd->fqcname,vcname); + } + else + { + prcode(fp, +" {%N, &%S, &sipClass_%C, 0},\n" + ,vd->pyname,vd->fqcname,vcname); + } + } + + if (!noIntro) + prcode(fp, +" {0, 0, 0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of void pointers to a dictionary. Return + * TRUE if there was at least one. + */ +static int generateVoidPointers(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (vd->type.atype != void_type && vd->type.atype != struct_type) + continue; + + if (needsHandler(vd)) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the void pointers to be added to this type dictionary. */\n" +"static sipVoidPtrInstanceDef voidPtrInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the void pointers to be added to this module dictionary. */\n" +"static sipVoidPtrInstanceDef voidPtrInstances[] = {\n" + ); + + noIntro = FALSE; + } + + if (isConstArg(&vd->type)) + prcode(fp, +" {%N, const_cast<%b *>(%S)},\n" + , vd->pyname, &vd->type, vd->fqcname); + else + prcode(fp, +" {%N, %S},\n" + , vd->pyname, vd->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of characters to a dictionary. Return TRUE + * if there was at least one. + */ +static int generateChars(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + argType vtype = vd->type.atype; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type || vtype == wstring_type) && vd->type.nrderefs == 0)) + continue; + + if (needsHandler(vd)) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the chars to be added to this type dictionary. */\n" +"static sipCharInstanceDef charInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the chars to be added to this module dictionary. */\n" +"static sipCharInstanceDef charInstances[] = {\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, %S},\n" + ,vd->pyname,vd->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of strings to a dictionary. Return TRUE if + * there is at least one. + */ +static int generateStrings(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + argType vtype = vd->type.atype; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type || vtype == wstring_type) && vd->type.nrderefs != 0)) + continue; + + if (needsHandler(vd)) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the strings to be added to this type dictionary. */\n" +"static sipStringInstanceDef stringInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the strings to be added to this module dictionary. */\n" +"static sipStringInstanceDef stringInstances[] = {\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, %S},\n" + ,vd->pyname,vd->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of enum instances to a dictionary. Return + * TRUE if there was at least one. + */ +static int generateEnums(sipSpec *pt, classDef *cd, FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (vd->type.atype != enum_type || vd->type.u.ed->fqcname == NULL) + continue; + + if (needsHandler(vd)) + continue; + + /* Skip enums that need inline code. */ + if (!generating_c && vd->accessfunc == NULL && vd->type.nrderefs == 0) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the enum instances to be added to this type dictionary. */\n" +"static sipEnumInstanceDef enumInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the enum instances to be added to this module dictionary. */\n" +"static sipEnumInstanceDef enumInstances[] = {\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, (int)%S, &sipEnum_%C},\n" + ,vd->pyname,vd->fqcname,vd->type.u.ed->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of ints to a dictionary. Return TRUE if + * there was at least one. + */ +static int generateInts(sipSpec *pt, classDef *cd, FILE *fp) +{ + int noIntro; + varDef *vd; + enumDef *ed; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + argType vtype = vd->type.atype; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (!(vtype == enum_type || vtype == ushort_type || + vtype == short_type || vtype == uint_type || + vtype == cint_type || vtype == int_type || + vtype == bool_type || vtype == cbool_type)) + continue; + + if (needsHandler(vd)) + continue; + + /* Named enums are handled elsewhere. */ + if (vtype == enum_type && vd->type.u.ed->fqcname != NULL) + continue; + + if (noIntro) + { + ints_intro(cd, fp); + noIntro = FALSE; + } + + prcode(fp, +" {%N, %S},\n" + ,vd->pyname,vd->fqcname); + } + + /* Now do global anonymous enums. */ + if (cd == NULL) + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + enumMemberDef *em; + + if (ed->ecd != cd || ed->module != pt->module) + continue; + + if (ed->fqcname != NULL) + continue; + + for (em = ed->members; em != NULL; em = em->next) + { + if (noIntro) + { + ints_intro(cd, fp); + noIntro = FALSE; + } + + prcode(fp, +" {%N, %s},\n" + , em->pyname, em->cname); + } + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the intro for a table of int instances. + */ +static void ints_intro(classDef *cd, FILE *fp) +{ + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the ints to be added to this type dictionary. */\n" +"static sipIntInstanceDef intInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the ints to be added to this module dictionary. */\n" +"static sipIntInstanceDef intInstances[] = {\n" + ); +} + + +/* + * Generate the code to add a set of longs to a dictionary. Return TRUE if + * there was at least one. + */ +static int generateLongs(sipSpec *pt, classDef *cd, FILE *fp) +{ + return generateVariableType(pt, cd, long_type, "long", "Long", "long", fp); +} + + +/* + * Generate the code to add a set of unsigned longs to a dictionary. Return + * TRUE if there was at least one. + */ +static int generateUnsignedLongs(sipSpec *pt, classDef *cd, FILE *fp) +{ + return generateVariableType(pt, cd, ulong_type, "unsigned long", "UnsignedLong", "unsignedLong", fp); +} + + +/* + * Generate the code to add a set of long longs to a dictionary. Return TRUE + * if there was at least one. + */ +static int generateLongLongs(sipSpec *pt, classDef *cd, FILE *fp) +{ + return generateVariableType(pt, cd, longlong_type, "long long", "LongLong", "longLong", fp); +} + + +/* + * Generate the code to add a set of unsigned long longs to a dictionary. + * Return TRUE if there was at least one. + */ +static int generateUnsignedLongLongs(sipSpec *pt, classDef *cd, FILE *fp) +{ + return generateVariableType(pt, cd, ulonglong_type, "unsigned long long", "UnsignedLongLong", "unsignedLongLong", fp); +} + + +/* + * Generate the code to add a set of a particular type to a dictionary. Return + * TRUE if there was at least one. + */ +static int generateVariableType(sipSpec *pt, classDef *cd, argType atype, const char *eng, const char *s1, const char *s2, FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + argType vtype = vd->type.atype; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (vtype != atype) + continue; + + if (needsHandler(vd)) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the %ss to be added to this type dictionary. */\n" +"static sip%sInstanceDef %sInstances_%C[] = {\n" + , eng + , s1, s2, classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the %ss to be added to this module dictionary. */\n" +"static sip%sInstanceDef %sInstances[] = {\n" + , eng + , s1, s2); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, %S},\n" + ,vd->pyname,vd->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the code to add a set of doubles to a dictionary. Return TRUE if + * there was at least one. + */ +static int generateDoubles(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro; + varDef *vd; + + noIntro = TRUE; + + for (vd = pt->vars; vd != NULL; vd = vd->next) + { + argType vtype = vd->type.atype; + + if (vd->ecd != cd || vd->module != pt->module) + continue; + + if (!(vtype == float_type || vtype == cfloat_type || vtype == double_type || vtype == cdouble_type)) + continue; + + if (needsHandler(vd)) + continue; + + if (noIntro) + { + if (cd != NULL) + prcode(fp, +"\n" +"\n" +"/* Define the doubles to be added to this type dictionary. */\n" +"static sipDoubleInstanceDef doubleInstances_%C[] = {\n" + ,classFQCName(cd)); + else + prcode(fp, +"\n" +"\n" +"/* Define the doubles to be added to this module dictionary. */\n" +"static sipDoubleInstanceDef doubleInstances[] = {\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, %S},\n" + ,vd->pyname,vd->fqcname); + } + + if (!noIntro) + prcode(fp, +" {0, 0}\n" +"};\n" + ); + + return !noIntro; +} + + +/* + * Generate the C/C++ code for an interface. + */ +static void generateIfaceCpp(sipSpec *pt,ifaceFileDef *iff,char *codeDir, + char *srcSuffix,FILE *master) +{ + char *cppfile, *cmname = iff->module->name; + classDef *cd; + mappedTypeDef *mtd; + FILE *fp; + + if (master == NULL) + { + cppfile = createIfaceFileName(codeDir,iff,srcSuffix); + fp = createCompilationUnit(pt, cppfile, "Interface wrapper code."); + } + else + fp = master; + + prcode(fp, +"\n" +"#include \"sipAPI%s.h\"\n" +"#include \"sip%s%F.h\"\n" + ,cmname + ,cmname,iff->fqcname); + + generateUsedIncludes(iff->used, FALSE, fp); + + for (cd = pt->classes; cd != NULL; cd = cd->next) + if (cd->iff == iff) + { + if (isProtectedClass(cd)) + prcode(fp, +"\n" +"#include \"sip%s%F.h\"\n" + ,cmname,cd->ecd->iff->fqcname); + + if (!isExternal(cd)) + generateClassCpp(cd, pt, fp); + } + + for (mtd = pt->mappedtypes; mtd != NULL; mtd = mtd->next) + if (mtd->iff == iff) + generateMappedTypeCpp(mtd,fp); + + if (master == NULL) + { + closeFile(fp); + free(cppfile); + } +} + + +/* + * Return a filename for an interface C++ or header file on the heap. + */ +static char *createIfaceFileName(char *codeDir,ifaceFileDef *iff,char *suffix) +{ + char *fn; + scopedNameDef *snd; + + fn = concat(codeDir,"/sip",iff->module->name,NULL); + + for (snd = iff->fqcname; snd != NULL; snd = snd->next) + append(&fn,snd->name); + + append(&fn,suffix); + + return fn; +} + + +/* + * Generate the C++ code for a mapped type version. + */ +static void generateMappedTypeCpp(mappedTypeDef *mtd,FILE *fp) +{ + int need_xfer; + + prcode(fp, +"\n" +"\n" +"/* Call the mapped type's destructor. */\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void release_%T(void *, int);}\n" + , &mtd->type); + + prcode(fp, +"static void release_%T(void *ptr, int%s)\n" +"{\n" + , &mtd->type, (generating_c ? " status" : "")); + + if (release_gil) + prcode(fp, +" Py_BEGIN_ALLOW_THREADS\n" + ); + + if (generating_c) + prcode(fp, +" sipFree(ptr);\n" + ); + else + prcode(fp, +" delete reinterpret_cast<%b *>(ptr);\n" + , &mtd->type); + + if (release_gil) + prcode(fp, +" Py_END_ALLOW_THREADS\n" + ); + + prcode(fp, +"}\n" +"\n" + ); + + generateConvertToDefinitions(mtd,NULL,fp); + + /* Generate the from type convertor. */ + + need_xfer = (generating_c || usedInCode(mtd->convfromcode, "sipTransferObj")); + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static PyObject *convertFrom_%T(void *, PyObject *);}\n" + , &mtd->type); + + prcode(fp, +"static PyObject *convertFrom_%T(void *sipCppV,PyObject *%s)\n" +"{\n" +" ", &mtd->type, (need_xfer ? "sipTransferObj" : "")); + + generateMappedTypeFromVoid(mtd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +"\n" + ); + + generateCppCodeBlock(mtd->convfromcode,fp); + + prcode(fp, +"}\n" +"\n" +"\n" +"sipMappedType sipMappedTypeDef_%T = {\n" +" \"%B\",\n" +" release_%T,\n" +" forceConvertTo_%T,\n" +" convertTo_%T,\n" +" convertFrom_%T\n" +"};\n" + , &mtd->type + , &mtd->type + , &mtd->type + , &mtd->type + , &mtd->type + , &mtd->type + , &mtd->type); +} + + +/* + * Generate the C++ code for a class. + */ +static void generateClassCpp(classDef *cd,sipSpec *pt,FILE *fp) +{ + varDef *vd; + + /* Generate any local class code. */ + + generateCppCodeBlock(cd->cppcode,fp); + + generateClassFunctions(pt,cd,fp); + + generateAccessFunctions(pt,cd,fp); + + /* Generate the variable handlers. */ + if (hasVarHandlers(cd)) + { + for (vd = pt->vars; vd != NULL; vd = vd->next) + if (vd->ecd == cd && needsHandler(vd)) + generateVariableHandler(vd,fp); + + /* Generate the variable table. */ + prcode(fp, +"\n" +"PyMethodDef variables_%C[] = {\n" + ,classFQCName(cd)); + + for (vd = pt->vars; vd != NULL; vd = vd->next) + if (vd->ecd == cd && needsHandler(vd)) + prcode(fp, +" {%N, var_%C, %s, NULL},\n" + ,vd->pyname,vd->fqcname,(isStaticVar(vd) ? "METH_STATIC" : "0")); + + prcode(fp, +" {0, 0, 0, 0}\n" +"};\n" + ); + } + + if (cd->iff->type != namespace_iface) + generateConvertToDefinitions(NULL,cd,fp); + + /* The type definition structure. */ + generateTypeDefinition(pt, cd, fp); +} + + +/* + * Return a sorted array of relevant functions for a namespace. + */ + +static sortedMethTab *createFunctionTable(classDef *cd,int *nrp) +{ + int nr; + sortedMethTab *mtab, *mt; + memberDef *md; + + /* First we need to count the number of applicable functions. */ + + nr = 0; + + for (md = cd->members; md != NULL; md = md->next) + ++nr; + + if ((*nrp = nr) == 0) + return NULL; + + /* Create the table of methods. */ + + mtab = sipMalloc(sizeof (sortedMethTab) * nr); + + /* Initialise the table. */ + + mt = mtab; + + for (md = cd->members; md != NULL; md = md->next) + { + mt->md = md; + mt->is_static = TRUE; + + ++mt; + } + + /* Finally sort the table. */ + + qsort(mtab,nr,sizeof (sortedMethTab),compareMethTab); + + return mtab; +} + + +/* + * Return a sorted array of relevant methods (either lazy or non-lazy) for a + * class. + */ +static sortedMethTab *createMethodTable(classDef *cd, int *nrp) +{ + int nr; + visibleList *vl; + sortedMethTab *mtab, *mt; + + /* + * First we need to count the number of applicable methods. Only provide + * an entry point if there is at least one overload that is defined in this + * class and is a non-abstract function or slot. We allow private (even + * though we don't actually generate code) because we need to intercept the + * name before it reaches a more public version further up the class + * hierarchy. We add the ctor and any variable handlers as special + * entries. + */ + nr = 0; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + if (vl->m->slot != no_slot) + continue; + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + /* + * Skip protected methods if we don't have the means to + * handle them. + */ + if (isProtected(od) && !hasShadow(cd)) + continue; + + if (skipOverload(od,vl->m,cd,vl->cd,TRUE)) + continue; + + ++nr; + + break; + } + } + + if ((*nrp = nr) == 0) + return NULL; + + /* Create the table of methods. */ + + mtab = sipMalloc(sizeof (sortedMethTab) * nr); + + /* Initialise the table. */ + + mt = mtab; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + int need_method, is_static; + overDef *od; + + if (vl->m->slot != no_slot) + continue; + + need_method = FALSE; + is_static = TRUE; + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + /* + * Skip protected methods if we don't have the means to + * handle them. + */ + if (isProtected(od) && !hasShadow(cd)) + continue; + + if (!skipOverload(od,vl->m,cd,vl->cd,TRUE)) + { + need_method = TRUE; + + if (!isPrivate(od) && !isStatic(od)) + is_static = FALSE; + } + } + + if (need_method) + { + mt->md = vl->m; + mt->is_static = is_static; + + ++mt; + } + } + + /* Finally sort the table. */ + + qsort(mtab,nr,sizeof (sortedMethTab),compareMethTab); + + return mtab; +} + + +/* + * The qsort helper to compare two sortedMethTab structures based on the Python + * name of the method. + */ + +static int compareMethTab(const void *m1,const void *m2) +{ + return strcmp(((sortedMethTab *)m1)->md->pyname->text, + ((sortedMethTab *)m2)->md->pyname->text); +} + + +/* + * Generate the sorted table of methods and return the number of entries. + */ +static int generateMethodTable(classDef *cd,FILE *fp) +{ + int nr; + sortedMethTab *mtab; + + mtab = (cd->iff->type == namespace_iface) ? + createFunctionTable(cd,&nr) : + createMethodTable(cd,&nr); + + if (mtab != NULL) + { + int i; + + prcode(fp, +"\n" +"\n" +"static PyMethodDef methods_%C[] = {\n" + ,classFQCName(cd)); + + for (i = 0; i < nr; ++i) + { + memberDef *md = mtab[i].md; + + /* + * For the moment we are suppressing the generation of + * METH_STATIC until we understand descriptors better. + * It could be that they will simplify the handling of + * lazy attributes and allow things to be cached in the + * type dictionary. + */ + mtab[i].is_static = FALSE; + + prcode(fp, +" {%N, meth_%C_%s, METH_VARARGS%s, NULL}%s\n" + ,md->pyname,classFQCName(cd),md->pyname->text,(mtab[i].is_static ? "|METH_STATIC" : ""),((i + 1) < nr) ? "," : ""); + } + + free(mtab); + + prcode(fp, +"};\n" + ); + } + + return nr; +} + + +/* + * Generate the "to type" convertor definitions. + */ + +static void generateConvertToDefinitions(mappedTypeDef *mtd,classDef *cd, + FILE *fp) +{ + codeBlock *convtocode; + ifaceFileDef *iff; + argDef type; + + if (cd != NULL) + { + convtocode = cd->convtocode; + + iff = cd->iff; + + type.atype = class_type; + type.u.cd = cd; + } + else + { + convtocode = mtd->convtocode; + + iff = mtd->iff; + + type.atype = mapped_type; + type.u.mtd = mtd; + } + + type.argflags = 0; + type.name = NULL; + type.nrderefs = 0; + type.defval = NULL; + + /* Generate the type convertors. */ + + if (convtocode != NULL) + { + int need_ptr, need_xfer; + + /* + * Sometimes type convertors are just stubs that set the error + * flag, so check if we actually need everything so that we + * can avoid compiler warnings. + */ + need_ptr = (generating_c || usedInCode(convtocode, "sipCppPtr")); + need_xfer = (generating_c || usedInCode(convtocode, "sipTransferObj")); + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static int convertTo_%T(PyObject *, void **, int *, PyObject *);}\n" + , &type); + + prcode(fp, +"static int convertTo_%T(PyObject *sipPy,void **%s,int *sipIsErr,PyObject *%s)\n" +"{\n" + , &type, (need_ptr ? "sipCppPtrV" : ""), (need_xfer ? "sipTransferObj" : "")); + + if (need_ptr) + if (generating_c) + prcode(fp, +" %b **sipCppPtr = (%b **)sipCppPtrV;\n" +"\n" + , &type, &type); + else + prcode(fp, +" %b **sipCppPtr = reinterpret_cast<%b **>(sipCppPtrV);\n" +"\n" + , &type, &type); + + generateCppCodeBlock(convtocode,fp); + + prcode(fp, +"}\n" + ); + } + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void *forceConvertTo_%T(PyObject *, int *);}\n" + , &type); + + prcode(fp, +"static void *forceConvertTo_%T(PyObject *valobj,int *iserrp)\n" +"{\n" +" if (*iserrp || valobj == NULL)\n" +" return NULL;\n" +"\n" + ,&type); + + if (convtocode != NULL) + prcode(fp, +" if (convertTo_%T(valobj,NULL,NULL,NULL))\n" +" {\n" +" void *val;\n" +"\n" +" /*\n" +" * Note that we throw away the flag that says if the value\n" +" * has just been created on the heap or not.\n" +" */\n" +" convertTo_%T(valobj,&val,iserrp,NULL);\n" +"\n" +" return val;\n" +" }\n" + ,&type + ,&type); + else + prcode(fp, +" if (valobj == Py_None || sipIsSubClassInstance(valobj,sipClass_%T))\n" +" return sipConvertToCpp(valobj,sipClass_%T,iserrp);\n" + ,&type + ,&type); + + if (cd != NULL) + prcode(fp, +"\n" +" sipBadClass(%N);\n" + , iff->name); + else + prcode(fp, +"\n" +" sipBadClass(\"%B\");\n" + , &mtd->type); + + prcode(fp, +"\n" +" *iserrp = 1;\n" +"\n" +" return NULL;\n" +"}\n" + ); +} + + +/* + * Generate a variable handler. + */ +static void generateVariableHandler(varDef *vd,FILE *fp) +{ + argType atype = vd->type.atype; + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static PyObject *var_%C(PyObject *, PyObject *);}\n" + , vd->fqcname); + + prcode(fp, +"static PyObject *var_%C(PyObject *%s,PyObject *sipPy)\n" +"{\n" + ,vd->fqcname,(isStaticVar(vd) ? "" : "sipSelf")); + + if (atype == class_type || atype == mapped_type) + prcode(fp, +" int sipIsErr = 0;\n" + ); + + if (vd->type.nrderefs == 0 && (atype == mapped_type || (atype == class_type && vd->type.u.cd->convtocode != NULL))) + prcode(fp, +" int sipValState;\n" + ); + + if (vd->getcode == NULL || vd->setcode == NULL) + { + prcode(fp, +" "); + + generateNamedValueType(&vd->type, "sipVal", fp); + + prcode(fp, ";\n" + ); + } + + if (!isStaticVar(vd)) + { + if (generating_c) + prcode(fp, +" %S *sipCpp = (%S *)sipGetCppPtr((sipWrapper *)sipSelf,sipClass_%C);\n" + ,classFQCName(vd->ecd),classFQCName(vd->ecd),classFQCName(vd->ecd)); + else + prcode(fp, +" %S *sipCpp = reinterpret_cast<%S *>(sipGetCppPtr((sipWrapper *)sipSelf,sipClass_%C));\n" + ,classFQCName(vd->ecd),classFQCName(vd->ecd),classFQCName(vd->ecd)); + + prcode(fp, +"\n" +" if (!sipCpp)\n" +" return NULL;\n" + ); + } + + prcode(fp, +"\n" +" if (sipPy == NULL)\n" +" {\n" + ); + + /* Generate the get handler part. */ + + if (vd->getcode != NULL) + { + generateCppCodeBlock(vd->getcode, fp); + + prcode(fp, +"\n" +" return sipPy;\n" + ); + } + else + { + int pyobj = FALSE; + + prcode(fp, +" sipVal = %s", (((atype == class_type || atype == mapped_type) && vd->type.nrderefs == 0) ? "&" : "")); + + generateVarMember(vd, fp); + + prcode(fp, ";\n" +"\n" + ); + + switch (atype) + { + case mapped_type: + prcode(fp, +" sipPy = sipConvertFromMappedType(sipVal,sipMappedType_%T,NULL);\n" + ,&vd->type); + + break; + + case class_type: + generateVarClassConversion(vd,fp); + break; + + case bool_type: + case cbool_type: + prcode(fp, +" sipPy = PyBool_FromLong(sipVal);\n" + ); + + break; + + case sstring_type: + case ustring_type: + case string_type: + if (vd->type.nrderefs == 0) + prcode(fp, +" sipPy = PyString_FromStringAndSize(%s&sipVal,1);\n" + ,(atype != string_type) ? "(char *)" : ""); + else + prcode(fp, +" sipPy = PyString_FromString(%ssipVal);\n" + ,(atype != string_type) ? "(char *)" : ""); + + break; + + case wstring_type: + if (vd->type.nrderefs == 0) + prcode(fp, +" sipPy = PyUnicode_FromWideChar(&sipVal,1);\n" + ); + else + prcode(fp, +" sipPy = PyUnicode_FromWideChar(sipVal,(SIP_SSIZE_T)wcslen(sipVal));\n" + ); + + break; + + case float_type: + case cfloat_type: + prcode(fp, +" sipPy = PyFloat_FromDouble((double)sipVal);\n" + ); + break; + + case double_type: + case cdouble_type: + prcode(fp, +" sipPy = PyFloat_FromDouble(sipVal);\n" + ); + break; + + case enum_type: + if (vd->type.u.ed->fqcname != NULL) + { + prcode(fp, +" sipPy = sipConvertFromNamedEnum(sipVal,sipEnum_%C);\n" + ,vd->type.u.ed->fqcname); + + break; + } + + /* Drop through. */ + + case short_type: + case cint_type: + case int_type: + prcode(fp, +" sipPy = PyInt_FromLong(sipVal);\n" + ); + break; + + case long_type: + prcode(fp, +" sipPy = PyLong_FromLong(sipVal);\n" + ); + break; + + case ushort_type: + case uint_type: + case ulong_type: + prcode(fp, +" sipPy = PyLong_FromUnsignedLong(sipVal);\n" + ); + break; + + case longlong_type: + prcode(fp, +" sipPy = PyLong_FromLongLong(sipVal);\n" + ); + break; + + case ulonglong_type: + prcode(fp, +" sipPy = PyLong_FromUnsignedLongLong(sipVal);\n" + ); + break; + + case struct_type: + prcode(fp, +" sipPy = sipConvertFromVoidPtr("); + + if (isConstArg(&vd->type)) + prcode(fp, "const_cast<%b *>(sipVal)", &vd->type); + else + prcode(fp, "sipVal"); + + prcode(fp, ");\n" + ); + + break; + + case void_type: + prcode(fp, +" sipPy = sipConvertFromVoidPtr("); + + if (isConstArg(&vd->type)) + prcode(fp, "const_cast<void *>(sipVal)"); + else + prcode(fp, "sipVal"); + + prcode(fp, ");\n" + ); + + break; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + prcode(fp, +" Py_XINCREF(sipVal);\n" + ); + pyobj = TRUE; + break; + } + + prcode(fp, +"\n" +" return %s;\n" + ,(pyobj ? "sipVal" : "sipPy")); + } + + prcode(fp, +" }\n" +"\n" + ); + + /* Generate the set handler part. */ + + if (vd->setcode != NULL) + { + prcode(fp, +" {\n" +" int sipErr = 0;\n" +"\n" + ); + + generateCppCodeBlock(vd->setcode, fp); + + prcode(fp, +"\n" +" if (sipErr)\n" +" return NULL;\n" +" }\n" + ); + } + else + { + char *deref; + int might_be_temp; + + might_be_temp = generateObjToCppConversion(&vd->type,fp); + + deref = ""; + + if (atype == class_type || atype == mapped_type) + { + if (vd->type.nrderefs == 0) + deref = "*"; + + prcode(fp, +"\n" +" if (sipIsErr)\n" +" return NULL;\n" +"\n" + ); + } + else + { + if ((atype == sstring_type || atype == ustring_type || atype == string_type || atype == wstring_type) && vd->type.nrderefs != 0) + { + prcode(fp, +"\n" +" if (sipVal == NULL)\n" + ); + } + else + prcode(fp, +"\n" +" if (PyErr_Occurred() != NULL)\n" + ); + + prcode(fp, +" {\n" +" sipBadSetType(%N,%N);\n" +" return NULL;\n" +" }\n" +"\n" + ,vd->ecd->iff->name,vd->pyname); + } + + if (atype == pyobject_type || atype == pytuple_type || + atype == pylist_type || atype == pydict_type || + atype == pycallable_type || atype == pyslice_type || + atype == pytype_type) + { + prcode(fp, +" Py_XDECREF("); + + generateVarMember(vd, fp); + + prcode(fp, ");\n" +" Py_INCREF(sipVal);\n" +"\n" + ); + } + + prcode(fp, +" "); + + generateVarMember(vd, fp); + + prcode(fp, " = %ssipVal;\n" + , deref); + + /* Note that wchar_t * leaks here. */ + + if (might_be_temp) + prcode(fp, +"\n" +" sipReleaseInstance(sipVal,sipClass_%C,sipValState);\n" + , classFQCName(vd->type.u.cd)); + else if (vd->type.atype == mapped_type && vd->type.nrderefs == 0) + prcode(fp, +"\n" +" sipReleaseMappedType(sipVal,sipMappedType_%T,sipValState);\n" + , &vd->type); + } + + prcode(fp, +"\n" +" Py_INCREF(Py_None);\n" +" return Py_None;\n" +"}\n" + ); +} + + +/* + * Generate the member variable of a class. + */ +static void generateVarMember(varDef *vd, FILE *fp) +{ + if (isStaticVar(vd)) + prcode(fp,"%S::",classFQCName(vd->ecd)); + else + prcode(fp,"sipCpp->"); + + prcode(fp, "%s", scopedNameTail(vd->fqcname)); +} + + +/* + * Generate an variable class conversion fragment. + */ +static void generateVarClassConversion(varDef *vd,FILE *fp) +{ + classDef *cd = vd->type.u.cd; + + prcode(fp, +" sipPy = sipConvertFromInstance("); + + if (isConstArg(&vd->type)) + prcode(fp,"const_cast<%b *>(sipVal)",&vd->type); + else + prcode(fp,"sipVal"); + + prcode(fp,",sipClass_%C,NULL);\n" + ,classFQCName(cd)); +} + + +/* + * Generate the declaration of a variable that is initialised from a Python + * object. Return TRUE if the value might be a temporary on the heap. + */ +static int generateObjToCppConversion(argDef *ad,FILE *fp) +{ + int might_be_temp = FALSE; + char *rhs = NULL; + + prcode(fp, +" sipVal = "); + + switch (ad->atype) + { + case mapped_type: + { + const char *tail; + + if (generating_c) + { + prcode(fp, "(%b *)", ad); + tail = ""; + } + else + { + prcode(fp, "reinterpret_cast<%b *>(", ad); + tail = ")"; + } + + /* + * Note that we don't support /Transfer/ but could do. + */ + + prcode(fp, "sipForceConvertToMappedType(sipPy,sipMappedType_%T,NULL,%s,%s,&sipIsErr)", ad, (ad->nrderefs ? "0" : "SIP_NOT_NONE"), (ad->nrderefs ? "NULL" : "&sipValState")); + + prcode(fp, "%s;\n" + , tail); + } + break; + + case class_type: + { + const char *tail; + + if (ad->nrderefs == 0 && ad->u.cd->convtocode != NULL) + might_be_temp = TRUE; + + if (generating_c) + { + prcode(fp, "(%b *)", ad); + tail = ""; + } + else + { + prcode(fp, "reinterpret_cast<%b *>(", ad); + tail = ")"; + } + + /* + * Note that we don't support /Transfer/ but could do. + * We could also support /Constrained/ (so long as we + * also supported it for all types). + */ + + prcode(fp, "sipForceConvertToInstance(sipPy,sipClass_%C,NULL,%s,%s,&sipIsErr)", classFQCName(ad->u.cd), (ad->nrderefs ? "0" : "SIP_NOT_NONE"), (might_be_temp ? "&sipValState" : "NULL")); + + prcode(fp, "%s;\n" + , tail); + } + break; + + case enum_type: + prcode(fp, "(%E)PyInt_AsLong(sipPy);\n" + , ad->u.ed); + break; + + case sstring_type: + if (ad->nrderefs == 0) + rhs = "(signed char)sipString_AsChar(sipPy)"; + else + rhs = "(signed char *)PyString_AsString(sipPy)"; + break; + + case ustring_type: + if (ad->nrderefs == 0) + rhs = "(unsigned char)sipString_AsChar(sipPy)"; + else + rhs = "(unsigned char *)PyString_AsString(sipPy)"; + break; + + case string_type: + if (ad->nrderefs == 0) + rhs = "sipString_AsChar(sipPy)"; + else + rhs = "PyString_AsString(sipPy)"; + break; + + case wstring_type: + if (ad->nrderefs == 0) + rhs = "sipUnicode_AsWChar(sipPy)"; + else + rhs = "sipUnicode_AsWString(sipPy)"; + break; + + case float_type: + case cfloat_type: + rhs = "(float)PyFloat_AsDouble(sipPy)"; + break; + + case double_type: + case cdouble_type: + rhs = "PyFloat_AsDouble(sipPy)"; + break; + + case bool_type: + case cbool_type: + rhs = "(bool)PyInt_AsLong(sipPy)"; + break; + + case ushort_type: + rhs = "(unsigned short)sipLong_AsUnsignedLong(sipPy)"; + break; + + case short_type: + rhs = "(short)PyInt_AsLong(sipPy)"; + break; + + case uint_type: + rhs = "(unsigned)sipLong_AsUnsignedLong(sipPy)"; + break; + + case int_type: + case cint_type: + rhs = "(int)PyInt_AsLong(sipPy)"; + break; + + case ulong_type: + rhs = "sipLong_AsUnsignedLong(sipPy)"; + break; + + case long_type: + rhs = "PyLong_AsLong(sipPy)"; + break; + + case ulonglong_type: + rhs = "PyLong_AsUnsignedLongLong(sipPy)"; + break; + + case longlong_type: + rhs = "PyLong_AsLongLong(sipPy)"; + break; + + case struct_type: + prcode(fp, "(struct %S *)sipConvertToVoidPtr(sipPy);\n" + , ad->u.sname); + break; + + case void_type: + rhs = "sipConvertToVoidPtr(sipPy)"; + break; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + rhs = "sipPy"; + break; + } + + if (rhs != NULL) + prcode(fp, "%s;\n" + , rhs); + + return might_be_temp; +} + + +/* + * Returns TRUE if the given method is a slot that takes zero arguments. + */ +static int isZeroArgSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == str_slot || st == int_slot || st == long_slot || + st == float_slot || st == invert_slot || st == neg_slot || + st == len_slot || st == nonzero_slot || st == pos_slot || + st == abs_slot || st == repr_slot || st == hash_slot); +} + + +/* + * Returns TRUE if the given method is a slot that takes more than one + * argument. + */ +static int isMultiArgSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == setitem_slot || st == call_slot); +} + + +/* + * Returns TRUE if the given method is a slot that returns void (ie. nothing + * other than an error indicator). + */ +int isVoidReturnSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == setitem_slot || st == delitem_slot); +} + + +/* + * Returns TRUE if the given method is a slot that returns int. + */ +int isIntReturnSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == len_slot || st == nonzero_slot || st == contains_slot || + st == cmp_slot); +} + + +/* + * Returns TRUE if the given method is a slot that returns long. + */ +int isLongReturnSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == hash_slot); +} + + +/* + * Returns TRUE if the given method is a slot that takes an int argument. + */ +static int isIntArgSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == repeat_slot || st == irepeat_slot); +} + + +/* + * Returns TRUE if the given method is an inplace number slot. + */ +static int isInplaceNumberSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == iadd_slot || st == isub_slot || st == imul_slot || + st == idiv_slot || st == imod_slot || + st == ior_slot || st == ixor_slot || st == iand_slot || + st == ilshift_slot || st == irshift_slot); +} + + +/* + * Returns TRUE if the given method is an inplace sequence slot. + */ +static int isInplaceSequenceSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == iconcat_slot || st == irepeat_slot); +} + + +/* + * Returns TRUE if the given method is a number slot slot. + */ +int isNumberSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == add_slot || st == sub_slot || st == mul_slot || + st == div_slot || st == mod_slot || + st == and_slot || st == or_slot || st == xor_slot || + st == lshift_slot || st == rshift_slot); +} + + +/* + * Returns TRUE if the given method is a rich compare slot. + */ +int isRichCompareSlot(memberDef *md) +{ + slotType st = md->slot; + + return (st == lt_slot || st == le_slot || st == eq_slot || + st == ne_slot || st == gt_slot || st == ge_slot); +} + + +/* + * Generate a Python slot handler for either a class, an enum or an extender. + */ +static void generateSlot(sipSpec *pt, classDef *cd, enumDef *ed, memberDef *md, FILE *fp) +{ + char *arg_str, *prefix, *ret_type; + int ret_int, nr_args; + overDef *od, *overs; + scopedNameDef *fqcname; + nameDef *pyname; + + if (ed != NULL) + { + prefix = "Enum"; + pyname = ed->pyname; + fqcname = ed->fqcname; + overs = ed->overs; + } + else if (cd != NULL) + { + prefix = "Class"; + pyname = cd->iff->name; + fqcname = classFQCName(cd); + overs = cd->overs; + } + else + { + prefix = NULL; + pyname = NULL; + fqcname = NULL; + overs = pt->overs; + } + + if (isVoidReturnSlot(md) || isIntReturnSlot(md)) + { + ret_int = TRUE; + ret_type = "int "; + } + else + { + ret_int = FALSE; + + if (isLongReturnSlot(md)) + ret_type = "long "; + else + ret_type = "PyObject *"; + } + + if (isIntArgSlot(md)) + { + nr_args = 0; + arg_str = "PyObject *sipSelf,int a0"; + } + else if (isMultiArgSlot(md)) + { + nr_args = 2; + arg_str = "PyObject *sipSelf,PyObject *sipArgs"; + } + else if (isZeroArgSlot(md)) + { + nr_args = 0; + arg_str = "PyObject *sipSelf"; + } + else if (isNumberSlot(md)) + { + nr_args = 2; + arg_str = "PyObject *sipArg0,PyObject *sipArg1"; + } + else + { + nr_args = 1; + arg_str = "PyObject *sipSelf,PyObject *sipArg"; + } + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + { + prcode(fp, +"extern \"C\" {static %sslot_", ret_type); + + if (fqcname != NULL) + prcode(fp, "%C_", fqcname); + + prcode(fp, "%s(%s);}\n" + , md->pyname->text, arg_str); + } + + prcode(fp, +"static %sslot_", ret_type); + + if (fqcname != NULL) + prcode(fp, "%C_", fqcname); + + prcode(fp, "%s(%s)\n" +"{\n" + , md->pyname->text, arg_str); + + if (isInplaceNumberSlot(md)) + prcode(fp, +" if (!PyObject_TypeCheck(sipSelf,(PyTypeObject *)sip%s_%C))\n" +" {\n" +" Py_INCREF(Py_NotImplemented);\n" +" return Py_NotImplemented;\n" +" }\n" +"\n" + , prefix, fqcname); + + if (!isNumberSlot(md)) + if (cd != NULL) + prcode(fp, +" %S *sipCpp = reinterpret_cast<%S *>(sipGetCppPtr((sipWrapper *)sipSelf,sipClass_%C));\n" +"\n" +" if (!sipCpp)\n" +" return %s;\n" +"\n" + , fqcname, fqcname, fqcname + , (md->slot == cmp_slot ? "-2" : (ret_int ? "-1" : "0"))); + else + prcode(fp, +" %S sipCpp = static_cast<%S>(PyInt_AsLong(sipSelf));\n" +"\n" + , fqcname, fqcname); + + if (nr_args > 0) + prcode(fp, +" int sipArgsParsed = 0;\n" + ); + + for (od = overs; od != NULL; od = od->next) + if (od->common == md && isAbstract(od)) + { + prcode(fp, +" bool sipSelfWasArg = !sipSelf;\n" + ); + + break; + } + + for (od = overs; od != NULL; od = od->next) + if (od->common == md) + generateFunctionBody(pt, od, cd, cd, (ed == NULL && !dontDerefSelf(od)), fp); + + if (nr_args > 0) + switch (md->slot) + { + case cmp_slot: + prcode(fp, +"\n" +" return 2;\n" + ); + break; + + case concat_slot: + case iconcat_slot: + case repeat_slot: + case irepeat_slot: + prcode(fp, +"\n" +" /* Raise an exception if the argument couldn't be parsed. */\n" +" sipBadOperatorArg(sipSelf,sipArg,%s);\n" +"\n" +" return NULL;\n" + ,slotName(md->slot)); + break; + + default: + if (isNumberSlot(md) || isRichCompareSlot(md)) + { + /* We can't extend enum slots. */ + if (cd == NULL) + prcode(fp, +"\n" +" Py_INCREF(Py_NotImplemented);\n" +" return Py_NotImplemented;\n" + ); + else if (isNumberSlot(md)) + prcode(fp, +"\n" +" return sipPySlotExtend(&sipModuleAPI_%s,%s,NULL,sipArg0,sipArg1);\n" + , pt->module->name, slotName(md->slot)); + else + prcode(fp, +"\n" +" return sipPySlotExtend(&sipModuleAPI_%s,%s,sip%s_%C,sipSelf,sipArg);\n" + , pt->module->name, slotName(md->slot), prefix, fqcname); + } + else if (isInplaceNumberSlot(md)) + prcode(fp, +"\n" +" PyErr_Clear();\n" +"\n" +" Py_INCREF(Py_NotImplemented);\n" +" return Py_NotImplemented;\n" + ); + else + prcode(fp, +"\n" +" /* Raise an exception if the arguments couldn't be parsed. */\n" +" sipNoMethod(sipArgsParsed,%N,%N);\n" +"\n" +" return %s;\n" + , pyname, md->pyname + ,ret_int ? "-1" : "0"); + } + + prcode(fp, +"}\n" + ); +} + + +/* + * Generate the member functions for a class. + */ +static void generateClassFunctions(sipSpec *pt,classDef *cd,FILE *fp) +{ + visibleList *vl; + memberDef *md; + + /* Any shadow code. */ + if (hasShadow(cd)) + generateShadowCode(pt,cd,fp); + + /* The member functions. */ + for (vl = cd->visible; vl != NULL; vl = vl->next) + if (vl->m->slot == no_slot) + generateFunction(pt, vl->m, vl->cd->overs, cd, vl->cd, fp); + + /* The slot functions. */ + for (md = cd->members; md != NULL; md = md->next) + if (cd->iff->type == namespace_iface) + generateOrdinaryFunction(pt,cd,md,fp); + else if (md->slot != no_slot && md->slot != unicode_slot) + generateSlot(pt, cd, NULL, md, fp); + + if (cd->iff->type != namespace_iface && !generating_c) + { + classList *cl; + int need_ptr, need_state; + + /* The cast function. */ + prcode(fp, +"\n" +"\n" +"/* Cast a pointer to a type somewhere in its superclass hierarchy. */\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void *cast_%C(void *, sipWrapperType *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static void *cast_%C(void *ptr,sipWrapperType *targetClass)\n" +"{\n" + ,classFQCName(cd)); + + if (cd->supers != NULL) + prcode(fp, +" void *res;\n" +"\n" + ); + + prcode(fp, +" if (targetClass == sipClass_%C)\n" +" return ptr;\n" + ,classFQCName(cd)); + + for (cl = cd->supers; cl != NULL; cl = cl->next) + { + scopedNameDef *sname = cl->cd->iff->fqcname; + + prcode(fp, +"\n" +" if ((res = sipCast_%C((%S *)(%S *)ptr,targetClass)) != NULL)\n" +" return res;\n" + ,sname,sname,classFQCName(cd)); + } + + prcode(fp, +"\n" +" return NULL;\n" +"}\n" + ); + + /* Generate the release function without compiler warnings. */ + need_ptr = need_state = FALSE; + + if (canCreate(cd) || isPublicDtor(cd)) + { + if (hasShadow(cd)) + need_ptr = need_state = TRUE; + else if (isPublicDtor(cd)) + need_ptr = TRUE; + } + + prcode(fp, +"\n" +"\n" +"/* Call the instance's destructor. */\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void release_%C(void *, int);}\n" + , classFQCName(cd)); + + prcode(fp, +"static void release_%C(void *%s,int%s)\n" +"{\n" + , classFQCName(cd), (need_ptr ? "ptr" : ""), (need_state ? " state" : "")); + + /* + * If there is an explicit public dtor then assume there is + * some way to call it which we haven't worked out (because we + * don't fully understand C++). + */ + if (canCreate(cd) || isPublicDtor(cd)) + { + int rgil = ((release_gil || isReleaseGILDtor(cd)) && !isHoldGILDtor(cd)); + + if (rgil) + prcode(fp, +" Py_BEGIN_ALLOW_THREADS\n" +"\n" + ); + + if (hasShadow(cd)) + { + prcode(fp, +" if (state & SIP_DERIVED_CLASS)\n" +" delete reinterpret_cast<sip%C *>(ptr);\n" + , classFQCName(cd)); + + if (isPublicDtor(cd)) + prcode(fp, +" else\n" +" delete reinterpret_cast<%U *>(ptr);\n" + , cd); + } + else if (isPublicDtor(cd)) + prcode(fp, +" delete reinterpret_cast<%U *>(ptr);\n" + , cd); + + if (rgil) + prcode(fp, +"\n" +" Py_END_ALLOW_THREADS\n" + ); + } + + prcode(fp, +"}\n" + ); + } + + /* The traverse function. */ + if (cd->travcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static int traverse_%C(void *, visitproc, void *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static int traverse_%C(void *sipCppV,visitproc sipVisit,void *sipArg)\n" +"{\n" +" ", classFQCName(cd)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" int sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->travcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + /* The clear function. */ + if (cd->clearcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static int clear_%C(void *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static int clear_%C(void *sipCppV)\n" +"{\n" +" ", classFQCName(cd)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" int sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->clearcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + /* The buffer interface functions. */ + if (cd->readbufcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static SIP_SSIZE_T getreadbuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n" + , classFQCName(cd)); + + prcode(fp, +"static SIP_SSIZE_T getreadbuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n" +"{\n" +" ", classFQCName(cd) + , argName("sipSelf", cd->readbufcode) + , argName("sipSegment", cd->readbufcode) + , argName("sipPtrPtr", cd->readbufcode)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" SIP_SSIZE_T sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->readbufcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + if (cd->writebufcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static SIP_SSIZE_T getwritebuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n" + , classFQCName(cd)); + + prcode(fp, +"static SIP_SSIZE_T getwritebuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n" +"{\n" +" ", classFQCName(cd) + , argName("sipSelf", cd->writebufcode) + , argName("sipSegment", cd->writebufcode) + , argName("sipPtrPtr", cd->writebufcode)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" SIP_SSIZE_T sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->writebufcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + if (cd->segcountcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static SIP_SSIZE_T getsegcount_%C(PyObject *, void *, SIP_SSIZE_T *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static SIP_SSIZE_T getsegcount_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T *%s)\n" +"{\n" +" ", classFQCName(cd) + , argName("sipSelf", cd->segcountcode) + , argName("sipLenPtr", cd->segcountcode)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" SIP_SSIZE_T sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->segcountcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + if (cd->charbufcode != NULL) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static SIP_SSIZE_T getcharbuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n" + , classFQCName(cd)); + + prcode(fp, +"static SIP_SSIZE_T getcharbuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n" +"{\n" +" ", classFQCName(cd) + , argName("sipSelf", cd->charbufcode) + , argName("sipSegment", cd->charbufcode) + , argName("sipPtrPtr", cd->charbufcode)); + + generateClassFromVoid(cd, "sipCpp", "sipCppV", fp); + + prcode(fp, ";\n" +" SIP_SSIZE_T sipRes;\n" +"\n" + ); + + generateCppCodeBlock(cd->charbufcode, fp); + + prcode(fp, +"\n" +" return sipRes;\n" +"}\n" + ); + } + + /* The dealloc function. */ + if (needDealloc(cd)) + { + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void dealloc_%C(sipWrapper *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static void dealloc_%C(sipWrapper *sipSelf)\n" +"{\n" + ,classFQCName(cd)); + + if (tracing) + prcode(fp, +" sipTrace(SIP_TRACE_DEALLOCS,\"dealloc_%C()\\n\");\n" +"\n" + ,classFQCName(cd)); + + /* Disable the virtual handlers. */ + if (hasShadow(cd)) + prcode(fp, +" if (sipIsDerived(sipSelf))\n" +" reinterpret_cast<sip%C *>(sipSelf->u.cppPtr)->sipPySelf = NULL;\n" +"\n" + ,classFQCName(cd)); + + if (generating_c || isPublicDtor(cd) || (hasShadow(cd) && isProtectedDtor(cd))) + { + prcode(fp, +" if (sipIsPyOwned(sipSelf))\n" +" {\n" + ); + + if (cd->dealloccode != NULL) + { + if (usedInCode(cd->dealloccode, "sipCpp")) + { + prcode(fp, +" "); + + generateClassFromVoid(cd, "sipCpp", "sipSelf->u.cppPtr", fp); + + prcode(fp, ";\n" + ); + } + + generateCppCodeBlock(cd->dealloccode,fp); + + prcode(fp, +"\n" + ); + } + + if (isDelayedDtor(cd)) + prcode(fp, +" sipAddDelayedDtor(sipSelf);\n" + ); + else if (generating_c) + prcode(fp, +" sipFree(sipSelf->u.cppPtr);\n" + ); + else + prcode(fp, +" release_%C(sipSelf->u.cppPtr,%s);\n" + , classFQCName(cd), (hasShadow(cd) ? "sipSelf->flags" : "0")); + + prcode(fp, +" }\n" + ); + } + + prcode(fp, +"}\n" + ); + } + + /* The type initialisation function. */ + if (canCreate(cd)) + generateTypeInit(pt, cd, fp); +} + + +/* + * Generate the shadow (derived) class code. + */ +static void generateShadowCode(sipSpec *pt,classDef *cd,FILE *fp) +{ + int nrVirts, virtNr; + virtOverDef *vod; + ctorDef *ct; + + nrVirts = countVirtuals(cd); + + /* Generate the wrapper class constructors. */ + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + char *prefix; + int a; + ctorDef *dct; + + if (isPrivateCtor(ct)) + continue; + + if (ct->cppsig == NULL) + continue; + + /* Check we haven't already handled this C++ signature. */ + for (dct = cd->ctors; dct != ct; dct = dct->next) + if (dct->cppsig != NULL && sameSignature(dct->cppsig, ct->cppsig, TRUE)) + break; + + if (dct != ct) + continue; + + prcode(fp, +"\n" +"sip%C::sip%C(",classFQCName(cd),classFQCName(cd)); + + generateArgs(ct->cppsig,Definition,fp); + + prcode(fp,")%X: %S(",ct->exceptions,classFQCName(cd)); + + prefix = ""; + + for (a = 0; a < ct->cppsig->nrArgs; ++a) + { + prcode(fp,"%sa%d",prefix,a); + prefix = ","; + } + + prcode(fp,"), sipPySelf(0)\n" +"{\n" + ); + + if (tracing) + { + prcode(fp, +" sipTrace(SIP_TRACE_CTORS,\"sip%C::sip%C(",classFQCName(cd),classFQCName(cd)); + generateArgs(ct->cppsig,Declaration,fp); + prcode(fp,")%X (this=0x%%08x)\\n\",this);\n" +"\n" + ,ct->exceptions); + } + + prcode(fp, +" sipCommonCtor(%s,%d);\n" +"}\n" + ,(nrVirts > 0 ? "sipPyMethods" : "NULL"),nrVirts); + } + + /* The destructor. */ + + if (!isPrivateDtor(cd)) + { + prcode(fp, +"\n" +"sip%C::~sip%C()%X\n" +"{\n" + ,classFQCName(cd),classFQCName(cd),cd->dtorexceptions); + + if (tracing) + prcode(fp, +" sipTrace(SIP_TRACE_DTORS,\"sip%C::~sip%C()%X (this=0x%%08x)\\n\",this);\n" +"\n" + ,classFQCName(cd),classFQCName(cd),cd->dtorexceptions); + + if (cd->dtorcode != NULL) + generateCppCodeBlock(cd->dtorcode,fp); + + prcode(fp, +" sipCommonDtor(sipPySelf);\n" +"}\n" + ); + } + + /* The metacall method if required. */ + if (isQObjectSubClass(cd) && optQ_OBJECT4(pt)) + { + prcode(fp, +"\n" +"const QMetaObject *sip%C::metaObject() const\n" +"{\n" +" return sip_%s_qt_metaobject(sipPySelf,sipClass_%C,%S::metaObject());\n" +"}\n" +"\n" +"int sip%C::qt_metacall(QMetaObject::Call _c,int _id,void **_a)\n" +"{\n" +" sip%C::metaObject();\n" +"\n" +" _id = %S::qt_metacall(_c,_id,_a);\n" +"\n" +" if (_id >= 0)\n" +" {\n" +" SIP_BLOCK_THREADS\n" +" _id = sip_%s_qt_metacall(sipPySelf,sipClass_%C,_c,_id,_a);\n" +" SIP_UNBLOCK_THREADS\n" +" }\n" +"\n" +" return _id;\n" +"}\n" + , classFQCName(cd) + , pt->module->name, classFQCName(cd), classFQCName(cd) + , classFQCName(cd) + , classFQCName(cd) + , classFQCName(cd) + , pt->module->name, classFQCName(cd)); + } + + /* Generate the virtual catchers. */ + + virtNr = 0; + + for (vod = cd->vmembers; vod != NULL; vod = vod->next) + { + overDef *od = &vod->o; + virtOverDef *dvod; + + if (isPrivate(od)) + continue; + + /* Check we haven't already handled this C++ signature. */ + for (dvod = cd->vmembers; dvod != vod; dvod = dvod->next) + if (strcmp(dvod->o.cppname,od->cppname) == 0 && sameSignature(dvod->o.cppsig,od->cppsig,TRUE)) + break; + + if (dvod != vod) + continue; + + generateVirtualCatcher(pt,cd,virtNr++,vod,fp); + } + + /* Generate the wrapper around each protected member function. */ + + generateProtectedDefinitions(cd,fp); + + /* Generate the emitters if needed. */ + if (!optNoEmitters(pt)) + generateEmitters(pt, cd, fp); +} + + +/* + * Generate the emitter functions. + */ +static void generateEmitters(sipSpec *pt, classDef *cd, FILE *fp) +{ + int noIntro; + visibleList *vl; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + for (od = vl->cd->overs; od != NULL; od = od->next) + if (od->common == vl->m && isSignal(od)) + { + generateEmitter(pt,cd,vl,fp); + break; + } + } + + /* Generate the table of signals to support fan-outs. */ + + noIntro = TRUE; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + for (od = vl->cd->overs; od != NULL; od = od->next) + if (od->common == vl->m && isSignal(od)) + { + if (noIntro) + { + setHasSigSlots(cd); + + prcode(fp, +"\n" +"static sipQtSignal signals_%C[] = {\n" + ,classFQCName(cd)); + + noIntro = FALSE; + } + + prcode(fp, +" {%N, %C_emit_%s},\n" + ,vl->m->pyname,classFQCName(cd),vl->m->pyname->text); + + break; + } + } + + if (!noIntro) + prcode(fp, +" {NULL, NULL}\n" +"};\n" + ); +} + + +/* + * Generate the protected enums for a class. + */ +static void generateProtectedEnums(sipSpec *pt,classDef *cd,FILE *fp) +{ + enumDef *ed; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + char *eol; + enumMemberDef *emd; + + /* Ignore unless this class is the publisher. */ + if (cd != ed->pcd) + continue; + + prcode(fp, +"\n" +" /* Expose this protected enum. */\n" +" enum"); + + if (ed->fqcname != NULL) + prcode(fp," sip%s",scopedNameTail(ed->fqcname)); + + prcode(fp," {"); + + eol = "\n"; + + for (emd = ed->members; emd != NULL; emd = emd->next) + { + prcode(fp,"%s" +" %s = %S::%s",eol,emd->cname,classFQCName(ed->ecd),emd->cname); + + eol = ",\n"; + } + + prcode(fp,"\n" +" };\n" + ); + } +} + + +/* + * Generate the catcher for a virtual function. + */ +static void generateVirtualCatcher(sipSpec *pt, classDef *cd, int virtNr, + virtOverDef *vod, FILE *fp) +{ + overDef *od = &vod->o; + virtHandlerDef *vhd = od->virthandler; + argDef *res, *ad; + int a; + + normaliseArgs(od->cppsig); + + res = &od->cppsig->result; + + if (res->atype == void_type && res->nrderefs == 0) + res = NULL; + + prcode(fp, +"\n"); + + generateBaseType(&od->cppsig->result,fp); + + prcode(fp," sip%C::%O(",classFQCName(cd),od); + generateArgs(od->cppsig,Definition,fp); + prcode(fp,")%s%X\n" +"{\n" + ,(isConst(od) ? " const" : ""),od->exceptions); + + if (tracing) + { + prcode(fp, +" sipTrace(SIP_TRACE_CATCHERS,\""); + + generateBaseType(&od->cppsig->result,fp); + prcode(fp," sip%C::%O(",classFQCName(cd),od); + generateArgs(od->cppsig,Declaration,fp); + prcode(fp,")%s%X (this=0x%%08x)\\n\",this);\n" +"\n" + ,(isConst(od) ? " const" : ""),od->exceptions); + } + + restoreArgs(od->cppsig); + + if (vhd->module == pt->module) + { + prcode(fp, +" extern "); + + generateBaseType(&od->cppsig->result,fp); + + prcode(fp," sipVH_%s_%d(sip_gilstate_t,PyObject *",vhd->module->name,vhd->virthandlernr); + } + else + { + prcode(fp, +" typedef "); + + generateBaseType(&od->cppsig->result,fp); + + prcode(fp," (*sipVH_%s_%d)(sip_gilstate_t,PyObject *",vhd->module->name,vhd->virthandlernr); + } + + if (vhd->cppsig->nrArgs > 0) + { + prcode(fp,","); + generateArgs(vhd->cppsig,Declaration,fp); + } + + prcode(fp,");\n" + ); + + if (isNewThread(od)) + prcode(fp, +"\n" +" SIP_BLOCK_THREADS\n" + ); + + prcode(fp, +"\n" +" sip_gilstate_t sipGILState;\n" +" PyObject *meth;\n" +"\n" +" meth = sipIsPyMethod(&sipGILState,"); + + if (isConst(od)) + prcode(fp,"const_cast<sipMethodCache *>("); + + prcode(fp,"&sipPyMethods[%d]",virtNr); + + if (isConst(od)) + prcode(fp,")"); + + prcode(fp,",sipPySelf,"); + + if (isAbstract(od)) + prcode(fp,"%N",cd->iff->name); + else + prcode(fp,"NULL"); + + prcode(fp,",%N);\n" +"\n" + ,od->common->pyname); + + if (isNewThread(od)) + prcode(fp, +" if (meth)\n" +" {\n" +" sipStartThread();\n" +" "); + else + { + prcode(fp, +" if (!meth)\n" + ); + + if (isAbstract(od)) + generateVirtHandlerErrorReturn(res,fp); + else + { + if (res == NULL) + prcode(fp, +" {\n" +" "); + else + prcode(fp, +" return "); + + generateUnambiguousClass(cd,vod->scope,fp); + + prcode(fp,"::%O(",od); + + for (a = 0; a < od->cppsig->nrArgs; ++a) + prcode(fp,"%sa%d",(a == 0 ? "" : ","),a); + + prcode(fp,");\n" + ); + + if (res == NULL) + prcode(fp, +" return;\n" +" }\n" + ); + } + + prcode(fp, +"\n" +" "); + + if (res != NULL) + prcode(fp,"return "); + } + + if (vhd->module == pt->module) + prcode(fp,"sipVH_%s_%d",vhd->module->name,vhd->virthandlernr); + else + prcode(fp,"((sipVH_%s_%d)(sipModuleAPI_%s_%s->em_virthandlers[%d]))",vhd->module->name,vhd->virthandlernr,pt->module->name,vhd->module->name,vhd->virthandlernr); + + prcode(fp,"(sipGILState,meth"); + + ad = od->cppsig->args; + + for (a = 0; a < od->cppsig->nrArgs; ++a) + { + if (ad->atype == class_type && isProtectedClass(ad->u.cd)) + prcode(fp,",static_cast<%U *>(a%d)",ad->u.cd,a); + else if (ad->atype == enum_type && isProtectedEnum(ad->u.ed)) + prcode(fp, ",(%E)a%d", ad->u.ed, a); + else + prcode(fp,",a%d",a); + + ++ad; + } + + prcode(fp,");\n" + ); + + if (isNewThread(od)) + prcode(fp, +" sipEndThread();\n" +" }\n" +"\n" +" SIP_UNBLOCK_THREADS\n" + ); + + prcode(fp, +"}\n" + ); +} + + +/* + * Generate the scope of the near class of a virtual taking duplicate + * super-classes into account. + */ +static void generateUnambiguousClass(classDef *cd,classDef *scope,FILE *fp) +{ + mroDef *mro; + + /* See if the near class has a duplicate. */ + for (mro = cd->mro; mro != NULL; mro = mro->next) + if (mro->cd == scope) + { + if (hasDuplicateSuper(mro)) + { + mroDef *guardc; + + /* + * Backtrack to find the class that directly + * sub-classes the duplicated one. This will + * be the one that disambiguates the duplicated + * one. + */ + guardc = mro; + + while (guardc != cd->mro) + { + mroDef *sub; + classList *cl; + + for (sub = cd->mro; sub->next != guardc; sub = sub->next) + ; + + for (cl = sub->cd->supers; cl != NULL; cl = cl->next) + if (cl->cd == mro->cd) + { + prcode(fp,"%S",classFQCName(sub->cd)); + + return; + } + + /* Try the previous one. */ + guardc = sub; + } + } + + break; + } + + /* If we got here there is nothing to worry about. */ + prcode(fp,"%S",classFQCName(scope)); +} + + +/* + * Generate a cast to zero. + */ +static void generateCastZero(argDef *ad,FILE *fp) +{ + if (ad->atype == enum_type) + prcode(fp,"(%E)",ad->u.ed); + + prcode(fp,"0"); +} + + +/* + * Generate the return statement for a virtual handler when there has been an + * error (ie. there is nothing sensible to return). + */ +static void generateVirtHandlerErrorReturn(argDef *res,FILE *fp) +{ + prcode(fp, +" return"); + + if (res == NULL) + { + prcode(fp,";\n" + ); + + return; + } + + prcode(fp," "); + + if (res->atype == mapped_type && res->nrderefs == 0) + { + argDef res_noconstref; + + /* + * We don't know anything about the mapped type so we just hope + * is has a default ctor. + */ + + if (isReference(res)) + prcode(fp,"*new "); + + res_noconstref = *res; + resetIsConstArg(&res_noconstref); + resetIsReference(&res_noconstref); + prcode(fp,"%B()",&res_noconstref); + } + else if (res->atype == class_type && res->nrderefs == 0) + { + ctorDef *ct = res->u.cd->defctor; + + /* + * If we don't have a suitable ctor then the generated code + * will issue an error message. + */ + if (ct != NULL && isPublicCtor(ct) && ct->cppsig != NULL) + { + argDef res_noconstref; + + /* + * If this is a badly designed class. We can only + * generate correct code by leaking memory. + */ + if (isReference(res)) + prcode(fp,"*new "); + + res_noconstref = *res; + resetIsConstArg(&res_noconstref); + resetIsReference(&res_noconstref); + prcode(fp,"%B",&res_noconstref); + + generateCallDefaultCtor(ct,fp); + } + else + { + fatalScopedName(classFQCName(res->u.cd)); + fatal(" must have a default constructor\n"); + } + } + else + generateCastZero(res,fp); + + prcode(fp,";\n" + ); +} + + +/* + * Generate the call to a default ctor. + */ +static void generateCallDefaultCtor(ctorDef *ct, FILE *fp) +{ + int a; + + prcode(fp, "("); + + for (a = 0; a < ct->cppsig->nrArgs; ++a) + { + argDef *ad = &ct->cppsig->args[a]; + + if (ad->defval != NULL) + break; + + if (a > 0) + prcode(fp, ","); + + /* + * Do what we can to provide type information to the compiler. + */ + if (ad->atype == class_type && ad->nrderefs > 0 && !isReference(ad)) + prcode(fp, "static_cast<%B>(0)", ad); + else if (ad->atype == enum_type) + prcode(fp, "static_cast<%E>(0)", ad->u.ed); + else if (ad->atype == float_type || ad->atype == cfloat_type) + prcode(fp, "0.0F"); + else if (ad->atype == double_type || ad->atype == cdouble_type) + prcode(fp, "0.0"); + else if (ad->atype == uint_type) + prcode(fp, "0U"); + else if (ad->atype == long_type || ad->atype == longlong_type) + prcode(fp, "0L"); + else if (ad->atype == ulong_type || ad->atype == ulonglong_type) + prcode(fp, "0UL"); + else if ((ad->atype == ustring_type || ad->atype == sstring_type || ad->atype == string_type) && ad->nrderefs == 0) + prcode(fp, "'\\0'"); + else if (ad->atype == wstring_type && ad->nrderefs == 0) + prcode(fp, "L'\\0'"); + else + prcode(fp, "0"); + } + + prcode(fp, ")"); +} + + +/* + * Generate the emitter function for a signal. + */ +static void generateEmitter(sipSpec *pt,classDef *cd,visibleList *vl,FILE *fp) +{ + char *pname = vl->m->pyname->text; + overDef *od; + + prcode(fp, +"\n" +"int sip%C::sipEmit_%s(PyObject *sipArgs)\n" +"{\n" +" int sipArgsParsed = 0;\n" + ,classFQCName(cd),pname); + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + int rgil = ((release_gil || isReleaseGIL(od)) && !isHoldGIL(od)); + + if (od->common != vl->m || !isSignal(od)) + continue; + + /* + * Generate the code that parses the args and emits the + * appropriate overloaded signal. + */ + prcode(fp, +"\n" +" {\n" + ); + + generateArgParser(pt, &od->pysig, cd, NULL, NULL, FALSE, fp); + + prcode(fp, +" {\n" + ); + + if (rgil) + prcode(fp, +" Py_BEGIN_ALLOW_THREADS\n" + ); + + prcode(fp, +" emit %s(" + ,od->cppname); + + generateArgs(od->cppsig,Call,fp); + + prcode(fp,");\n" + ); + + if (rgil) + prcode(fp, +" Py_END_ALLOW_THREADS\n" + ); + + deleteTemps(&od->pysig, fp); + + prcode(fp, +"\n" +" return 0;\n" +" }\n" +" }\n" + ); + } + + prcode(fp, +"\n" +" sipNoMethod(sipArgsParsed,%N,%N);\n" +"\n" +" return -1;\n" +"}\n" +"\n" + , cd->iff->name, vl->m->pyname); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static int %C_emit_%s(sipWrapper *, PyObject *);}\n" + , classFQCName(cd), pname); + + prcode(fp, +"static int %C_emit_%s(sipWrapper *w,PyObject *sipArgs)\n" +"{\n" +" sip%C *ptr = reinterpret_cast<sip%C *>(sipGetComplexCppPtr(w));\n" +"\n" +" return (ptr ? ptr->sipEmit_%s(sipArgs) : -1);\n" +"}\n" + ,classFQCName(cd),pname + ,classFQCName(cd),classFQCName(cd) + ,pname); +} + + +/* + * Generate the declarations of the protected wrapper functions for a class. + */ + +static void generateProtectedDeclarations(classDef *cd,FILE *fp) +{ + int noIntro; + visibleList *vl; + + noIntro = TRUE; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + if (vl->m->slot != no_slot) + continue; + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + if (od->common != vl->m || !isProtected(od)) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +" /*\n" +" * There is a public method for every protected method visible from\n" +" * this class.\n" +" */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" "); + + if (isStatic(od)) + prcode(fp,"static "); + + generateBaseType(&od->cppsig->result,fp); + + if (!isStatic(od) && !isAbstract(od) && (isVirtual(od) || isVirtualReimp(od))) + { + prcode(fp, " sipProtectVirt_%s(bool", od->cppname); + + if (od->cppsig->nrArgs > 0) + prcode(fp, ","); + } + else + prcode(fp, " sipProtect_%s(", od->cppname); + + generateArgs(od->cppsig,Declaration,fp); + prcode(fp,")%s;\n" + ,(isConst(od) ? " const" : "")); + } + } +} + + +/* + * Generate the definitions of the protected wrapper functions for a class. + */ +static void generateProtectedDefinitions(classDef *cd,FILE *fp) +{ + visibleList *vl; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + if (vl->m->slot != no_slot) + continue; + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + char *mname = od->cppname; + int parens; + argDef *res; + + if (od->common != vl->m || !isProtected(od)) + continue; + + prcode(fp, +"\n" + ); + + generateBaseType(&od->cppsig->result,fp); + + if (!isStatic(od) && !isAbstract(od) && (isVirtual(od) || isVirtualReimp(od))) + { + prcode(fp, " sip%C::sipProtectVirt_%s(bool sipSelfWasArg", classFQCName(cd), mname); + + if (od->cppsig->nrArgs > 0) + prcode(fp, ","); + } + else + prcode(fp, " sip%C::sipProtect_%s(", classFQCName(cd), mname); + + generateArgs(od->cppsig,Definition,fp); + prcode(fp,")%s\n" +"{\n" + ,(isConst(od) ? " const" : "")); + + parens = 1; + + res = &od->cppsig->result; + + if (res->atype == void_type && res->nrderefs == 0) + prcode(fp, +" "); + else + { + prcode(fp, +" return "); + + if (res->atype == class_type && isProtectedClass(res->u.cd)) + { + prcode(fp,"static_cast<%U *>(",res->u.cd); + ++parens; + } + else if (res->atype == enum_type && isProtectedEnum(res->u.ed)) + /* + * One or two older compilers can't + * handle a static_cast here so we + * revert to a C-style cast. + */ + prcode(fp,"(%E)",res->u.ed); + } + + if (!isAbstract(od)) + if (isVirtual(od) || isVirtualReimp(od)) + { + prcode(fp, "(sipSelfWasArg ? %S::%s(", classFQCName(vl->cd), mname); + + generateProtectedCallArgs(od, fp); + + prcode(fp, ") : "); + ++parens; + } + else + prcode(fp, "%S::", classFQCName(vl->cd)); + + prcode(fp,"%s(",mname); + + generateProtectedCallArgs(od, fp); + + while (parens--) + prcode(fp,")"); + + prcode(fp,";\n" +"}\n" + ); + } + } +} + + +/* + * Generate the arguments for a call to a protected method. + */ +static void generateProtectedCallArgs(overDef *od, FILE *fp) +{ + int a; + + for (a = 0; a < od->cppsig->nrArgs; ++a) + { + argDef *ad = &od->cppsig->args[a]; + + if (a > 0) + prcode(fp, ","); + + if (ad->atype == enum_type && isProtectedEnum(ad->u.ed)) + prcode(fp, "(%S)", ad->u.ed->fqcname); + + prcode(fp, "a%d", a); + } +} + + +/* + * Generate the function that does most of the work to handle a particular + * virtual function. + */ +static void generateVirtualHandler(sipSpec *pt,virtHandlerDef *vhd,FILE *fp) +{ + int a, nrvals, copy, isref; + argDef *res, res_noconstref; + + res = &vhd->cppsig->result; + + copy = isref = FALSE; + + if (res->atype == void_type && res->nrderefs == 0) + res = NULL; + else + { + /* + * If we are returning a reference to an instance then we take care to + * handle Python errors but still return a valid C++ instance. If we + * are returning an instance then we take care to make a local copy of + * the instance returned from Python before the Python object is + * garbage collected and the C++ instance (possibly) destroyed. + */ + if ((res->atype == class_type || res->atype == mapped_type) && res->nrderefs == 0) + if (isReference(res)) + isref = TRUE; + else + copy = TRUE; + + res_noconstref = *res; + resetIsConstArg(&res_noconstref); + resetIsReference(&res_noconstref); + } + + prcode(fp, +"\n" + ); + + generateBaseType(&vhd->cppsig->result, fp); + + prcode(fp," sipVH_%s_%d(sip_gilstate_t sipGILState,PyObject *sipMethod" + ,pt->module->name,vhd->virthandlernr); + + if (vhd->cppsig->nrArgs > 0) + { + prcode(fp,","); + + generateArgs(vhd->cppsig, Definition, fp); + } + + prcode(fp,")\n" +"{\n" + ); + + if (res != NULL) + { + prcode(fp, " "); + + /* + * wchar_t * return values are always on the heap. To reduce memory + * leaks we keep the last result around until we have a new one. This + * means that ownership of the return value stays with the function + * returning it - which is consistent with how other types work, even + * thought it may not be what's required in all cases. + */ + if (res->atype == wstring_type && res->nrderefs == 1) + prcode(fp, "static "); + + generateBaseType(&res_noconstref,fp); + + prcode(fp," %ssipRes",(isref ? "*" : "")); + + if (copy && res->atype == class_type && res->nrderefs == 0) + { + ctorDef *ct = res->u.cd->defctor; + + if (ct != NULL && isPublicCtor(ct) && ct->cppsig != NULL && ct->cppsig->nrArgs > 0 && ct->cppsig->args[0].defval == NULL) + generateCallDefaultCtor(ct,fp); + } + else if (!copy) + { + /* + * We initialise the result to try and suppress a + * compiler warning. + */ + prcode(fp," = "); + generateCastZero(res,fp); + } + + prcode(fp,";\n" + ); + + if (res->atype == wstring_type && res->nrderefs == 1) + prcode(fp, +"\n" +" if (sipRes)\n" +" {\n" +" // Return any previous result to the heap.\n" +" sipFree(%s);\n" +" sipRes = 0;\n" +" }\n" +"\n" + , (isConstArg(res) ? "const_cast<wchar_t *>(sipRes)" : "sipRes")); + } + + if (vhd->virtcode != NULL) + { + int error_flag = needErrorFlag(vhd->virtcode); + + if (error_flag) + prcode(fp, +" int sipIsErr = 0;\n" + ); + + prcode(fp, +"\n" + ); + + generateCppCodeBlock(vhd->virtcode,fp); + + if (error_flag) + prcode(fp, +"\n" +" if (sipIsErr)\n" +" PyErr_Print();\n" + ); + + prcode(fp, +"\n" +" Py_DECREF(sipMethod);\n" +"\n" +" SIP_RELEASE_GIL(sipGILState)\n" + ); + + if (res != NULL) + prcode(fp, +"\n" +" return sipRes;\n" + ); + + prcode(fp, +"}\n" + ); + + return; + } + + /* See how many values we expect. */ + nrvals = (res != NULL ? 1 : 0); + + for (a = 0; a < vhd->pysig->nrArgs; ++a) + if (isOutArg(&vhd->pysig->args[a])) + ++nrvals; + + if (copy) + { + prcode(fp, +" "); + + generateBaseType(&res_noconstref,fp); + + prcode(fp," *sipResOrig;\n"); + + if (res->atype == class_type && res->u.cd->convtocode != NULL) + prcode(fp, +" int sipResState;\n" + ); + } + + /* Call the method. */ + prcode(fp, +" PyObject *sipResObj = sipCallMethod(0,sipMethod,"); + + generateTupleBuilder(vhd->pysig, fp); + + prcode(fp,");\n" +"\n" +" %s (!sipResObj || sipParseResult(0,sipMethod,sipResObj,\"",(isref ? "int sipIsErr =" : "if")); + + /* Build the format string. */ + if (nrvals == 0) + prcode(fp,"Z"); + else + { + if (nrvals > 1) + prcode(fp,"("); + + if (res != NULL) + prcode(fp, "%s", getParseResultFormat(res, TRUE, isTransferVH(vhd))); + + for (a = 0; a < vhd->pysig->nrArgs; ++a) + { + argDef *ad = &vhd->pysig->args[a]; + + if (isOutArg(ad)) + prcode(fp, "%s", getParseResultFormat(ad, FALSE, FALSE)); + } + + if (nrvals > 1) + prcode(fp,")"); + } + + prcode(fp,"\""); + + /* Pass the destination pointers. */ + if (res != NULL) + { + generateParseResultExtraArgs(res, TRUE, fp); + prcode(fp,",&sipRes%s",(copy ? "Orig" : "")); + } + + for (a = 0; a < vhd->pysig->nrArgs; ++a) + { + argDef *ad = &vhd->pysig->args[a]; + + if (isOutArg(ad)) + { + generateParseResultExtraArgs(ad, FALSE, fp); + prcode(fp,",%sa%d",(isReference(ad) ? "&" : ""),a); + } + } + + if (isref) + prcode(fp,") < 0);\n" +"\n" +" if (sipIsErr)\n" + ); + else + prcode(fp,") < 0)\n" + ); + + prcode(fp, +" PyErr_Print();\n" + ); + + /* Make a copy if needed. */ + if (copy) + { + prcode(fp, +" else\n" +" {\n" +" sipRes = *sipResOrig;\n" + ); + + if (res->atype == mapped_type) + prcode(fp, +" delete sipResOrig;\n" + ); + else if (res->atype == class_type && res->u.cd->convtocode != NULL) + prcode(fp, +" sipReleaseInstance(sipResOrig,sipClass_%C,sipResState);\n" + , classFQCName(res->u.cd)); + + prcode(fp, +" }\n" + ); + } + + prcode(fp, +"\n" +" Py_XDECREF(sipResObj);\n" +" Py_DECREF(sipMethod);\n" +"\n" +" SIP_RELEASE_GIL(sipGILState)\n" + ); + + if (res != NULL) + { + if (isref) + { + prcode(fp, +"\n" +" if (sipIsErr)\n" + ); + + generateVirtHandlerErrorReturn(res,fp); + } + + prcode(fp, +"\n" +" return %ssipRes;\n" + ,(isref ? "*" : "")); + } + + prcode(fp, +"}\n" + ); +} + + +/* + * Generate the extra arguments needed by sipParseResult() for a particular + * type. + */ +static void generateParseResultExtraArgs(argDef *ad, int isres, FILE *fp) +{ + switch (ad->atype) + { + case mapped_type: + prcode(fp, ",sipMappedType_%T", ad); + break; + + case class_type: + prcode(fp, ",sipClass_%C", classFQCName(ad->u.cd)); + + if (isres && ad->nrderefs == 0 && ad->u.cd->convtocode != NULL && !isReference(ad)) + prcode(fp, ",&sipResState"); + + break; + + case pytuple_type: + prcode(fp,",&PyTuple_Type"); + break; + + case pylist_type: + prcode(fp,",&PyList_Type"); + break; + + case pydict_type: + prcode(fp,",&PyDict_Type"); + break; + + case pyslice_type: + prcode(fp,",&PySlice_Type"); + break; + + case pytype_type: + prcode(fp,",&PyType_Type"); + break; + + case enum_type: + if (ad->u.ed->fqcname != NULL) + prcode(fp,",sipEnum_%C",ad->u.ed->fqcname); + break; + } +} + + +/* + * Return the format characters used by sipParseResult() for a particular type. + */ +static const char *getParseResultFormat(argDef *ad, int isres, int xfervh) +{ + switch (ad->atype) + { + case mapped_type: + { + static const char *s[] = { + "D0", "D1", "D2", "D3", + "D4", "D5", "D6", "D7" + }; + + int f = 0x04; + + if (isres && ad->nrderefs == 0) + f |= 0x01; + + if (isres && xfervh) + f |= 0x02; + + return s[f]; + } + + case class_type: + { + static char s[] = "C?"; + + int f = 0x04; + + if (isres && ad->nrderefs == 0) + { + f |= 0x01; + + if (ad->u.cd->convtocode != NULL) + { + f &= ~0x04; + + /* + * If it is a reference then we are + * going to return the dereference. To + * make sure it remains valid we can + * either leak the temporary from the + * %ConvertToCode or we can suppress + * the %ConvertToCode. We choose the + * latter. + */ + if (isReference(ad)) + f |= 0x10; + } + } + + if (isres && xfervh) + f |= 0x02; + + s[1] = '0' + f; + + return s; + } + + case bool_type: + case cbool_type: + return "b"; + + case sstring_type: + case ustring_type: + case string_type: + return ((ad->nrderefs == 0) ? "c" : "s"); + + case wstring_type: + return ((ad->nrderefs == 0) ? "w" : "x"); + + case enum_type: + return ((ad->u.ed->fqcname != NULL) ? "E" : "e"); + + case ushort_type: + return "t"; + + case short_type: + return "h"; + + case int_type: + case cint_type: + return "i"; + + case uint_type: + return "u"; + + case long_type: + return "l"; + + case ulong_type: + return "m"; + + case longlong_type: + return "n"; + + case ulonglong_type: + return "o"; + + case void_type: + case struct_type: + return "V"; + + case float_type: + case cfloat_type: + return "f"; + + case double_type: + case cdouble_type: + return "d"; + + case pyobject_type: + return "O"; + + case pytuple_type: + case pylist_type: + case pydict_type: + case pyslice_type: + case pytype_type: + return (isAllowNone(ad) ? "N" : "T"); + } + + /* We should never get here. */ + return " "; +} + + +/* + * Generate the code to build a tuple of Python arguments. + */ +static void generateTupleBuilder(signatureDef *sd,FILE *fp) +{ + int a, arraylenarg; + + prcode(fp,"\""); + + for (a = 0; a < sd->nrArgs; ++a) + { + char *fmt = ""; + argDef *ad = &sd->args[a]; + + if (!isInArg(ad)) + continue; + + switch (ad->atype) + { + case sstring_type: + case ustring_type: + case string_type: + if (ad->nrderefs == 0 || (ad->nrderefs == 1 && isOutArg(ad))) + fmt = "c"; + else if (isArray(ad)) + fmt = "a"; + else + fmt = "s"; + + break; + + case wstring_type: + if (ad->nrderefs == 0 || (ad->nrderefs == 1 && isOutArg(ad))) + fmt = "w"; + else if (isArray(ad)) + fmt = "A"; + else + fmt = "x"; + + break; + + case bool_type: + case cbool_type: + fmt = "b"; + break; + + case enum_type: + fmt = (ad->u.ed->fqcname != NULL) ? "E" : "e"; + break; + + case cint_type: + fmt = "i"; + break; + + case uint_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "u"; + + break; + + case int_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "i"; + + break; + + case ushort_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "t"; + + break; + + case short_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "h"; + + break; + + case long_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "l"; + + break; + + case ulong_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "m"; + + break; + + case longlong_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "n"; + + break; + + case ulonglong_type: + if (isArraySize(ad)) + arraylenarg = a; + else + fmt = "o"; + + break; + + case struct_type: + case void_type: + fmt = "V"; + break; + + case float_type: + case cfloat_type: + fmt = "f"; + break; + + case double_type: + case cdouble_type: + fmt = "d"; + break; + + case signal_type: + case slot_type: + case slotcon_type: + case slotdis_type: + fmt = "s"; + break; + + case mapped_type: + fmt = "D"; + break; + + case class_type: + fmt = "C"; + break; + + case rxcon_type: + case rxdis_type: + case qobject_type: + fmt = "O"; + break; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + fmt = "S"; + break; + } + + prcode(fp,fmt); + } + + prcode(fp,"\""); + + for (a = 0; a < sd->nrArgs; ++a) + { + int derefs; + argDef *ad = &sd->args[a]; + + if (!isInArg(ad)) + continue; + + derefs = ad->nrderefs; + + switch (ad->atype) + { + case sstring_type: + case ustring_type: + case string_type: + case wstring_type: + if (!(ad->nrderefs == 0 || (ad->nrderefs == 1 && isOutArg(ad)))) + --derefs; + + break; + + case mapped_type: + case class_type: + if (ad->nrderefs > 0) + --derefs; + + break; + + case struct_type: + case void_type: + --derefs; + break; + } + + if (ad->atype == mapped_type || ad->atype == class_type || + ad->atype == rxcon_type || ad->atype == rxdis_type || + ad->atype == qobject_type) + { + prcode(fp,","); + + if (isConstArg(ad)) + prcode(fp,"const_cast<%b *>(",ad); + + if (ad->nrderefs == 0) + prcode(fp,"&"); + else + while (derefs-- != 0) + prcode(fp,"*"); + + prcode(fp,"a%d",a); + + if (isConstArg(ad)) + prcode(fp,")"); + + if (ad->atype == mapped_type) + prcode(fp, ",sipMappedType_%T,NULL", ad); + else if (ad->atype == class_type) + prcode(fp, ",sipClass_%C,NULL", classFQCName(ad->u.cd)); + else + prcode(fp,",sipClass_QObject"); + } + else + { + if (!isArraySize(ad)) + { + prcode(fp,","); + + while (derefs-- != 0) + prcode(fp,"*"); + + prcode(fp,"a%d",a); + } + + if (isArray(ad)) + { + argType astype = sd->args[arraylenarg].atype; + + prcode(fp,",%sa%d",(astype == int_type ? "" : "(int)"),arraylenarg); + } + else if (ad->atype == enum_type && ad->u.ed->fqcname != NULL) + prcode(fp,",sipEnum_%C",ad->u.ed->fqcname); + } + } +} + + +/* + * Generate the class interface #include directives required by either a class + * or a module. + */ +static void generateUsedIncludes(ifaceFileList *iffl, int header, FILE *fp) +{ + int newl = TRUE; + + while (iffl != NULL) + { + if (header == iffl->header) + { + if (newl) + { + prcode(fp, +"\n" + ); + + newl = FALSE; + } + + prcode(fp, +"#include \"sip%s%F.h\"\n" + , iffl->iff->module->name, iffl->iff->fqcname); + } + + iffl = iffl->next; + } + + if (!newl) + prcode(fp, +"\n" + ); +} + + +/* + * Generate the header file for the C++ interface. + */ +static void generateIfaceHeader(sipSpec *pt,ifaceFileDef *iff,char *codeDir) +{ + char *wfile; + char *cmname = iff->module->name; + classDef *cd; + mappedTypeDef *mtd; + exceptionDef *xd; + int genused; + FILE *fp; + + /* Create the header file. */ + + wfile = createIfaceFileName(codeDir,iff,".h"); + fp = createFile(pt,wfile,"Interface header file."); + + prcode(fp, +"\n" +"#ifndef _%s%F_h\n" +"#define _%s%F_h\n" +"\n" + ,cmname,iff->fqcname,cmname,iff->fqcname); + + genused = TRUE; + + for (cd = pt->classes; cd != NULL; cd = cd->next) + if (cd->iff == iff) + { + if (iff->module == pt->module) + generateClassHeader(cd,genused,pt,fp); + else if (!isExternal(cd)) + generateImportedClassHeader(cd,pt,fp); + + genused = FALSE; + } + + genused = TRUE; + + for (mtd = pt->mappedtypes; mtd != NULL; mtd = mtd->next) + if (mtd->iff == iff) + { + if (iff->module == pt->module) + generateMappedTypeHeader(mtd,genused,fp); + else + generateImportedMappedTypeHeader(mtd,pt,fp); + + genused = FALSE; + } + + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + if (xd->iff == iff) + { + generateCppCodeBlock(xd->hdrcode,fp); + + if (xd->exceptionnr >= 0) + { + prcode(fp, +"\n" +"#define sipException_%C sipModuleAPI_%s" + ,iff->fqcname,pt->module->name); + + if (iff->module == pt->module) + prcode(fp,"."); + else + prcode(fp,"_%s->",iff->module->name); + + prcode(fp,"em_exceptions[%d]\n" + ,xd->exceptionnr); + } + } + + prcode(fp, +"\n" +"#endif\n" + ); + + closeFile(fp); + free(wfile); +} + + +/* + * Generate the C++ header code for an imported mapped type. + */ +static void generateImportedMappedTypeHeader(mappedTypeDef *mtd,sipSpec *pt, + FILE *fp) +{ + char *mname = pt->module->name; + char *imname = mtd->iff->module->name; + argDef type; + + generateCppCodeBlock(mtd->hdrcode,fp); + + type.atype = mapped_type; + type.u.mtd = mtd; + type.argflags = 0; + type.name = NULL; + type.nrderefs = 0; + type.defval = NULL; + + prcode(fp, +"\n" +"#define sipMappedType_%T sipModuleAPI_%s_%s->em_mappedtypes[%d]\n" +"#define sipForceConvertTo_%T sipModuleAPI_%s_%s->em_mappedtypes[%d]->mt_fcto\n" +"#define sipConvertFrom_%T sipModuleAPI_%s_%s->em_mappedtypes[%d]->mt_cfrom\n" + ,&type,mname,imname,mtd->mappednr + ,&type,mname,imname,mtd->mappednr + ,&type,mname,imname,mtd->mappednr); +} + + +/* + * Generate the C++ header code for a generated mapped type. + */ +static void generateMappedTypeHeader(mappedTypeDef *mtd,int genused,FILE *fp) +{ + prcode(fp, +"\n" +"\n" + ); + + generateCppCodeBlock(mtd->hdrcode,fp); + + if (genused) + generateUsedIncludes(mtd->iff->used, TRUE, fp); + + prcode(fp, +"\n" +"#define sipMappedType_%T &sipMappedTypeDef_%T\n" +"#define sipForceConvertTo_%T sipMappedTypeDef_%T.mt_fcto\n" +"#define sipConvertFrom_%T sipMappedTypeDef_%T.mt_cfrom\n" +"\n" +"extern sipMappedType sipMappedTypeDef_%T;\n" + ,&mtd->type,&mtd->type + ,&mtd->type,&mtd->type + ,&mtd->type,&mtd->type + ,&mtd->type); +} + + +/* + * Generate the C++ header code for an imported class. + */ +static void generateImportedClassHeader(classDef *cd,sipSpec *pt,FILE *fp) +{ + char *mname = pt->module->name; + char *imname = cd->iff->module->name; + classDef *hcd; + + for (hcd = cd; hcd != NULL; hcd = hcd->ecd) + generateCppCodeBlock(hcd->hdrcode,fp); + + prcode(fp, +"\n" +"#define sipClass_%C sipModuleAPI_%s_%s->em_types[%d]\n" +"#define sipCast_%C sipModuleAPI_%s_%s->em_types[%d]->type->td_cast\n" +"#define sipForceConvertTo_%C sipModuleAPI_%s_%s->em_types[%d]->type->td_fcto\n" + ,classFQCName(cd),mname,imname,cd->classnr + ,classFQCName(cd),mname,imname,cd->classnr + ,classFQCName(cd),mname,imname,cd->classnr); + + generateEnumMacros(pt, cd, fp); +} + + +/* + * Generate the C++ header code for a generated class. + */ +static void generateClassHeader(classDef *cd,int genused,sipSpec *pt,FILE *fp) +{ + char *mname = pt->module->name; + classDef *hcd; + + for (hcd = cd; hcd != NULL; hcd = hcd->ecd) + generateCppCodeBlock(hcd->hdrcode,fp); + + if (genused) + generateUsedIncludes(cd->iff->used, TRUE, fp); + + if (cd->iff->type != namespace_iface) + { + prcode(fp, +"\n" +"#define sipClass_%C sipModuleAPI_%s.em_types[%d]\n" + ,classFQCName(cd),mname,cd->classnr); + + if (!isExternal(cd)) + prcode(fp, +"#define sipCast_%C sipType_%s_%C.td_cast\n" +"#define sipForceConvertTo_%C sipType_%s_%C.td_fcto\n" + , classFQCName(cd), mname, classFQCName(cd) + , classFQCName(cd), mname, classFQCName(cd)); + } + + generateEnumMacros(pt, cd, fp); + + if (!isExternal(cd)) + prcode(fp, +"\n" +"extern sipTypeDef sipType_%s_%C;\n" + , mname, classFQCName(cd)); + + if (hasShadow(cd)) + generateShadowClassDeclaration(pt,cd,fp); +} + + +/* + * Generate the sipEnum_* macros. + */ +static void generateEnumMacros(sipSpec *pt, classDef *cd, FILE *fp) +{ + enumDef *ed; + int noIntro = TRUE; + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + if (ed->fqcname == NULL || ed->ecd != cd) + continue; + + if (noIntro) + { + prcode(fp, +"\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +"#define sipEnum_%C sipModuleAPI_%s", ed->fqcname, pt->module->name); + + if (pt->module == ed->module) + prcode(fp, "."); + else + prcode(fp, "_%s->", ed->module->name); + + prcode(fp, "em_enums[%d]\n" + , ed->enumnr); + } +} + + +/* + * Generate the shadow class declaration. + */ +static void generateShadowClassDeclaration(sipSpec *pt,classDef *cd,FILE *fp) +{ + int noIntro, nrVirts; + ctorDef *ct; + virtOverDef *vod; + classDef *pcd; + + prcode(fp, +"\n" +"\n" +"class sip%C : public %S\n" +"{\n" +"public:\n" + ,classFQCName(cd),classFQCName(cd)); + + /* Define a shadow class for any protected classes we have. */ + + for (pcd = pt->classes; pcd != NULL; pcd = pcd->next) + { + if (pcd->ecd != cd || !isProtectedClass(pcd)) + continue; + + prcode(fp, +" class sip%s : public %s {};\n" + ,classBaseName(pcd),classBaseName(pcd)); + } + + /* The constructor declarations. */ + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + ctorDef *dct; + + if (isPrivateCtor(ct)) + continue; + + if (ct->cppsig == NULL) + continue; + + /* Check we haven't already handled this C++ signature. */ + for (dct = cd->ctors; dct != ct; dct = dct->next) + if (dct->cppsig != NULL && sameSignature(dct->cppsig, ct->cppsig, TRUE)) + break; + + if (dct != ct) + continue; + + prcode(fp, +" sip%C(",classFQCName(cd)); + + generateArgs(ct->cppsig,Declaration,fp); + + prcode(fp,")%X;\n" + ,ct->exceptions); + } + + /* The destructor. */ + + if (!isPrivateDtor(cd)) + prcode(fp, +" %s~sip%C()%X;\n" + ,(cd->vmembers != NULL ? "virtual " : ""),classFQCName(cd),cd->dtorexceptions); + + /* The metacall methods if required. */ + if (isQObjectSubClass(cd) && optQ_OBJECT4(pt)) + prcode(fp, +"\n" +" const QMetaObject *metaObject() const;\n" +" int qt_metacall(QMetaObject::Call,int,void **);\n" + ); + + /* The exposure of protected enums. */ + + generateProtectedEnums(pt,cd,fp); + + /* The wrapper around each protected member function. */ + + generateProtectedDeclarations(cd,fp); + + /* The public wrapper around each signal emitter. */ + if (!optNoEmitters(pt)) + { + visibleList *vl; + + noIntro = TRUE; + + for (vl = cd->visible; vl != NULL; vl = vl->next) + { + overDef *od; + + if (vl->m->slot != no_slot) + continue; + + for (od = vl->cd->overs; od != NULL; od = od->next) + { + if (od->common != vl->m || !isSignal(od)) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +" /*\n" +" * There is a public method for every Qt signal that can be emitted\n" +" * by this object. This function is called by Python to emit the\n" +" * signal.\n" +" */\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" int sipEmit_%s(PyObject *);\n" + ,vl->m->pyname->text); + + break; + } + } + } + + /* The catcher around each virtual function in the hierarchy. */ + noIntro = TRUE; + + for (vod = cd->vmembers; vod != NULL; vod = vod->next) + { + overDef *od = &vod->o; + virtOverDef *dvod; + + if (isPrivate(od)) + continue; + + /* Check we haven't already handled this C++ signature. */ + for (dvod = cd->vmembers; dvod != vod; dvod = dvod->next) + if (strcmp(dvod->o.cppname,od->cppname) == 0 && sameSignature(dvod->o.cppsig,od->cppsig,TRUE)) + break; + + if (dvod != vod) + continue; + + if (noIntro) + { + prcode(fp, +"\n" +" /*\n" +" * There is a protected method for every virtual method visible from\n" +" * this class.\n" +" */\n" +"protected:\n" + ); + + noIntro = FALSE; + } + + prcode(fp, +" "); + + prOverloadDecl(fp, od, FALSE); + prcode(fp, ";\n"); + } + + prcode(fp, +"\n" +"public:\n" +" sipWrapper *sipPySelf;\n" + ); + + /* The private declarations. */ + + prcode(fp, +"\n" +"private:\n" +" sip%C(const sip%C &);\n" +" sip%C &operator = (const sip%C &);\n" + ,classFQCName(cd),classFQCName(cd) + ,classFQCName(cd),classFQCName(cd)); + + if ((nrVirts = countVirtuals(cd)) > 0) + prcode(fp, +"\n" +" sipMethodCache sipPyMethods[%d];\n" + ,nrVirts); + + prcode(fp, +"};\n" + ); +} + + +/* + * Generate the C++ declaration for an overload. + */ +void prOverloadDecl(FILE *fp, overDef *od, int defval) +{ + int a; + + normaliseArgs(od->cppsig); + + generateBaseType(&od->cppsig->result, fp); + + prcode(fp, " %O(", od); + + for (a = 0; a < od->cppsig->nrArgs; ++a) + { + argDef *ad = &od->cppsig->args[a]; + + if (a > 0) + prcode(fp, ","); + + generateBaseType(ad, fp); + + if (defval && ad->defval != NULL) + { + prcode(fp, " = "); + generateExpression(ad->defval, fp); + } + } + + prcode(fp, ")%s%X", (isConst(od) ? " const" : ""), od->exceptions); + + restoreArgs(od->cppsig); +} + + +/* + * Generate typed arguments. + */ +static void generateArgs(signatureDef *sd,funcArgType ftype,FILE *fp) +{ + int a; + + for (a = 0; a < sd->nrArgs; ++a) + { + if (a > 0) + prcode(fp,","); + + generateSingleArg(&sd->args[a],a,ftype,fp); + } +} + + +/* + * Generate the declaration of a named variable to hold a result from a C++ + * function call. + */ +static void generateNamedValueType(argDef *ad,char *name,FILE *fp) +{ + argDef mod = *ad; + + if (ad->nrderefs == 0) + if (ad->atype == class_type || ad->atype == mapped_type) + mod.nrderefs = 1; + else + resetIsConstArg(&mod); + + resetIsReference(&mod); + generateNamedBaseType(&mod,name,fp); +} + + +/* + * Generate a single argument. + */ +static void generateSingleArg(argDef *ad,int argnr,funcArgType ftype,FILE *fp) +{ + char name[50]; + int genType, genName, derefPtr; + + /* Break the type down to individual modifications. */ + + genType = FALSE; + genName = FALSE; + derefPtr = FALSE; + + switch (ftype) + { + case Call: + genName = TRUE; + derefPtr = TRUE; + break; + + case Declaration: + genType = TRUE; + break; + + case Definition: + genType = TRUE; + genName = TRUE; + break; + } + + if (genName) + { + char *ind = ""; + + if (derefPtr) + switch (ad->atype) + { + case sstring_type: + case ustring_type: + case string_type: + case wstring_type: + if (ad->nrderefs > (isOutArg(ad) ? 0 : 1)) + ind = "&"; + + break; + + case mapped_type: + case class_type: + if (ad->nrderefs == 2) + ind = "&"; + else if (ad->nrderefs == 0) + ind = "*"; + + break; + + case struct_type: + case void_type: + if (ad->nrderefs == 2) + ind = "&"; + + break; + + default: + if (ad->nrderefs == 1) + ind = "&"; + } + + sprintf(name,"%sa%d",ind,argnr); + } + else + name[0] = '\0'; + + if (genType) + generateNamedBaseType(ad,name,fp); + else if (genName) + prcode(fp,name); +} + + +/* + * Generate a C++ type. + */ +static void generateBaseType(argDef *ad,FILE *fp) +{ + generateNamedBaseType(ad,"",fp); +} + + +/* + * Generate a C++ type and name. + */ +static void generateNamedBaseType(argDef *ad,char *name,FILE *fp) +{ + int nr_derefs = ad->nrderefs; + + /* + * A function type is handled differently because of the position of + * the name. + */ + if (ad->atype == function_type) + { + int i; + signatureDef *sig = ad->u.sa; + + generateBaseType(&sig->result,fp); + + prcode(fp," ("); + + for (i = 0; i < nr_derefs; ++i) + prcode(fp,"*"); + + prcode(fp,"%s)(",name); + generateArgs(sig,Declaration,fp); + prcode(fp,")"); + + return; + } + + if (isConstArg(ad)) + prcode(fp,"const "); + + switch (ad->atype) + { + case sstring_type: + prcode(fp,"signed char"); + break; + + case ustring_type: + prcode(fp,"unsigned char"); + break; + + case wstring_type: + prcode(fp,"wchar_t"); + break; + + case signal_type: + case slot_type: + case anyslot_type: + case slotcon_type: + case slotdis_type: + nr_derefs = 1; + + /* Drop through. */ + + case string_type: + prcode(fp,"char"); + break; + + case ushort_type: + prcode(fp,"unsigned short"); + break; + + case short_type: + prcode(fp,"short"); + break; + + case uint_type: + prcode(fp,"unsigned"); + break; + + case int_type: + case cint_type: + prcode(fp,"int"); + break; + + case ulong_type: + prcode(fp,"unsigned long"); + break; + + case long_type: + prcode(fp,"long"); + break; + + case ulonglong_type: + prcode(fp,"unsigned PY_LONG_LONG"); + break; + + case longlong_type: + prcode(fp,"PY_LONG_LONG"); + break; + + case struct_type: + prcode(fp,"struct %S",ad->u.sname); + break; + + case void_type: + prcode(fp,"void"); + break; + + case bool_type: + case cbool_type: + prcode(fp,"bool"); + break; + + case float_type: + case cfloat_type: + prcode(fp,"float"); + break; + + case double_type: + case cdouble_type: + prcode(fp,"double"); + break; + + case defined_type: + /* + * The only defined types still remaining are arguments to + * templates. + */ + + prcode(fp,"%S",ad->u.snd); + break; + + case rxcon_type: + case rxdis_type: + nr_derefs = 1; + prcode(fp,"QObject"); + break; + + case mapped_type: + generateBaseType(&ad->u.mtd->type,fp); + break; + + case class_type: + prcode(fp,"%U",ad->u.cd); + break; + + case template_type: + { + static const char tail[] = ">"; + int a; + templateDef *td = ad->u.td; + + prcode(fp, "%S%s", td->fqname, (prcode_xml ? "<" : "<")); + + for (a = 0; a < td->types.nrArgs; ++a) + { + if (a > 0) + prcode(fp,","); + + generateBaseType(&td->types.args[a],fp); + } + + if (prcode_last == tail) + prcode(fp, " "); + + prcode(fp, (prcode_xml ? ">" : tail)); + break; + } + + case enum_type: + prcode(fp,"%E",ad->u.ed); + break; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + case qobject_type: + case ellipsis_type: + prcode(fp, "PyObject *"); + break; + } + + if (nr_derefs > 0) + { + int i; + + prcode(fp," "); + + for (i = 0; i < nr_derefs; ++i) + prcode(fp,"*"); + } + + if (isReference(ad)) + prcode(fp, (prcode_xml ? "&" : "&")); + + if (*name != '\0') + { + if (nr_derefs == 0) + prcode(fp," "); + + prcode(fp,name); + } +} + + +/* + * Generate the definition of an argument variable and any supporting + * variables. + */ +static void generateVariable(argDef *ad,int argnr,FILE *fp) +{ + argType atype = ad->atype; + argDef orig; + + if (isInArg(ad) && ad->defval != NULL && + (atype == class_type || atype == mapped_type) && + (ad->nrderefs == 0 || isReference(ad))) + { + /* + * Generate something to hold the default value as it cannot be + * assigned straight away. + */ + prcode(fp, +" %B a%ddef = ",ad,argnr); + + generateExpression(ad->defval,fp); + + prcode(fp,";\n" + ); + } + + /* Adjust the type so we have the type that will really handle it. */ + + orig = *ad; + + switch (atype) + { + case sstring_type: + case ustring_type: + case string_type: + case wstring_type: + if (!isReference(ad)) + if (ad->nrderefs == 2) + ad->nrderefs = 1; + else if (ad->nrderefs == 1 && isOutArg(ad)) + ad->nrderefs = 0; + + break; + + case mapped_type: + case class_type: + case void_type: + case struct_type: + ad->nrderefs = 1; + break; + + default: + ad->nrderefs = 0; + } + + /* Array sizes are always integers. */ + if (isArraySize(ad)) + ad->atype = int_type; + + resetIsReference(ad); + + if (ad->nrderefs == 0) + resetIsConstArg(ad); + + prcode(fp, +" %B a%d",ad,argnr); + + if (atype == anyslot_type) + prcode(fp, "Name"); + + *ad = orig; + + generateDefaultValue(ad, argnr, fp); + + prcode(fp,";\n" + ); + + /* Some types have supporting variables. */ + if (isInArg(ad)) + switch (atype) + { + case class_type: + if (ad->u.cd->convtocode != NULL && !isConstrained(ad)) + prcode(fp, +" int a%dState = 0;\n" + ,argnr); + + if (isGetWrapper(ad)) + prcode(fp, +" PyObject *a%dWrapper;\n" + ,argnr); + + break; + + case mapped_type: + prcode(fp, +" int a%dState = 0;\n" + ,argnr); + break; + + case anyslot_type: + prcode(fp, +" PyObject *a%dCallable", argnr); + generateDefaultValue(ad, argnr, fp); + prcode(fp, ";\n" + ); + break; + } +} + + +/* + * Generate a default value. + */ +static void generateDefaultValue(argDef *ad, int argnr, FILE *fp) +{ + if (isInArg(ad) && ad->defval != NULL) + { + prcode(fp," = "); + + if ((ad->atype == class_type || ad->atype == mapped_type) && + (ad->nrderefs == 0 || isReference(ad))) + prcode(fp, "&a%ddef", argnr); + else + generateExpression(ad->defval,fp); + } +} + + +/* + * Generate a simple function call. + */ +static void generateSimpleFunctionCall(fcallDef *fcd,FILE *fp) +{ + int i; + + prcode(fp, "%B(", &fcd->type); + + for (i = 0; i < fcd->nrArgs; ++i) + { + if (i > 0) + prcode(fp,","); + + generateExpression(fcd->args[i],fp); + } + + prcode(fp,")"); +} + + +/* + * Generate the type structure that contains all the information needed by the + * metatype. A sub-set of this is used to extend namespaces. + */ +static void generateTypeDefinition(sipSpec *pt, classDef *cd, FILE *fp) +{ + char *mname = pt->module->name; + const char *sep; + int is_slots, nr_methods, nr_enums; + int is_inst_class, is_inst_voidp, is_inst_char, is_inst_string; + int is_inst_int, is_inst_long, is_inst_ulong, is_inst_longlong; + int is_inst_ulonglong, is_inst_double, is_inst_enum; + memberDef *md; + + if (cd->supers != NULL) + { + classList *cl; + + prcode(fp, +"\n" +"\n" +"/* Define this type's super-types. */\n" +"static sipEncodedClassDef supers_%C[] = {",classFQCName(cd)); + + for (cl = cd->supers; cl != NULL; cl = cl->next) + { + if (cl != cd->supers) + prcode(fp,", "); + + generateEncodedClass(pt,cl->cd,(cl->next == NULL),fp); + } + + prcode(fp,"};\n" + ); + } + + /* Generate the slots table. */ + is_slots = FALSE; + + for (md = cd->members; md != NULL; md = md->next) + { + const char *stype; + + if (md->slot == no_slot) + continue; + + if (!is_slots) + { + prcode(fp, +"\n" +"\n" +"/* Define this type's Python slots. */\n" +"static sipPySlotDef slots_%C[] = {\n" + ,classFQCName(cd)); + + is_slots = TRUE; + } + + if ((stype = slotName(md->slot)) != NULL) + prcode(fp, +" {(void *)slot_%C_%s, %s},\n" + ,classFQCName(cd),md->pyname->text,stype); + } + + if (is_slots) + prcode(fp, +" {0, (sipPySlotType)0}\n" +"};\n" + ); + + /* Generate the attributes tables. */ + nr_methods = generateMethodTable(cd,fp); + nr_enums = generateEnumMemberTable(pt,cd,fp); + + /* Generate each instance table. */ + is_inst_class = generateClasses(pt,cd,fp); + is_inst_voidp = generateVoidPointers(pt,cd,fp); + is_inst_char = generateChars(pt,cd,fp); + is_inst_string = generateStrings(pt,cd,fp); + is_inst_int = generateInts(pt,cd,fp); + is_inst_long = generateLongs(pt,cd,fp); + is_inst_ulong = generateUnsignedLongs(pt,cd,fp); + is_inst_longlong = generateLongLongs(pt,cd,fp); + is_inst_ulonglong = generateUnsignedLongLongs(pt,cd,fp); + is_inst_double = generateDoubles(pt,cd,fp); + is_inst_enum = generateEnums(pt,cd,fp); + + prcode(fp, +"\n" +"\n" +"sipTypeDef sipType_%s_%C = {\n" +" 0,\n" +" ", mname, classFQCName(cd)); + + sep = ""; + + if (cd->userflags) + { + prcode(fp, "%s%x", sep, ((cd->userflags << TYPE_FLAGS_SHIFT) & TYPE_FLAGS_MASK)); + sep = "|"; + } + + if (isAbstractClass(cd)) + { + prcode(fp, "%sSIP_TYPE_ABSTRACT", sep); + sep = "|"; + } + + if (cd->subbase != NULL) + { + prcode(fp, "%sSIP_TYPE_SCC", sep); + sep = "|"; + } + + if (*sep == '\0') + prcode(fp, "0"); + + prcode(fp, ",\n"); + + if (cd->real != NULL) + prcode(fp, +" 0,\n" + ); + else if (cd->ecd != NULL && cd->ecd->real != NULL) + prcode(fp, +" \"%s.%P\",\n" + , cd->ecd->real->iff->module->name, cd->ecd, cd->pyname); + else + prcode(fp, +" \"%s.%P\",\n" + , mname, cd->ecd, cd->pyname); + + if (isRenamedClass(cd)) + prcode(fp, +" \"%S\",\n" + , classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + prcode(fp, +" "); + + if (cd->real != NULL) + generateEncodedClass(pt, cd->real, 0, fp); + else if (cd->ecd != NULL) + generateEncodedClass(pt, cd->ecd, 0, fp); + else + prcode(fp, "{0, 0, 1}"); + + prcode(fp, ",\n" + ); + + if (cd->supers != NULL) + prcode(fp, +" supers_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (is_slots) + prcode(fp, +" slots_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (nr_methods == 0) + prcode(fp, +" 0, 0,\n" + ); + else + prcode(fp, +" %d, methods_%C,\n" + ,nr_methods,classFQCName(cd)); + + if (nr_enums == 0) + prcode(fp, +" 0, 0,\n" + ); + else + prcode(fp, +" %d, enummembers_%C,\n" + ,nr_enums,classFQCName(cd)); + + if (hasVarHandlers(cd)) + prcode(fp, +" variables_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (canCreate(cd)) + prcode(fp, +" init_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->travcode != NULL) + prcode(fp, +" traverse_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->clearcode != NULL) + prcode(fp, +" clear_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->readbufcode != NULL) + prcode(fp, +" getreadbuffer_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->writebufcode != NULL) + prcode(fp, +" getwritebuffer_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->segcountcode != NULL) + prcode(fp, +" getsegcount_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->charbufcode != NULL) + prcode(fp, +" getcharbuffer_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (needDealloc(cd)) + prcode(fp, +" dealloc_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + if (cd->iff->type == namespace_iface || generating_c) + prcode(fp, +" 0,\n" +" 0,\n" + ); + else + prcode(fp, +" cast_%C,\n" +" release_%C,\n" + , classFQCName(cd) + , classFQCName(cd)); + + if (cd->iff->type == namespace_iface) + prcode(fp, +" 0,\n" +" 0,\n" + ); + else + { + prcode(fp, +" forceConvertTo_%C,\n" + ,classFQCName(cd)); + + if (cd->convtocode != NULL) + prcode(fp, +" convertTo_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + } + + if (!optNoEmitters(pt) && hasSigSlots(cd)) + prcode(fp, +" signals_%C,\n" + ,classFQCName(cd)); + else + prcode(fp, +" 0,\n" + ); + + prcode(fp, +" {"); + + if (is_inst_class) + prcode(fp,"classInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_voidp) + prcode(fp,"voidPtrInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_char) + prcode(fp,"charInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_string) + prcode(fp,"stringInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_int) + prcode(fp,"intInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_long) + prcode(fp,"longInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_ulong) + prcode(fp,"unsignedLongInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_longlong) + prcode(fp,"longLongInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_ulonglong) + prcode(fp,"unsignedLongLongInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_double) + prcode(fp,"doubleInstances_%C, ",classFQCName(cd)); + else + prcode(fp,"0, "); + + if (is_inst_enum) + prcode(fp,"enumInstances_%C",classFQCName(cd)); + else + prcode(fp,"0"); + + prcode(fp,"},\n" +" 0\n" +"};\n" + ); +} + + +/* + * Return the sip module's string equivalent of a slot. + */ +static const char *slotName(slotType st) +{ + const char *sn; + + switch (st) + { + case str_slot: + sn = "str_slot"; + break; + + case int_slot: + sn = "int_slot"; + break; + + case long_slot: + sn = "long_slot"; + break; + + case float_slot: + sn = "float_slot"; + break; + + case len_slot: + sn = "len_slot"; + break; + + case contains_slot: + sn = "contains_slot"; + break; + + case add_slot: + sn = "add_slot"; + break; + + case concat_slot: + sn = "concat_slot"; + break; + + case sub_slot: + sn = "sub_slot"; + break; + + case mul_slot: + sn = "mul_slot"; + break; + + case repeat_slot: + sn = "repeat_slot"; + break; + + case div_slot: + sn = "div_slot"; + break; + + case mod_slot: + sn = "mod_slot"; + break; + + case and_slot: + sn = "and_slot"; + break; + + case or_slot: + sn = "or_slot"; + break; + + case xor_slot: + sn = "xor_slot"; + break; + + case lshift_slot: + sn = "lshift_slot"; + break; + + case rshift_slot: + sn = "rshift_slot"; + break; + + case iadd_slot: + sn = "iadd_slot"; + break; + + case iconcat_slot: + sn = "iconcat_slot"; + break; + + case isub_slot: + sn = "isub_slot"; + break; + + case imul_slot: + sn = "imul_slot"; + break; + + case irepeat_slot: + sn = "irepeat_slot"; + break; + + case idiv_slot: + sn = "idiv_slot"; + break; + + case imod_slot: + sn = "imod_slot"; + break; + + case iand_slot: + sn = "iand_slot"; + break; + + case ior_slot: + sn = "ior_slot"; + break; + + case ixor_slot: + sn = "ixor_slot"; + break; + + case ilshift_slot: + sn = "ilshift_slot"; + break; + + case irshift_slot: + sn = "irshift_slot"; + break; + + case invert_slot: + sn = "invert_slot"; + break; + + case call_slot: + sn = "call_slot"; + break; + + case getitem_slot: + sn = "getitem_slot"; + break; + + case setitem_slot: + sn = "setitem_slot"; + break; + + case delitem_slot: + sn = "delitem_slot"; + break; + + case lt_slot: + sn = "lt_slot"; + break; + + case le_slot: + sn = "le_slot"; + break; + + case eq_slot: + sn = "eq_slot"; + break; + + case ne_slot: + sn = "ne_slot"; + break; + + case gt_slot: + sn = "gt_slot"; + break; + + case ge_slot: + sn = "ge_slot"; + break; + + case cmp_slot: + sn = "cmp_slot"; + break; + + case nonzero_slot: + sn = "nonzero_slot"; + break; + + case neg_slot: + sn = "neg_slot"; + break; + + case pos_slot: + sn = "pos_slot"; + break; + + case abs_slot: + sn = "abs_slot"; + break; + + case repr_slot: + sn = "repr_slot"; + break; + + case hash_slot: + sn = "hash_slot"; + break; + + default: + sn = NULL; + } + + return sn; +} + + +/* + * Generate the code to register a class as a Qt metatype. + */ +static void generateRegisterMetaType(classDef *cd, FILE *fp) +{ + int pub_def_ctor, pub_copy_ctor; + ctorDef *ct; + + /* + * We register types with Qt if the class is not abstract, has a public + * default ctor, a public copy ctor, a public dtor and isn't one of the + * internally supported types. + */ + if (isAbstractClass(cd)) + return; + + if (!isPublicDtor(cd)) + return; + + if (classFQCName(cd)->next == NULL) + { + if (strcmp(classBaseName(cd), "QChar") == 0) + return; + + if (strcmp(classBaseName(cd), "QString") == 0) + return; + + if (strcmp(classBaseName(cd), "QByteArray") == 0) + return; + } + + pub_def_ctor = pub_copy_ctor = FALSE; + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + if (ct->cppsig == NULL || !isPublicCtor(ct)) + continue; + + if (ct->cppsig->nrArgs == 0) + pub_def_ctor = TRUE; + else if (ct->cppsig->nrArgs == 1) + { + argDef *ad = &ct->cppsig->args[0]; + + if (ad->atype == class_type && ad->u.cd == cd && isReference(ad) && + isConstArg(ad) && ad->nrderefs == 0 && ad->defval == NULL) + pub_copy_ctor = TRUE; + } + } + + if (pub_def_ctor && pub_copy_ctor) + prcode(fp, +" qRegisterMetaType<%S>(\"%S\");\n" + , classFQCName(cd), classFQCName(cd)); +} + + +/* + * Generate the initialisation function or cast operators for the type. + */ +static void generateTypeInit(sipSpec *pt, classDef *cd, FILE *fp) +{ + ctorDef *ct; + int need_self, need_owner; + + /* + * See if we need to name the self and owner arguments so that we can + * avoid a compiler warning about an unused argument. + */ + need_self = (generating_c || hasShadow(cd)); + need_owner = generating_c; + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + int a; + + if (usedInCode(ct->methodcode, "sipSelf")) + need_self = TRUE; + + for (a = 0; a < ct->pysig.nrArgs; ++a) + if (isThisTransferred(&ct->pysig.args[a])) + { + need_owner = TRUE; + break; + } + } + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static void *init_%C(sipWrapper *, PyObject *, sipWrapper **, int *);}\n" + , classFQCName(cd)); + + prcode(fp, +"static void *init_%C(sipWrapper *%s,PyObject *sipArgs,sipWrapper **%s,int *sipArgsParsed)\n" +"{\n" + ,classFQCName(cd),(need_self ? "sipSelf" : ""),(need_owner ? "sipOwner" : "")); + + if (hasShadow(cd)) + prcode(fp, +" sip%C *sipCpp = 0;\n" + ,classFQCName(cd)); + else + prcode(fp, +" %U *sipCpp = 0;\n" + ,cd); + + if (tracing) + prcode(fp, +"\n" +" sipTrace(SIP_TRACE_INITS,\"init_%C()\\n\");\n" + ,classFQCName(cd)); + + /* + * Generate the code that parses the Python arguments and calls the + * correct constructor. + */ + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + int needSecCall, error_flag = FALSE; + + if (isPrivateCtor(ct)) + continue; + + prcode(fp, +"\n" +" if (!sipCpp)\n" +" {\n" + ); + + if (ct->methodcode != NULL && needErrorFlag(ct->methodcode)) + { + prcode(fp, +" int sipIsErr = 0;\n" + ); + + error_flag = TRUE; + } + + needSecCall = generateArgParser(pt, &ct->pysig, cd, ct, NULL, FALSE, fp); + generateConstructorCall(cd,ct,error_flag,fp); + + if (needSecCall) + { + prcode(fp, +" }\n" +"\n" +" if (!sipCpp)\n" +" {\n" + ); + + if (error_flag) + prcode(fp, +" int sipIsErr = 0;\n" + ); + + generateArgParser(pt, &ct->pysig, cd, ct, NULL, TRUE, fp); + generateConstructorCall(cd,ct,error_flag,fp); + } + + prcode(fp, +" }\n" + ); + } + + if (hasShadow(cd)) + prcode(fp, +"\n" +" if (sipCpp)\n" +" sipCpp->sipPySelf = sipSelf;\n" + ); + + prcode(fp, +"\n" +" return sipCpp;\n" +"}\n" + ); +} + + +/* + * Count the number of virtual members in a class. + */ +static int countVirtuals(classDef *cd) +{ + int nrvirts; + virtOverDef *vod; + + nrvirts = 0; + + for (vod = cd->vmembers; vod != NULL; vod = vod->next) + if (!isPrivate(&vod->o)) + ++nrvirts; + + return nrvirts; +} + + +/* + * Generate the try block for a call. + */ +static void generateTry(throwArgs *ta,FILE *fp) +{ + /* + * Generate the block if there was no throw specifier, or a non-empty + * throw specifier. + */ + if (exceptions && (ta == NULL || ta->nrArgs > 0)) + prcode(fp, +" try\n" +" {\n" + ); +} + + +/* + * Generate the catch block for a call. + */ +static void generateCatch(throwArgs *ta, signatureDef *sd, FILE *fp) +{ + /* + * Generate the block if there was no throw specifier, or a non-empty + * throw specifier. + */ + if (exceptions && (ta == NULL || ta->nrArgs > 0)) + { + prcode(fp, +" }\n" + ); + + if (ta == NULL) + { + prcode(fp, +" catch (...)\n" +" {\n" + ); + + deleteTemps(sd, fp); + + if (release_gil) + prcode(fp, +" Py_BLOCK_THREADS\n" +"\n" + ); + + prcode(fp, +" sipRaiseUnknownException();\n" +" return NULL;\n" +" }\n" + ); + } + else + { + int a; + + for (a = 0; a < ta->nrArgs; ++a) + { + exceptionDef *xd = ta->args[a]; + scopedNameDef *ename = xd->iff->fqcname; + + prcode(fp, +" catch (%S &%s)\n" +" {\n" + ,ename,(xd->cd != NULL || usedInCode(xd->raisecode, "sipExceptionRef")) ? "sipExceptionRef" : ""); + + deleteTemps(sd, fp); + + if (xd->cd != NULL) + { + /* The exception is a wrapped class. */ + + prcode(fp, +" /* Hope that there is a valid copy ctor. */\n" +" %S *sipExceptionCopy = new %S(sipExceptionRef);\n" +"\n" + ,ename,ename); + + if (release_gil) + prcode(fp, +" Py_BLOCK_THREADS\n" +"\n" + ); + + prcode(fp, +" sipRaise%sClassException(sipClass_%C,sipExceptionCopy);\n" + ,(xd->cd->subbase != NULL ? "Sub" : ""),ename); + } + else + generateCppCodeBlock(xd->raisecode,fp); + + prcode(fp, +"\n" +" return NULL;\n" +" }\n" + ); + } + } + } +} + + +/* + * Generate a throw specifier. + */ +static void generateThrowSpecifier(throwArgs *ta,FILE *fp) +{ + if (exceptions && ta != NULL) + { + int a; + + prcode(fp," throw("); + + for (a = 0; a < ta->nrArgs; ++a) + { + if (a > 0) + prcode(fp,","); + + prcode(fp,"%S",ta->args[a]->iff->fqcname); + } + + prcode(fp,")"); + } +} + + +/* + * Generate a single constructor call. + */ +static void generateConstructorCall(classDef *cd,ctorDef *ct,int error_flag, + FILE *fp) +{ + prcode(fp, +" {\n" + ); + + /* Call any pre-hook. */ + if (ct->prehook != NULL) + prcode(fp, +" sipCallHook(\"%s\");\n" +"\n" + ,ct->prehook); + + if (ct->methodcode != NULL) + generateCppCodeBlock(ct->methodcode,fp); + else if (generating_c) + prcode(fp, +" sipCpp = sipMalloc(sizeof (%S));\n" + ,classFQCName(cd)); + else + { + int rgil = ((release_gil || isReleaseGILCtor(ct)) && !isHoldGILCtor(ct)); + + if (rgil) + prcode(fp, +" Py_BEGIN_ALLOW_THREADS\n" + ); + + generateTry(ct->exceptions,fp); + + if (hasShadow(cd)) + prcode(fp, +" sipCpp = new sip%C(",classFQCName(cd)); + else + prcode(fp, +" sipCpp = new %U(",cd); + + if (isCastCtor(ct)) + { + classDef *ocd; + + /* We have to fiddle the type to generate the correct code. */ + ocd = ct->pysig.args[0].u.cd; + ct->pysig.args[0].u.cd = cd; + prcode(fp, "a0->operator %B()", &ct->pysig.args[0]); + ct->pysig.args[0].u.cd = ocd; + } + else + generateArgs(ct->cppsig, Call, fp); + + prcode(fp,");\n" + ); + + generateCatch(ct->exceptions, &ct->pysig, fp); + + if (rgil) + prcode(fp, +" Py_END_ALLOW_THREADS\n" + ); + } + + gc_ellipsis(&ct->pysig, fp); + + deleteTemps(&ct->pysig, fp); + + if (error_flag) + prcode(fp, +"\n" +" if (sipIsErr)\n" +" return 0;\n" + ); + + /* Call any post-hook. */ + if (ct->posthook != NULL) + prcode(fp, +"\n" +" sipCallHook(\"%s\");\n" + ,ct->posthook); + + prcode(fp, +" }\n" + ); +} + + +/* + * See if a member overload should be skipped. + */ +static int skipOverload(overDef *od,memberDef *md,classDef *cd,classDef *ccd, + int want_local) +{ + /* Skip if it's not the right name. */ + if (od->common != md) + return TRUE; + + /* Skip if it's a signal. */ + if (isSignal(od)) + return TRUE; + + /* Skip if it's a private abstract. */ + if (isAbstract(od) && isPrivate(od)) + return TRUE; + + /* + * If we are disallowing them, skip if it's not in the current class + * unless it is protected. + */ + if (want_local && !isProtected(od) && ccd != cd) + return TRUE; + + return FALSE; +} + + +/* + * Generate a class member function. + */ +static void generateFunction(sipSpec *pt,memberDef *md,overDef *overs, + classDef *cd,classDef *ocd,FILE *fp) +{ + overDef *od; + int need_method, need_self, need_args, need_selfarg; + + /* + * Check that there is at least one overload that needs to be handled. + * See if we can avoid naming the "self" argument (and suppress a + * compiler warning). Finally see if we need to remember if "self" was + * explicitly passed as an argument. + */ + need_method = need_self = need_args = need_selfarg = FALSE; + + for (od = overs; od != NULL; od = od->next) + { + /* + * Skip protected methods if we don't have the means to handle + * them. + */ + if (isProtected(od) && !hasShadow(cd)) + continue; + + if (!skipOverload(od,md,cd,ocd,TRUE)) + { + need_method = TRUE; + + if (!isPrivate(od)) + { + need_args = TRUE; + + if (!isStatic(od)) + { + need_self = TRUE; + + if (isAbstract(od) || isVirtual(od) || isVirtualReimp(od) || usedInCode(od->methodcode, "sipSelfWasArg")) + need_selfarg = TRUE; + } + } + } + } + + if (need_method) + { + char *pname = md->pyname->text; + + prcode(fp, +"\n" +"\n" + ); + + if (!generating_c) + prcode(fp, +"extern \"C\" {static PyObject *meth_%C_%s(PyObject *, PyObject *);}\n" + , classFQCName(cd), pname); + + prcode(fp, +"static PyObject *meth_%C_%s(PyObject *%s,PyObject *%s)\n" +"{\n" + ,classFQCName(cd),pname,(need_self ? "sipSelf" : ""),(need_args ? "sipArgs" : "")); + + if (tracing) + prcode(fp, +" sipTrace(SIP_TRACE_METHODS,\"meth_%C_%s()\\n\");\n" +"\n" + ,classFQCName(cd),pname); + + if (need_args) + prcode(fp, +" int sipArgsParsed = 0;\n" + ); + + if (need_selfarg) + prcode(fp, +" bool sipSelfWasArg = !sipSelf;\n" + ); + + for (od = overs; od != NULL; od = od->next) + { + /* + * If we are handling one variant then we must handle + * them all. + */ + if (skipOverload(od,md,cd,ocd,FALSE)) + continue; + + if (isPrivate(od)) + continue; + + generateFunctionBody(pt,od,cd,ocd,TRUE,fp); + } + + prcode(fp, +"\n" +" /* Raise an exception if the arguments couldn't be parsed. */\n" +" sipNoMethod(%s,%N,%N);\n" +"\n" +" return NULL;\n" +"}\n" + ,(need_args ? "sipArgsParsed" : "0"),cd->iff->name,md->pyname); + } +} + + +/* + * Generate the function calls for a particular overload. + */ +static void generateFunctionBody(sipSpec *pt,overDef *od,classDef *cd, + classDef *ocd,int deref,FILE *fp) +{ + int needSecCall; + signatureDef saved; + + prcode(fp, +"\n" +" {\n" + ); + + /* In case we have to fiddle with it. */ + saved = od->pysig; + + if (isNumberSlot(od->common)) + { + /* + * Number slots must have two arguments because we parse them + * slightly differently. + */ + if (od->pysig.nrArgs == 1) + { + od->pysig.nrArgs = 2; + od->pysig.args[1] = od->pysig.args[0]; + + /* Insert self as the first argument. */ + od->pysig.args[0].atype = class_type; + od->pysig.args[0].name = NULL; + od->pysig.args[0].argflags = ARG_IS_REF|ARG_IN; + od->pysig.args[0].nrderefs = 0; + od->pysig.args[0].defval = NULL; + od->pysig.args[0].u.cd = ocd; + } + + generateArgParser(pt, &od->pysig, cd, NULL, od, FALSE, fp); + needSecCall = FALSE; + } + else if (isIntArgSlot(od->common) || isZeroArgSlot(od->common)) + needSecCall = FALSE; + else + needSecCall = generateArgParser(pt, &od->pysig, cd, NULL, od, FALSE, fp); + + generateFunctionCall(cd,ocd,od,deref,fp); + + if (needSecCall) + { + prcode(fp, +" }\n" +"\n" +" {\n" + ); + + generateArgParser(pt, &od->pysig, cd, NULL, od, TRUE, fp); + generateFunctionCall(cd,ocd,od,deref,fp); + } + + prcode(fp, +" }\n" + ); + + od->pysig = saved; +} + + +/* + * Generate the code to handle the result of a call to a member function. + */ +static void generateHandleResult(overDef *od,int isNew,char *prefix,FILE *fp) +{ + char *vname, vnamebuf[50]; + int a, nrvals, only, has_owner; + argDef *res, *ad; + + res = &od->pysig.result; + + if (res->atype == void_type && res->nrderefs == 0) + res = NULL; + + /* See if we are returning 0, 1 or more values. */ + nrvals = 0; + + if (res != NULL) + { + only = -1; + ++nrvals; + } + + has_owner = FALSE; + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + if (isOutArg(&od->pysig.args[a])) + { + only = a; + ++nrvals; + } + + if (isThisTransferred(&od->pysig.args[a])) + has_owner = TRUE; + } + + /* Handle the trivial case. */ + if (nrvals == 0) + { + prcode(fp, +" Py_INCREF(Py_None);\n" +" %s Py_None;\n" + ,prefix); + + return; + } + + /* Handle results that are classes or mapped types separately. */ + if (res != NULL) + if (res->atype == mapped_type) + { + prcode(fp, +" PyObject *sipResObj = sipConvertFromMappedType("); + + if (isConstArg(res)) + prcode(fp,"const_cast<%b *>(sipRes)",res); + else + prcode(fp,"sipRes"); + + prcode(fp,",sipMappedType_%T,%s);\n" + , res, isResultTransferredBack(od) ? "Py_None" : "NULL"); + + if (isNew) + prcode(fp, +" delete sipRes;\n" + ); + + /* Shortcut if this is the only value returned. */ + if (nrvals == 1) + { + prcode(fp, +"\n" +" %s sipResObj;\n" + ,prefix); + + return; + } + } + else if (res->atype == class_type) + { + classDef *cd = res->u.cd; + + if (isNew || isFactory(od)) + { + prcode(fp, +" %s sipConvertFromNewInstance(",(nrvals == 1 ? prefix : "PyObject *sipResObj =")); + + if (isConstArg(res)) + prcode(fp,"const_cast<%b *>(sipRes)",res); + else + prcode(fp,"sipRes"); + + prcode(fp,",sipClass_%C,%s);\n" + ,classFQCName(cd),((has_owner && isFactory(od)) ? "(PyObject *)sipOwner" : "NULL")); + + /* + * Shortcut if this is the only value returned. + */ + if (nrvals == 1) + return; + } + else + { + prcode(fp, +" %s sipConvertFromInstance(",(nrvals == 1 ? prefix : "PyObject *sipResObj =")); + + if (isConstArg(res)) + prcode(fp,"const_cast<%b *>(sipRes)",res); + else + prcode(fp,"sipRes"); + + prcode(fp, ",sipClass_%C,%s);\n" + , classFQCName(cd), (isResultTransferredBack(od) ? "Py_None" : "NULL")); + + /* + * Shortcut if this is the only value returned. + */ + if (nrvals == 1) + return; + } + } + + /* If there are multiple values then build a tuple. */ + if (nrvals > 1) + { + prcode(fp, +" %s sipBuildResult(0,\"(",prefix); + + /* Build the format string. */ + if (res != NULL) + prcode(fp,"%c",((res->atype == mapped_type || res->atype == class_type) ? 'R' : getBuildResultFormat(res))); + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + if (isOutArg(ad)) + prcode(fp,"%c",getBuildResultFormat(ad)); + } + + prcode(fp,")\""); + + /* Pass the values for conversion. */ + if (res != NULL) + { + prcode(fp,",sipRes"); + + if (res->atype == mapped_type || res->atype == class_type) + prcode(fp,"Obj"); + else if (res->atype == enum_type && res->u.ed->fqcname != NULL) + prcode(fp,",sipEnum_%C",res->u.ed->fqcname); + } + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + if (isOutArg(ad)) + { + prcode(fp,",a%d",a); + + if (ad->atype == mapped_type) + prcode(fp, ",sipMappedType_%T,%s", ad, (isTransferredBack(ad) ? "Py_None" : "NULL")); + else if (ad->atype == class_type) + prcode(fp, ",sipClass_%C,%s", classFQCName(ad->u.cd), (isTransferredBack(ad) ? "Py_None" : "NULL")); + else if (ad->atype == enum_type && ad->u.ed->fqcname != NULL) + prcode(fp,",sipEnum_%C",ad->u.ed->fqcname); + } + } + + prcode(fp,");\n" + ); + + /* All done for multiple values. */ + return; + } + + /* Deal with the only returned value. */ + if (only < 0) + { + ad = res; + vname = "sipRes"; + } + else + { + ad = &od->pysig.args[only]; + + sprintf(vnamebuf,"a%d",only); + vname = vnamebuf; + } + + switch (ad->atype) + { + case mapped_type: + prcode(fp, +" %s sipConvertFromMappedType(", prefix); + + if (isConstArg(ad)) + prcode(fp,"const_cast<%b *>(%s)",ad,vname); + else + prcode(fp,"%s",vname); + + prcode(fp,",sipMappedType_%T,%s);\n" + , ad, (isTransferredBack(ad) ? "Py_None" : "NULL")); + + break; + + case class_type: + { + classDef *cd = ad->u.cd; + int needNew = needNewInstance(ad); + + if (needNew) + prcode(fp, +" %s sipConvertFromNewInstance(", prefix); + else + prcode(fp, +" %s sipConvertFromInstance(", prefix); + + if (isConstArg(ad)) + prcode(fp,"const_cast<%b *>(%s)",ad,vname); + else + prcode(fp,"%s",vname); + + prcode(fp,",sipClass_%C,",classFQCName(cd)); + + if (needNew) + prcode(fp,"NULL"); + else + prcode(fp,"%s\n" + , (isTransferredBack(ad) ? "Py_None" : "NULL")); + + prcode(fp,");\n" + ); + } + + break; + + case bool_type: + case cbool_type: + prcode(fp, +" %s PyBool_FromLong(%s);\n" + ,prefix,vname); + + break; + + case sstring_type: + case ustring_type: + case string_type: + if (ad->nrderefs == 0) + prcode(fp, +" %s PyString_FromStringAndSize(%s&%s,1);\n" + ,prefix,(ad->atype != string_type) ? "(char *)" : "",vname); + else + prcode(fp, +" if (%s == NULL)\n" +" {\n" +" Py_INCREF(Py_None);\n" +" return Py_None;\n" +" }\n" +"\n" +" %s PyString_FromString(%s%s);\n" + ,vname + ,prefix,(ad->atype != string_type) ? "(char *)" : "",vname); + + break; + + case wstring_type: + if (ad->nrderefs == 0) + prcode(fp, +" %s PyUnicode_FromWideChar(&%s,1);\n" + , prefix, vname); + else + prcode(fp, +" if (%s == NULL)\n" +" {\n" +" Py_INCREF(Py_None);\n" +" return Py_None;\n" +" }\n" +"\n" +" %s PyUnicode_FromWideChar(%s,(SIP_SSIZE_T)wcslen(%s));\n" + , vname + , prefix, vname, vname); + + break; + + case enum_type: + if (ad->u.ed->fqcname != NULL) + { + prcode(fp, +" %s sipConvertFromNamedEnum(%s,sipEnum_%C);\n" + ,prefix,vname,ad->u.ed->fqcname); + + break; + } + + /* Drop through. */ + + case short_type: + case int_type: + case cint_type: + prcode(fp, +" %s PyInt_FromLong(%s);\n" + ,prefix,vname); + + break; + + case long_type: + prcode(fp, +" %s PyLong_FromLong(%s);\n" + ,prefix,vname); + + break; + + case ushort_type: + case uint_type: + case ulong_type: + prcode(fp, +" %s PyLong_FromUnsignedLong(%s);\n" + ,prefix,vname); + + break; + + case longlong_type: + prcode(fp, +" %s PyLong_FromLongLong(%s);\n" + ,prefix,vname); + + break; + + case ulonglong_type: + prcode(fp, +" %s PyLong_FromUnsignedLongLong(%s);\n" + ,prefix,vname); + + break; + + case void_type: + prcode(fp, +" %s sipConvertFromVoidPtr(", prefix); + + if (isConstArg(ad)) + prcode(fp, "const_cast<void *>(%s)", vname); + else + prcode(fp, "%s", vname); + + prcode(fp, ");\n" + ); + + break; + + case struct_type: + prcode(fp, +" %s sipConvertFromVoidPtr(",prefix); + + if (isConstArg(ad)) + prcode(fp,"const_cast<%b *>(%s)",ad,vname); + else + prcode(fp,"%s",vname); + + prcode(fp,");\n" + ); + + break; + + case float_type: + case cfloat_type: + prcode(fp, +" %s PyFloat_FromDouble((double)%s);\n" + ,prefix,vname); + + break; + + case double_type: + case cdouble_type: + prcode(fp, +" %s PyFloat_FromDouble(%s);\n" + ,prefix,vname); + + break; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + prcode(fp, +" %s %s;\n" + ,prefix,vname); + + break; + } +} + + +/* + * Return the format character used by sipBuildResult() for a particular type. + */ +static char getBuildResultFormat(argDef *ad) +{ + switch (ad->atype) + { + case mapped_type: + return 'D'; + + case class_type: + if (needNewInstance(ad)) + return 'B'; + + return 'C'; + + case bool_type: + case cbool_type: + return 'b'; + + case sstring_type: + case ustring_type: + case string_type: + return (ad->nrderefs > (isOutArg(ad) ? 1 : 0)) ? 's' : 'c'; + + case wstring_type: + return (ad->nrderefs > (isOutArg(ad) ? 1 : 0)) ? 'x' : 'w'; + + case enum_type: + return (ad->u.ed->fqcname != NULL) ? 'E' : 'e'; + + case short_type: + return 'h'; + + case ushort_type: + return 't'; + + case int_type: + case cint_type: + return 'i'; + + case uint_type: + return 'u'; + + case long_type: + return 'l'; + + case ulong_type: + return 'm'; + + case longlong_type: + return 'n'; + + case ulonglong_type: + return 'o'; + + case void_type: + case struct_type: + return 'V'; + + case float_type: + case cfloat_type: + return 'f'; + + case double_type: + case cdouble_type: + return 'd'; + + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + return 'R'; + } + + /* We should never get here. */ + return ' '; +} + + +/* + * Generate a function call. + */ +static void generateFunctionCall(classDef *cd,classDef *ocd,overDef *od, + int deref, FILE *fp) +{ + int needsNew, error_flag = FALSE, newline, is_result, a, deltemps; + argDef *res = &od->pysig.result, orig_res; + + prcode(fp, +" {\n" + ); + + /* + * If there is no shadow class then protected methods can never be + * called. + */ + if (isProtected(od) && !hasShadow(cd)) + { + prcode(fp, +" /* Never reached. */\n" +" }\n" + ); + + return; + } + + /* Save the full result type as we may want to fiddle with it. */ + orig_res = *res; + + /* See if we need to make a copy of the result on the heap. */ + if ((res->atype == class_type || res->atype == mapped_type) && + !isReference(res) && + res->nrderefs == 0) + { + needsNew = TRUE; + resetIsConstArg(res); + } + else + needsNew = FALSE; + + /* See if sipRes is needed. */ + is_result = (!isInplaceNumberSlot(od->common) && + !isInplaceSequenceSlot(od->common) && + (res->atype != void_type || res->nrderefs != 0)); + + newline = FALSE; + + if (is_result) + { + prcode(fp, +" "); + + generateNamedValueType(res,"sipRes",fp); + + /* + * The typical %MethodCode usually causes a compiler warning, + * so we initialise the result in that case to try and suppress + * it. + */ + if (od->methodcode != NULL) + { + prcode(fp," = "); + + generateCastZero(res,fp); + } + + prcode(fp,";\n" + ); + + newline = TRUE; + } + + deltemps = TRUE; + + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + /* + * If we have an In,Out argument that has conversion code then we delay + * the destruction of any temporary variables until after we have + * converted the outputs. + */ + if (isInArg(ad) && isOutArg(ad) && hasConvertToCode(ad) && deltemps) + { + deltemps = FALSE; + + prcode(fp, +" PyObject *sipResult;\n" + ); + + newline = TRUE; + } + + /* + * If we are returning a class via an output only reference or pointer + * then we need an instance on the heap. + */ + if (needNewInstance(ad)) + { + prcode(fp, +" a%d = new %b();\n" + ,a,ad); + + newline = TRUE; + } + } + + if (od->methodcode != NULL) + { + /* See if the handwritten code seems to be using the error flag. */ + if (needErrorFlag(od->methodcode)) + { + prcode(fp, +" int sipIsErr = 0;\n" + ); + + newline = TRUE; + error_flag = TRUE; + } + } + + if (newline) + prcode(fp, +"\n" + ); + + /* If it is abstract make sure that self was bound. */ + if (isAbstract(od)) + prcode(fp, +" if (sipSelfWasArg)\n" +" {\n" +" sipAbstractMethod(%N,%N);\n" +" return NULL;\n" +" }\n" +"\n" + , cd->iff->name, od->common->pyname); + + /* Call any pre-hook. */ + if (od->prehook != NULL) + prcode(fp, +" sipCallHook(\"%s\");\n" +"\n" + ,od->prehook); + + if (od->methodcode != NULL) + generateCppCodeBlock(od->methodcode,fp); + else + { + int rgil = ((release_gil || isReleaseGIL(od)) && !isHoldGIL(od)); + + if (needsNew && generating_c) + { + prcode(fp, +" if ((sipRes = (%b *)sipMalloc(sizeof (%b))) == NULL)\n" +" {\n" + ,res,res); + + gc_ellipsis(&od->pysig, fp); + + prcode(fp, +" return NULL;\n" +" }\n" +"\n" + ); + } + + if (rgil) + prcode(fp, +" Py_BEGIN_ALLOW_THREADS\n" + ); + + generateTry(od->exceptions,fp); + + prcode(fp, +" "); + + if (od->common->slot != cmp_slot && is_result) + { + /* Construct a copy on the heap if needed. */ + if (needsNew) + { + if (generating_c) + prcode(fp,"*sipRes = "); + else + prcode(fp,"sipRes = new %b(",res); + } + else + prcode(fp,"sipRes = "); + + /* See if we need the address of the result. */ + if ((res->atype == class_type || res->atype == mapped_type) && isReference(res)) + prcode(fp,"&"); + } + + switch (od->common->slot) + { + case no_slot: + generateCppFunctionCall(cd,ocd,od,fp); + break; + + case getitem_slot: + prcode(fp, "(*sipCpp)["); + generateSlotArg(&od->pysig, 0, fp); + prcode(fp,"]"); + break; + + case call_slot: + prcode(fp, "(*sipCpp)("); + generateArgs(od->cppsig,Call,fp); + prcode(fp,")"); + break; + + case int_slot: + case long_slot: + case float_slot: + prcode(fp, "*sipCpp"); + break; + + case add_slot: + generateNumberSlotCall(od,"+",fp); + break; + + case concat_slot: + generateBinarySlotCall(od,"+",deref,fp); + break; + + case sub_slot: + generateNumberSlotCall(od,"-",fp); + break; + + case mul_slot: + generateNumberSlotCall(od,"*",fp); + break; + + case repeat_slot: + generateBinarySlotCall(od,"*",deref,fp); + break; + + case div_slot: + generateNumberSlotCall(od,"/",fp); + break; + + case mod_slot: + generateNumberSlotCall(od,"%",fp); + break; + + case and_slot: + generateNumberSlotCall(od,"&",fp); + break; + + case or_slot: + generateNumberSlotCall(od,"|",fp); + break; + + case xor_slot: + generateNumberSlotCall(od,"^",fp); + break; + + case lshift_slot: + generateNumberSlotCall(od,"<<",fp); + break; + + case rshift_slot: + generateNumberSlotCall(od,">>",fp); + break; + + case iadd_slot: + case iconcat_slot: + generateBinarySlotCall(od,"+=",deref,fp); + break; + + case isub_slot: + generateBinarySlotCall(od,"-=",deref,fp); + break; + + case imul_slot: + case irepeat_slot: + generateBinarySlotCall(od,"*=",deref,fp); + break; + + case idiv_slot: + generateBinarySlotCall(od,"/=",deref,fp); + break; + + case imod_slot: + generateBinarySlotCall(od,"%=",deref,fp); + break; + + case iand_slot: + generateBinarySlotCall(od,"&=",deref,fp); + break; + + case ior_slot: + generateBinarySlotCall(od,"|=",deref,fp); + break; + + case ixor_slot: + generateBinarySlotCall(od,"^=",deref,fp); + break; + + case ilshift_slot: + generateBinarySlotCall(od,"<<=",deref,fp); + break; + + case irshift_slot: + generateBinarySlotCall(od,">>=",deref,fp); + break; + + case invert_slot: + prcode(fp, "~(*sipCpp)"); + break; + + case lt_slot: + generateBinarySlotCall(od,"<",deref,fp); + break; + + case le_slot: + generateBinarySlotCall(od,"<=",deref,fp); + break; + + case eq_slot: + generateBinarySlotCall(od,"==",deref,fp); + break; + + case ne_slot: + generateBinarySlotCall(od,"!=",deref,fp); + break; + + case gt_slot: + generateBinarySlotCall(od,">",deref,fp); + break; + + case ge_slot: + generateBinarySlotCall(od,">=",deref,fp); + break; + + case neg_slot: + prcode(fp, "-(*sipCpp)"); + break; + + case pos_slot: + prcode(fp, "+(*sipCpp)"); + break; + + case cmp_slot: + prcode(fp,"if "); + generateBinarySlotCall(od,"<",deref,fp); + prcode(fp,"\n" +" sipRes = -1;\n" +" else if "); + generateBinarySlotCall(od,">",deref,fp); + prcode(fp,"\n" +" sipRes = 1;\n" +" else\n" +" sipRes = 0"); + + break; + } + + if (needsNew && !generating_c) + prcode(fp,")"); + + prcode(fp,";\n" + ); + + generateCatch(od->exceptions, &od->pysig, fp); + + if (rgil) + prcode(fp, +" Py_END_ALLOW_THREADS\n" + ); + } + + /* Handle /TransferThis/ for non-factory methods. */ + if (!isFactory(od)) + for (a = 0; a < od->pysig.nrArgs; ++a) + { + argDef *ad = &od->pysig.args[a]; + + if (!isInArg(ad)) + continue; + + if (isThisTransferred(ad)) + { + prcode(fp, +"\n" +" if (sipOwner)\n" +" sipTransferTo(sipSelf, (PyObject *)sipOwner);\n" +" else\n" +" sipTransferBack(sipSelf);\n" + ); + + break; + } + } + + gc_ellipsis(&od->pysig, fp); + + if (deltemps) + deleteTemps(&od->pysig, fp); + + prcode(fp, +"\n" + ); + + /* Handle sipIsErr if it was used. */ + if (error_flag) + prcode(fp, +" if (sipIsErr)\n" +" return %s;\n" +"\n" + ,((isVoidReturnSlot(od->common) || isIntReturnSlot(od->common) || isLongReturnSlot(od->common)) ? "-1" : "0")); + + /* Call any post-hook. */ + if (od->posthook != NULL) + prcode(fp, +"\n" +" sipCallHook(\"%s\");\n" + ,od->posthook); + + if (isVoidReturnSlot(od->common)) + prcode(fp, +" return 0;\n" + ); + else if (isInplaceNumberSlot(od->common) || isInplaceSequenceSlot(od->common)) + prcode(fp, +" Py_INCREF(sipSelf);\n" +" return sipSelf;\n" + ); + else if (isIntReturnSlot(od->common) || isLongReturnSlot(od->common)) + prcode(fp, +" return sipRes;\n" + ); + else + { + generateHandleResult(od,needsNew,(deltemps ? "return" : "sipResult ="),fp); + + /* Delete the temporaries now if we haven't already done so. */ + if (!deltemps) + { + deleteTemps(&od->pysig, fp); + + prcode(fp, +"\n" +" return sipResult;\n" + ); + } + } + + prcode(fp, +" }\n" + ); + + /* Restore the full type of the result. */ + *res = orig_res; +} + + +/* + * Generate a call to a C++ function. + */ +static void generateCppFunctionCall(classDef *cd,classDef *ocd,overDef *od, + FILE *fp) +{ + char *mname = od->cppname; + int parens = 1; + + /* + * If the function is protected then call the public wrapper. If it is + * virtual then call explicit scoped function if "self" was passed as + * the first argument. + */ + + if (cd == NULL) + prcode(fp,"%s(",mname); + else if (cd->iff->type == namespace_iface) + prcode(fp,"%S::%s(",classFQCName(cd),mname); + else if (isStatic(od)) + { + if (isProtected(od)) + prcode(fp,"sip%C::sipProtect_%s(",classFQCName(cd),mname); + else + prcode(fp,"%S::%s(",classFQCName(ocd),mname); + } + else if (isProtected(od)) + { + if (!isAbstract(od) && (isVirtual(od) || isVirtualReimp(od))) + { + prcode(fp, "sipCpp->sipProtectVirt_%s(sipSelfWasArg", mname); + + if (od->cppsig->nrArgs > 0) + prcode(fp, ","); + } + else + prcode(fp, "sipCpp->sipProtect_%s(", mname); + } + else if (!isAbstract(od) && (isVirtual(od) || isVirtualReimp(od))) + { + prcode(fp, "(sipSelfWasArg ? sipCpp->%U::%s(", ocd, mname); + generateArgs(od->cppsig, Call, fp); + prcode(fp, ") : sipCpp->%s(", mname); + ++parens; + } + else + prcode(fp, "sipCpp->%s(", mname); + + generateArgs(od->cppsig, Call, fp); + + while (parens--) + prcode(fp, ")"); +} + + +/* + * Generate argument to a slot. + */ +static void generateSlotArg(signatureDef *sd, int argnr, FILE *fp) +{ + argDef *ad; + int deref; + + ad = &sd->args[argnr]; + deref = ((ad->atype == class_type || ad->atype == mapped_type) && ad->nrderefs == 0); + + prcode(fp, "%sa%d", (deref ? "*" : ""), argnr); +} + + +/* + * Generate the call to a binary (non-number) slot method. + */ +static void generateBinarySlotCall(overDef *od, char *op, int deref, FILE *fp) +{ + if (deref) + prcode(fp, "((*sipCpp) %s ", op); + else + prcode(fp, "(sipCpp %s ", op); + + generateSlotArg(&od->pysig, 0, fp); + prcode(fp, ")"); +} + + +/* + * Generate the call to a binary number slot method. + */ +static void generateNumberSlotCall(overDef *od, char *op, FILE *fp) +{ + prcode(fp, "("); + generateSlotArg(&od->pysig, 0, fp); + prcode(fp, " %s ", op); + generateSlotArg(&od->pysig, 1, fp); + prcode(fp, ")"); +} + + +/* + * Generate the argument variables for a member function/constructor/operator. + */ +static int generateArgParser(sipSpec *pt, signatureDef *sd, classDef *cd, + ctorDef *ct, overDef *od, int secCall, FILE *fp) +{ + int a, isQtSlot, optargs, arraylenarg, sigarg, handle_self; + int slotconarg, slotdisarg, need_owner; + + /* If the class is just a namespace, then ignore it. */ + + if (cd != NULL && cd->iff->type == namespace_iface) + cd = NULL; + + handle_self = (od != NULL && od->common->slot == no_slot && !isStatic(od) && cd != NULL); + + /* Assume there isn't a Qt slot. */ + + isQtSlot = FALSE; + + /* + * Generate the local variables that will hold the parsed arguments and + * values returned via arguments. + */ + + sigarg = -1; + need_owner = FALSE; + + for (a = 0; a < sd->nrArgs; ++a) + { + argDef *ad = &sd->args[a]; + + switch (ad->atype) + { + case signal_type: + sigarg = a; + break; + + case rxcon_type: + case rxdis_type: + isQtSlot = TRUE; + break; + + case slotcon_type: + slotconarg = a; + break; + + case slotdis_type: + slotdisarg = a; + break; + } + + if (isArraySize(ad)) + arraylenarg = a; + + generateVariable(ad,a,fp); + + if (isThisTransferred(ad)) + need_owner = TRUE; + } + + if (od != NULL && need_owner) + prcode(fp, +" sipWrapper *sipOwner = 0;\n" + ); + + if (handle_self) + { + if (isProtected(od) && hasShadow(cd)) + prcode(fp, +" sip%C *sipCpp;\n" + ,classFQCName(cd)); + else + prcode(fp, +" %U *sipCpp;\n" + ,cd); + + prcode(fp, +"\n" + ); + } + else if (sd->nrArgs != 0) + prcode(fp, +"\n" + ); + + /* Generate the call to the parser function. */ + if (od != NULL && isNumberSlot(od->common)) + prcode(fp, +" if (sipParsePair(%ssipArgsParsed,sipArg0,sipArg1,\"", (ct != NULL ? "" : "&")); + else + prcode(fp, +" if (sipParseArgs(%ssipArgsParsed,sipArg%s,\"", (ct != NULL ? "" : "&"), (od == NULL || od->common->slot == no_slot || isMultiArgSlot(od->common)) ? "s" : ""); + + /* Generate the format string. */ + optargs = FALSE; + + if (handle_self) + prcode(fp,"%c",(isProtected(od) ? 'p' : 'B')); + else if (isQtSlot && od == NULL) + prcode(fp,"C"); + + for (a = 0; a < sd->nrArgs; ++a) + { + char *fmt = ""; + argDef *ad = &sd->args[a]; + + if (!isInArg(ad)) + continue; + + if (ad->defval != NULL && !optargs) + { + prcode(fp,"|"); + optargs = TRUE; + } + + switch (ad->atype) + { + case sstring_type: + case ustring_type: + case string_type: + if (ad->nrderefs == 0 || (isOutArg(ad) && ad->nrderefs == 1)) + fmt = "c"; + else if (isArray(ad)) + fmt = "a"; + else + fmt = "s"; + + break; + + case wstring_type: + if (ad->nrderefs == 0 || (isOutArg(ad) && ad->nrderefs == 1)) + fmt = "w"; + else if (isArray(ad)) + fmt = "A"; + else + fmt = "x"; + + break; + + case enum_type: + fmt = (ad->u.ed->fqcname != NULL) ? "E" : "e"; + break; + + case bool_type: + fmt = "b"; + break; + + case cbool_type: + fmt = "Xb"; + break; + + case int_type: + if (!isArraySize(ad)) + fmt = "i"; + + break; + + case uint_type: + if (!isArraySize(ad)) + fmt = "u"; + + break; + + case cint_type: + fmt = "Xi"; + break; + + case short_type: + if (!isArraySize(ad)) + fmt = "h"; + + break; + + case ushort_type: + if (!isArraySize(ad)) + fmt = "t"; + + break; + + case long_type: + if (!isArraySize(ad)) + fmt = "l"; + + break; + + case ulong_type: + if (!isArraySize(ad)) + fmt = "m"; + + break; + + case longlong_type: + if (!isArraySize(ad)) + fmt = "n"; + + break; + + case ulonglong_type: + if (!isArraySize(ad)) + fmt = "o"; + + break; + + case struct_type: + case void_type: + fmt = "v"; + break; + + case float_type: + fmt = "f"; + break; + + case cfloat_type: + fmt = "Xf"; + break; + + case double_type: + fmt = "d"; + break; + + case cdouble_type: + fmt = "Xd"; + break; + + case signal_type: + fmt = "G"; + break; + + case slot_type: + fmt = "S"; + break; + + case anyslot_type: + fmt = "U"; + break; + + case slotcon_type: + case slotdis_type: + fmt = (secCall ? "" : "S"); + break; + + case rxcon_type: + fmt = (secCall ? "y" : "q"); + break; + + case rxdis_type: + fmt = (secCall ? "Y" : "Q"); + break; + + case mapped_type: + fmt = getSubFormatChar('M',ad); + break; + + case class_type: + fmt = getSubFormatChar('J', ad); + break; + + case pyobject_type: + fmt = getSubFormatChar('P',ad); + break; + + case pytuple_type: + case pylist_type: + case pydict_type: + case pyslice_type: + case pytype_type: + fmt = (isAllowNone(ad) ? "N" : "T"); + break; + + case pycallable_type: + fmt = (isAllowNone(ad) ? "H" : "F"); + break; + + case qobject_type: + fmt = "R"; + break; + + case ellipsis_type: + fmt = "W"; + break; + } + + prcode(fp,fmt); + } + + prcode(fp,"\""); + + /* Generate the parameters corresponding to the format string. */ + + if (handle_self) + prcode(fp,",&sipSelf,sipClass_%C,&sipCpp",classFQCName(cd)); + else if (isQtSlot && od == NULL) + prcode(fp,",sipSelf"); + + for (a = 0; a < sd->nrArgs; ++a) + { + argDef *ad = &sd->args[a]; + + if (!isInArg(ad)) + continue; + + switch (ad->atype) + { + case mapped_type: + prcode(fp,",sipMappedType_%T,&a%d,&a%dState",ad,a,a); + break; + + case class_type: + prcode(fp, ",sipClass_%T,&a%d", ad, a); + + if (isThisTransferred(ad)) + prcode(fp, ",%ssipOwner", (ct != NULL ? "" : "&")); + else if (isGetWrapper(ad)) + prcode(fp, ",&a%dWrapper", a); + + if (ad->u.cd->convtocode != NULL && !isConstrained(ad)) + prcode(fp, ",&a%dState", a); + + break; + + case rxcon_type: + { + if (sigarg > 0) + prcode(fp,",a%d",sigarg); + else + { + prcode(fp,",\"("); + + generateArgs(sd->args[slotconarg].u.sa,Declaration,fp); + + prcode(fp,")\""); + } + + prcode(fp,",&a%d,&a%d",a,slotconarg); + + break; + } + + case rxdis_type: + { + prcode(fp,",\"("); + + generateArgs(sd->args[slotdisarg].u.sa,Declaration,fp); + + prcode(fp,")\",&a%d,&a%d",a,slotdisarg); + + break; + } + + case slotcon_type: + case slotdis_type: + if (!secCall) + prcode(fp,",&a%d",a); + + break; + + case anyslot_type: + prcode(fp, ",&a%dName,&a%dCallable", a, a); + break; + + case pytuple_type: + prcode(fp,",&PyTuple_Type,&a%d",a); + break; + + case pylist_type: + prcode(fp,",&PyList_Type,&a%d",a); + break; + + case pydict_type: + prcode(fp,",&PyDict_Type,&a%d",a); + break; + + case pyslice_type: + prcode(fp,",&PySlice_Type,&a%d",a); + break; + + case pytype_type: + prcode(fp,",&PyType_Type,&a%d",a); + break; + + case enum_type: + if (ad->u.ed->fqcname != NULL) + prcode(fp,",sipEnum_%C",ad->u.ed->fqcname); + + prcode(fp,",&a%d",a); + break; + + default: + if (!isArraySize(ad)) + prcode(fp,",&a%d",a); + + if (isArray(ad)) + prcode(fp,",&a%d",arraylenarg); + } + } + + prcode(fp,"))\n"); + + return isQtSlot; +} + + +/* + * Get the format character string for something that has sub-formats. + */ + +static char *getSubFormatChar(char fc,argDef *ad) +{ + static char fmt[3]; + char flags; + + fmt[0] = fc; + + flags = 0; + + if (fc != 'P' && ad->nrderefs == 0) + flags |= 0x01; + + if (isTransferred(ad)) + flags |= 0x02; + + if (isTransferredBack(ad)) + flags |= 0x04; + + if (fc == 'J') + { + if (isThisTransferred(ad)) + flags |= 0x20; + + if (isGetWrapper(ad)) + flags |= 0x08; + + if (ad->u.cd->convtocode == NULL || isConstrained(ad)) + flags |= 0x10; + } + + fmt[1] = '0' + flags; + + fmt[2] = '\0'; + + return fmt; +} + + +/* + * Return TRUE if a type has %ConvertToTypeCode. + */ +static int hasConvertToCode(argDef *ad) +{ + codeBlock *convtocode; + + if (ad->atype == class_type && !isConstrained(ad)) + convtocode = ad->u.cd->convtocode; + else if (ad->atype == mapped_type) + convtocode = ad->u.mtd->convtocode; + else + convtocode = NULL; + + return (convtocode != NULL); +} + + +/* + * Garbage collect any ellipsis argument. + */ +static void gc_ellipsis(signatureDef *sd, FILE *fp) +{ + if (sd->nrArgs > 0 && sd->args[sd->nrArgs - 1].atype == ellipsis_type) + prcode(fp, +"\n" +" Py_DECREF(a%d);\n" + , sd->nrArgs - 1); +} + + +/* + * Delete any temporary variables on the heap created by type convertors. + */ +static void deleteTemps(signatureDef *sd, FILE *fp) +{ + int a, first = TRUE; + + for (a = 0; a < sd->nrArgs; ++a) + { + argDef *ad = &sd->args[a]; + + if (!isInArg(ad)) + continue; + + if (ad->atype == wstring_type && ad->nrderefs == 1) + { + if (generating_c || !isConstArg(ad)) + prcode(fp, +" sipFree(a%d);\n" + , a); + else + prcode(fp, +" sipFree(const_cast<wchar_t *>(a%d));\n" + , a); + } + else if (hasConvertToCode(ad)) + { + const char *fstr, *sstr; + + if (ad->atype == mapped_type) + fstr = sstr = "MappedType"; + else + { + fstr = "Instance"; + sstr = "Class"; + } + + if (first) + { + prcode(fp, +"\n" + ); + + first = FALSE; + } + + if (generating_c || !isConstArg(ad)) + prcode(fp, +" sipRelease%s(a%d,sip%s_%T,a%dState);\n" + , fstr, a, sstr, ad, a); + else + prcode(fp, +" sipRelease%s(const_cast<%b *>(a%d),sip%s_%T,a%dState);\n" + , fstr, ad, a, sstr, ad, a); + } + } +} + + +/* + * Generate a C++ code block. + */ +static void generateCppCodeBlock(codeBlock *code,FILE *fp) +{ + int reset_line = FALSE; + codeBlock *cb; + + for (cb = code; cb != NULL; cb = cb->next) + { + char *cp; + + /* + * Fragmented fragments (possibly created when applying + * template types) don't have a filename. + */ + if ((cp = cb->filename) != NULL) + { + reset_line = TRUE; + + prcode(fp, +"#line %d \"", cb->linenr); + + while (*cp != '\0') + { + prcode(fp, "%c", *cp); + + if (*cp == '\\') + prcode(fp, "\\"); + + ++cp; + } + + prcode(fp, "\"\n" + ); + } + + prcode(fp, "%s", cb->frag); + } + + if (reset_line) + { + char *bn; + + /* Just use the base name. */ + + if ((bn = strrchr(currentFileName,'/')) != NULL) + ++bn; + else + bn = currentFileName; + + prcode(fp, +"#line %d \"%s\"\n" + ,currentLineNr + 1,bn); + } +} + + +/* + * Create a source file. + */ +static FILE *createCompilationUnit(sipSpec *pt, char *fname, char *description) +{ + FILE *fp = createFile(pt, fname, description); + + if (fp != NULL) + generateCppCodeBlock(pt->unitcode, fp); + + return fp; +} + + +/* + * Create a file with an optional standard header. + */ +static FILE *createFile(sipSpec *pt,char *fname,char *description) +{ + FILE *fp; + + /* Create the file. */ + + if ((fp = fopen(fname,"w")) == NULL) + fatal("Unable to create file \"%s\"\n",fname); + + /* The "stack" doesn't have to be very deep. */ + previousLineNr = currentLineNr; + currentLineNr = 1; + previousFileName = currentFileName; + currentFileName = fname; + + if (description != NULL) + { + int needComment; + codeBlock *cb; + time_t now; + + /* Write the header. */ + now = time(NULL); + + prcode(fp, +"/*\n" +" * %s\n" +" *\n" +" * Generated by SIP %s on %s" + ,description + ,sipVersion,ctime(&now)); + + if (pt->copying != NULL) + prcode(fp, +" *\n" + ); + + needComment = TRUE; + + for (cb = pt->copying; cb != NULL; cb = cb->next) + { + char *cp; + + for (cp = cb->frag; *cp != '\0'; ++cp) + { + if (needComment) + { + needComment = FALSE; + prcode(fp," * "); + } + + prcode(fp,"%c",*cp); + + if (*cp == '\n') + needComment = TRUE; + } + } + + prcode(fp, +" */\n" + ); + } + + return fp; +} + + +/* + * Close a file and report any errors. + */ +static void closeFile(FILE *fp) +{ + if (ferror(fp)) + fatal("Error writing to \"%s\"\n",currentFileName); + + if (fclose(fp)) + fatal("Error closing \"%s\"\n",currentFileName); + + currentLineNr = previousLineNr; + currentFileName = previousFileName; +} + + +/* + * Print formatted code. + */ +void prcode(FILE *fp, const char *fmt, ...) +{ + char ch; + va_list ap; + + prcode_last = fmt; + + va_start(ap,fmt); + + while ((ch = *fmt++) != '\0') + if (ch == '%') + { + ch = *fmt++; + + switch (ch) + { + case 'c': + { + char c = (char)va_arg(ap,int); + + if (c == '\n') + ++currentLineNr; + + fputc(c,fp); + break; + } + + case 's': + { + const char *cp = va_arg(ap,const char *); + + while (*cp != '\0') + { + if (*cp == '\n') + ++currentLineNr; + + fputc(*cp,fp); + ++cp; + } + + break; + } + + case 'l': + fprintf(fp,"%ld",va_arg(ap,long)); + break; + + case 'u': + fprintf(fp,"%u",va_arg(ap,unsigned)); + break; + + case 'd': + fprintf(fp,"%d",va_arg(ap,int)); + break; + + case 'g': + fprintf(fp,"%g",va_arg(ap,double)); + break; + + case 'x': + fprintf(fp,"0x%08x",va_arg(ap,unsigned)); + break; + + case '\0': + fputc('%',fp); + --fmt; + break; + + case '\n': + fputc('\n',fp); + ++currentLineNr; + break; + + case 'b': + { + argDef *ad, orig; + + ad = va_arg(ap,argDef *); + orig = *ad; + + resetIsConstArg(ad); + resetIsReference(ad); + ad->nrderefs = 0; + + generateBaseType(ad,fp); + + *ad = orig; + + break; + } + + case 'M': + prcode_xml = !prcode_xml; + break; + + case 'B': + generateBaseType(va_arg(ap,argDef *),fp); + break; + + case 'T': + prTypeName(fp,va_arg(ap,argDef *),FALSE); + break; + + case 'I': + { + int indent = va_arg(ap,int); + + while (indent-- > 0) + fputc('\t',fp); + + break; + } + + case 'N': + { + nameDef *nd = va_arg(ap,nameDef *); + + fprintf(fp,"sipNm_%s_%s",nd->module->name,nd->text); + break; + } + + case 'E': + { + enumDef *ed = va_arg(ap,enumDef *); + + if (ed->fqcname == NULL) + fprintf(fp,"int"); + else if (isProtectedEnum(ed)) + { + fprintf(fp,"sip"); + prScopedName(fp,classFQCName(ed->pcd),"_"); + + fprintf(fp,"::sip%s",scopedNameTail(ed->fqcname)); + } + else + prScopedName(fp,ed->fqcname,"::"); + + break; + } + + case 'F': + prScopedName(fp,va_arg(ap,scopedNameDef *),""); + break; + + case 'C': + prScopedName(fp,va_arg(ap,scopedNameDef *),"_"); + break; + + case 'S': + if (generating_c) + fprintf(fp,"struct "); + + prScopedName(fp,va_arg(ap,scopedNameDef *),"::"); + break; + + case 'U': + if (generating_c) + fprintf(fp,"struct "); + + prScopedClassName(fp,va_arg(ap,classDef *),"::"); + break; + + case 'O': + prOverloadName(fp, va_arg(ap, overDef *)); + break; + + case 'P': + { + classDef *ecd = va_arg(ap, classDef *); + const char *pyname = va_arg(ap, const char *); + + prScopedPythonName(fp, ecd, pyname); + break; + } + + case 'X': + generateThrowSpecifier(va_arg(ap,throwArgs *),fp); + break; + + default: + fputc(ch,fp); + } + } + else if (ch == '\n') + { + fputc('\n',fp); + ++currentLineNr; + } + else + fputc(ch,fp); + + va_end(ap); +} + + +/* + * Generate the C++ name of an overloaded function. + */ +void prOverloadName(FILE *fp, overDef *od) +{ + char *pt1, *pt2; + + pt1 = "operator"; + + switch (od->common->slot) + { + case add_slot: + pt2 = "+"; + break; + + case sub_slot: + pt2 = "-"; + break; + + case mul_slot: + pt2 = "*"; + break; + + case div_slot: + pt2 = "/"; + break; + + case mod_slot: + pt2 = "%"; + break; + + case and_slot: + pt2 = "&"; + break; + + case or_slot: + pt2 = "|"; + break; + + case xor_slot: + pt2 = "^"; + break; + + case lshift_slot: + pt2 = "<<"; + break; + + case rshift_slot: + pt2 = ">>"; + break; + + case iadd_slot: + pt2 = "+="; + break; + + case isub_slot: + pt2 = "-="; + break; + + case imul_slot: + pt2 = "*="; + break; + + case idiv_slot: + pt2 = "/="; + break; + + case imod_slot: + pt2 = "%="; + break; + + case iand_slot: + pt2 = "&="; + break; + + case ior_slot: + pt2 = "|="; + break; + + case ixor_slot: + pt2 = "^="; + break; + + case ilshift_slot: + pt2 = "<<="; + break; + + case irshift_slot: + pt2 = ">>="; + break; + + case invert_slot: + pt2 = "~"; + break; + + case call_slot: + pt2 = "()"; + break; + + case getitem_slot: + pt2 = "[]"; + break; + + case lt_slot: + pt2 = "<"; + break; + + case le_slot: + pt2 = "<="; + break; + + case eq_slot: + pt2 = "=="; + break; + + case ne_slot: + pt2 = "!="; + break; + + case gt_slot: + pt2 = ">"; + break; + + case ge_slot: + pt2 = ">="; + break; + + default: + pt1 = ""; + pt2 = od->cppname; + } + + fprintf(fp, "%s%s", pt1, pt2); +} + + +/* + * Generate a scoped name with the given separator string. + */ +static void prScopedName(FILE *fp,scopedNameDef *snd,char *sep) +{ + while (snd != NULL) + { + fprintf(fp,"%s",snd->name); + + if ((snd = snd->next) != NULL) + fprintf(fp,"%s",sep); + } +} + + +/* + * Generate a scoped class name with the given separator string. At the moment + * this provides (probably) broken support for protected classes. + */ +static void prScopedClassName(FILE *fp,classDef *cd,char *sep) +{ + scopedNameDef *snd = classFQCName(cd); + + while (snd != NULL) + { + if (isProtectedClass(cd)) + fprintf(fp,"sip"); + + fprintf(fp,"%s",snd->name); + + if ((snd = snd->next) != NULL) + fprintf(fp,"%s",sep); + } +} + + +/* + * Generate a scoped Python name. + */ +void prScopedPythonName(FILE *fp, classDef *scope, const char *pyname) +{ + if (scope != NULL) + { + prScopedPythonName(fp, scope->ecd, NULL); + fprintf(fp, "%s.", scope->pyname); + } + + if (pyname != NULL) + fprintf(fp, "%s", pyname); +} + + +/* + * Generate a type name to be used as part of an identifier name. + */ +static void prTypeName(FILE *fp,argDef *ad,int intmpl) +{ + if (intmpl) + { + char buf[10]; + int flgs; + + /* We use numbers so they don't conflict with names. */ + + sprintf(buf,"%02d",ad->atype); + + flgs = 0; + + if (isConstArg(ad)) + flgs += 1; + + if (isReference(ad)) + flgs += 2; + + prcode(fp,"%s%d%d",buf,flgs,ad->nrderefs); + } + + /* Now add a name if there is one associated with the type. */ + + switch (ad->atype) + { + case struct_type: + prcode(fp,"%C",ad->u.sname); + break; + + case defined_type: + prcode(fp,"%C",ad->u.snd); + break; + + case enum_type: + prcode(fp,"%C",ad->u.ed->fqcname); + break; + + case mapped_type: + prTypeName(fp,&ad->u.mtd->type,intmpl); + break; + + case class_type: + prcode(fp,"%C",classFQCName(ad->u.cd)); + break; + + case template_type: + { + int a; + templateDef *td = ad->u.td; + + prcode(fp,"%C",td->fqname); + + for (a = 0; a < td->types.nrArgs; ++a) + { + prcode(fp,"_"); + prTypeName(fp,&td->types.args[a],TRUE); + } + + break; + } + } +} + + +/* + * Return TRUE if handwritten code uses the error flag. + */ +static int needErrorFlag(codeBlock *cb) +{ + return usedInCode(cb, "sipIsErr"); +} + + +/* + * Return TRUE if the argument type means an instance needs to be created on + * the heap to pass back to Python. + */ +static int needNewInstance(argDef *ad) +{ + return ((ad->atype == mapped_type || ad->atype == class_type) && + ((isReference(ad) && ad->nrderefs == 0) || (!isReference(ad) && ad->nrderefs == 1)) && + !isInArg(ad) && isOutArg(ad)); +} + + +/* + * Reset and save any argument flags so that the signature will be rendered + * exactly as defined in C++. + */ +static void normaliseArgs(signatureDef *sd) +{ + int a; + argDef *ad = sd->args; + + for (a = 0; a < sd->nrArgs; ++a) + { + if (ad->atype == class_type && isProtectedClass(ad->u.cd)) + { + resetIsProtectedClass(ad->u.cd); + setWasProtectedClass(ad->u.cd); + } + else if (ad->atype == enum_type && isProtectedEnum(ad->u.ed)) + { + resetIsProtectedEnum(ad->u.ed); + setWasProtectedEnum(ad->u.ed); + } + + ++ad; + } +} + + +/* + * Restore any argument flags modified by normaliseArgs(). + */ +static void restoreArgs(signatureDef *sd) +{ + int a; + argDef *ad = sd->args; + + for (a = 0; a < sd->nrArgs; ++a) + { + if (ad->atype == class_type && wasProtectedClass(ad->u.cd)) + { + resetWasProtectedClass(ad->u.cd); + setIsProtectedClass(ad->u.cd); + } + else if (ad->atype == enum_type && wasProtectedEnum(ad->u.ed)) + { + resetWasProtectedEnum(ad->u.ed); + setIsProtectedEnum(ad->u.ed); + } + + ++ad; + } +} + + +/* + * Return TRUE if a dealloc function is needed for a class. + */ +static int needDealloc(classDef *cd) +{ + if (cd->iff->type == namespace_iface) + return FALSE; + + /* All of these conditions cause some code to be generated. */ + + if (tracing) + return TRUE; + + if (generating_c) + return TRUE; + + if (cd->dealloccode != NULL) + return TRUE; + + if (isPublicDtor(cd)) + return TRUE; + + if (hasShadow(cd)) + return TRUE; + + return FALSE; +} + + +/* + * Return the argument name to use in a function definition for handwritten + * code. + */ +static const char *argName(const char *name, codeBlock *cb) +{ + static const char noname[] = ""; + + /* Always use the name in C code. */ + if (generating_c) + return name; + + /* Use the name if it is used in the handwritten code. */ + if (usedInCode(cb, name)) + return name; + + /* Don't use the name and avoid a compiler warning. */ + return noname; +} + + +/* + * Returns TRUE if a string is used in a code block. + */ +static int usedInCode(codeBlock *code, const char *str) +{ + while (code != NULL) + { + if (strstr(code->frag, str) != NULL) + return TRUE; + + code = code->next; + } + + return FALSE; +} + + +/* + * Generate an assignment statement from a void * variable to a class instance + * variable. + */ +static void generateClassFromVoid(classDef *cd, const char *cname, + const char *vname, FILE *fp) +{ + if (generating_c) + prcode(fp, "%S *%s = (%S *)%s", classFQCName(cd), cname, classFQCName(cd), vname); + else + prcode(fp, "%S *%s = reinterpret_cast<%S *>(%s)", classFQCName(cd), cname, classFQCName(cd), vname); +} + + +/* + * Generate an assignment statement from a void * variable to a mapped type + * variable. + */ +static void generateMappedTypeFromVoid(mappedTypeDef *mtd, const char *cname, + const char *vname, FILE *fp) +{ + if (generating_c) + prcode(fp, "%b *%s = (%b *)%s", &mtd->type, cname, &mtd->type, vname); + else + prcode(fp, "%b *%s = reinterpret_cast<%b *>(%s)", &mtd->type, cname, &mtd->type, vname); +} diff --git a/python/sip/sipgen/heap.c b/python/sip/sipgen/heap.c new file mode 100644 index 00000000..d6e94ccc --- /dev/null +++ b/python/sip/sipgen/heap.c @@ -0,0 +1,115 @@ +/* + * Wrappers around standard functions that use the heap. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <sys/types.h> + +#include "sip.h" + + +static void nomem(void); + + +/* + * Wrap malloc() and handle any errors. + */ + +void *sipMalloc(size_t n) +{ + void *h; + + if ((h = malloc(n)) == NULL) + nomem(); + + return h; +} + + +/* + * Wrap strdup() and handle any errors. + */ + +char *sipStrdup(char *s) +{ + char *h; + + if ((h = strdup(s)) == NULL) + nomem(); + + return h; +} + + +/* + * Return a string on the heap which is the concatonation of all the arguments. + */ + +char *concat(const char *s, ...) +{ + const char *sp; + char *new; + size_t len; + va_list ap; + + /* Find the length of the final string. */ + + len = 1; + va_start(ap,s); + + for (sp = s; sp != NULL; sp = va_arg(ap, const char *)) + len += strlen(sp); + + va_end(ap); + + /* Create the new string. */ + + new = sipMalloc(len); + *new = '\0'; + + va_start(ap,s); + + for (sp = s; sp != NULL; sp = va_arg(ap, const char *)) + strcat(new,sp); + + va_end(ap); + + return new; +} + + +/* + * Append a string to another that is on the heap. + */ + +void append(char **s,char *new) +{ + if ((*s = realloc(*s,strlen(*s) + strlen(new) + 1)) == NULL) + nomem(); + + strcat(*s,new); +} + + +/* + * Display a standard error message when the heap is exhausted. + */ + +static void nomem(void) +{ + fatal("Unable to allocate memory on the heap\n"); +} diff --git a/python/sip/sipgen/lexer.c b/python/sip/sipgen/lexer.c new file mode 100644 index 00000000..138acfeb --- /dev/null +++ b/python/sip/sipgen/lexer.c @@ -0,0 +1,3266 @@ +#line 2 "lexer.c.tmp" + +#line 4 "lexer.c.tmp" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 +#define YY_FLEX_SUBMINOR_VERSION 33 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; +#endif /* ! C99 */ + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +#if __STDC__ + +#define YY_USE_CONST + +#endif /* __STDC__ */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart(yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#define YY_BUF_SIZE 16384 +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +extern int yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + #define YY_LESS_LINENO(n) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +/* The following is because we cannot portably get our hands on size_t + * (without autoconf's help, which isn't available because we want + * flex-generated scanners to compile on their own). + */ + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef unsigned int yy_size_t; +#endif + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +int yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +static void yyensure_buffer_stack (void ); +static void yy_load_buffer_state (void ); +static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); + +#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +typedef unsigned char YY_CHAR; + +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; + +typedef int yy_state_type; + +extern int yylineno; + +int yylineno = 1; + +extern char *yytext; +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state (void ); +static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); +static int yy_get_next_buffer (void ); +static void yy_fatal_error (yyconst char msg[] ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 111 +#define YY_END_OF_BUFFER 112 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[735] = + { 0, + 0, 0, 0, 0, 0, 0, 112, 110, 65, 66, + 110, 110, 110, 110, 110, 68, 68, 110, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 110, 65, 110, 109, + 108, 109, 78, 76, 78, 0, 73, 0, 74, 0, + 68, 0, 72, 69, 72, 75, 67, 0, 47, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 48, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 77, 69, 64, 72, 67, 70, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 27, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 69, 40, 71, 25, 23, + 71, 71, 71, 33, 71, 71, 71, 28, 71, 71, + + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 38, 71, 71, 71, 31, 71, 0, 0, 0, 0, + 0, 0, 0, 0, 101, 15, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 107, 69, 71, 71, 71, 71, 71, 16, 36, 71, + 71, 39, 29, 71, 71, 71, 71, 71, 26, 71, + 71, 22, 71, 71, 71, 44, 71, 71, 71, 71, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 71, 71, 71, 71, 71, 71, 30, 71, + 71, 71, 71, 71, 18, 71, 34, 37, 17, 71, + 71, 71, 71, 71, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 20, 71, 21, 71, 41, 71, 32, 24, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, + 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 57, 45, 71, 43, + 71, 46, 35, 6, 0, 0, 0, 0, 0, 7, + 0, 79, 0, 0, 10, 0, 0, 105, 3, 11, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 106, 0, 0, 0, 0, 0, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 42, 19, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 103, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 8, 90, 0, + 88, 0, 71, 71, 52, 51, 71, 71, 71, 55, + 71, 71, 56, 71, 71, 0, 0, 0, 0, 0, + 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 9, 0, 0, 0, 100, 0, 0, 0, + 0, 58, 71, 71, 54, 50, 63, 71, 71, 71, + 71, 104, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 12, 91, 89, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 71, 49, 71, 71, 61, + + 62, 0, 0, 0, 0, 0, 0, 0, 102, 0, + 94, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 71, 59, 60, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 99, 2, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 93, 0, 0, 0, + 0, 85, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 84, 0, 0, 0, 0, 0, + + 97, 0, 0, 0, 81, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 83, 0, 0, 92, 98, 95, + 0, 80, 0, 0, 0, 96, 0, 0, 0, 82, + 0, 86, 87, 0 + } ; + +static yyconst flex_int32_t yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 5, 1, 1, 6, 1, 7, 1, + 1, 8, 9, 1, 10, 11, 12, 13, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 15, 1, 1, + 1, 1, 1, 1, 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, 26, + 1, 1, 1, 1, 41, 1, 42, 43, 44, 45, + + 46, 47, 48, 49, 50, 26, 51, 52, 53, 54, + 55, 56, 26, 57, 58, 59, 60, 61, 62, 63, + 64, 26, 1, 65, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst flex_int32_t yy_meta[66] = + { 0, + 1, 1, 2, 1, 1, 1, 1, 1, 1, 3, + 3, 3, 4, 4, 1, 4, 4, 4, 4, 4, + 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 1 + } ; + +static yyconst flex_int16_t yy_base[743] = + { 0, + 0, 64, 1581, 65, 64, 66, 1583, 1585, 1585, 1585, + 70, 73, 68, 73, 80, 80, 84, 1567, 89, 92, + 95, 98, 102, 105, 110, 113, 120, 123, 126, 129, + 134, 137, 151, 159, 169, 165, 1516, 150, 209, 1585, + 1585, 1560, 1585, 1585, 1567, 124, 1585, 181, 1585, 64, + 179, 189, 192, 237, 195, 1585, 0, 0, 1585, 200, + 211, 242, 206, 248, 252, 255, 258, 264, 267, 270, + 274, 277, 281, 285, 288, 291, 294, 300, 303, 309, + 315, 320, 332, 337, 342, 348, 351, 355, 358, 1585, + 369, 391, 1534, 1553, 186, 1521, 96, 1529, 72, 286, + + 1524, 332, 1517, 324, 1530, 65, 108, 1517, 1520, 1515, + 1585, 370, 222, 382, 0, 0, 390, 417, 420, 423, + 426, 429, 432, 435, 438, 441, 444, 449, 452, 455, + 458, 466, 471, 481, 484, 492, 495, 498, 501, 506, + 509, 512, 524, 527, 532, 537, 541, 1540, 253, 140, + 1515, 1542, 1515, 1520, 1541, 1507, 326, 1517, 1515, 429, + 1517, 148, 1499, 1585, 1501, 1512, 1511, 294, 1495, 1508, + 1493, 1509, 1492, 1503, 1498, 1516, 1487, 1492, 1488, 1493, + 1485, 1496, 546, 242, 553, 559, 564, 568, 575, 578, + 582, 585, 593, 596, 599, 602, 605, 608, 611, 614, + + 617, 620, 623, 626, 640, 631, 643, 650, 660, 664, + 667, 680, 685, 688, 691, 694, 1496, 1483, 1493, 1491, + 1490, 1490, 1473, 1469, 1585, 1585, 1486, 1476, 1471, 1477, + 1471, 1509, 1471, 1473, 1478, 1477, 1466, 1472, 1460, 1469, + 1459, 1458, 362, 1458, 242, 1497, 1468, 1467, 1453, 1452, + 1585, 390, 698, 703, 706, 710, 718, 727, 742, 745, + 748, 751, 754, 757, 760, 763, 766, 770, 773, 776, + 784, 787, 790, 793, 796, 805, 813, 820, 825, 829, + 1450, 1451, 1449, 1447, 1460, 1455, 1448, 1446, 1442, 1455, + 1458, 1444, 1441, 1437, 1442, 1448, 1448, 1438, 1440, 1436, + + 1443, 1465, 1434, 1423, 1440, 1430, 1428, 1428, 1430, 456, + 1463, 1420, 832, 846, 836, 839, 867, 872, 850, 875, + 880, 883, 886, 889, 893, 896, 899, 902, 905, 908, + 916, 919, 923, 929, 1427, 1420, 650, 1425, 1419, 1421, + 1415, 1414, 1415, 1429, 1409, 1424, 1409, 1422, 1408, 1415, + 1419, 1418, 1416, 1407, 1405, 1405, 1408, 1398, 1438, 1435, + 1395, 1408, 1402, 1396, 1404, 1394, 1406, 932, 935, 945, + 948, 966, 969, 974, 979, 982, 990, 995, 1001, 1005, + 1010, 1013, 1016, 1021, 1024, 1027, 1030, 1033, 1036, 1401, + 1428, 1396, 1398, 1397, 1385, 1395, 1381, 1391, 1388, 1391, + + 1390, 1378, 1388, 1387, 1585, 1386, 1385, 1378, 1394, 1410, + 508, 1385, 1369, 1375, 1365, 1374, 1367, 1368, 1370, 1373, + 1364, 1372, 1374, 1370, 1362, 1041, 1044, 1047, 1062, 1066, + 1069, 1072, 1075, 1078, 1089, 1097, 1100, 1105, 1108, 1111, + 1114, 1117, 1120, 1585, 1358, 1370, 1369, 1362, 1359, 1585, + 185, 1585, 1353, 1362, 1585, 1388, 1348, 1585, 1585, 1585, + 1358, 1339, 1347, 1346, 1354, 1347, 1345, 1338, 1346, 1340, + 1349, 1343, 1337, 1585, 1345, 1344, 1344, 1342, 1369, 1123, + 1128, 1132, 1135, 1138, 1150, 1153, 1161, 1164, 1167, 1173, + 1176, 1179, 1186, 1341, 1328, 1339, 1365, 1323, 1324, 1325, + + 1325, 391, 1323, 1319, 1585, 1320, 1330, 1329, 1331, 1348, + 1313, 1320, 1327, 1314, 1321, 1305, 1284, 1585, 1585, 1290, + 1585, 1283, 1191, 1194, 1201, 1204, 1207, 1210, 1217, 1221, + 1224, 1228, 1231, 1238, 1241, 1278, 1296, 1294, 1255, 1259, + 1245, 389, 1585, 1244, 1247, 1221, 1218, 1217, 1212, 1211, + 1200, 1190, 1585, 1182, 1171, 1191, 1585, 1148, 1141, 1136, + 1133, 1244, 1250, 1259, 1262, 1265, 1268, 1271, 1274, 1277, + 1280, 1585, 1109, 1106, 1097, 1139, 1099, 1091, 1073, 1092, + 1063, 1058, 1084, 1585, 1585, 1585, 1051, 1051, 1042, 1042, + 1014, 1020, 1585, 1046, 1019, 1285, 1292, 1297, 1305, 1308, + + 1311, 1014, 1003, 975, 958, 974, 965, 947, 1585, 953, + 1585, 942, 938, 935, 932, 916, 928, 914, 915, 919, + 1317, 1320, 1323, 917, 915, 902, 906, 888, 932, 903, + 891, 891, 906, 863, 863, 846, 829, 813, 823, 820, + 1330, 817, 809, 836, 798, 777, 775, 808, 764, 774, + 763, 767, 769, 750, 1585, 1585, 747, 733, 732, 722, + 695, 701, 700, 702, 686, 722, 1585, 690, 688, 673, + 681, 1585, 706, 705, 701, 666, 650, 669, 623, 635, + 612, 619, 1585, 613, 603, 602, 592, 591, 593, 580, + 526, 510, 504, 500, 1585, 485, 478, 484, 483, 480, + + 1585, 459, 453, 479, 1585, 443, 417, 439, 374, 367, + 353, 343, 339, 322, 1585, 338, 273, 1585, 1585, 1585, + 271, 1585, 263, 193, 190, 1585, 152, 122, 96, 1585, + 80, 1585, 1585, 1585, 1342, 1346, 1350, 1354, 1356, 1358, + 1362, 92 + } ; + +static yyconst flex_int16_t yy_def[743] = + { 0, + 734, 1, 735, 735, 736, 736, 734, 734, 734, 734, + 737, 738, 734, 739, 734, 734, 734, 734, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 737, 734, 738, 734, 734, + 734, 739, 739, 739, 739, 734, 741, 742, 734, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 739, 739, 741, 742, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 739, 739, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 740, + 740, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 740, 740, 740, 740, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 740, 740, 740, 740, 740, 740, 740, 740, 740, + 740, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 740, 740, 740, 740, 740, + + 740, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 740, 740, 740, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 740, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 0, 734, 734, 734, 734, 734, 734, + 734, 734 + } ; + +static yyconst flex_int16_t yy_nxt[1651] = + { 0, + 8, 9, 10, 9, 11, 8, 12, 8, 8, 13, + 14, 15, 16, 17, 18, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 20, 19, + 19, 19, 19, 21, 19, 19, 19, 19, 19, 19, + 19, 19, 22, 23, 24, 25, 26, 19, 19, 27, + 19, 28, 19, 29, 30, 31, 19, 32, 33, 34, + 35, 36, 19, 19, 37, 38, 44, 41, 44, 39, + 42, 45, 47, 45, 47, 49, 112, 112, 50, 49, + 51, 51, 52, 53, 52, 54, 54, 56, 176, 162, + 50, 57, 51, 51, 50, 116, 51, 51, 52, 55, + + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 177, 52, 55, 52, 52, 55, 52, 163, 62, 52, + 55, 52, 52, 55, 52, 733, 47, 61, 47, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 52, 55, + 52, 732, 58, 52, 55, 52, 52, 55, 52, 159, + 64, 91, 63, 65, 70, 92, 66, 178, 160, 67, + 52, 55, 52, 68, 71, 230, 731, 74, 52, 55, + 52, 179, 69, 72, 52, 55, 52, 73, 52, 55, + 52, 218, 231, 49, 75, 78, 79, 49, 80, 50, + 76, 51, 51, 77, 219, 81, 82, 730, 52, 83, + + 52, 52, 113, 52, 52, 499, 52, 84, 89, 52, + 55, 52, 86, 156, 85, 52, 55, 52, 87, 500, + 52, 55, 52, 88, 93, 94, 95, 96, 97, 98, + 99, 52, 100, 52, 729, 101, 102, 117, 103, 104, + 157, 105, 106, 107, 108, 109, 52, 728, 52, 54, + 54, 52, 55, 52, 252, 252, 114, 52, 55, 52, + 119, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 306, 307, 118, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 114, 52, 55, 52, 52, 55, 52, 120, + 52, 55, 52, 121, 52, 55, 52, 52, 55, 52, + + 52, 55, 52, 52, 55, 52, 159, 727, 122, 52, + 55, 52, 52, 55, 52, 217, 726, 123, 52, 55, + 52, 126, 125, 124, 52, 55, 52, 725, 127, 52, + 55, 52, 164, 131, 129, 128, 134, 130, 165, 166, + 132, 52, 55, 52, 236, 133, 52, 55, 52, 237, + 136, 52, 55, 52, 135, 724, 138, 52, 55, 52, + 52, 55, 52, 137, 52, 55, 52, 52, 55, 52, + 91, 139, 140, 168, 92, 172, 723, 169, 173, 223, + 174, 224, 112, 112, 722, 303, 170, 721, 141, 183, + 184, 185, 304, 52, 186, 186, 142, 143, 720, 52, + + 55, 52, 252, 252, 146, 144, 147, 145, 148, 544, + 149, 98, 719, 545, 100, 183, 187, 101, 150, 718, + 103, 151, 578, 579, 152, 153, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 717, 188, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 716, 189, 227, 364, 193, 52, 55, 52, 365, 190, + 52, 55, 52, 191, 228, 197, 192, 194, 715, 195, + 52, 55, 52, 52, 55, 52, 714, 713, 196, 198, + + 199, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 712, 200, 52, 55, 52, 52, 55, + 52, 52, 55, 52, 711, 464, 201, 710, 709, 202, + 465, 708, 203, 52, 55, 52, 52, 55, 52, 707, + 204, 52, 55, 52, 706, 205, 52, 55, 52, 705, + 52, 55, 52, 206, 184, 184, 207, 211, 252, 252, + 208, 209, 52, 210, 52, 186, 186, 704, 52, 212, + 52, 186, 186, 52, 55, 52, 213, 52, 55, 52, + 703, 215, 216, 253, 52, 55, 52, 52, 55, 52, + 214, 52, 55, 52, 52, 55, 52, 702, 254, 255, + + 256, 257, 52, 55, 52, 52, 55, 52, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 701, 258, + 52, 55, 52, 259, 260, 700, 699, 262, 261, 52, + 55, 52, 52, 55, 52, 265, 698, 697, 266, 52, + 55, 52, 696, 263, 695, 267, 694, 392, 264, 52, + 55, 52, 268, 52, 55, 52, 52, 55, 52, 693, + 692, 270, 393, 394, 269, 271, 691, 395, 272, 52, + 55, 52, 273, 274, 52, 55, 52, 52, 55, 52, + + 52, 55, 52, 52, 55, 52, 690, 52, 55, 52, + 689, 275, 52, 55, 52, 52, 55, 52, 688, 52, + 55, 52, 687, 686, 277, 276, 313, 52, 55, 52, + 685, 684, 278, 683, 682, 315, 52, 55, 52, 681, + 680, 317, 314, 679, 318, 678, 677, 279, 316, 676, + 280, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 675, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 674, 673, + 319, 320, 672, 52, 55, 52, 52, 55, 52, 52, + + 55, 52, 52, 55, 52, 52, 55, 52, 671, 324, + 670, 669, 321, 325, 52, 55, 52, 668, 322, 667, + 666, 323, 52, 55, 52, 665, 664, 326, 327, 52, + 55, 52, 663, 328, 52, 55, 52, 330, 52, 55, + 52, 52, 55, 52, 662, 52, 55, 52, 52, 55, + 52, 329, 375, 661, 660, 52, 55, 52, 331, 52, + 55, 52, 659, 369, 370, 658, 333, 657, 376, 334, + 656, 368, 371, 332, 655, 372, 52, 55, 52, 373, + 374, 52, 55, 52, 52, 55, 52, 654, 377, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 52, 55, + + 52, 378, 52, 55, 52, 52, 55, 52, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 653, 380, 652, 651, 379, 52, 55, 52, 52, 55, + 52, 382, 52, 55, 52, 650, 649, 381, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 383, 648, 647, + 427, 646, 645, 384, 52, 55, 52, 52, 55, 52, + 644, 643, 386, 642, 387, 426, 385, 640, 428, 639, + 638, 429, 637, 636, 388, 52, 55, 52, 52, 55, + 52, 635, 430, 52, 55, 52, 634, 389, 52, 55, + 52, 52, 55, 52, 633, 431, 632, 631, 435, 52, + + 55, 52, 630, 434, 52, 55, 52, 629, 628, 432, + 52, 55, 52, 433, 52, 55, 52, 627, 436, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 626, 437, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 52, 55, 52, 439, 625, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 438, + 624, 441, 620, 619, 482, 618, 440, 480, 617, 442, + 481, 52, 55, 52, 443, 52, 55, 52, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 484, 616, 485, 615, 614, 483, 613, 488, 52, 55, + + 52, 612, 486, 611, 610, 487, 52, 55, 52, 52, + 55, 52, 490, 489, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 609, 608, 52, 55, 52, + 491, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 607, 606, 523, 492, 524, 605, 604, 527, 493, 52, + 55, 52, 52, 55, 52, 603, 525, 528, 602, 526, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 529, + 530, 531, 52, 55, 52, 52, 55, 52, 52, 55, + 52, 595, 594, 534, 535, 52, 55, 52, 593, 533, + + 52, 55, 52, 52, 55, 52, 592, 532, 591, 563, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 590, 589, 564, 562, 52, 55, 52, 565, + 52, 55, 52, 52, 55, 52, 566, 52, 55, 52, + 52, 55, 52, 588, 587, 568, 569, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 586, 585, 567, 52, + 55, 52, 584, 583, 571, 582, 596, 570, 52, 55, + 52, 52, 55, 52, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 52, 55, 52, 52, 55, 52, 52, + 55, 52, 581, 597, 52, 55, 52, 599, 580, 577, + + 598, 52, 55, 52, 576, 600, 52, 55, 52, 575, + 574, 621, 573, 601, 52, 55, 52, 52, 55, 52, + 52, 55, 52, 572, 561, 622, 52, 55, 52, 52, + 55, 52, 52, 55, 52, 560, 641, 559, 623, 52, + 55, 52, 40, 40, 40, 40, 43, 43, 43, 43, + 46, 46, 46, 46, 48, 48, 48, 48, 55, 55, + 60, 60, 115, 558, 115, 115, 557, 556, 555, 554, + 553, 552, 551, 550, 549, 548, 547, 546, 543, 542, + 541, 540, 539, 538, 537, 536, 522, 521, 520, 519, + 518, 517, 516, 515, 514, 513, 512, 511, 510, 509, + + 508, 507, 506, 505, 504, 503, 502, 501, 498, 497, + 496, 495, 494, 479, 478, 477, 476, 475, 474, 473, + 472, 471, 470, 469, 468, 467, 466, 463, 462, 461, + 460, 459, 458, 457, 456, 455, 454, 453, 452, 451, + 450, 449, 448, 447, 446, 445, 444, 425, 424, 423, + 422, 421, 420, 419, 418, 417, 416, 415, 414, 413, + 412, 411, 410, 409, 408, 407, 406, 405, 404, 403, + 402, 401, 400, 399, 398, 397, 396, 391, 390, 367, + 366, 363, 362, 361, 360, 359, 358, 357, 356, 355, + 354, 353, 352, 351, 350, 349, 348, 347, 346, 345, + + 344, 343, 342, 341, 340, 339, 338, 337, 336, 335, + 312, 311, 310, 309, 308, 305, 302, 301, 300, 299, + 298, 297, 296, 295, 294, 293, 292, 291, 290, 289, + 288, 287, 286, 285, 284, 283, 282, 281, 237, 227, + 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, + 241, 240, 239, 238, 235, 234, 233, 232, 229, 226, + 225, 222, 221, 220, 178, 176, 172, 156, 182, 181, + 180, 175, 171, 167, 161, 158, 155, 154, 111, 110, + 90, 59, 734, 41, 7, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734 + } ; + +static yyconst flex_int16_t yy_chk[1651] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 2, 5, 4, 6, 2, + 4, 5, 11, 6, 11, 12, 50, 50, 13, 12, + 13, 13, 14, 14, 14, 14, 14, 15, 106, 99, + 16, 15, 16, 16, 17, 742, 17, 17, 19, 19, + + 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, + 106, 23, 23, 23, 24, 24, 24, 99, 21, 25, + 25, 25, 26, 26, 26, 731, 46, 20, 46, 27, + 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, + 30, 729, 16, 31, 31, 31, 32, 32, 32, 97, + 23, 38, 22, 23, 26, 38, 23, 107, 97, 24, + 33, 33, 33, 25, 26, 162, 728, 29, 34, 34, + 34, 107, 25, 27, 36, 36, 36, 28, 35, 35, + 35, 150, 162, 48, 30, 32, 32, 48, 32, 51, + 31, 51, 51, 31, 150, 32, 33, 727, 52, 33, + + 52, 53, 53, 53, 55, 451, 55, 33, 36, 60, + 60, 60, 34, 95, 33, 63, 63, 63, 35, 451, + 61, 61, 61, 35, 39, 39, 39, 39, 39, 39, + 39, 113, 39, 113, 725, 39, 39, 61, 39, 39, + 95, 39, 39, 39, 39, 39, 54, 724, 54, 54, + 54, 62, 62, 62, 184, 184, 54, 64, 64, 64, + 63, 65, 65, 65, 66, 66, 66, 67, 67, 67, + 245, 245, 62, 68, 68, 68, 69, 69, 69, 70, + 70, 70, 54, 71, 71, 71, 72, 72, 72, 64, + 73, 73, 73, 65, 74, 74, 74, 75, 75, 75, + + 76, 76, 76, 77, 77, 77, 149, 723, 66, 78, + 78, 78, 79, 79, 79, 149, 721, 67, 80, 80, + 80, 70, 69, 68, 81, 81, 81, 717, 71, 82, + 82, 82, 100, 75, 73, 72, 77, 74, 100, 100, + 76, 83, 83, 83, 168, 76, 84, 84, 84, 168, + 79, 85, 85, 85, 78, 716, 81, 86, 86, 86, + 87, 87, 87, 80, 88, 88, 88, 89, 89, 89, + 91, 81, 82, 102, 91, 104, 714, 102, 104, 157, + 104, 157, 112, 112, 713, 243, 102, 712, 83, 112, + 114, 114, 243, 114, 114, 114, 84, 85, 711, 117, + + 117, 117, 252, 252, 88, 86, 89, 87, 92, 502, + 92, 92, 710, 502, 92, 112, 117, 92, 92, 709, + 92, 92, 542, 542, 92, 92, 118, 118, 118, 119, + 119, 119, 120, 120, 120, 121, 121, 121, 122, 122, + 122, 123, 123, 123, 124, 124, 124, 125, 125, 125, + 126, 126, 126, 127, 127, 127, 708, 118, 128, 128, + 128, 129, 129, 129, 130, 130, 130, 131, 131, 131, + 707, 119, 160, 310, 123, 132, 132, 132, 310, 120, + 133, 133, 133, 121, 160, 127, 122, 124, 706, 125, + 134, 134, 134, 135, 135, 135, 704, 703, 126, 129, + + 130, 136, 136, 136, 137, 137, 137, 138, 138, 138, + 139, 139, 139, 702, 131, 140, 140, 140, 141, 141, + 141, 142, 142, 142, 700, 411, 132, 699, 698, 133, + 411, 697, 134, 143, 143, 143, 144, 144, 144, 696, + 135, 145, 145, 145, 694, 136, 146, 146, 146, 693, + 147, 147, 147, 137, 183, 183, 138, 142, 183, 183, + 139, 140, 185, 141, 185, 185, 185, 692, 186, 143, + 186, 186, 186, 187, 187, 187, 144, 188, 188, 188, + 691, 146, 147, 188, 189, 189, 189, 190, 190, 190, + 145, 191, 191, 191, 192, 192, 192, 690, 188, 188, + + 188, 188, 193, 193, 193, 194, 194, 194, 195, 195, + 195, 196, 196, 196, 197, 197, 197, 198, 198, 198, + 199, 199, 199, 200, 200, 200, 201, 201, 201, 202, + 202, 202, 203, 203, 203, 204, 204, 204, 689, 191, + 206, 206, 206, 192, 193, 688, 687, 196, 195, 205, + 205, 205, 207, 207, 207, 200, 686, 685, 201, 208, + 208, 208, 684, 197, 682, 202, 681, 337, 199, 209, + 209, 209, 203, 210, 210, 210, 211, 211, 211, 680, + 679, 205, 337, 337, 204, 205, 678, 337, 206, 212, + 212, 212, 207, 208, 213, 213, 213, 214, 214, 214, + + 215, 215, 215, 216, 216, 216, 677, 253, 253, 253, + 676, 209, 254, 254, 254, 255, 255, 255, 675, 256, + 256, 256, 674, 673, 212, 210, 253, 257, 257, 257, + 671, 670, 213, 669, 668, 255, 258, 258, 258, 666, + 665, 257, 254, 664, 257, 663, 662, 214, 256, 661, + 216, 259, 259, 259, 260, 260, 260, 261, 261, 261, + 262, 262, 262, 263, 263, 263, 264, 264, 264, 265, + 265, 265, 266, 266, 266, 267, 267, 267, 660, 268, + 268, 268, 269, 269, 269, 270, 270, 270, 659, 658, + 260, 261, 657, 271, 271, 271, 272, 272, 272, 273, + + 273, 273, 274, 274, 274, 275, 275, 275, 654, 267, + 653, 652, 264, 268, 276, 276, 276, 651, 265, 650, + 649, 266, 277, 277, 277, 648, 647, 270, 271, 278, + 278, 278, 646, 273, 279, 279, 279, 275, 280, 280, + 280, 313, 313, 313, 645, 315, 315, 315, 316, 316, + 316, 274, 315, 644, 643, 314, 314, 314, 277, 319, + 319, 319, 642, 314, 314, 640, 279, 639, 316, 280, + 638, 313, 314, 278, 637, 314, 317, 317, 317, 314, + 314, 318, 318, 318, 320, 320, 320, 636, 317, 321, + 321, 321, 322, 322, 322, 323, 323, 323, 324, 324, + + 324, 318, 325, 325, 325, 326, 326, 326, 327, 327, + 327, 328, 328, 328, 329, 329, 329, 330, 330, 330, + 635, 321, 634, 633, 320, 331, 331, 331, 332, 332, + 332, 323, 333, 333, 333, 632, 631, 322, 334, 334, + 334, 368, 368, 368, 369, 369, 369, 324, 630, 629, + 369, 628, 627, 326, 370, 370, 370, 371, 371, 371, + 626, 625, 331, 624, 332, 368, 330, 620, 370, 619, + 618, 371, 617, 616, 333, 372, 372, 372, 373, 373, + 373, 615, 372, 374, 374, 374, 614, 334, 375, 375, + 375, 376, 376, 376, 613, 373, 612, 610, 376, 377, + + 377, 377, 608, 375, 378, 378, 378, 607, 606, 374, + 379, 379, 379, 374, 380, 380, 380, 605, 377, 381, + 381, 381, 382, 382, 382, 383, 383, 383, 604, 378, + 384, 384, 384, 385, 385, 385, 386, 386, 386, 387, + 387, 387, 388, 388, 388, 389, 389, 389, 380, 603, + 426, 426, 426, 427, 427, 427, 428, 428, 428, 379, + 602, 383, 595, 594, 428, 592, 381, 426, 591, 385, + 427, 429, 429, 429, 387, 430, 430, 430, 431, 431, + 431, 432, 432, 432, 433, 433, 433, 434, 434, 434, + 430, 590, 431, 589, 588, 429, 587, 434, 435, 435, + + 435, 583, 432, 582, 581, 433, 436, 436, 436, 437, + 437, 437, 436, 435, 438, 438, 438, 439, 439, 439, + 440, 440, 440, 441, 441, 441, 442, 442, 442, 443, + 443, 443, 480, 480, 480, 580, 579, 481, 481, 481, + 437, 482, 482, 482, 483, 483, 483, 484, 484, 484, + 578, 577, 480, 439, 481, 576, 575, 484, 441, 485, + 485, 485, 486, 486, 486, 574, 482, 485, 573, 483, + 487, 487, 487, 488, 488, 488, 489, 489, 489, 486, + 487, 488, 490, 490, 490, 491, 491, 491, 492, 492, + 492, 561, 560, 491, 491, 493, 493, 493, 559, 490, + + 523, 523, 523, 524, 524, 524, 558, 489, 556, 524, + 525, 525, 525, 526, 526, 526, 527, 527, 527, 528, + 528, 528, 555, 554, 527, 523, 529, 529, 529, 528, + 530, 530, 530, 531, 531, 531, 529, 532, 532, 532, + 533, 533, 533, 552, 551, 532, 532, 534, 534, 534, + 535, 535, 535, 562, 562, 562, 550, 549, 531, 563, + 563, 563, 548, 547, 535, 546, 563, 534, 564, 564, + 564, 565, 565, 565, 566, 566, 566, 567, 567, 567, + 568, 568, 568, 569, 569, 569, 570, 570, 570, 571, + 571, 571, 545, 564, 596, 596, 596, 569, 544, 541, + + 568, 597, 597, 597, 540, 570, 598, 598, 598, 539, + 538, 596, 537, 571, 599, 599, 599, 600, 600, 600, + 601, 601, 601, 536, 522, 598, 621, 621, 621, 622, + 622, 622, 623, 623, 623, 520, 621, 517, 599, 641, + 641, 641, 735, 735, 735, 735, 736, 736, 736, 736, + 737, 737, 737, 737, 738, 738, 738, 738, 739, 739, + 740, 740, 741, 516, 741, 741, 515, 514, 513, 512, + 511, 510, 509, 508, 507, 506, 504, 503, 501, 500, + 499, 498, 497, 496, 495, 494, 479, 478, 477, 476, + 475, 473, 472, 471, 470, 469, 468, 467, 466, 465, + + 464, 463, 462, 461, 457, 456, 454, 453, 449, 448, + 447, 446, 445, 425, 424, 423, 422, 421, 420, 419, + 418, 417, 416, 415, 414, 413, 412, 410, 409, 408, + 407, 406, 404, 403, 402, 401, 400, 399, 398, 397, + 396, 395, 394, 393, 392, 391, 390, 367, 366, 365, + 364, 363, 362, 361, 360, 359, 358, 357, 356, 355, + 354, 353, 352, 351, 350, 349, 348, 347, 346, 345, + 344, 343, 342, 341, 340, 339, 338, 336, 335, 312, + 311, 309, 308, 307, 306, 305, 304, 303, 302, 301, + 300, 299, 298, 297, 296, 295, 294, 293, 292, 291, + + 290, 289, 288, 287, 286, 285, 284, 283, 282, 281, + 250, 249, 248, 247, 246, 244, 242, 241, 240, 239, + 238, 237, 236, 235, 234, 233, 232, 231, 230, 229, + 228, 227, 224, 223, 222, 221, 220, 219, 218, 217, + 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, + 172, 171, 170, 169, 167, 166, 165, 163, 161, 159, + 158, 156, 155, 154, 153, 152, 151, 148, 110, 109, + 108, 105, 103, 101, 98, 96, 94, 93, 45, 42, + 37, 18, 7, 3, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734, + 734, 734, 734, 734, 734, 734, 734, 734, 734, 734 + } ; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "lexer.l" +/* + * The SIP lexer. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +#line 17 "lexer.l" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "sip.h" +#include "parser.h" + + +#ifndef FLEX_SCANNER +#error "Only flex is supported at the moment" +#endif + + +#define YY_FATAL_ERROR(s) fatallex(s) + +#define MAX_INCLUDE_DEPTH 10 +#define MAX_CODE_LINE_LENGTH 1000 + + +static struct inputFile { + int lineno; /* The line number. */ + YY_BUFFER_STATE bs; /* The flex buffer state handle. */ + char *name; /* The file name. */ + char *cwd; /* The path part of the file name. */ + parserContext pc; /* The parser context. */ +} inputFileStack[MAX_INCLUDE_DEPTH]; + +static int currentFile = -1; /* Index of the current input file. */ +static char codeLine[MAX_CODE_LINE_LENGTH + 2]; /* The current code line. */ +static int codeIdx = -1; /* Index of next code character. */ + +static FILE *openFile(char *); +static void fatallex(char *); + + +#line 1113 "lexer.c.tmp" + +#define INITIAL 0 +#define code 1 +#define ccomment 2 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ + +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +static int yy_init_globals (void ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + + static void yyunput (int c,char *buf_ptr ); + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#ifdef __cplusplus +static int yyinput (void ); +#else +static int input (void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + size_t n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(yyin); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + if ( yyleng > 0 ) \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = \ + (yytext[yyleng - 1] == '\n'); \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + register yy_state_type yy_current_state; + register char *yy_cp, *yy_bp; + register int yy_act; + +#line 56 "lexer.l" + + +#line 1274 "lexer.c.tmp" + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); + yy_current_state += YY_AT_BOL(); +yy_match: + do + { + register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 735 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 1585 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +YY_RULE_SETUP +#line 58 "lexer.l" +{return TK_OPTIONS;} + YY_BREAK +case 2: +YY_RULE_SETUP +#line 59 "lexer.l" +{return TK_NOEMITTERS;} + YY_BREAK +case 3: +YY_RULE_SETUP +#line 60 "lexer.l" +{return TK_INCLUDE;} + YY_BREAK +case 4: +YY_RULE_SETUP +#line 61 "lexer.l" +{return TK_OPTINCLUDE;} + YY_BREAK +case 5: +YY_RULE_SETUP +#line 62 "lexer.l" +{return TK_IMPORT;} + YY_BREAK +case 6: +YY_RULE_SETUP +#line 63 "lexer.l" +{return TK_MODULE;} + YY_BREAK +case 7: +YY_RULE_SETUP +#line 64 "lexer.l" +{return TK_CMODULE;} + YY_BREAK +case 8: +YY_RULE_SETUP +#line 65 "lexer.l" +{return TK_TIMELINE;} + YY_BREAK +case 9: +YY_RULE_SETUP +#line 66 "lexer.l" +{return TK_PLATFORMS;} + YY_BREAK +case 10: +YY_RULE_SETUP +#line 67 "lexer.l" +{return TK_FEATURE;} + YY_BREAK +case 11: +YY_RULE_SETUP +#line 68 "lexer.l" +{return TK_LICENSE;} + YY_BREAK +case 12: +YY_RULE_SETUP +#line 69 "lexer.l" +{return TK_MAPPEDTYPE;} + YY_BREAK +case 13: +YY_RULE_SETUP +#line 70 "lexer.l" +{return TK_EXCEPTION;} + YY_BREAK +case 14: +YY_RULE_SETUP +#line 71 "lexer.l" +{return TK_IF;} + YY_BREAK +case 15: +YY_RULE_SETUP +#line 72 "lexer.l" +{return TK_END;} + YY_BREAK +case 16: +YY_RULE_SETUP +#line 73 "lexer.l" +{return TK_CLASS;} + YY_BREAK +case 17: +YY_RULE_SETUP +#line 74 "lexer.l" +{return TK_STRUCT;} + YY_BREAK +case 18: +YY_RULE_SETUP +#line 75 "lexer.l" +{return TK_PUBLIC;} + YY_BREAK +case 19: +YY_RULE_SETUP +#line 76 "lexer.l" +{return TK_PROTECTED;} + YY_BREAK +case 20: +YY_RULE_SETUP +#line 77 "lexer.l" +{return TK_PRIVATE;} + YY_BREAK +case 21: +YY_RULE_SETUP +#line 78 "lexer.l" +{return TK_SIGNALS;} + YY_BREAK +case 22: +YY_RULE_SETUP +#line 79 "lexer.l" +{return TK_SLOTS;} + YY_BREAK +case 23: +YY_RULE_SETUP +#line 80 "lexer.l" +{return TK_CHAR;} + YY_BREAK +case 24: +YY_RULE_SETUP +#line 81 "lexer.l" +{return TK_WCHAR_T;} + YY_BREAK +case 25: +YY_RULE_SETUP +#line 82 "lexer.l" +{return TK_BOOL;} + YY_BREAK +case 26: +YY_RULE_SETUP +#line 83 "lexer.l" +{return TK_SHORT;} + YY_BREAK +case 27: +YY_RULE_SETUP +#line 84 "lexer.l" +{return TK_INT;} + YY_BREAK +case 28: +YY_RULE_SETUP +#line 85 "lexer.l" +{return TK_LONG;} + YY_BREAK +case 29: +YY_RULE_SETUP +#line 86 "lexer.l" +{return TK_FLOAT;} + YY_BREAK +case 30: +YY_RULE_SETUP +#line 87 "lexer.l" +{return TK_DOUBLE;} + YY_BREAK +case 31: +YY_RULE_SETUP +#line 88 "lexer.l" +{return TK_VOID;} + YY_BREAK +case 32: +YY_RULE_SETUP +#line 89 "lexer.l" +{return TK_VIRTUAL;} + YY_BREAK +case 33: +YY_RULE_SETUP +#line 90 "lexer.l" +{return TK_ENUM;} + YY_BREAK +case 34: +YY_RULE_SETUP +#line 91 "lexer.l" +{return TK_SIGNED;} + YY_BREAK +case 35: +YY_RULE_SETUP +#line 92 "lexer.l" +{return TK_UNSIGNED;} + YY_BREAK +case 36: +YY_RULE_SETUP +#line 93 "lexer.l" +{return TK_CONST;} + YY_BREAK +case 37: +YY_RULE_SETUP +#line 94 "lexer.l" +{return TK_STATIC;} + YY_BREAK +case 38: +YY_RULE_SETUP +#line 95 "lexer.l" +{return TK_TRUE;} + YY_BREAK +case 39: +YY_RULE_SETUP +#line 96 "lexer.l" +{return TK_FALSE;} + YY_BREAK +case 40: +YY_RULE_SETUP +#line 97 "lexer.l" +{return TK_NULL;} + YY_BREAK +case 41: +YY_RULE_SETUP +#line 98 "lexer.l" +{return TK_TYPEDEF;} + YY_BREAK +case 42: +YY_RULE_SETUP +#line 99 "lexer.l" +{return TK_NAMESPACE;} + YY_BREAK +case 43: +YY_RULE_SETUP +#line 100 "lexer.l" +{return TK_OPERATOR;} + YY_BREAK +case 44: +YY_RULE_SETUP +#line 101 "lexer.l" +{return TK_THROW;} + YY_BREAK +case 45: +YY_RULE_SETUP +#line 102 "lexer.l" +{return TK_EXPLICIT;} + YY_BREAK +case 46: +YY_RULE_SETUP +#line 103 "lexer.l" +{return TK_TEMPLATE;} + YY_BREAK +case 47: +YY_RULE_SETUP +#line 104 "lexer.l" +{return TK_SCOPE;} + YY_BREAK +case 48: +YY_RULE_SETUP +#line 105 "lexer.l" +{return TK_LOGICAL_OR;} + YY_BREAK +case 49: +YY_RULE_SETUP +#line 106 "lexer.l" +{return TK_PYOBJECT;} + YY_BREAK +case 50: +YY_RULE_SETUP +#line 107 "lexer.l" +{return TK_PYTUPLE;} + YY_BREAK +case 51: +YY_RULE_SETUP +#line 108 "lexer.l" +{return TK_PYLIST;} + YY_BREAK +case 52: +YY_RULE_SETUP +#line 109 "lexer.l" +{return TK_PYDICT;} + YY_BREAK +case 53: +YY_RULE_SETUP +#line 110 "lexer.l" +{return TK_PYCALLABLE;} + YY_BREAK +case 54: +YY_RULE_SETUP +#line 111 "lexer.l" +{return TK_PYSLICE;} + YY_BREAK +case 55: +YY_RULE_SETUP +#line 112 "lexer.l" +{return TK_PYTYPE;} + YY_BREAK +case 56: +YY_RULE_SETUP +#line 113 "lexer.l" +{return TK_SIPSIGNAL;} + YY_BREAK +case 57: +YY_RULE_SETUP +#line 114 "lexer.l" +{return TK_SIPSLOT;} + YY_BREAK +case 58: +YY_RULE_SETUP +#line 115 "lexer.l" +{return TK_SIPANYSLOT;} + YY_BREAK +case 59: +YY_RULE_SETUP +#line 116 "lexer.l" +{return TK_SIPRXCON;} + YY_BREAK +case 60: +YY_RULE_SETUP +#line 117 "lexer.l" +{return TK_SIPRXDIS;} + YY_BREAK +case 61: +YY_RULE_SETUP +#line 118 "lexer.l" +{return TK_SIPSLOTCON;} + YY_BREAK +case 62: +YY_RULE_SETUP +#line 119 "lexer.l" +{return TK_SIPSLOTDIS;} + YY_BREAK +case 63: +YY_RULE_SETUP +#line 120 "lexer.l" +{return TK_QOBJECT;} + YY_BREAK +case 64: +YY_RULE_SETUP +#line 121 "lexer.l" +{return TK_ELLIPSIS;} + YY_BREAK +case 65: +YY_RULE_SETUP +#line 124 "lexer.l" +{ /* Ignore whitespace. */ + ; +} + YY_BREAK +case 66: +/* rule 66 can match eol */ +YY_RULE_SETUP +#line 128 "lexer.l" +{ /* Maintain the line number. */ + ++inputFileStack[currentFile].lineno; + + if (codeIdx == 0) + { + BEGIN code; + } +} + YY_BREAK +case 67: +YY_RULE_SETUP +#line 137 "lexer.l" +{ /* Ignore C++ style comments. */ + ; +} + YY_BREAK +case 68: +YY_RULE_SETUP +#line 142 "lexer.l" +{ /* A signed decimal number. */ + yylval.number = strtol(yytext,NULL,0); + return TK_NUMBER; +} + YY_BREAK +case 69: +YY_RULE_SETUP +#line 148 "lexer.l" +{/* A floating point number. */ + yylval.real = strtod(yytext,NULL); + return TK_REAL; +} + YY_BREAK +case 70: +YY_RULE_SETUP +#line 154 "lexer.l" +{ /* An unsigned hexadecimal number. */ + yylval.number = strtol(yytext,NULL,16); + return TK_NUMBER; +} + YY_BREAK +case 71: +YY_RULE_SETUP +#line 160 "lexer.l" +{ /* An identifier name. */ + yylval.text = sipStrdup(yytext); + return TK_NAME; +} + YY_BREAK +case 72: +YY_RULE_SETUP +#line 166 "lexer.l" +{ /* A relative pathname. */ + yylval.text = sipStrdup(yytext); + return TK_PATHNAME; +} + YY_BREAK +case 73: +/* rule 73 can match eol */ +YY_RULE_SETUP +#line 172 "lexer.l" +{ /* A double-quoted string. */ + char *dp, *sp; + + /* Copy the string without the quotes. */ + + yylval.text = sipMalloc(strlen(yytext) + 1); + + dp = yylval.text; + sp = yytext; + + while (*sp != '\0') + { + if (*sp != '"') + *dp++ = *sp; + + ++sp; + } + + *dp = '\0'; + + return TK_STRING; +} + YY_BREAK +case 74: +/* rule 74 can match eol */ +YY_RULE_SETUP +#line 196 "lexer.l" +{ /* A single-quoted character. */ + if (strlen(yytext) != 3) + fatallex("Exactly one character expected between single quotes"); + + yylval.qchar = yytext[1]; + + return TK_QCHAR; +} + YY_BREAK +case 75: +YY_RULE_SETUP +#line 206 "lexer.l" +{ /* Ignore C-style comments. */ + BEGIN ccomment; +} + YY_BREAK +case 76: +/* rule 76 can match eol */ +YY_RULE_SETUP +#line 209 "lexer.l" +{ + ++inputFileStack[currentFile].lineno; +} + YY_BREAK +case 77: +YY_RULE_SETUP +#line 212 "lexer.l" +{ + BEGIN INITIAL; +} + YY_BREAK +case 78: +YY_RULE_SETUP +#line 215 "lexer.l" +{ + ; +} + YY_BREAK +case 79: +YY_RULE_SETUP +#line 220 "lexer.l" +{ /* The software license. */ + codeIdx = 0; + return TK_COPYING; +} + YY_BREAK +case 80: +YY_RULE_SETUP +#line 225 "lexer.l" +{ /* The start of a from-type code block. */ + codeIdx = 0; + return TK_FROMTYPE; +} + YY_BREAK +case 81: +YY_RULE_SETUP +#line 230 "lexer.l" +{ /* The start of a to-type code block. */ + codeIdx = 0; + return TK_TOTYPE; +} + YY_BREAK +case 82: +YY_RULE_SETUP +#line 235 "lexer.l" +{ /* The start of a to-sub-class code block. */ + codeIdx = 0; + return TK_TOSUBCLASS; +} + YY_BREAK +case 83: +YY_RULE_SETUP +#line 240 "lexer.l" +{ /* The start of an exported header code block. */ + codeIdx = 0; + return TK_EXPHEADERCODE; +} + YY_BREAK +case 84: +YY_RULE_SETUP +#line 245 "lexer.l" +{ /* The start of a module header code block. */ + codeIdx = 0; + return TK_MODHEADERCODE; +} + YY_BREAK +case 85: +YY_RULE_SETUP +#line 250 "lexer.l" +{ /* The start of a type header code block. */ + codeIdx = 0; + return TK_TYPEHEADERCODE; +} + YY_BREAK +case 86: +YY_RULE_SETUP +#line 255 "lexer.l" +{ /* The start of a pre-initialisation code block. */ + codeIdx = 0; + return TK_PREINITCODE; +} + YY_BREAK +case 87: +YY_RULE_SETUP +#line 260 "lexer.l" +{ /* The start of a post-initialisation code block. */ + codeIdx = 0; + return TK_POSTINITCODE; +} + YY_BREAK +case 88: +YY_RULE_SETUP +#line 265 "lexer.l" +{ /* The start of a unit code block. */ + codeIdx = 0; + return TK_UNITCODE; +} + YY_BREAK +case 89: +YY_RULE_SETUP +#line 270 "lexer.l" +{ /* The start of a module code block. */ + codeIdx = 0; + return TK_MODCODE; +} + YY_BREAK +case 90: +YY_RULE_SETUP +#line 275 "lexer.l" +{ /* The start of a type code block. */ + codeIdx = 0; + return TK_TYPECODE; +} + YY_BREAK +case 91: +YY_RULE_SETUP +#line 280 "lexer.l" +{ /* The start of a C++ method code block. */ + codeIdx = 0; + return TK_METHODCODE; +} + YY_BREAK +case 92: +YY_RULE_SETUP +#line 285 "lexer.l" +{ /* The start of a C++ virtual code block. */ + codeIdx = 0; + return TK_VIRTUALCATCHERCODE; +} + YY_BREAK +case 93: +YY_RULE_SETUP +#line 290 "lexer.l" +{ /* The start of a traverse code block. */ + codeIdx = 0; + return TK_TRAVERSECODE; +} + YY_BREAK +case 94: +YY_RULE_SETUP +#line 295 "lexer.l" +{ /* The start of a clear code block. */ + codeIdx = 0; + return TK_CLEARCODE; +} + YY_BREAK +case 95: +YY_RULE_SETUP +#line 300 "lexer.l" +{ /* The start of a read buffer code block. */ + codeIdx = 0; + return TK_READBUFFERCODE; +} + YY_BREAK +case 96: +YY_RULE_SETUP +#line 305 "lexer.l" +{ /* The start of a write buffer code block. */ + codeIdx = 0; + return TK_WRITEBUFFERCODE; +} + YY_BREAK +case 97: +YY_RULE_SETUP +#line 310 "lexer.l" +{ /* The start of a segment count code block. */ + codeIdx = 0; + return TK_SEGCOUNTCODE; +} + YY_BREAK +case 98: +YY_RULE_SETUP +#line 315 "lexer.l" +{ /* The start of a char buffer code block. */ + codeIdx = 0; + return TK_CHARBUFFERCODE; +} + YY_BREAK +case 99: +YY_RULE_SETUP +#line 320 "lexer.l" +{ /* The start of a pre-Python code block. */ + codeIdx = 0; + return TK_PREPYCODE; +} + YY_BREAK +case 100: +YY_RULE_SETUP +#line 325 "lexer.l" +{ /* The start of a raise exception code block. */ + codeIdx = 0; + return TK_RAISECODE; +} + YY_BREAK +case 101: +YY_RULE_SETUP +#line 330 "lexer.l" +{ /* The start of a documentation block. */ + codeIdx = 0; + return TK_DOC; +} + YY_BREAK +case 102: +YY_RULE_SETUP +#line 335 "lexer.l" +{ /* The start of an exported documentation block. */ + codeIdx = 0; + return TK_EXPORTEDDOC; +} + YY_BREAK +case 103: +YY_RULE_SETUP +#line 340 "lexer.l" +{ /* The start of a Makefile code block. */ + codeIdx = 0; + return TK_MAKEFILE; +} + YY_BREAK +case 104: +YY_RULE_SETUP +#line 345 "lexer.l" +{ /* The start of an access code block. */ + codeIdx = 0; + return TK_ACCESSCODE; +} + YY_BREAK +case 105: +YY_RULE_SETUP +#line 350 "lexer.l" +{ /* The start of a get code block. */ + codeIdx = 0; + return TK_GETCODE; +} + YY_BREAK +case 106: +YY_RULE_SETUP +#line 355 "lexer.l" +{ /* The start of a set code block. */ + codeIdx = 0; + return TK_SETCODE; +} + YY_BREAK +case 107: +YY_RULE_SETUP +#line 360 "lexer.l" +{ /* The end of a code block. */ + BEGIN INITIAL; + codeIdx = -1; + return TK_END; +} + YY_BREAK +case 108: +/* rule 108 can match eol */ +YY_RULE_SETUP +#line 366 "lexer.l" +{ /* The end of a code line . */ + struct inputFile *ifp; + + codeLine[codeIdx] = '\n'; + codeLine[codeIdx + 1] = '\0'; + codeIdx = 0; + + ifp = &inputFileStack[currentFile]; + + yylval.codeb = sipMalloc(sizeof (codeBlock)); + + yylval.codeb -> frag = sipStrdup(codeLine); + yylval.codeb -> linenr = ifp -> lineno++; + yylval.codeb -> filename = sipStrdup(ifp -> name); + yylval.codeb -> next = NULL; + + return TK_CODELINE; +} + YY_BREAK +case 109: +YY_RULE_SETUP +#line 385 "lexer.l" +{ /* The contents of a code line. */ + if (codeIdx == MAX_CODE_LINE_LENGTH) + fatallex("Line is too long"); + + codeLine[codeIdx++] = yytext[0]; +} + YY_BREAK +case 110: +YY_RULE_SETUP +#line 392 "lexer.l" +{ /* Anything else is returned as is. */ + return yytext[0]; +} + YY_BREAK +case 111: +YY_RULE_SETUP +#line 396 "lexer.l" +ECHO; + YY_BREAK +#line 2092 "lexer.c.tmp" +case YY_STATE_EOF(INITIAL): +case YY_STATE_EOF(code): +case YY_STATE_EOF(ccomment): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + register char *source = (yytext_ptr); + register int number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + int num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart(yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = (yy_start); + yy_current_state += YY_AT_BOL(); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 735 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + register int yy_is_jam; + register char *yy_cp = (yy_c_buf_p); + + register YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 735 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 734); + + return yy_is_jam ? 0 : yy_current_state; +} + + static void yyunput (int c, register char * yy_bp ) +{ + register char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register int number_to_move = (yy_n_chars) + 2; + register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + register char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + int offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart(yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = (c == '\n'); + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyrestart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_init_buffer(YY_CURRENT_BUFFER,input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void yy_load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer(b,file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yy_delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree((void *) b->yy_ch_buf ); + + yyfree((void *) b ); +} + +#ifndef _UNISTD_H /* assume unistd.h has isatty() for us */ +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __THROW /* this is a gnuism */ +extern int isatty (int ) __THROW; +#else +extern int isatty (int ); +#endif +#ifdef __cplusplus +} +#endif +#endif + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + yy_flush_buffer(b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yy_flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void yyensure_buffer_stack (void) +{ + int num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + int grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer(b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param str a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) +{ + + return yy_scan_bytes(yystr,strlen(yystr) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param bytes the byte buffer to scan + * @param len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, int _yybytes_len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = _yybytes_len + 2; + buf = (char *) yyalloc(n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < _yybytes_len; ++i ) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer(buf,n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yy_fatal_error (yyconst char* msg ) +{ + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int yyget_lineno (void) +{ + + return yylineno; +} + +/** Get the input stream. + * + */ +FILE *yyget_in (void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE *yyget_out (void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +int yyget_leng (void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char *yyget_text (void) +{ + return yytext; +} + +/** Set the current line number. + * @param line_number + * + */ +void yyset_lineno (int line_number ) +{ + + yylineno = line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void yyset_in (FILE * in_str ) +{ + yyin = in_str ; +} + +void yyset_out (FILE * out_str ) +{ + yyout = out_str ; +} + +int yyget_debug (void) +{ + return yy_flex_debug; +} + +void yyset_debug (int bdebug ) +{ + yy_flex_debug = bdebug ; +} + +static int yy_init_globals (void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + (yy_buffer_stack) = 0; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = (char *) 0; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = (FILE *) 0; + yyout = (FILE *) 0; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int yylex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals( ); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 396 "lexer.l" + + + +/* + * Hook into EOF handling. Return 0 if there is more to process. + */ + +int yywrap() +{ + char *cwd; + struct inputFile *ifp; + + if ((cwd = inputFileStack[currentFile].cwd) != NULL) + free(cwd); + + ifp = &inputFileStack[currentFile--]; + + /* Tell the parser if this is the end of a file. */ + + parserEOF(ifp -> name,&ifp -> pc); + + /* Tidy up this file. */ + + fclose(yyin); + free(ifp -> name); + + /* See if this was the original file. */ + + if (currentFile < 0) + return 1; + + yy_delete_buffer(YY_CURRENT_BUFFER); + yy_switch_to_buffer(ifp -> bs); + + return 0; +} + + +/* + * Set up an input file to be read by the lexer, opening it if necessary. + */ +void setInputFile(FILE *fp,char *name,parserContext *pc,int optional) +{ + char *fullname = NULL; + + if (currentFile >= MAX_INCLUDE_DEPTH - 1) + fatal("Too many nested %%Include, %%OptionalInclude or %%Import statements\n"); + + if (fp != NULL || (fp = openFile(name)) != NULL) + fullname = sipStrdup(name); + else + { + char *cwd; + + /* Try the directory that contains the current file. */ + if (currentFile >= 0 && (cwd = inputFileStack[currentFile].cwd) != NULL) + { + fullname = concat(cwd,"/",name,NULL); + + if ((fp = openFile(fullname)) == NULL) + { + free(fullname); + fullname = NULL; + } + } + } + + /* Try the include path if we haven't found anything yet. */ + if (fullname == NULL) + { + stringList *sl; + + fullname = NULL; + + for (sl = includeDirList; sl != NULL; sl = sl -> next) + { + if (fullname != NULL) + free(fullname); + + fullname = concat(sl -> s,"/",name,NULL); + + if ((fp = openFile(fullname)) != NULL) + break; + } + + if (fp == NULL && !optional) + fatal("Unable to find file \"%s\"\n",name); + } + + if (fp != NULL) + { + char *cwd; + + yyin = fp; + + ++currentFile; + + /* + * Remember the directory containing the new file and make it + * "current". + */ + if ((cwd = strchr(fullname,'/')) != NULL) + { + cwd = sipStrdup(fullname); + *strrchr(cwd,'/') = '\0'; + } + + inputFileStack[currentFile].lineno = 1; + inputFileStack[currentFile].name = fullname; + inputFileStack[currentFile].pc = *pc; + inputFileStack[currentFile].cwd = cwd; + + if (currentFile > 0) + { + inputFileStack[currentFile].bs = YY_CURRENT_BUFFER; + yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE)); + } + } +} + + +/* + * Open a file for reading or return NULL if it doesn't exist. Any other error + * is fatal. + */ +static FILE *openFile(char *name) +{ + FILE *fp; + + if ((fp = fopen(name,"r")) == NULL && errno != ENOENT) + fatal("Error in opening file %s\n",name); + + return fp; +} + + +/* + * Handle fatal yacc errors. + */ +void yyerror(char *s) +{ + if (currentFile < 0) + fatal("%s\n", s); + + fatal("%s:%d: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} + + +/* + * Handle warnings while parsing. + */ +void yywarning(char *s) +{ + warning("%s:%d: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} + + +/* + * Handle fatal lex errors. + */ +static void fatallex(char *s) +{ + fatal("%s:%d: Lexical analyser error: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} + diff --git a/python/sip/sipgen/lexer.l b/python/sip/sipgen/lexer.l new file mode 100644 index 00000000..ef234193 --- /dev/null +++ b/python/sip/sipgen/lexer.l @@ -0,0 +1,567 @@ +/* + * The SIP lexer. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +%{ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "sip.h" +#include "parser.h" + + +#ifndef FLEX_SCANNER +#error "Only flex is supported at the moment" +#endif + + +#define YY_FATAL_ERROR(s) fatallex(s) + +#define MAX_INCLUDE_DEPTH 10 +#define MAX_CODE_LINE_LENGTH 1000 + + +static struct inputFile { + int lineno; /* The line number. */ + YY_BUFFER_STATE bs; /* The flex buffer state handle. */ + char *name; /* The file name. */ + char *cwd; /* The path part of the file name. */ + parserContext pc; /* The parser context. */ +} inputFileStack[MAX_INCLUDE_DEPTH]; + +static int currentFile = -1; /* Index of the current input file. */ +static char codeLine[MAX_CODE_LINE_LENGTH + 2]; /* The current code line. */ +static int codeIdx = -1; /* Index of next code character. */ + +static FILE *openFile(char *); +static void fatallex(char *); +%} + +%x code +%x ccomment + +%% + +^[ \t]*%SIPOptions {return TK_OPTIONS;} +^[ \t]*%SIPNoEmitters {return TK_NOEMITTERS;} +^[ \t]*%Include {return TK_INCLUDE;} +^[ \t]*%OptionalInclude {return TK_OPTINCLUDE;} +^[ \t]*%Import {return TK_IMPORT;} +^[ \t]*%Module {return TK_MODULE;} +^[ \t]*%CModule {return TK_CMODULE;} +^[ \t]*%Timeline {return TK_TIMELINE;} +^[ \t]*%Platforms {return TK_PLATFORMS;} +^[ \t]*%Feature {return TK_FEATURE;} +^[ \t]*%License {return TK_LICENSE;} +^[ \t]*%MappedType {return TK_MAPPEDTYPE;} +^[ \t]*%Exception {return TK_EXCEPTION;} +^[ \t]*%If {return TK_IF;} +<INITIAL>^[ \t]*%End {return TK_END;} +class {return TK_CLASS;} +struct {return TK_STRUCT;} +public {return TK_PUBLIC;} +protected {return TK_PROTECTED;} +private {return TK_PRIVATE;} +signals {return TK_SIGNALS;} +slots {return TK_SLOTS;} +char {return TK_CHAR;} +wchar_t {return TK_WCHAR_T;} +bool {return TK_BOOL;} +short {return TK_SHORT;} +int {return TK_INT;} +long {return TK_LONG;} +float {return TK_FLOAT;} +double {return TK_DOUBLE;} +void {return TK_VOID;} +virtual {return TK_VIRTUAL;} +enum {return TK_ENUM;} +signed {return TK_SIGNED;} +unsigned {return TK_UNSIGNED;} +const {return TK_CONST;} +static {return TK_STATIC;} +true {return TK_TRUE;} +false {return TK_FALSE;} +NULL {return TK_NULL;} +typedef {return TK_TYPEDEF;} +namespace {return TK_NAMESPACE;} +operator {return TK_OPERATOR;} +throw {return TK_THROW;} +explicit {return TK_EXPLICIT;} +template {return TK_TEMPLATE;} +:: {return TK_SCOPE;} +\|\| {return TK_LOGICAL_OR;} +SIP_PYOBJECT {return TK_PYOBJECT;} +SIP_PYTUPLE {return TK_PYTUPLE;} +SIP_PYLIST {return TK_PYLIST;} +SIP_PYDICT {return TK_PYDICT;} +SIP_PYCALLABLE {return TK_PYCALLABLE;} +SIP_PYSLICE {return TK_PYSLICE;} +SIP_PYTYPE {return TK_PYTYPE;} +SIP_SIGNAL {return TK_SIPSIGNAL;} +SIP_SLOT {return TK_SIPSLOT;} +SIP_ANYSLOT {return TK_SIPANYSLOT;} +SIP_RXOBJ_CON {return TK_SIPRXCON;} +SIP_RXOBJ_DIS {return TK_SIPRXDIS;} +SIP_SLOT_CON {return TK_SIPSLOTCON;} +SIP_SLOT_DIS {return TK_SIPSLOTDIS;} +SIP_QOBJECT {return TK_QOBJECT;} +\.\.\. {return TK_ELLIPSIS;} + + +[ \t\r] { /* Ignore whitespace. */ + ; +} + +\n { /* Maintain the line number. */ + ++inputFileStack[currentFile].lineno; + + if (codeIdx == 0) + { + BEGIN code; + } +} + +\/\/.* { /* Ignore C++ style comments. */ + ; +} + + +-?[0-9]+ { /* A signed decimal number. */ + yylval.number = strtol(yytext,NULL,0); + return TK_NUMBER; +} + + +-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {/* A floating point number. */ + yylval.real = strtod(yytext,NULL); + return TK_REAL; +} + + +0x[0-9a-fA-F]+ { /* An unsigned hexadecimal number. */ + yylval.number = strtol(yytext,NULL,16); + return TK_NUMBER; +} + + +[_A-Za-z][_A-Za-z0-9]* { /* An identifier name. */ + yylval.text = sipStrdup(yytext); + return TK_NAME; +} + + +[._A-Za-z][._/A-Za-z0-9\-]*[._A-Za-z0-9] { /* A relative pathname. */ + yylval.text = sipStrdup(yytext); + return TK_PATHNAME; +} + + +\"[^"\n]*["\n] { /* A double-quoted string. */ + char *dp, *sp; + + /* Copy the string without the quotes. */ + + yylval.text = sipMalloc(strlen(yytext) + 1); + + dp = yylval.text; + sp = yytext; + + while (*sp != '\0') + { + if (*sp != '"') + *dp++ = *sp; + + ++sp; + } + + *dp = '\0'; + + return TK_STRING; +} + + +\'[^'\n]*['\n] { /* A single-quoted character. */ + if (strlen(yytext) != 3) + fatallex("Exactly one character expected between single quotes"); + + yylval.qchar = yytext[1]; + + return TK_QCHAR; +} + + +\/\* { /* Ignore C-style comments. */ + BEGIN ccomment; +} +<ccomment>\n { + ++inputFileStack[currentFile].lineno; +} +<ccomment>\*\/ { + BEGIN INITIAL; +} +<ccomment>. { + ; +} + + +^%Copying { /* The software license. */ + codeIdx = 0; + return TK_COPYING; +} + +^%ConvertFromTypeCode { /* The start of a from-type code block. */ + codeIdx = 0; + return TK_FROMTYPE; +} + +^%ConvertToTypeCode { /* The start of a to-type code block. */ + codeIdx = 0; + return TK_TOTYPE; +} + +^%ConvertToSubClassCode { /* The start of a to-sub-class code block. */ + codeIdx = 0; + return TK_TOSUBCLASS; +} + +^%ExportedHeaderCode { /* The start of an exported header code block. */ + codeIdx = 0; + return TK_EXPHEADERCODE; +} + +^%ModuleHeaderCode { /* The start of a module header code block. */ + codeIdx = 0; + return TK_MODHEADERCODE; +} + +^%TypeHeaderCode { /* The start of a type header code block. */ + codeIdx = 0; + return TK_TYPEHEADERCODE; +} + +^%PreInitialisationCode { /* The start of a pre-initialisation code block. */ + codeIdx = 0; + return TK_PREINITCODE; +} + +^%PostInitialisationCode { /* The start of a post-initialisation code block. */ + codeIdx = 0; + return TK_POSTINITCODE; +} + +^%UnitCode { /* The start of a unit code block. */ + codeIdx = 0; + return TK_UNITCODE; +} + +^%ModuleCode { /* The start of a module code block. */ + codeIdx = 0; + return TK_MODCODE; +} + +^%TypeCode { /* The start of a type code block. */ + codeIdx = 0; + return TK_TYPECODE; +} + +^%MethodCode { /* The start of a C++ method code block. */ + codeIdx = 0; + return TK_METHODCODE; +} + +^%VirtualCatcherCode { /* The start of a C++ virtual code block. */ + codeIdx = 0; + return TK_VIRTUALCATCHERCODE; +} + +^%GCTraverseCode { /* The start of a traverse code block. */ + codeIdx = 0; + return TK_TRAVERSECODE; +} + +^%GCClearCode { /* The start of a clear code block. */ + codeIdx = 0; + return TK_CLEARCODE; +} + +^%BIGetReadBufferCode { /* The start of a read buffer code block. */ + codeIdx = 0; + return TK_READBUFFERCODE; +} + +^%BIGetWriteBufferCode { /* The start of a write buffer code block. */ + codeIdx = 0; + return TK_WRITEBUFFERCODE; +} + +^%BIGetSegCountCode { /* The start of a segment count code block. */ + codeIdx = 0; + return TK_SEGCOUNTCODE; +} + +^%BIGetCharBufferCode { /* The start of a char buffer code block. */ + codeIdx = 0; + return TK_CHARBUFFERCODE; +} + +^%PrePythonCode { /* The start of a pre-Python code block. */ + codeIdx = 0; + return TK_PREPYCODE; +} + +^%RaiseCode { /* The start of a raise exception code block. */ + codeIdx = 0; + return TK_RAISECODE; +} + +^%Doc { /* The start of a documentation block. */ + codeIdx = 0; + return TK_DOC; +} + +^%ExportedDoc { /* The start of an exported documentation block. */ + codeIdx = 0; + return TK_EXPORTEDDOC; +} + +^%Makefile { /* The start of a Makefile code block. */ + codeIdx = 0; + return TK_MAKEFILE; +} + +^%AccessCode { /* The start of an access code block. */ + codeIdx = 0; + return TK_ACCESSCODE; +} + +^%GetCode { /* The start of a get code block. */ + codeIdx = 0; + return TK_GETCODE; +} + +^%SetCode { /* The start of a set code block. */ + codeIdx = 0; + return TK_SETCODE; +} + +<code>^%End { /* The end of a code block. */ + BEGIN INITIAL; + codeIdx = -1; + return TK_END; +} + +<code>\n { /* The end of a code line . */ + struct inputFile *ifp; + + codeLine[codeIdx] = '\n'; + codeLine[codeIdx + 1] = '\0'; + codeIdx = 0; + + ifp = &inputFileStack[currentFile]; + + yylval.codeb = sipMalloc(sizeof (codeBlock)); + + yylval.codeb -> frag = sipStrdup(codeLine); + yylval.codeb -> linenr = ifp -> lineno++; + yylval.codeb -> filename = sipStrdup(ifp -> name); + yylval.codeb -> next = NULL; + + return TK_CODELINE; +} + +<code>. { /* The contents of a code line. */ + if (codeIdx == MAX_CODE_LINE_LENGTH) + fatallex("Line is too long"); + + codeLine[codeIdx++] = yytext[0]; +} + +. { /* Anything else is returned as is. */ + return yytext[0]; +} + +%% + +/* + * Hook into EOF handling. Return 0 if there is more to process. + */ + +int yywrap() +{ + char *cwd; + struct inputFile *ifp; + + if ((cwd = inputFileStack[currentFile].cwd) != NULL) + free(cwd); + + ifp = &inputFileStack[currentFile--]; + + /* Tell the parser if this is the end of a file. */ + + parserEOF(ifp -> name,&ifp -> pc); + + /* Tidy up this file. */ + + fclose(yyin); + free(ifp -> name); + + /* See if this was the original file. */ + + if (currentFile < 0) + return 1; + + yy_delete_buffer(YY_CURRENT_BUFFER); + yy_switch_to_buffer(ifp -> bs); + + return 0; +} + + +/* + * Set up an input file to be read by the lexer, opening it if necessary. + */ +void setInputFile(FILE *fp,char *name,parserContext *pc,int optional) +{ + char *fullname = NULL; + + if (currentFile >= MAX_INCLUDE_DEPTH - 1) + fatal("Too many nested %%Include, %%OptionalInclude or %%Import statements\n"); + + if (fp != NULL || (fp = openFile(name)) != NULL) + fullname = sipStrdup(name); + else + { + char *cwd; + + /* Try the directory that contains the current file. */ + if (currentFile >= 0 && (cwd = inputFileStack[currentFile].cwd) != NULL) + { + fullname = concat(cwd,"/",name,NULL); + + if ((fp = openFile(fullname)) == NULL) + { + free(fullname); + fullname = NULL; + } + } + } + + /* Try the include path if we haven't found anything yet. */ + if (fullname == NULL) + { + stringList *sl; + + fullname = NULL; + + for (sl = includeDirList; sl != NULL; sl = sl -> next) + { + if (fullname != NULL) + free(fullname); + + fullname = concat(sl -> s,"/",name,NULL); + + if ((fp = openFile(fullname)) != NULL) + break; + } + + if (fp == NULL && !optional) + fatal("Unable to find file \"%s\"\n",name); + } + + if (fp != NULL) + { + char *cwd; + + yyin = fp; + + ++currentFile; + + /* + * Remember the directory containing the new file and make it + * "current". + */ + if ((cwd = strchr(fullname,'/')) != NULL) + { + cwd = sipStrdup(fullname); + *strrchr(cwd,'/') = '\0'; + } + + inputFileStack[currentFile].lineno = 1; + inputFileStack[currentFile].name = fullname; + inputFileStack[currentFile].pc = *pc; + inputFileStack[currentFile].cwd = cwd; + + if (currentFile > 0) + { + inputFileStack[currentFile].bs = YY_CURRENT_BUFFER; + yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE)); + } + } +} + + +/* + * Open a file for reading or return NULL if it doesn't exist. Any other error + * is fatal. + */ +static FILE *openFile(char *name) +{ + FILE *fp; + + if ((fp = fopen(name,"r")) == NULL && errno != ENOENT) + fatal("Error in opening file %s\n",name); + + return fp; +} + + +/* + * Handle fatal yacc errors. + */ +void yyerror(char *s) +{ + if (currentFile < 0) + fatal("%s\n", s); + + fatal("%s:%d: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} + + +/* + * Handle warnings while parsing. + */ +void yywarning(char *s) +{ + warning("%s:%d: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} + + +/* + * Handle fatal lex errors. + */ +static void fatallex(char *s) +{ + fatal("%s:%d: Lexical analyser error: %s\n", + inputFileStack[currentFile].name, + inputFileStack[currentFile].lineno, + s); +} diff --git a/python/sip/sipgen/main.c b/python/sip/sipgen/main.c new file mode 100644 index 00000000..ce1e859b --- /dev/null +++ b/python/sip/sipgen/main.c @@ -0,0 +1,484 @@ +/* + * The main module for SIP. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <ctype.h> + +#include "sip.h" + + +#ifndef PACKAGE +#define PACKAGE "sip" +#endif + +#define VERSION "4.6 (4.6)" + + +/* Global variables - see sip.h for their meaning. */ +char *sipVersion; +stringList *includeDirList; + +static char *sipPackage = PACKAGE; +static int warnings = FALSE; + + +static void help(void); +static void version(void); +static void usage(void); +static char parseopt(int,char **,char *,char **,int *,char **); +static int parseInt(char *,char); + + +int main(int argc,char **argv) +{ + char *filename, *docFile, *codeDir, *srcSuffix, *flagFile; + char arg, *optarg, *buildFile, *apiFile, *xmlFile; + int optnr, exceptions, tracing, releaseGIL, parts; + FILE *file; + sipSpec spec; + stringList *versions, *xfeatures; + + /* Initialise. */ + sipVersion = VERSION; + includeDirList = NULL; + versions = NULL; + xfeatures = NULL; + buildFile = NULL; + codeDir = NULL; + docFile = NULL; + srcSuffix = NULL; + flagFile = NULL; + apiFile = NULL; + xmlFile = NULL; + exceptions = FALSE; + tracing = FALSE; + releaseGIL = FALSE; + parts = 0; + + /* Parse the command line. */ + optnr = 1; + + while ((arg = parseopt(argc, argv, "hVa:b:ec:d:gI:j:m:rs:t:wx:z:", &flagFile, &optnr, &optarg)) != '\0') + switch (arg) + { + case 'a': + /* Where to generate the API file. */ + apiFile = optarg; + break; + + case 'm': + /* Where to generate the XML file. */ + xmlFile = optarg; + break; + + case 'b': + /* Generate a build file. */ + buildFile = optarg; + break; + + case 'e': + /* Enable exceptions. */ + exceptions = TRUE; + break; + + case 'g': + /* Always release the GIL. */ + releaseGIL = TRUE; + break; + + case 'j': + /* Generate the code in this number of parts. */ + parts = parseInt(optarg,'j'); + break; + + case 'z': + /* Read a file for the next flags. */ + if (flagFile != NULL) + fatal("The -z flag cannot be specified in an argument file\n"); + + flagFile = optarg; + break; + + case 'c': + /* Where to generate the code. */ + codeDir = optarg; + break; + + case 'd': + /* Where to generate the documentation. */ + docFile = optarg; + break; + + case 't': + /* Which platform or version to generate code for. */ + appendString(&versions,optarg); + break; + + case 'x': + /* Which features are disabled. */ + appendString(&xfeatures,optarg); + break; + + case 'I': + /* Where to get included files from. */ + appendString(&includeDirList,optarg); + break; + + case 'r': + /* Enable tracing. */ + tracing = TRUE; + break; + + case 's': + /* The suffix to use for source files. */ + srcSuffix = optarg; + break; + + case 'w': + /* Enable warning messages. */ + warnings = TRUE; + break; + + case 'h': + /* Help message. */ + help(); + break; + + case 'V': + /* Display the version number. */ + version(); + break; + + default: + usage(); + } + + if (optnr < argc) + { + file = NULL; + filename = argv[optnr++]; + + if (optnr < argc) + usage(); + } + else + { + file = stdin; + filename = "stdin"; + } + + /* Parse the input file. */ + parse(&spec,file,filename,versions,xfeatures); + + /* Verify and transform the parse tree. */ + transform(&spec); + + /* Generate code. */ + generateCode(&spec, codeDir, buildFile, docFile, srcSuffix, exceptions, + tracing, releaseGIL, parts, xfeatures); + + /* Generate the API file. */ + if (apiFile != NULL) + generateAPI(&spec, apiFile); + + /* Generate the XML export. */ + if (xmlFile != NULL) + generateXML(&spec, xmlFile); + + /* All done. */ + return 0; +} + + +/* + * Parse the next command line argument - similar to UNIX getopts(). Allow a + * flag to specify that a file contains further arguments. + */ +static char parseopt(int argc,char **argv,char *opts,char **flags,int *optnrp, + char **optargp) +{ + char arg, *op, *fname; + int optnr; + static FILE *fp = NULL; + + /* Deal with any file first. */ + + fname = *flags; + + if (fname != NULL && fp == NULL && (fp = fopen(fname,"r")) == NULL) + fatal("Unable to open %s\n",fname); + + if (fp != NULL) + { + char buf[200], *cp, *fname; + int ch; + + fname = *flags; + cp = buf; + + while ((ch = fgetc(fp)) != EOF) + { + /* Skip leading whitespace. */ + + if (cp == buf && isspace(ch)) + continue; + + if (ch == '\n') + break; + + if (cp == &buf[sizeof (buf) - 1]) + fatal("A flag in %s is too long\n",fname); + + *cp++ = (char)ch; + } + + *cp = '\0'; + + if (ch == EOF) + { + fclose(fp); + fp = NULL; + *flags = NULL; + } + + /* + * Get the option character and any optional argument from the + * line. + */ + + if (buf[0] != '\0') + { + if (buf[0] != '-' || buf[1] == '\0') + fatal("An non-flag was given in %s\n",fname); + + arg = buf[1]; + + /* Find any optional argument. */ + + for (cp = &buf[2]; *cp != '\0'; ++cp) + if (!isspace(*cp)) + break; + + if (*cp == '\0') + cp = NULL; + else + cp = sipStrdup(cp); + + *optargp = cp; + + if ((op = strchr(opts,arg)) == NULL) + fatal("An invalid flag was given in %s\n",fname); + + if (op[1] == ':' && cp == NULL) + fatal("Missing flag argument in %s\n",fname); + + if (op[1] != ':' && cp != NULL) + fatal("Unexpected flag argument in %s\n",fname); + + return arg; + } + } + + /* Check there is an argument and it is a switch. */ + + optnr = *optnrp; + + if (optnr >= argc || argv[optnr] == NULL || argv[optnr][0] != '-') + return '\0'; + + /* Check it is a valid switch. */ + + arg = argv[optnr][1]; + + if (arg == '\0' || (op = strchr(opts,arg)) == NULL) + usage(); + + /* Check for the switch parameter, if any. */ + + if (op[1] == ':') + { + if (argv[optnr][2] != '\0') + { + *optargp = &argv[optnr][2]; + ++optnr; + } + else if (optnr + 1 >= argc || argv[optnr + 1] == NULL) + usage(); + else + { + *optargp = argv[optnr + 1]; + optnr += 2; + } + } + else if (argv[optnr][2] != '\0') + usage(); + else + { + *optargp = NULL; + ++optnr; + } + + *optnrp = optnr; + + return arg; +} + + +/* + * Parse an integer option. + */ +static int parseInt(char *arg, char opt) +{ + char *endptr; + int val; + + val = strtol(arg, &endptr, 10); + + if (*arg == '\0' || *endptr != '\0') + fatal("Invalid integer argument for -%c flag\n", opt); + + return val; +} + + +/* + * Append a string to a list of them. + */ +void appendString(stringList **headp, const char *s) +{ + stringList *sl; + + /* Create the new entry. */ + + sl = sipMalloc(sizeof (stringList)); + + sl -> s = s; + sl -> next = NULL; + + /* Append it to the list. */ + + while (*headp != NULL) + headp = &(*headp) -> next; + + *headp = sl; +} + + +/* + * Display a warning message. + */ +void warning(char *fmt,...) +{ + static int start = TRUE; + + va_list ap; + + if (!warnings) + return; + + if (start) + { + fprintf(stderr,"%s: Warning: ",sipPackage); + start = FALSE; + } + + va_start(ap,fmt); + vfprintf(stderr,fmt,ap); + va_end(ap); + + if (strchr(fmt,'\n') != NULL) + start = TRUE; +} + + +/* + * Display all or part of a one line error message describing a fatal error. + * If the message is complete (it has a newline) then the program exits. + */ +void fatal(char *fmt,...) +{ + static int start = TRUE; + + va_list ap; + + if (start) + { + fprintf(stderr,"%s: ",sipPackage); + start = FALSE; + } + + va_start(ap,fmt); + vfprintf(stderr,fmt,ap); + va_end(ap); + + if (strchr(fmt,'\n') != NULL) + exit(1); +} + + +/* + * Display the SIP version number on stdout and exit with zero exit status. + */ +static void version(void) +{ + printf("%s\n",sipVersion); + exit(0); +} + + +/* + * Display the help message on stdout and exit with zero exit status. + */ +static void help(void) +{ + printf( +"Usage:\n" +" %s [-h] [-V] [-a file] [-c dir] [-d file] [-e] [-g] [-I dir] [-j #] [-m file] [-r] [-s suffix] [-t version] [-w] [-x feature] [-z file] [file]\n" +"where:\n" +" -h display this help message\n" +" -V display the %s version number\n" +" -a file the name of the QScintilla API file [default not generated]\n" +" -b file the name of the build file [default none generated]\n" +" -c dir the name of the code directory [default not generated]\n" +" -d file the name of the documentation file [default not generated]\n" +" -e enable support for exceptions [default disabled]\n" +" -g always release and reacquire the GIL [default only when specified]\n" +" -I dir look in this directory when including files\n" +" -j # split the generated code into # files [default 1 per class]\n" +" -m file the name of the XML export file [default not generated]\n" +" -r generate code with tracing enabled [default disabled]\n" +" -s suffix the suffix to use for C or C++ source files [default \".c\" or \".cpp\"]\n" +" -t tag the version/platform to generate code for\n" +" -w enable warning messages\n" +" -x feature this feature is disabled\n" +" -z file the name of a file containing more command line flags\n" +" file the name of the specification file [default stdin]\n" + ,sipPackage,sipPackage); + + exit(0); +} + + +/* + * Display the usage message. + */ +static void usage(void) +{ + fatal("Usage: %s [-h] [-V] [-a file] [-b file] [-c dir] [-d file] [-e] [-g] [-I dir] [-j #] [-m file] [-r] [-s suffix] [-t tag] [-w] [-x feature] [-z file] [file]\n",sipPackage); +} diff --git a/python/sip/sipgen/parser.c b/python/sip/sipgen/parser.c new file mode 100644 index 00000000..f6e31832 --- /dev/null +++ b/python/sip/sipgen/parser.c @@ -0,0 +1,7526 @@ +/* A Bison parser, made by GNU Bison 1.875d. */ + +/* Skeleton parser for Yacc-like parsing with Bison, + Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. + + 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, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* As a special exception, when this file is copied by Bison into a + Bison output file, you may use that output file without restriction. + This special exception was added by the Free Software Foundation + in version 1.24 of Bison. */ + +/* Written by Richard Stallman by simplifying the original so called + ``semantic'' parser. */ + +/* All symbols defined below should begin with yy or YY, to avoid + infringing on user name space. This should be done even for local + variables, as they might otherwise be expanded by user macros. + There are some unavoidable exceptions within include files to + define necessary library symbols; they are noted "INFRINGES ON + USER NAME SPACE" below. */ + +/* Identify Bison output. */ +#define YYBISON 1 + +/* Skeleton name. */ +#define YYSKELETON_NAME "yacc.c" + +/* Pure parsers. */ +#define YYPURE 0 + +/* Using locations. */ +#define YYLSP_NEEDED 0 + + + +/* Tokens. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + /* Put the tokens into the symbol table, so that GDB and other debuggers + know about them. */ + enum yytokentype { + TK_OPTIONS = 258, + TK_NOEMITTERS = 259, + TK_DOC = 260, + TK_EXPORTEDDOC = 261, + TK_MAKEFILE = 262, + TK_ACCESSCODE = 263, + TK_GETCODE = 264, + TK_SETCODE = 265, + TK_PREINITCODE = 266, + TK_POSTINITCODE = 267, + TK_UNITCODE = 268, + TK_MODCODE = 269, + TK_TYPECODE = 270, + TK_PREPYCODE = 271, + TK_COPYING = 272, + TK_MAPPEDTYPE = 273, + TK_CODELINE = 274, + TK_IF = 275, + TK_END = 276, + TK_NAME = 277, + TK_PATHNAME = 278, + TK_STRING = 279, + TK_VIRTUALCATCHERCODE = 280, + TK_TRAVERSECODE = 281, + TK_CLEARCODE = 282, + TK_READBUFFERCODE = 283, + TK_WRITEBUFFERCODE = 284, + TK_SEGCOUNTCODE = 285, + TK_CHARBUFFERCODE = 286, + TK_METHODCODE = 287, + TK_FROMTYPE = 288, + TK_TOTYPE = 289, + TK_TOSUBCLASS = 290, + TK_INCLUDE = 291, + TK_OPTINCLUDE = 292, + TK_IMPORT = 293, + TK_EXPHEADERCODE = 294, + TK_MODHEADERCODE = 295, + TK_TYPEHEADERCODE = 296, + TK_MODULE = 297, + TK_CMODULE = 298, + TK_CLASS = 299, + TK_STRUCT = 300, + TK_PUBLIC = 301, + TK_PROTECTED = 302, + TK_PRIVATE = 303, + TK_SIGNALS = 304, + TK_SLOTS = 305, + TK_BOOL = 306, + TK_SHORT = 307, + TK_INT = 308, + TK_LONG = 309, + TK_FLOAT = 310, + TK_DOUBLE = 311, + TK_CHAR = 312, + TK_WCHAR_T = 313, + TK_VOID = 314, + TK_PYOBJECT = 315, + TK_PYTUPLE = 316, + TK_PYLIST = 317, + TK_PYDICT = 318, + TK_PYCALLABLE = 319, + TK_PYSLICE = 320, + TK_PYTYPE = 321, + TK_VIRTUAL = 322, + TK_ENUM = 323, + TK_SIGNED = 324, + TK_UNSIGNED = 325, + TK_SCOPE = 326, + TK_LOGICAL_OR = 327, + TK_CONST = 328, + TK_STATIC = 329, + TK_SIPSIGNAL = 330, + TK_SIPSLOT = 331, + TK_SIPANYSLOT = 332, + TK_SIPRXCON = 333, + TK_SIPRXDIS = 334, + TK_SIPSLOTCON = 335, + TK_SIPSLOTDIS = 336, + TK_NUMBER = 337, + TK_REAL = 338, + TK_TYPEDEF = 339, + TK_NAMESPACE = 340, + TK_TIMELINE = 341, + TK_PLATFORMS = 342, + TK_FEATURE = 343, + TK_LICENSE = 344, + TK_QCHAR = 345, + TK_TRUE = 346, + TK_FALSE = 347, + TK_NULL = 348, + TK_OPERATOR = 349, + TK_THROW = 350, + TK_QOBJECT = 351, + TK_EXCEPTION = 352, + TK_RAISECODE = 353, + TK_EXPLICIT = 354, + TK_TEMPLATE = 355, + TK_ELLIPSIS = 356 + }; +#endif +#define TK_OPTIONS 258 +#define TK_NOEMITTERS 259 +#define TK_DOC 260 +#define TK_EXPORTEDDOC 261 +#define TK_MAKEFILE 262 +#define TK_ACCESSCODE 263 +#define TK_GETCODE 264 +#define TK_SETCODE 265 +#define TK_PREINITCODE 266 +#define TK_POSTINITCODE 267 +#define TK_UNITCODE 268 +#define TK_MODCODE 269 +#define TK_TYPECODE 270 +#define TK_PREPYCODE 271 +#define TK_COPYING 272 +#define TK_MAPPEDTYPE 273 +#define TK_CODELINE 274 +#define TK_IF 275 +#define TK_END 276 +#define TK_NAME 277 +#define TK_PATHNAME 278 +#define TK_STRING 279 +#define TK_VIRTUALCATCHERCODE 280 +#define TK_TRAVERSECODE 281 +#define TK_CLEARCODE 282 +#define TK_READBUFFERCODE 283 +#define TK_WRITEBUFFERCODE 284 +#define TK_SEGCOUNTCODE 285 +#define TK_CHARBUFFERCODE 286 +#define TK_METHODCODE 287 +#define TK_FROMTYPE 288 +#define TK_TOTYPE 289 +#define TK_TOSUBCLASS 290 +#define TK_INCLUDE 291 +#define TK_OPTINCLUDE 292 +#define TK_IMPORT 293 +#define TK_EXPHEADERCODE 294 +#define TK_MODHEADERCODE 295 +#define TK_TYPEHEADERCODE 296 +#define TK_MODULE 297 +#define TK_CMODULE 298 +#define TK_CLASS 299 +#define TK_STRUCT 300 +#define TK_PUBLIC 301 +#define TK_PROTECTED 302 +#define TK_PRIVATE 303 +#define TK_SIGNALS 304 +#define TK_SLOTS 305 +#define TK_BOOL 306 +#define TK_SHORT 307 +#define TK_INT 308 +#define TK_LONG 309 +#define TK_FLOAT 310 +#define TK_DOUBLE 311 +#define TK_CHAR 312 +#define TK_WCHAR_T 313 +#define TK_VOID 314 +#define TK_PYOBJECT 315 +#define TK_PYTUPLE 316 +#define TK_PYLIST 317 +#define TK_PYDICT 318 +#define TK_PYCALLABLE 319 +#define TK_PYSLICE 320 +#define TK_PYTYPE 321 +#define TK_VIRTUAL 322 +#define TK_ENUM 323 +#define TK_SIGNED 324 +#define TK_UNSIGNED 325 +#define TK_SCOPE 326 +#define TK_LOGICAL_OR 327 +#define TK_CONST 328 +#define TK_STATIC 329 +#define TK_SIPSIGNAL 330 +#define TK_SIPSLOT 331 +#define TK_SIPANYSLOT 332 +#define TK_SIPRXCON 333 +#define TK_SIPRXDIS 334 +#define TK_SIPSLOTCON 335 +#define TK_SIPSLOTDIS 336 +#define TK_NUMBER 337 +#define TK_REAL 338 +#define TK_TYPEDEF 339 +#define TK_NAMESPACE 340 +#define TK_TIMELINE 341 +#define TK_PLATFORMS 342 +#define TK_FEATURE 343 +#define TK_LICENSE 344 +#define TK_QCHAR 345 +#define TK_TRUE 346 +#define TK_FALSE 347 +#define TK_NULL 348 +#define TK_OPERATOR 349 +#define TK_THROW 350 +#define TK_QOBJECT 351 +#define TK_EXCEPTION 352 +#define TK_RAISECODE 353 +#define TK_EXPLICIT 354 +#define TK_TEMPLATE 355 +#define TK_ELLIPSIS 356 + + + + +/* Copy the first part of user declarations. */ +#line 16 "parser.y" + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "sip.h" + + +#define MAX_NESTED_IF 10 +#define MAX_NESTED_SCOPE 10 + +#define inMainModule() (currentSpec -> module == currentModule) + + +static sipSpec *currentSpec; /* The current spec being parsed. */ +static stringList *neededQualifiers; /* The list of required qualifiers. */ +static stringList *excludedQualifiers; /* The list of excluded qualifiers. */ +static moduleDef *currentModule; /* The current module being parsed. */ +static mappedTypeDef *currentMappedType; /* The current mapped type. */ +static enumDef *currentEnum; /* The current enum being parsed. */ +static int sectionFlags; /* The current section flags. */ +static int currentOverIsVirt; /* Set if the overload is virtual. */ +static int currentCtorIsExplicit; /* Set if the ctor is explicit. */ +static int currentIsStatic; /* Set if the current is static. */ +static char *previousFile; /* The file just parsed. */ +static parserContext newContext; /* The new pending context. */ +static int skipStackPtr; /* The skip stack pointer. */ +static int skipStack[MAX_NESTED_IF]; /* Stack of skip flags. */ +static classDef *scopeStack[MAX_NESTED_SCOPE]; /* The scope stack. */ +static int sectFlagsStack[MAX_NESTED_SCOPE]; /* The section flags stack. */ +static int currentScopeIdx; /* The scope stack index. */ +static int currentTimelineOrder; /* The current timeline order. */ + + +static char *getPythonName(optFlags *optflgs, char *cname); +static nameDef *cacheName(sipSpec *,char *); +static classDef *findClass(sipSpec *,ifaceFileType,scopedNameDef *); +static classDef *findClassWithInterface(sipSpec *pt, ifaceFileDef *iff); +static classDef *newClass(sipSpec *,ifaceFileType,scopedNameDef *); +static void finishClass(sipSpec *,moduleDef *,classDef *,optFlags *); +static exceptionDef *findException(sipSpec *pt, scopedNameDef *fqname, int new); +static mappedTypeDef *newMappedType(sipSpec *,argDef *); +static enumDef *newEnum(sipSpec *,moduleDef *,char *,optFlags *,int); +static void instantiateClassTemplate(sipSpec *pt, moduleDef *mod, classDef *scope, scopedNameDef *fqname, classTmplDef *tcd, templateDef *td); +static void newTypedef(sipSpec *,moduleDef *,char *,argDef *); +static void newVar(sipSpec *,moduleDef *,char *,int,argDef *,optFlags *, + codeBlock *,codeBlock *,codeBlock *); +static void newCtor(char *,int,signatureDef *,optFlags *,codeBlock *, + throwArgs *,signatureDef *,int); +static void newFunction(sipSpec *,moduleDef *,int,int,int,char *, + signatureDef *,int,int,optFlags *,codeBlock *, + codeBlock *,throwArgs *,signatureDef *); +static optFlag *findOptFlag(optFlags *,char *,flagType); +static memberDef *findFunction(sipSpec *,moduleDef *,classDef *,nameDef *,int, + int); +static void checkAttributes(sipSpec *,classDef *,char *,int); +static void newModule(FILE *,char *); +static void appendCodeBlock(codeBlock **,codeBlock *); +static void parseFile(FILE *,char *,moduleDef *,int); +static void handleEOF(void); +static void handleEOM(void); +static qualDef *findQualifier(char *); +static scopedNameDef *text2scopedName(char *); +static scopedNameDef *scopeScopedName(scopedNameDef *name); +static void pushScope(classDef *); +static void popScope(void); +static classDef *currentScope(void); +static void newQualifier(moduleDef *,int,int,char *,qualType); +static void newImport(char *); +static void usedInMainModule(sipSpec *,ifaceFileDef *); +static int timePeriod(char *,char *); +static int platOrFeature(char *,int); +static int isNeeded(qualDef *); +static int notSkipping(void); +static void getHooks(optFlags *,char **,char **); +static int getReleaseGIL(optFlags *); +static int getHoldGIL(optFlags *); +static void templateSignature(signatureDef *sd, int result, classTmplDef *tcd, templateDef *td, classDef *ncd); +static void templateType(argDef *ad, classTmplDef *tcd, templateDef *td, classDef *ncd); +static int search_back(const char *end, const char *start, const char *target); +static char *getType(scopedNameDef *ename, argDef *ad); +static char *scopedNameToString(scopedNameDef *name); +static void addUsedFromCode(sipSpec *pt, ifaceFileList **used, const char *sname); +static int sameName(scopedNameDef *snd, const char *sname); +static int optFind(sipSpec *pt, const char *opt); + + +/* Enabling traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE 0 +#endif + +#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) +#line 103 "parser.y" +typedef union YYSTYPE { + char qchar; + char *text; + long number; + double real; + argDef memArg; + signatureDef signature; + signatureDef *optsignature; + throwArgs *throwlist; + codeBlock *codeb; + valueDef value; + valueDef *valp; + optFlags optflags; + optFlag flag; + scopedNameDef *scpvalp; + fcallDef fcall; + int boolean; + exceptionDef exceptionbase; + classDef *klass; +} YYSTYPE; +/* Line 191 of yacc.c. */ +#line 386 "parser.c" +# define yystype YYSTYPE /* obsolescent; will be withdrawn */ +# define YYSTYPE_IS_DECLARED 1 +# define YYSTYPE_IS_TRIVIAL 1 +#endif + + + +/* Copy the second part of user declarations. */ + + +/* Line 214 of yacc.c. */ +#line 398 "parser.c" + +#if ! defined (yyoverflow) || YYERROR_VERBOSE + +# ifndef YYFREE +# define YYFREE free +# endif +# ifndef YYMALLOC +# define YYMALLOC malloc +# endif + +/* The parser invokes alloca or malloc; define the necessary symbols. */ + +# ifdef YYSTACK_USE_ALLOCA +# if YYSTACK_USE_ALLOCA +# define YYSTACK_ALLOC alloca +# endif +# else +# if defined (alloca) || defined (_ALLOCA_H) +# define YYSTACK_ALLOC alloca +# else +# ifdef __GNUC__ +# define YYSTACK_ALLOC __builtin_alloca +# endif +# endif +# endif + +# ifdef YYSTACK_ALLOC + /* Pacify GCC's `empty if-body' warning. */ +# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) +# else +# if defined (__STDC__) || defined (__cplusplus) +# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# endif +# define YYSTACK_ALLOC YYMALLOC +# define YYSTACK_FREE YYFREE +# endif +#endif /* ! defined (yyoverflow) || YYERROR_VERBOSE */ + + +#if (! defined (yyoverflow) \ + && (! defined (__cplusplus) \ + || (defined (YYSTYPE_IS_TRIVIAL) && YYSTYPE_IS_TRIVIAL))) + +/* A type that is properly aligned for any stack member. */ +union yyalloc +{ + short int yyss; + YYSTYPE yyvs; + }; + +/* The size of the maximum gap between one aligned stack and the next. */ +# define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) + +/* The size of an array large to enough to hold all stacks, each with + N elements. */ +# define YYSTACK_BYTES(N) \ + ((N) * (sizeof (short int) + sizeof (YYSTYPE)) \ + + YYSTACK_GAP_MAXIMUM) + +/* Copy COUNT objects from FROM to TO. The source and destination do + not overlap. */ +# ifndef YYCOPY +# if defined (__GNUC__) && 1 < __GNUC__ +# define YYCOPY(To, From, Count) \ + __builtin_memcpy (To, From, (Count) * sizeof (*(From))) +# else +# define YYCOPY(To, From, Count) \ + do \ + { \ + register YYSIZE_T yyi; \ + for (yyi = 0; yyi < (Count); yyi++) \ + (To)[yyi] = (From)[yyi]; \ + } \ + while (0) +# endif +# endif + +/* Relocate STACK from its old location to the new one. The + local variables YYSIZE and YYSTACKSIZE give the old and new number of + elements in the stack, and YYPTR gives the new location of the + stack. Advance YYPTR to a properly aligned location for the next + stack. */ +# define YYSTACK_RELOCATE(Stack) \ + do \ + { \ + YYSIZE_T yynewbytes; \ + YYCOPY (&yyptr->Stack, Stack, yysize); \ + Stack = &yyptr->Stack; \ + yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ + yyptr += yynewbytes / sizeof (*yyptr); \ + } \ + while (0) + +#endif + +#if defined (__STDC__) || defined (__cplusplus) + typedef signed char yysigned_char; +#else + typedef short int yysigned_char; +#endif + +/* YYFINAL -- State number of the termination state. */ +#define YYFINAL 4 +/* YYLAST -- Last index in YYTABLE. */ +#define YYLAST 1105 + +/* YYNTOKENS -- Number of terminals. */ +#define YYNTOKENS 124 +/* YYNNTS -- Number of nonterminals. */ +#define YYNNTS 134 +/* YYNRULES -- Number of rules. */ +#define YYNRULES 339 +/* YYNRULES -- Number of states. */ +#define YYNSTATES 562 + +/* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ +#define YYUNDEFTOK 2 +#define YYMAXUTOK 356 + +#define YYTRANSLATE(YYX) \ + ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) + +/* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */ +static const unsigned char yytranslate[] = +{ + 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 108, 2, 2, 2, 122, 114, 2, + 102, 103, 112, 111, 104, 109, 2, 113, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 119, 107, + 117, 110, 118, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 120, 2, 121, 123, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 105, 115, 106, 116, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 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 +}; + +#if YYDEBUG +/* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in + YYRHS. */ +static const unsigned short int yyprhs[] = +{ + 0, 0, 3, 5, 8, 9, 12, 14, 16, 18, + 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, + 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, + 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, + 80, 82, 84, 89, 91, 95, 97, 107, 108, 112, + 115, 116, 121, 122, 128, 133, 135, 138, 140, 143, + 146, 147, 155, 157, 160, 161, 167, 169, 172, 174, + 177, 178, 184, 186, 189, 191, 196, 198, 201, 205, + 210, 212, 216, 218, 221, 225, 227, 229, 231, 233, + 234, 236, 239, 242, 245, 246, 249, 250, 253, 254, + 257, 260, 263, 266, 269, 270, 272, 275, 278, 281, + 284, 287, 290, 293, 296, 299, 302, 305, 308, 311, + 314, 319, 322, 324, 327, 328, 337, 338, 340, 341, + 343, 344, 346, 348, 351, 353, 355, 360, 361, 363, + 364, 367, 368, 371, 373, 377, 379, 381, 383, 385, + 387, 389, 390, 392, 394, 396, 398, 401, 403, 407, + 409, 411, 416, 418, 420, 422, 424, 426, 428, 430, + 431, 433, 437, 442, 453, 454, 463, 466, 471, 472, + 480, 481, 484, 486, 490, 492, 493, 497, 499, 502, + 504, 506, 508, 510, 512, 514, 516, 518, 520, 522, + 524, 526, 528, 530, 532, 534, 536, 538, 540, 543, + 546, 550, 554, 558, 561, 562, 564, 576, 577, 581, + 583, 593, 594, 600, 601, 608, 609, 611, 625, 640, + 654, 656, 658, 660, 662, 664, 666, 668, 670, 673, + 676, 679, 682, 685, 688, 691, 694, 697, 700, 704, + 708, 710, 713, 716, 718, 721, 724, 727, 729, 732, + 733, 735, 736, 739, 740, 744, 746, 750, 752, 756, + 758, 760, 762, 763, 766, 767, 770, 772, 773, 775, + 779, 783, 787, 791, 794, 797, 803, 809, 812, 815, + 816, 820, 822, 824, 826, 827, 831, 833, 841, 846, + 850, 854, 855, 857, 858, 861, 863, 868, 871, 874, + 876, 878, 881, 883, 885, 888, 891, 895, 897, 899, + 901, 904, 907, 909, 911, 913, 915, 917, 919, 921, + 923, 925, 927, 929, 931, 935, 936, 941, 942, 944 +}; + +/* YYRHS -- A `-1'-separated list of the rules' RHS. */ +static const short int yyrhs[] = +{ + 125, 0, -1, 126, -1, 125, 126, -1, -1, 127, + 128, -1, 160, -1, 130, -1, 132, -1, 170, -1, + 164, -1, 165, -1, 166, -1, 151, -1, 146, -1, + 150, -1, 159, -1, 171, -1, 172, -1, 181, -1, + 183, -1, 184, -1, 185, -1, 186, -1, 187, -1, + 188, -1, 189, -1, 136, -1, 138, -1, 129, -1, + 155, -1, 158, -1, 143, -1, 211, -1, 215, -1, + 213, -1, 133, -1, 210, -1, 192, -1, 231, -1, + 249, -1, 173, -1, 3, 102, 131, 103, -1, 22, + -1, 131, 104, 22, -1, 4, -1, 97, 206, 134, + 235, 105, 174, 135, 106, 107, -1, -1, 102, 206, + 103, -1, 98, 190, -1, -1, 18, 254, 137, 140, + -1, -1, 214, 18, 254, 139, 140, -1, 105, 141, + 106, 107, -1, 142, -1, 141, 142, -1, 173, -1, + 33, 190, -1, 34, 190, -1, -1, 85, 22, 144, + 105, 145, 106, 107, -1, 129, -1, 145, 129, -1, + -1, 87, 147, 105, 148, 106, -1, 149, -1, 148, + 149, -1, 22, -1, 88, 22, -1, -1, 86, 152, + 105, 153, 106, -1, 154, -1, 153, 154, -1, 22, + -1, 20, 102, 157, 103, -1, 22, -1, 108, 22, + -1, 156, 72, 22, -1, 156, 72, 108, 22, -1, + 156, -1, 195, 109, 195, -1, 21, -1, 89, 235, + -1, 161, 162, 163, -1, 42, -1, 43, -1, 22, + -1, 23, -1, -1, 82, -1, 36, 23, -1, 37, + 23, -1, 38, 23, -1, -1, 8, 190, -1, -1, + 9, 190, -1, -1, 10, 190, -1, 17, 190, -1, + 39, 190, -1, 40, 190, -1, 41, 190, -1, -1, + 173, -1, 26, 190, -1, 27, 190, -1, 28, 190, + -1, 29, 190, -1, 30, 190, -1, 31, 190, -1, + 14, 190, -1, 15, 190, -1, 11, 190, -1, 12, + 190, -1, 13, 190, -1, 16, 190, -1, 5, 190, + -1, 6, 190, -1, 7, 23, 194, 190, -1, 191, + 21, -1, 19, -1, 191, 19, -1, -1, 68, 195, + 235, 193, 105, 196, 106, 107, -1, -1, 23, -1, + -1, 22, -1, -1, 197, -1, 198, -1, 197, 198, + -1, 155, -1, 158, -1, 22, 200, 235, 199, -1, + -1, 104, -1, -1, 110, 205, -1, -1, 110, 202, + -1, 205, -1, 202, 203, 205, -1, 109, -1, 111, + -1, 112, -1, 113, -1, 114, -1, 115, -1, -1, + 108, -1, 116, -1, 109, -1, 111, -1, 204, 208, + -1, 207, -1, 206, 71, 207, -1, 22, -1, 206, + -1, 254, 102, 209, 103, -1, 83, -1, 82, -1, + 91, -1, 92, -1, 93, -1, 24, -1, 90, -1, + -1, 202, -1, 209, 104, 202, -1, 84, 250, 22, + 107, -1, 84, 250, 102, 253, 22, 103, 102, 255, + 103, 107, -1, -1, 45, 22, 212, 235, 105, 221, + 106, 107, -1, 214, 215, -1, 100, 117, 255, 118, + -1, -1, 44, 206, 216, 217, 235, 220, 107, -1, + -1, 119, 218, -1, 219, -1, 218, 104, 219, -1, + 206, -1, -1, 105, 221, 106, -1, 222, -1, 221, + 222, -1, 155, -1, 158, -1, 143, -1, 211, -1, + 215, -1, 133, -1, 210, -1, 192, -1, 182, -1, + 173, -1, 175, -1, 176, -1, 177, -1, 178, -1, + 179, -1, 180, -1, 225, -1, 224, -1, 244, -1, + 35, 190, -1, 34, 190, -1, 46, 223, 119, -1, + 47, 223, 119, -1, 48, 223, 119, -1, 49, 119, + -1, -1, 50, -1, 230, 116, 22, 102, 103, 256, + 234, 235, 107, 239, 240, -1, -1, 99, 226, 227, + -1, 227, -1, 22, 102, 241, 103, 256, 235, 228, + 107, 239, -1, -1, 120, 102, 241, 103, 121, -1, + -1, 120, 250, 102, 241, 103, 121, -1, -1, 67, + -1, 250, 22, 102, 241, 103, 233, 256, 234, 235, + 229, 107, 239, 240, -1, 250, 94, 232, 102, 241, + 103, 233, 256, 234, 235, 229, 107, 239, 240, -1, + 94, 250, 102, 241, 103, 233, 256, 234, 235, 229, + 107, 239, 240, -1, 111, -1, 109, -1, 112, -1, + 113, -1, 122, -1, 114, -1, 115, -1, 123, -1, + 117, 117, -1, 118, 118, -1, 111, 110, -1, 109, + 110, -1, 112, 110, -1, 113, 110, -1, 122, 110, + -1, 114, 110, -1, 115, 110, -1, 123, 110, -1, + 117, 117, 110, -1, 118, 118, 110, -1, 116, -1, + 102, 103, -1, 120, 121, -1, 117, -1, 117, 110, + -1, 110, 110, -1, 108, 110, -1, 118, -1, 118, + 110, -1, -1, 73, -1, -1, 110, 82, -1, -1, + 113, 236, 113, -1, 237, -1, 236, 104, 237, -1, + 22, -1, 22, 110, 238, -1, 22, -1, 24, -1, + 82, -1, -1, 32, 190, -1, -1, 25, 190, -1, + 242, -1, -1, 243, -1, 242, 104, 243, -1, 75, + 195, 201, -1, 76, 195, 201, -1, 77, 195, 201, + -1, 78, 195, -1, 79, 195, -1, 80, 102, 241, + 103, 195, -1, 81, 102, 241, 103, 195, -1, 96, + 195, -1, 251, 201, -1, -1, 74, 245, 246, -1, + 246, -1, 247, -1, 249, -1, -1, 67, 248, 231, + -1, 231, -1, 250, 22, 235, 107, 167, 168, 169, + -1, 73, 254, 253, 252, -1, 254, 253, 252, -1, + 250, 195, 235, -1, -1, 114, -1, -1, 253, 112, + -1, 206, -1, 206, 117, 255, 118, -1, 45, 206, + -1, 70, 52, -1, 52, -1, 70, -1, 70, 53, + -1, 53, -1, 54, -1, 70, 54, -1, 54, 54, + -1, 70, 54, 54, -1, 55, -1, 56, -1, 51, + -1, 69, 57, -1, 70, 57, -1, 57, -1, 58, + -1, 59, -1, 60, -1, 61, -1, 62, -1, 63, + -1, 64, -1, 65, -1, 66, -1, 101, -1, 250, + -1, 255, 104, 250, -1, -1, 95, 102, 257, 103, + -1, -1, 206, -1, 257, 104, 206, -1 +}; + +/* YYRLINE[YYN] -- source line where rule number YYN was defined. */ +static const unsigned short int yyrline[] = +{ + 0, 289, 289, 290, 293, 293, 312, 313, 314, 315, + 316, 317, 318, 319, 320, 321, 322, 323, 327, 331, + 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, + 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, + 357, 358, 371, 374, 377, 382, 391, 430, 434, 508, + 513, 513, 519, 519, 558, 572, 573, 576, 580, 589, + 600, 600, 637, 638, 641, 641, 668, 669, 672, 677, + 682, 682, 707, 708, 711, 716, 729, 732, 735, 738, + 743, 744, 749, 755, 782, 807, 810, 815, 816, 832, + 835, 838, 843, 848, 853, 856, 861, 864, 869, 872, + 877, 883, 888, 893, 898, 901, 904, 909, 914, 919, + 924, 929, 934, 939, 944, 950, 956, 962, 971, 977, + 982, 988, 991, 992, 1003, 1003, 1014, 1017, 1022, 1025, + 1030, 1031, 1034, 1035, 1038, 1039, 1040, 1073, 1074, 1077, + 1078, 1081, 1084, 1089, 1090, 1108, 1111, 1114, 1117, 1120, + 1123, 1128, 1131, 1134, 1137, 1140, 1145, 1163, 1164, 1172, + 1177, 1187, 1197, 1201, 1205, 1209, 1213, 1217, 1221, 1227, + 1232, 1238, 1256, 1260, 1283, 1283, 1303, 1328, 1333, 1333, + 1382, 1383, 1386, 1387, 1390, 1405, 1408, 1413, 1414, 1417, + 1418, 1419, 1420, 1421, 1422, 1423, 1424, 1425, 1429, 1433, + 1440, 1447, 1454, 1461, 1468, 1475, 1476, 1477, 1478, 1489, + 1500, 1507, 1514, 1521, 1530, 1533, 1538, 1587, 1587, 1588, + 1591, 1617, 1620, 1627, 1630, 1638, 1641, 1646, 1663, 1688, + 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, + 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, + 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1797, + 1800, 1805, 1808, 1816, 1819, 1825, 1829, 1841, 1845, 1851, + 1855, 1859, 1865, 1868, 1873, 1876, 1881, 1929, 1934, 1940, + 1967, 1976, 1985, 1994, 2002, 2010, 2025, 2040, 2046, 2052, + 2052, 2053, 2056, 2057, 2060, 2060, 2061, 2064, 2097, 2103, + 2111, 2168, 2171, 2179, 2182, 2187, 2191, 2201, 2214, 2217, + 2220, 2223, 2226, 2229, 2232, 2235, 2238, 2241, 2244, 2247, + 2250, 2253, 2256, 2259, 2262, 2265, 2268, 2271, 2274, 2277, + 2280, 2283, 2286, 2291, 2297, 2313, 2316, 2343, 2349, 2356 +}; +#endif + +#if YYDEBUG || YYERROR_VERBOSE +/* YYTNME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at YYNTOKENS, nonterminals. */ +static const char *const yytname[] = +{ + "$end", "error", "$undefined", "TK_OPTIONS", "TK_NOEMITTERS", "TK_DOC", + "TK_EXPORTEDDOC", "TK_MAKEFILE", "TK_ACCESSCODE", "TK_GETCODE", + "TK_SETCODE", "TK_PREINITCODE", "TK_POSTINITCODE", "TK_UNITCODE", + "TK_MODCODE", "TK_TYPECODE", "TK_PREPYCODE", "TK_COPYING", + "TK_MAPPEDTYPE", "TK_CODELINE", "TK_IF", "TK_END", "TK_NAME", + "TK_PATHNAME", "TK_STRING", "TK_VIRTUALCATCHERCODE", "TK_TRAVERSECODE", + "TK_CLEARCODE", "TK_READBUFFERCODE", "TK_WRITEBUFFERCODE", + "TK_SEGCOUNTCODE", "TK_CHARBUFFERCODE", "TK_METHODCODE", "TK_FROMTYPE", + "TK_TOTYPE", "TK_TOSUBCLASS", "TK_INCLUDE", "TK_OPTINCLUDE", "TK_IMPORT", + "TK_EXPHEADERCODE", "TK_MODHEADERCODE", "TK_TYPEHEADERCODE", "TK_MODULE", + "TK_CMODULE", "TK_CLASS", "TK_STRUCT", "TK_PUBLIC", "TK_PROTECTED", + "TK_PRIVATE", "TK_SIGNALS", "TK_SLOTS", "TK_BOOL", "TK_SHORT", "TK_INT", + "TK_LONG", "TK_FLOAT", "TK_DOUBLE", "TK_CHAR", "TK_WCHAR_T", "TK_VOID", + "TK_PYOBJECT", "TK_PYTUPLE", "TK_PYLIST", "TK_PYDICT", "TK_PYCALLABLE", + "TK_PYSLICE", "TK_PYTYPE", "TK_VIRTUAL", "TK_ENUM", "TK_SIGNED", + "TK_UNSIGNED", "TK_SCOPE", "TK_LOGICAL_OR", "TK_CONST", "TK_STATIC", + "TK_SIPSIGNAL", "TK_SIPSLOT", "TK_SIPANYSLOT", "TK_SIPRXCON", + "TK_SIPRXDIS", "TK_SIPSLOTCON", "TK_SIPSLOTDIS", "TK_NUMBER", "TK_REAL", + "TK_TYPEDEF", "TK_NAMESPACE", "TK_TIMELINE", "TK_PLATFORMS", + "TK_FEATURE", "TK_LICENSE", "TK_QCHAR", "TK_TRUE", "TK_FALSE", "TK_NULL", + "TK_OPERATOR", "TK_THROW", "TK_QOBJECT", "TK_EXCEPTION", "TK_RAISECODE", + "TK_EXPLICIT", "TK_TEMPLATE", "TK_ELLIPSIS", "'('", "')'", "','", "'{'", + "'}'", "';'", "'!'", "'-'", "'='", "'+'", "'*'", "'/'", "'&'", "'|'", + "'~'", "'<'", "'>'", "':'", "'['", "']'", "'%'", "'^'", "$accept", + "specification", "statement", "@1", "modstatement", "nsstatement", + "options", "optionlist", "noemitters", "exception", "baseexception", + "raisecode", "mappedtype", "@2", "mappedtypetmpl", "@3", "mtdefinition", + "mtbody", "mtline", "namespace", "@4", "nsbody", "platforms", "@5", + "platformlist", "platform", "feature", "timeline", "@6", "qualifierlist", + "qualifiername", "ifstart", "oredqualifiers", "qualifiers", "ifend", + "license", "module", "modlang", "modname", "optnumber", "include", + "optinclude", "import", "optaccesscode", "optgetcode", "optsetcode", + "copying", "exphdrcode", "modhdrcode", "typehdrcode", "opttypehdrcode", + "travcode", "clearcode", "readbufcode", "writebufcode", "segcountcode", + "charbufcode", "modcode", "typecode", "preinitcode", "postinitcode", + "unitcode", "prepycode", "doc", "exporteddoc", "makefile", "codeblock", + "codelines", "enum", "@7", "optfilename", "optname", "optenumbody", + "enumbody", "enumline", "optcomma", "optenumassign", "optassign", "expr", + "binop", "optunop", "value", "scopedname", "scopepart", "simplevalue", + "exprlist", "typedef", "struct", "@8", "classtmpl", "template", "class", + "@9", "superclasses", "superlist", "superclass", "optclassbody", + "classbody", "classline", "optslot", "dtor", "ctor", "@10", "simplector", + "optctorsig", "optsig", "optvirtual", "function", "operatorname", + "optconst", "optabstract", "optflags", "flaglist", "flag", "flagvalue", + "methodcode", "virtualcatchercode", "arglist", "rawarglist", "argvalue", + "varmember", "@11", "varmem", "member", "@12", "variable", "cpptype", + "argtype", "optref", "deref", "basetype", "cpptypelist", "optexceptions", + "exceptionlist", 0 +}; +#endif + +# ifdef YYPRINT +/* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to + token YYLEX-NUM. */ +static const unsigned short int yytoknum[] = +{ + 0, 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, 40, 41, 44, 123, 125, 59, 33, 45, + 61, 43, 42, 47, 38, 124, 126, 60, 62, 58, + 91, 93, 37, 94 +}; +# endif + +/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ +static const unsigned short int yyr1[] = +{ + 0, 124, 125, 125, 127, 126, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, + 129, 129, 130, 131, 131, 132, 133, 134, 134, 135, + 137, 136, 139, 138, 140, 141, 141, 142, 142, 142, + 144, 143, 145, 145, 147, 146, 148, 148, 149, 150, + 152, 151, 153, 153, 154, 155, 156, 156, 156, 156, + 157, 157, 158, 159, 160, 161, 161, 162, 162, 163, + 163, 164, 165, 166, 167, 167, 168, 168, 169, 169, + 170, 171, 172, 173, 174, 174, 175, 176, 177, 178, + 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, + 189, 190, 191, 191, 193, 192, 194, 194, 195, 195, + 196, 196, 197, 197, 198, 198, 198, 199, 199, 200, + 200, 201, 201, 202, 202, 203, 203, 203, 203, 203, + 203, 204, 204, 204, 204, 204, 205, 206, 206, 207, + 208, 208, 208, 208, 208, 208, 208, 208, 208, 209, + 209, 209, 210, 210, 212, 211, 213, 214, 216, 215, + 217, 217, 218, 218, 219, 220, 220, 221, 221, 222, + 222, 222, 222, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 223, 223, 224, 226, 225, 225, + 227, 228, 228, 229, 229, 230, 230, 231, 231, 231, + 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, + 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, + 232, 232, 232, 232, 232, 232, 232, 232, 232, 233, + 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, + 238, 238, 239, 239, 240, 240, 241, 242, 242, 242, + 243, 243, 243, 243, 243, 243, 243, 243, 243, 245, + 244, 244, 246, 246, 248, 247, 247, 249, 250, 250, + 251, 252, 252, 253, 253, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 255, 255, 256, 256, 257, 257, 257 +}; + +/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ +static const unsigned char yyr2[] = +{ + 0, 2, 1, 2, 0, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 4, 1, 3, 1, 9, 0, 3, 2, + 0, 4, 0, 5, 4, 1, 2, 1, 2, 2, + 0, 7, 1, 2, 0, 5, 1, 2, 1, 2, + 0, 5, 1, 2, 1, 4, 1, 2, 3, 4, + 1, 3, 1, 2, 3, 1, 1, 1, 1, 0, + 1, 2, 2, 2, 0, 2, 0, 2, 0, 2, + 2, 2, 2, 2, 0, 1, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 4, 2, 1, 2, 0, 8, 0, 1, 0, 1, + 0, 1, 1, 2, 1, 1, 4, 0, 1, 0, + 2, 0, 2, 1, 3, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 1, 1, 2, 1, 3, 1, + 1, 4, 1, 1, 1, 1, 1, 1, 1, 0, + 1, 3, 4, 10, 0, 8, 2, 4, 0, 7, + 0, 2, 1, 3, 1, 0, 3, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, + 3, 3, 3, 2, 0, 1, 11, 0, 3, 1, + 9, 0, 5, 0, 6, 0, 1, 13, 14, 13, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, + 1, 2, 2, 1, 2, 2, 2, 1, 2, 0, + 1, 0, 2, 0, 3, 1, 3, 1, 3, 1, + 1, 1, 0, 2, 0, 2, 1, 0, 1, 3, + 3, 3, 3, 2, 2, 5, 5, 2, 2, 0, + 3, 1, 1, 1, 0, 3, 1, 7, 4, 3, + 3, 0, 1, 0, 2, 1, 4, 2, 2, 1, + 1, 2, 1, 1, 2, 2, 3, 1, 1, 1, + 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 3, 0, 4, 0, 1, 3 +}; + +/* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state + STATE-NUM when YYTABLE doesn't specify something else to do. Zero + means the default is an error. */ +static const unsigned short int yydefact[] = +{ + 4, 4, 2, 0, 1, 3, 0, 45, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, + 159, 0, 0, 0, 0, 0, 0, 85, 86, 0, + 0, 319, 309, 312, 313, 317, 318, 322, 323, 324, + 325, 326, 327, 328, 329, 330, 331, 128, 0, 310, + 0, 0, 0, 70, 64, 0, 263, 0, 0, 0, + 332, 5, 29, 7, 8, 36, 27, 28, 32, 14, + 15, 13, 30, 31, 16, 6, 0, 10, 11, 12, + 9, 17, 18, 41, 19, 20, 21, 22, 23, 24, + 25, 26, 38, 305, 157, 37, 33, 35, 0, 34, + 39, 40, 0, 303, 0, 122, 118, 0, 119, 126, + 114, 115, 116, 112, 117, 100, 0, 50, 128, 91, + 92, 93, 101, 102, 103, 178, 159, 307, 315, 129, + 263, 320, 308, 311, 314, 321, 303, 0, 60, 0, + 0, 69, 0, 83, 0, 47, 0, 87, 88, 89, + 0, 0, 0, 176, 263, 0, 301, 43, 0, 123, + 121, 127, 0, 0, 76, 0, 80, 0, 0, 180, + 263, 124, 316, 301, 0, 303, 0, 0, 0, 267, + 0, 265, 277, 0, 263, 333, 0, 90, 84, 158, + 0, 52, 277, 0, 0, 0, 231, 0, 230, 232, + 233, 235, 236, 250, 253, 257, 0, 234, 237, 0, + 304, 302, 299, 42, 0, 120, 0, 51, 77, 0, + 75, 128, 0, 263, 0, 0, 298, 172, 0, 0, + 74, 0, 72, 68, 0, 66, 0, 0, 264, 128, + 128, 128, 128, 128, 0, 0, 128, 0, 276, 278, + 128, 141, 0, 0, 0, 177, 306, 0, 0, 94, + 251, 256, 241, 255, 240, 242, 243, 245, 246, 254, + 238, 258, 239, 252, 244, 247, 277, 44, 0, 0, + 0, 55, 57, 78, 0, 81, 184, 181, 182, 185, + 225, 130, 0, 62, 0, 0, 71, 73, 65, 67, + 269, 270, 271, 268, 266, 141, 141, 141, 283, 284, + 277, 277, 287, 259, 0, 263, 151, 288, 48, 104, + 334, 53, 259, 0, 96, 248, 249, 0, 58, 59, + 0, 56, 79, 0, 225, 0, 0, 159, 0, 0, + 0, 0, 0, 0, 0, 0, 214, 214, 214, 0, + 294, 289, 217, 194, 191, 189, 190, 198, 199, 200, + 201, 202, 203, 204, 197, 196, 195, 192, 193, 225, + 187, 206, 205, 219, 0, 296, 207, 291, 292, 293, + 139, 134, 135, 0, 131, 132, 0, 0, 63, 280, + 281, 282, 0, 0, 260, 335, 279, 300, 152, 154, + 155, 153, 142, 0, 143, 105, 0, 335, 95, 0, + 98, 259, 54, 183, 225, 179, 113, 277, 106, 107, + 108, 109, 110, 111, 209, 208, 215, 0, 0, 0, + 213, 0, 0, 0, 0, 188, 0, 151, 263, 0, + 133, 0, 61, 128, 128, 0, 261, 145, 146, 147, + 148, 149, 150, 151, 167, 163, 162, 168, 164, 165, + 166, 160, 156, 0, 0, 0, 261, 97, 0, 297, + 335, 186, 0, 210, 211, 212, 295, 0, 294, 290, + 0, 218, 175, 0, 140, 137, 125, 0, 285, 286, + 337, 0, 263, 144, 151, 49, 0, 263, 99, 261, + 335, 0, 0, 138, 136, 0, 338, 0, 262, 223, + 170, 0, 46, 223, 263, 263, 335, 173, 336, 0, + 0, 0, 161, 151, 0, 223, 221, 261, 339, 0, + 272, 171, 272, 0, 0, 0, 263, 277, 0, 274, + 274, 272, 277, 272, 0, 0, 273, 0, 229, 227, + 274, 0, 220, 272, 0, 275, 228, 0, 274, 224, + 222, 216 +}; + +/* YYDEFGOTO[NTERM-NUM]. */ +static const short int yydefgoto[] = +{ + -1, 1, 2, 3, 61, 62, 63, 158, 64, 353, + 184, 465, 66, 163, 67, 257, 217, 280, 281, 354, + 176, 294, 69, 140, 234, 235, 70, 71, 139, 231, + 232, 355, 166, 167, 356, 74, 75, 76, 149, 188, + 77, 78, 79, 324, 410, 469, 80, 81, 82, 357, + 406, 358, 359, 360, 361, 362, 363, 84, 364, 85, + 86, 87, 88, 89, 90, 91, 106, 107, 365, 225, + 162, 130, 383, 384, 385, 504, 438, 317, 402, 453, + 403, 404, 93, 94, 462, 511, 366, 367, 170, 97, + 295, 368, 169, 223, 287, 288, 335, 369, 370, 427, + 371, 372, 433, 373, 535, 521, 374, 375, 209, 395, + 492, 143, 180, 181, 303, 539, 548, 247, 248, 249, + 376, 432, 377, 378, 431, 379, 250, 251, 212, 156, + 103, 186, 446, 507 +}; + +/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ +#define YYPACT_NINF -484 +static const short int yypact[] = +{ + -484, 79, -484, 506, -484, -484, 29, -484, 127, 127, + 132, 127, 127, 127, 127, 127, 127, 1004, 58, -484, + -484, 139, 157, 176, 127, 127, 127, -484, -484, 179, + 189, -484, -484, -484, 158, -484, -484, -484, -484, -484, + -484, -484, -484, -484, -484, -484, -484, 193, 159, 137, + 1004, 221, 195, -484, -484, 203, 115, 221, 179, 112, + -484, -484, -484, -484, -484, -484, -484, -484, -484, -484, + -484, -484, -484, -484, -484, -484, 69, -484, -484, -484, + -484, -484, -484, -484, -484, -484, -484, -484, -484, -484, + -484, -484, -484, -28, -484, -484, -484, -484, 41, -484, + -484, -484, 12, -484, 204, -484, -484, 92, -484, 207, + -484, -484, -484, -484, -484, -484, 179, -484, 14, -484, + -484, -484, -484, -484, -484, 160, 2, 160, -484, -484, + 115, -484, -484, -484, 178, -484, -484, 19, -484, 128, + 130, -484, 214, -484, 135, -18, 221, -484, -484, 165, + 179, 221, 1004, -484, -39, 240, 47, -484, 46, -484, + -484, -484, 127, 136, 133, 226, 177, 151, 148, 141, + 115, -484, -484, 47, 154, -484, 153, 241, 242, 152, + -14, -484, 867, 179, 115, -484, -40, -484, -484, -484, + -6, -484, 867, 161, 162, 183, 186, 187, 188, 190, + 197, 198, 213, -484, -15, -1, 146, 215, 216, 169, + -484, -484, -484, -484, 266, -484, 39, -484, -484, 17, + -484, 193, 179, 115, 184, 219, -484, -484, 6, 814, + -484, 18, -484, -484, 20, -484, 28, 214, -484, 193, + 193, 193, 193, 193, 201, 235, 193, 225, 234, -484, + 193, 230, -27, 238, 221, -484, -484, 136, 244, 336, + -484, -484, -484, -484, -484, -484, -484, -484, -484, -484, + 249, -484, 251, -484, -484, -484, 867, -484, 127, 127, + 13, -484, -484, -484, 342, -484, 160, 261, -484, 262, + 755, 108, 265, -484, 113, 325, -484, -484, -484, -484, + -484, -484, -484, -484, -484, 230, 230, 230, -484, -484, + 867, 867, -484, 297, 867, 115, -8, -484, -484, 331, + -484, -484, 297, 127, 366, -484, -484, 273, -484, -484, + 270, -484, -484, 179, 755, 272, 127, 278, 127, 127, + 127, 127, 127, 127, 127, 127, 332, 332, 332, 267, + 268, -484, -484, -484, -484, -484, -484, -484, -484, -484, + -484, -484, -484, -484, -484, -484, -484, -484, -484, 589, + -484, -484, -484, -484, 269, -484, -484, -484, -484, -484, + 271, -484, -484, 281, 108, -484, 286, 282, -484, -484, + -484, -484, 287, 288, -484, 298, -484, -484, -484, -484, + -484, -484, 109, 927, -484, -484, 294, 298, -484, 127, + 387, 297, -484, -484, 672, -484, -484, 867, -484, -484, + -484, -484, -484, -484, -484, -484, -484, 283, 284, 285, + -484, 978, 420, 379, 299, -484, 383, -8, 115, 300, + -484, 221, -484, 193, 193, 306, 301, -484, -484, -484, + -484, -484, -484, -8, -484, -484, -484, -484, -484, -484, + -484, -34, -484, 307, 127, 304, 301, -484, 127, -484, + 298, -484, 309, -484, -484, -484, -484, 33, -484, -484, + 278, -484, -484, 311, -484, 310, -484, 81, -484, -484, + 179, 333, 115, -484, 84, -484, 313, 115, -484, 301, + 298, 314, 315, -484, -484, 316, 160, 100, -484, 302, + 109, 102, -484, 302, 115, 115, 298, -484, -484, 179, + 221, 317, -484, -8, 318, 302, 308, 301, 160, 319, + 394, 109, 394, 320, 328, 324, 115, 867, 127, 407, + 407, 394, 867, 394, 326, 335, -484, 127, -484, -484, + 407, 337, -484, 394, 322, -484, -484, 323, 407, -484, + -484, -484 +}; + +/* YYPGOTO[NTERM-NUM]. */ +static const short int yypgoto[] = +{ + -484, -484, 433, -484, -484, -197, -484, -484, -484, 11, + -484, -484, -484, -484, -484, -484, 182, -484, 155, 21, + -484, -484, -484, -484, -484, 211, -484, -484, -484, -484, + 210, 10, -484, -484, 15, -484, -484, -484, -484, -484, + -484, -484, -484, -484, -484, -484, -484, -484, -484, 22, + -484, -484, -484, -484, -484, -484, -484, -484, -484, -484, + -484, -484, -484, -484, -484, -484, -9, -484, 23, -484, + -484, -98, -484, -484, 63, -484, -484, -169, -436, -484, + -484, -376, -20, 303, -484, -484, 24, 26, -484, -484, + 445, 16, -484, -484, -484, 116, -484, 117, -321, -139, + -484, -484, -484, 25, -484, -443, -484, -2, -484, -291, + -433, -119, -484, 217, -484, -472, -483, -171, -484, 138, + -484, -484, 30, -484, -484, 27, 5, -484, 277, -87, + -5, -129, -384, -484 +}; + +/* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule which + number is the opposite. If zero, do what YYDEFACT says. + If YYTABLE_NINF, syntax error. */ +#define YYTABLE_NINF -306 +static const short int yytable[] = +{ + 108, 100, 110, 111, 112, 113, 114, 115, 102, 125, + 127, 171, 117, 72, 65, 122, 123, 124, 73, 99, + 168, 258, 190, 466, 68, 83, 92, 95, 292, 96, + 101, 407, 293, 497, 154, 193, 164, 150, 145, 283, + 230, 174, 233, 150, 150, 136, 278, 279, 435, 173, + 300, 224, 301, 150, 26, 501, 137, 549, 510, 152, + 540, 484, 144, 192, 254, 253, 514, 556, -305, 550, + 524, 552, 278, 279, 142, 561, 318, 493, 255, 4, + 26, 558, 533, 151, 183, 29, 499, 531, 228, 151, + 237, 147, 148, 435, 536, 269, 127, 388, 254, 238, + 398, 399, 270, 400, 289, 327, 155, -174, 401, 271, + 302, 159, 256, 160, 153, -174, 515, 272, 210, 330, + 470, 175, 165, 285, 296, 284, 298, 155, 18, 19, + 380, 104, 527, 18, 19, 20, 389, 390, 391, 392, + 393, 305, 306, 307, 308, 309, 105, 191, 312, 213, + 214, 185, 315, 215, 26, 109, 185, 29, 30, 210, + 118, 211, 119, 252, 31, 32, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, + 120, 47, 48, 49, 505, 254, 50, -169, -169, 132, + 133, 134, 398, 399, 135, 400, 397, 51, 52, 121, + 401, 20, 286, 518, 519, 522, 523, 57, 428, 429, + 58, 126, 128, 59, 60, 129, 131, 138, 447, 387, + 448, 449, 450, 451, 452, 141, 157, 100, 142, 146, + 161, 150, 172, 177, 102, 178, 179, 182, 282, 72, + 65, 216, -129, 20, 73, 99, 472, 187, 218, 219, + 68, 83, 92, 95, 220, 96, 101, 221, 229, 320, + 222, 227, 236, 230, 233, 260, 116, 273, 259, 328, + 329, 276, 31, 32, 33, 34, 35, 36, 37, 38, + 39, 40, 41, 42, 43, 44, 45, 46, 277, 290, + 48, 49, 100, 261, 50, 102, 262, 263, 264, 102, + 265, 381, 282, 310, 72, 65, 382, 266, 267, 73, + 99, 153, 487, 286, 408, 68, 83, 92, 95, 485, + 96, 101, 60, 268, 291, 274, 275, 416, 313, 418, + 419, 420, 421, 422, 423, 424, 425, 311, 314, 102, + 316, 405, 194, 319, 323, 488, 489, 322, 195, 196, + 197, 198, 199, 200, 201, 202, 203, 204, 205, 325, + 206, 326, 207, 208, 332, 333, 545, 334, 386, 29, + 394, 551, 26, 509, 102, 409, 411, 412, 513, 415, + 417, 437, 426, 461, -226, 436, 430, 439, 441, 442, + 443, 444, 464, 445, 381, 525, 526, 468, 463, 382, + 467, 480, 473, 474, 475, 483, 482, 486, 490, 494, + 496, 491, 500, 502, 503, 508, 192, 544, 516, 102, + 512, 537, 520, 517, 530, 532, 538, 541, 534, 476, + 542, 543, 547, 553, 5, 331, 477, 102, 554, 321, + 557, 297, 20, 559, 560, 299, 185, 440, 98, 413, + 226, 414, 396, 189, 304, 495, 0, 0, 481, 498, + 0, 0, 479, 0, 0, 116, 0, 0, 0, 0, + 506, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 478, 0, 48, + 49, 0, 0, 50, 0, 0, 0, 0, 0, 528, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, + 7, 8, 9, 10, 57, 0, 0, 11, 12, 13, + 14, 60, 15, 16, 17, 529, 18, 19, 20, 546, + 0, 0, 0, 0, 0, 0, 0, 0, 555, 0, + 0, 0, 21, 22, 23, 24, 25, 26, 27, 28, + 29, 30, 0, 0, 0, 0, 0, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 0, 47, 48, 49, 0, 0, 50, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 51, 52, 53, 54, 55, 56, 0, 0, 0, 0, + 57, 0, 0, 58, 336, 0, 59, 60, 0, 18, + 19, 337, 0, 0, 0, 338, 339, 340, 341, 342, + 343, 0, 0, 344, 345, 0, 0, 0, 0, 0, + 26, 0, 0, 29, 30, 346, 347, 348, 349, 0, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 350, 47, 48, 49, + 0, 0, 50, 351, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 51, 52, 0, 0, 0, 0, 0, + 0, 0, 0, 57, 0, 0, 58, 336, 352, 0, + 60, 0, 18, 19, 337, 434, 0, 0, 338, 339, + 340, 341, 342, 343, 0, 0, 344, 345, 0, 0, + 0, 0, 0, 26, 0, 0, 29, 30, 346, 347, + 348, 349, 0, 31, 32, 33, 34, 35, 36, 37, + 38, 39, 40, 41, 42, 43, 44, 45, 46, 350, + 47, 48, 49, 0, 0, 50, 351, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 51, 52, 0, 0, + 0, 0, 0, 0, 0, 0, 57, 0, 0, 58, + 336, 352, 0, 60, 0, 18, 19, 337, 471, 0, + 0, 338, 339, 340, 341, 342, 343, 0, 0, 344, + 345, 0, 0, 0, 0, 0, 26, 0, 0, 29, + 30, 346, 347, 348, 349, 0, 31, 32, 33, 34, + 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 350, 47, 48, 49, 0, 0, 50, 351, + 0, 0, 0, 0, 18, 19, 20, 0, 0, 51, + 52, 0, 0, 0, 0, 0, 0, 0, 0, 57, + 0, 0, 58, 0, 352, 26, 60, 0, 29, 30, + 0, 0, 0, 0, 0, 31, 32, 33, 34, 35, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, + 46, 0, 47, 48, 49, 0, 0, 50, 0, 20, + 0, 0, 0, 0, 0, 0, 0, 0, 51, 52, + 0, 0, 0, 0, 0, 0, 0, 0, 57, 0, + 0, 58, 116, 0, 59, 60, 0, 0, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, + 43, 44, 45, 46, 0, 0, 48, 49, 0, 0, + 50, 0, 239, 240, 241, 242, 243, 244, 245, 20, + 0, 454, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 246, 0, 0, 0, 0, 60, 0, + 0, 0, 116, 0, 0, 0, 0, 0, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, + 43, 44, 45, 46, 0, 0, 48, 49, 0, 0, + 20, 0, 0, 0, 0, 0, 0, 0, 0, 455, + 456, 0, 0, 0, 0, 0, 0, 457, 458, 459, + 460, 0, 0, 116, 0, 0, 20, 0, 60, 31, + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 0, 0, 48, 49, 116, + 0, 50, 0, 0, 0, 31, 32, 33, 34, 35, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, + 46, 0, 57, 48, 49, 0, 0, 0, 0, 60, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 60 +}; + +static const short int yycheck[] = +{ + 9, 3, 11, 12, 13, 14, 15, 16, 3, 29, + 30, 130, 17, 3, 3, 24, 25, 26, 3, 3, + 118, 192, 151, 407, 3, 3, 3, 3, 22, 3, + 3, 322, 229, 466, 22, 154, 22, 71, 58, 22, + 22, 22, 22, 71, 71, 50, 33, 34, 369, 136, + 22, 170, 24, 71, 41, 22, 51, 540, 494, 18, + 532, 437, 57, 102, 104, 184, 499, 550, 102, 541, + 513, 543, 33, 34, 113, 558, 103, 453, 118, 0, + 41, 553, 525, 117, 102, 44, 470, 523, 175, 117, + 104, 22, 23, 414, 527, 110, 116, 294, 104, 113, + 108, 109, 117, 111, 223, 276, 94, 105, 116, 110, + 82, 19, 118, 21, 98, 113, 500, 118, 112, 106, + 411, 102, 108, 221, 106, 108, 106, 94, 20, 21, + 22, 102, 516, 20, 21, 22, 305, 306, 307, 310, + 311, 239, 240, 241, 242, 243, 19, 152, 246, 103, + 104, 146, 250, 162, 41, 23, 151, 44, 45, 112, + 102, 114, 23, 183, 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, + 23, 68, 69, 70, 103, 104, 73, 103, 104, 52, + 53, 54, 108, 109, 57, 111, 315, 84, 85, 23, + 116, 22, 222, 103, 104, 103, 104, 94, 347, 348, + 97, 22, 54, 100, 101, 22, 57, 22, 109, 106, + 111, 112, 113, 114, 115, 22, 22, 229, 113, 117, + 23, 71, 54, 105, 229, 105, 22, 102, 216, 229, + 229, 105, 109, 22, 229, 229, 417, 82, 22, 72, + 229, 229, 229, 229, 103, 229, 229, 109, 105, 254, + 119, 107, 110, 22, 22, 103, 45, 121, 107, 278, + 279, 102, 51, 52, 53, 54, 55, 56, 57, 58, + 59, 60, 61, 62, 63, 64, 65, 66, 22, 105, + 69, 70, 294, 110, 73, 290, 110, 110, 110, 294, + 110, 291, 280, 102, 294, 294, 291, 110, 110, 294, + 294, 295, 441, 333, 323, 294, 294, 294, 294, 438, + 294, 294, 101, 110, 105, 110, 110, 336, 103, 338, + 339, 340, 341, 342, 343, 344, 345, 102, 104, 334, + 110, 319, 102, 105, 8, 443, 444, 103, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, 118, 110, + 120, 110, 122, 123, 22, 104, 537, 105, 103, 44, + 73, 542, 41, 492, 369, 9, 103, 107, 497, 107, + 102, 110, 50, 403, 116, 116, 119, 106, 102, 107, + 103, 103, 98, 95, 384, 514, 515, 10, 403, 384, + 409, 22, 119, 119, 119, 22, 107, 107, 102, 102, + 106, 110, 103, 102, 104, 82, 102, 536, 103, 414, + 107, 102, 120, 107, 107, 107, 32, 107, 120, 431, + 102, 107, 25, 107, 1, 280, 431, 432, 103, 257, + 103, 231, 22, 121, 121, 234, 441, 384, 3, 333, + 173, 334, 314, 150, 237, 464, -1, -1, 433, 468, + -1, -1, 432, -1, -1, 45, -1, -1, -1, -1, + 490, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, -1, 69, + 70, -1, -1, 73, -1, -1, -1, -1, -1, 519, + -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, + 4, 5, 6, 7, 94, -1, -1, 11, 12, 13, + 14, 101, 16, 17, 18, 520, 20, 21, 22, 538, + -1, -1, -1, -1, -1, -1, -1, -1, 547, -1, + -1, -1, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, -1, -1, -1, -1, -1, 51, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, -1, 68, 69, 70, -1, -1, 73, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 84, 85, 86, 87, 88, 89, -1, -1, -1, -1, + 94, -1, -1, 97, 15, -1, 100, 101, -1, 20, + 21, 22, -1, -1, -1, 26, 27, 28, 29, 30, + 31, -1, -1, 34, 35, -1, -1, -1, -1, -1, + 41, -1, -1, 44, 45, 46, 47, 48, 49, -1, + 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, + 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, + -1, -1, 73, 74, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 84, 85, -1, -1, -1, -1, -1, + -1, -1, -1, 94, -1, -1, 97, 15, 99, -1, + 101, -1, 20, 21, 22, 106, -1, -1, 26, 27, + 28, 29, 30, 31, -1, -1, 34, 35, -1, -1, + -1, -1, -1, 41, -1, -1, 44, 45, 46, 47, + 48, 49, -1, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, + 68, 69, 70, -1, -1, 73, 74, -1, -1, -1, + -1, -1, -1, -1, -1, -1, 84, 85, -1, -1, + -1, -1, -1, -1, -1, -1, 94, -1, -1, 97, + 15, 99, -1, 101, -1, 20, 21, 22, 106, -1, + -1, 26, 27, 28, 29, 30, 31, -1, -1, 34, + 35, -1, -1, -1, -1, -1, 41, -1, -1, 44, + 45, 46, 47, 48, 49, -1, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, + 65, 66, 67, 68, 69, 70, -1, -1, 73, 74, + -1, -1, -1, -1, 20, 21, 22, -1, -1, 84, + 85, -1, -1, -1, -1, -1, -1, -1, -1, 94, + -1, -1, 97, -1, 99, 41, 101, -1, 44, 45, + -1, -1, -1, -1, -1, 51, 52, 53, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, + 66, -1, 68, 69, 70, -1, -1, 73, -1, 22, + -1, -1, -1, -1, -1, -1, -1, -1, 84, 85, + -1, -1, -1, -1, -1, -1, -1, -1, 94, -1, + -1, 97, 45, -1, 100, 101, -1, -1, 51, 52, + 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, + 63, 64, 65, 66, -1, -1, 69, 70, -1, -1, + 73, -1, 75, 76, 77, 78, 79, 80, 81, 22, + -1, 24, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 96, -1, -1, -1, -1, 101, -1, + -1, -1, 45, -1, -1, -1, -1, -1, 51, 52, + 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, + 63, 64, 65, 66, -1, -1, 69, 70, -1, -1, + 22, -1, -1, -1, -1, -1, -1, -1, -1, 82, + 83, -1, -1, -1, -1, -1, -1, 90, 91, 92, + 93, -1, -1, 45, -1, -1, 22, -1, 101, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 64, 65, 66, -1, -1, 69, 70, 45, + -1, 73, -1, -1, -1, 51, 52, 53, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, + 66, -1, 94, 69, 70, -1, -1, -1, -1, 101, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, 101 +}; + +/* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const unsigned short int yystos[] = +{ + 0, 125, 126, 127, 0, 126, 3, 4, 5, 6, + 7, 11, 12, 13, 14, 16, 17, 18, 20, 21, + 22, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, + 73, 84, 85, 86, 87, 88, 89, 94, 97, 100, + 101, 128, 129, 130, 132, 133, 136, 138, 143, 146, + 150, 151, 155, 158, 159, 160, 161, 164, 165, 166, + 170, 171, 172, 173, 181, 183, 184, 185, 186, 187, + 188, 189, 192, 206, 207, 210, 211, 213, 214, 215, + 231, 249, 250, 254, 102, 19, 190, 191, 190, 23, + 190, 190, 190, 190, 190, 190, 45, 254, 102, 23, + 23, 23, 190, 190, 190, 206, 22, 206, 54, 22, + 195, 57, 52, 53, 54, 57, 254, 250, 22, 152, + 147, 22, 113, 235, 250, 206, 117, 22, 23, 162, + 71, 117, 18, 215, 22, 94, 253, 22, 131, 19, + 21, 23, 194, 137, 22, 108, 156, 157, 195, 216, + 212, 235, 54, 253, 22, 102, 144, 105, 105, 22, + 236, 237, 102, 102, 134, 250, 255, 82, 163, 207, + 255, 254, 102, 235, 102, 108, 109, 110, 111, 112, + 113, 114, 115, 116, 117, 118, 120, 122, 123, 232, + 112, 114, 252, 103, 104, 190, 105, 140, 22, 72, + 103, 109, 119, 217, 235, 193, 252, 107, 253, 105, + 22, 153, 154, 22, 148, 149, 110, 104, 113, 75, + 76, 77, 78, 79, 80, 81, 96, 241, 242, 243, + 250, 251, 206, 235, 104, 118, 118, 139, 241, 107, + 103, 110, 110, 110, 110, 110, 110, 110, 110, 110, + 117, 110, 118, 121, 110, 110, 102, 22, 33, 34, + 141, 142, 173, 22, 108, 195, 206, 218, 219, 235, + 105, 105, 22, 129, 145, 214, 106, 154, 106, 149, + 22, 24, 82, 238, 237, 195, 195, 195, 195, 195, + 102, 102, 195, 103, 104, 195, 110, 201, 103, 105, + 250, 140, 103, 8, 167, 110, 110, 241, 190, 190, + 106, 142, 22, 104, 105, 220, 15, 22, 26, 27, + 28, 29, 30, 31, 34, 35, 46, 47, 48, 49, + 67, 74, 99, 133, 143, 155, 158, 173, 175, 176, + 177, 178, 179, 180, 182, 192, 210, 211, 215, 221, + 222, 224, 225, 227, 230, 231, 244, 246, 247, 249, + 22, 155, 158, 196, 197, 198, 103, 106, 129, 201, + 201, 201, 241, 241, 73, 233, 243, 235, 108, 109, + 111, 116, 202, 204, 205, 173, 174, 233, 190, 9, + 168, 103, 107, 219, 221, 107, 190, 102, 190, 190, + 190, 190, 190, 190, 190, 190, 50, 223, 223, 223, + 119, 248, 245, 226, 106, 222, 116, 110, 200, 106, + 198, 102, 107, 103, 103, 95, 256, 109, 111, 112, + 113, 114, 115, 203, 24, 82, 83, 90, 91, 92, + 93, 206, 208, 254, 98, 135, 256, 190, 10, 169, + 233, 106, 241, 119, 119, 119, 231, 250, 67, 246, + 22, 227, 107, 22, 205, 235, 107, 255, 195, 195, + 102, 110, 234, 205, 102, 190, 106, 234, 190, 256, + 103, 22, 102, 104, 199, 103, 206, 257, 82, 235, + 202, 209, 107, 235, 234, 256, 103, 107, 103, 104, + 120, 229, 103, 104, 229, 235, 235, 256, 206, 250, + 107, 202, 107, 229, 120, 228, 234, 102, 32, 239, + 239, 107, 102, 107, 235, 241, 190, 25, 240, 240, + 239, 241, 239, 107, 103, 190, 240, 103, 239, 121, + 121, 240 +}; + +#if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__) +# define YYSIZE_T __SIZE_TYPE__ +#endif +#if ! defined (YYSIZE_T) && defined (size_t) +# define YYSIZE_T size_t +#endif +#if ! defined (YYSIZE_T) +# if defined (__STDC__) || defined (__cplusplus) +# include <stddef.h> /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# endif +#endif +#if ! defined (YYSIZE_T) +# define YYSIZE_T unsigned int +#endif + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY (-2) +#define YYEOF 0 + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab + + +/* Like YYERROR except do call yyerror. This remains here temporarily + to ease the transition to the new meaning of YYERROR, for GCC. + Once GCC version 2 has supplanted version 1, this can go. */ + +#define YYFAIL goto yyerrlab + +#define YYRECOVERING() (!!yyerrstatus) + +#define YYBACKUP(Token, Value) \ +do \ + if (yychar == YYEMPTY && yylen == 1) \ + { \ + yychar = (Token); \ + yylval = (Value); \ + yytoken = YYTRANSLATE (yychar); \ + YYPOPSTACK; \ + goto yybackup; \ + } \ + else \ + { \ + yyerror ("syntax error: cannot back up");\ + YYERROR; \ + } \ +while (0) + +#define YYTERROR 1 +#define YYERRCODE 256 + +/* YYLLOC_DEFAULT -- Compute the default location (before the actions + are run). */ + +#ifndef YYLLOC_DEFAULT +# define YYLLOC_DEFAULT(Current, Rhs, N) \ + ((Current).first_line = (Rhs)[1].first_line, \ + (Current).first_column = (Rhs)[1].first_column, \ + (Current).last_line = (Rhs)[N].last_line, \ + (Current).last_column = (Rhs)[N].last_column) +#endif + +/* YYLEX -- calling `yylex' with the right arguments. */ + +#ifdef YYLEX_PARAM +# define YYLEX yylex (YYLEX_PARAM) +#else +# define YYLEX yylex () +#endif + +/* Enable debugging if requested. */ +#if YYDEBUG + +# ifndef YYFPRINTF +# include <stdio.h> /* INFRINGES ON USER NAME SPACE */ +# define YYFPRINTF fprintf +# endif + +# define YYDPRINTF(Args) \ +do { \ + if (yydebug) \ + YYFPRINTF Args; \ +} while (0) + +# define YYDSYMPRINT(Args) \ +do { \ + if (yydebug) \ + yysymprint Args; \ +} while (0) + +# define YYDSYMPRINTF(Title, Token, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yysymprint (stderr, \ + Token, Value); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (0) + +/*------------------------------------------------------------------. +| yy_stack_print -- Print the state stack from its BOTTOM up to its | +| TOP (included). | +`------------------------------------------------------------------*/ + +#if defined (__STDC__) || defined (__cplusplus) +static void +yy_stack_print (short int *bottom, short int *top) +#else +static void +yy_stack_print (bottom, top) + short int *bottom; + short int *top; +#endif +{ + YYFPRINTF (stderr, "Stack now"); + for (/* Nothing. */; bottom <= top; ++bottom) + YYFPRINTF (stderr, " %d", *bottom); + YYFPRINTF (stderr, "\n"); +} + +# define YY_STACK_PRINT(Bottom, Top) \ +do { \ + if (yydebug) \ + yy_stack_print ((Bottom), (Top)); \ +} while (0) + + +/*------------------------------------------------. +| Report that the YYRULE is going to be reduced. | +`------------------------------------------------*/ + +#if defined (__STDC__) || defined (__cplusplus) +static void +yy_reduce_print (int yyrule) +#else +static void +yy_reduce_print (yyrule) + int yyrule; +#endif +{ + int yyi; + unsigned int yylno = yyrline[yyrule]; + YYFPRINTF (stderr, "Reducing stack by rule %d (line %u), ", + yyrule - 1, yylno); + /* Print the symbols being reduced, and their result. */ + for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) + YYFPRINTF (stderr, "%s ", yytname [yyrhs[yyi]]); + YYFPRINTF (stderr, "-> %s\n", yytname [yyr1[yyrule]]); +} + +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (yydebug) \ + yy_reduce_print (Rule); \ +} while (0) + +/* Nonzero means print parse trace. It is left uninitialized so that + multiple parsers can coexist. */ +int yydebug; +#else /* !YYDEBUG */ +# define YYDPRINTF(Args) +# define YYDSYMPRINT(Args) +# define YYDSYMPRINTF(Title, Token, Value, Location) +# define YY_STACK_PRINT(Bottom, Top) +# define YY_REDUCE_PRINT(Rule) +#endif /* !YYDEBUG */ + + +/* YYINITDEPTH -- initial size of the parser's stacks. */ +#ifndef YYINITDEPTH +# define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only + if the built-in stack extension method is used). + + Do not make this value too large; the results are undefined if + SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH) + evaluated with infinite-precision integer arithmetic. */ + +#if defined (YYMAXDEPTH) && YYMAXDEPTH == 0 +# undef YYMAXDEPTH +#endif + +#ifndef YYMAXDEPTH +# define YYMAXDEPTH 10000 +#endif + + + +#if YYERROR_VERBOSE + +# ifndef yystrlen +# if defined (__GLIBC__) && defined (_STRING_H) +# define yystrlen strlen +# else +/* Return the length of YYSTR. */ +static YYSIZE_T +# if defined (__STDC__) || defined (__cplusplus) +yystrlen (const char *yystr) +# else +yystrlen (yystr) + const char *yystr; +# endif +{ + register const char *yys = yystr; + + while (*yys++ != '\0') + continue; + + return yys - yystr - 1; +} +# endif +# endif + +# ifndef yystpcpy +# if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE) +# define yystpcpy stpcpy +# else +/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in + YYDEST. */ +static char * +# if defined (__STDC__) || defined (__cplusplus) +yystpcpy (char *yydest, const char *yysrc) +# else +yystpcpy (yydest, yysrc) + char *yydest; + const char *yysrc; +# endif +{ + register char *yyd = yydest; + register const char *yys = yysrc; + + while ((*yyd++ = *yys++) != '\0') + continue; + + return yyd - 1; +} +# endif +# endif + +#endif /* !YYERROR_VERBOSE */ + + + +#if YYDEBUG +/*--------------------------------. +| Print this symbol on YYOUTPUT. | +`--------------------------------*/ + +#if defined (__STDC__) || defined (__cplusplus) +static void +yysymprint (FILE *yyoutput, int yytype, YYSTYPE *yyvaluep) +#else +static void +yysymprint (yyoutput, yytype, yyvaluep) + FILE *yyoutput; + int yytype; + YYSTYPE *yyvaluep; +#endif +{ + /* Pacify ``unused variable'' warnings. */ + (void) yyvaluep; + + if (yytype < YYNTOKENS) + { + YYFPRINTF (yyoutput, "token %s (", yytname[yytype]); +# ifdef YYPRINT + YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); +# endif + } + else + YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]); + + switch (yytype) + { + default: + break; + } + YYFPRINTF (yyoutput, ")"); +} + +#endif /* ! YYDEBUG */ +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ + +#if defined (__STDC__) || defined (__cplusplus) +static void +yydestruct (int yytype, YYSTYPE *yyvaluep) +#else +static void +yydestruct (yytype, yyvaluep) + int yytype; + YYSTYPE *yyvaluep; +#endif +{ + /* Pacify ``unused variable'' warnings. */ + (void) yyvaluep; + + switch (yytype) + { + + default: + break; + } +} + + +/* Prevent warnings from -Wmissing-prototypes. */ + +#ifdef YYPARSE_PARAM +# if defined (__STDC__) || defined (__cplusplus) +int yyparse (void *YYPARSE_PARAM); +# else +int yyparse (); +# endif +#else /* ! YYPARSE_PARAM */ +#if defined (__STDC__) || defined (__cplusplus) +int yyparse (void); +#else +int yyparse (); +#endif +#endif /* ! YYPARSE_PARAM */ + + + +/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; + +/* Number of syntax errors so far. */ +int yynerrs; + + + +/*----------. +| yyparse. | +`----------*/ + +#ifdef YYPARSE_PARAM +# if defined (__STDC__) || defined (__cplusplus) +int yyparse (void *YYPARSE_PARAM) +# else +int yyparse (YYPARSE_PARAM) + void *YYPARSE_PARAM; +# endif +#else /* ! YYPARSE_PARAM */ +#if defined (__STDC__) || defined (__cplusplus) +int +yyparse (void) +#else +int +yyparse () + +#endif +#endif +{ + + register int yystate; + register int yyn; + int yyresult; + /* Number of tokens to shift before error messages enabled. */ + int yyerrstatus; + /* Lookahead token as an internal (translated) token number. */ + int yytoken = 0; + + /* Three stacks and their tools: + `yyss': related to states, + `yyvs': related to semantic values, + `yyls': related to locations. + + Refer to the stacks thru separate pointers, to allow yyoverflow + to reallocate them elsewhere. */ + + /* The state stack. */ + short int yyssa[YYINITDEPTH]; + short int *yyss = yyssa; + register short int *yyssp; + + /* The semantic value stack. */ + YYSTYPE yyvsa[YYINITDEPTH]; + YYSTYPE *yyvs = yyvsa; + register YYSTYPE *yyvsp; + + + +#define YYPOPSTACK (yyvsp--, yyssp--) + + YYSIZE_T yystacksize = YYINITDEPTH; + + /* The variables used to return semantic value and location from the + action routines. */ + YYSTYPE yyval; + + + /* When reducing, the number of symbols on the RHS of the reduced + rule. */ + int yylen; + + YYDPRINTF ((stderr, "Starting parse\n")); + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + + /* Initialize stack pointers. + Waste one element of value and location stack + so that they stay on the same level as the state stack. + The wasted elements are never initialized. */ + + yyssp = yyss; + yyvsp = yyvs; + + + goto yysetstate; + +/*------------------------------------------------------------. +| yynewstate -- Push a new state, which is found in yystate. | +`------------------------------------------------------------*/ + yynewstate: + /* In all cases, when you get here, the value and location stacks + have just been pushed. so pushing a state here evens the stacks. + */ + yyssp++; + + yysetstate: + *yyssp = yystate; + + if (yyss + yystacksize - 1 <= yyssp) + { + /* Get the current used size of the three stacks, in elements. */ + YYSIZE_T yysize = yyssp - yyss + 1; + +#ifdef yyoverflow + { + /* Give user a chance to reallocate the stack. Use copies of + these so that the &'s don't force the real ones into + memory. */ + YYSTYPE *yyvs1 = yyvs; + short int *yyss1 = yyss; + + + /* Each stack pointer address is followed by the size of the + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ + yyoverflow ("parser stack overflow", + &yyss1, yysize * sizeof (*yyssp), + &yyvs1, yysize * sizeof (*yyvsp), + + &yystacksize); + + yyss = yyss1; + yyvs = yyvs1; + } +#else /* no yyoverflow */ +# ifndef YYSTACK_RELOCATE + goto yyoverflowlab; +# else + /* Extend the stack our own way. */ + if (YYMAXDEPTH <= yystacksize) + goto yyoverflowlab; + yystacksize *= 2; + if (YYMAXDEPTH < yystacksize) + yystacksize = YYMAXDEPTH; + + { + short int *yyss1 = yyss; + union yyalloc *yyptr = + (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); + if (! yyptr) + goto yyoverflowlab; + YYSTACK_RELOCATE (yyss); + YYSTACK_RELOCATE (yyvs); + +# undef YYSTACK_RELOCATE + if (yyss1 != yyssa) + YYSTACK_FREE (yyss1); + } +# endif +#endif /* no yyoverflow */ + + yyssp = yyss + yysize - 1; + yyvsp = yyvs + yysize - 1; + + + YYDPRINTF ((stderr, "Stack size increased to %lu\n", + (unsigned long int) yystacksize)); + + if (yyss + yystacksize - 1 <= yyssp) + YYABORT; + } + + YYDPRINTF ((stderr, "Entering state %d\n", yystate)); + + goto yybackup; + +/*-----------. +| yybackup. | +`-----------*/ +yybackup: + +/* Do appropriate processing given the current state. */ +/* Read a lookahead token if we need one and don't already have one. */ +/* yyresume: */ + + /* First try to decide what to do without reference to lookahead token. */ + + yyn = yypact[yystate]; + if (yyn == YYPACT_NINF) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ + if (yychar == YYEMPTY) + { + YYDPRINTF ((stderr, "Reading a token: ")); + yychar = YYLEX; + } + + if (yychar <= YYEOF) + { + yychar = yytoken = YYEOF; + YYDPRINTF ((stderr, "Now at end of input.\n")); + } + else + { + yytoken = YYTRANSLATE (yychar); + YYDSYMPRINTF ("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) + goto yydefault; + yyn = yytable[yyn]; + if (yyn <= 0) + { + if (yyn == 0 || yyn == YYTABLE_NINF) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + if (yyn == YYFINAL) + YYACCEPT; + + /* Shift the lookahead token. */ + YYDPRINTF ((stderr, "Shifting token %s, ", yytname[yytoken])); + + /* Discard the token being shifted unless it is eof. */ + if (yychar != YYEOF) + yychar = YYEMPTY; + + *++yyvsp = yylval; + + + /* Count tokens shifted since error; after three, turn off error + status. */ + if (yyerrstatus) + yyerrstatus--; + + yystate = yyn; + goto yynewstate; + + +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ +yydefault: + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + goto yyreduce; + + +/*-----------------------------. +| yyreduce -- Do a reduction. | +`-----------------------------*/ +yyreduce: + /* yyn is the number of a rule to reduce with. */ + yylen = yyr2[yyn]; + + /* If YYLEN is nonzero, implement the default value of the action: + `$$ = $1'. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. Assigning to YYVAL + unconditionally makes the parser a bit smaller, and it avoids a + GCC warning that YYVAL may be used uninitialized. */ + yyval = yyvsp[1-yylen]; + + + YY_REDUCE_PRINT (yyn); + switch (yyn) + { + case 4: +#line 293 "parser.y" + { + /* + * We don't do these in parserEOF() because the parser + * is reading ahead and that would be too early. + */ + + if (previousFile != NULL) + { + handleEOF(); + + if (newContext.prevmod != NULL) + handleEOM(); + + free(previousFile); + previousFile = NULL; + } + } + break; + + case 17: +#line 323 "parser.y" + { + if (notSkipping()) + appendCodeBlock(¤tSpec->exphdrcode, yyvsp[0].codeb); + } + break; + + case 18: +#line 327 "parser.y" + { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> hdrcode,yyvsp[0].codeb); + } + break; + + case 19: +#line 331 "parser.y" + { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> cppcode,yyvsp[0].codeb); + } + break; + + case 41: +#line 358 "parser.y" + { + if (notSkipping()) + { + classDef *scope = currentScope(); + + if (scope == NULL) + yyerror("%TypeHeaderCode can only be used in a namespace, class or mapped type"); + + appendCodeBlock(&scope->hdrcode, yyvsp[0].codeb); + } + } + break; + + case 43: +#line 374 "parser.y" + { + appendString(¤tSpec->options, yyvsp[0].text); + } + break; + + case 44: +#line 377 "parser.y" + { + appendString(¤tSpec->options, yyvsp[0].text); + } + break; + + case 45: +#line 382 "parser.y" + { + if (notSkipping()) + { + yywarning("%SIPNoEmitters is deprecated, please use %SIPOptions instead"); + appendString(¤tSpec->options, "QtNoEmitters"); + } + } + break; + + case 46: +#line 391 "parser.y" + { + if (notSkipping()) + { + exceptionDef *xd; + char *pyname; + + if (currentSpec->genc) + yyerror("%Exception not allowed in a C module"); + + pyname = getPythonName(&yyvsp[-5].optflags, scopedNameTail(yyvsp[-7].scpvalp)); + + checkAttributes(currentSpec, NULL, pyname, FALSE); + + xd = findException(currentSpec, yyvsp[-7].scpvalp, TRUE); + + if (xd->cd != NULL) + yyerror("%Exception name has already been seen as a class name - it must be defined before being used"); + + if (xd->iff->module != NULL) + yyerror("The %Exception has already been defined"); + + /* Complete the definition. */ + + xd->iff->module = currentModule; + xd->pyname = pyname; + xd->bibase = yyvsp[-6].exceptionbase.bibase; + xd->base = yyvsp[-6].exceptionbase.base; + xd->hdrcode = yyvsp[-3].codeb; + xd->raisecode = yyvsp[-2].codeb; + + if (xd->bibase != NULL || xd->base != NULL) + xd->exceptionnr = currentModule->nrexceptions++; + + if (inMainModule() && xd->base != NULL && xd->base->iff->module != currentModule) + addToUsedList(¤tSpec->used, xd->base->iff); + } + } + break; + + case 47: +#line 430 "parser.y" + { + yyval.exceptionbase.bibase = NULL; + yyval.exceptionbase.base = NULL; + } + break; + + case 48: +#line 434 "parser.y" + { + exceptionDef *xd; + + yyval.exceptionbase.bibase = NULL; + yyval.exceptionbase.base = NULL; + + /* See if it is a defined exception. */ + for (xd = currentSpec->exceptions; xd != NULL; xd = xd->next) + if (sameScopedName(xd->iff->fqcname, yyvsp[-1].scpvalp)) + { + yyval.exceptionbase.base = xd; + break; + } + + if (xd == NULL && yyvsp[-1].scpvalp->next == NULL && strncmp(yyvsp[-1].scpvalp->name, "SIP_", 4) == 0) + { + /* See if it is a builtin exception. */ + + static char *builtins[] = { + "Exception", + "StopIteration", + "StandardError", + "ArithmeticError", + "LookupError", + "AssertionError", + "AttributeError", + "EOFError", + "FloatingPointError", + "EnvironmentError", + "IOError", + "OSError", + "ImportError", + "IndexError", + "KeyError", + "KeyboardInterrupt", + "MemoryError", + "NameError", + "OverflowError", + "RuntimeError", + "NotImplementedError", + "SyntaxError", + "IndentationError", + "TabError", + "ReferenceError", + "SystemError", + "SystemExit", + "TypeError", + "UnboundLocalError", + "UnicodeError", + "UnicodeEncodeError", + "UnicodeDecodeError", + "UnicodeTranslateError", + "ValueError", + "ZeroDivisionError", + "WindowsError", + "VMSError", + NULL + }; + + char **cp; + + for (cp = builtins; *cp != NULL; ++cp) + if (strcmp(yyvsp[-1].scpvalp->name + 4, *cp) == 0) + { + yyval.exceptionbase.bibase = *cp; + break; + } + } + + if (yyval.exceptionbase.bibase == NULL && yyval.exceptionbase.base == NULL) + yyerror("Unknown exception base type"); + } + break; + + case 49: +#line 508 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 50: +#line 513 "parser.y" + { + if (notSkipping()) + currentMappedType = newMappedType(currentSpec,&yyvsp[0].memArg); + } + break; + + case 52: +#line 519 "parser.y" + { + int a; + + if (currentSpec->genc) + yyerror("%MappedType templates not allowed in a C module"); + + /* Check the template arguments are all just simple names. */ + for (a = 0; a < yyvsp[-2].signature.nrArgs; ++a) + if (yyvsp[-2].signature.args[a].atype != defined_type || yyvsp[-2].signature.args[a].u.snd->next != NULL) + yyerror("%MappedType template arguments must be simple names"); + + if (yyvsp[0].memArg.atype != template_type) + yyerror("%MappedType template must map a template type"); + + if (notSkipping()) + { + mappedTypeTmplDef *mtt; + + /* Check a template hasn't already been provided. */ + for (mtt = currentSpec->mappedtypetemplates; mtt != NULL; mtt = mtt->next) + if (sameScopedName(mtt->mt->type.u.td->fqname, yyvsp[0].memArg.u.td->fqname) && sameTemplateSignature(&mtt->mt->type.u.td->types, &yyvsp[0].memArg.u.td->types, TRUE)) + yyerror("%MappedType template for this type has already been defined"); + + yyvsp[0].memArg.nrderefs = 0; + yyvsp[0].memArg.argflags = 0; + + mtt = sipMalloc(sizeof (mappedTypeTmplDef)); + + mtt->sig = yyvsp[-2].signature; + mtt->mt = allocMappedType(&yyvsp[0].memArg); + mtt->next = currentSpec->mappedtypetemplates; + + currentSpec->mappedtypetemplates = mtt; + + currentMappedType = mtt->mt; + } + } + break; + + case 54: +#line 558 "parser.y" + { + if (notSkipping()) + { + if (currentMappedType->convfromcode == NULL) + yyerror("%MappedType must have a %ConvertFromTypeCode directive"); + + if (currentMappedType->convtocode == NULL) + yyerror("%MappedType must have a %ConvertToTypeCode directive"); + + currentMappedType = NULL; + } + } + break; + + case 57: +#line 576 "parser.y" + { + if (notSkipping()) + appendCodeBlock(¤tMappedType -> hdrcode,yyvsp[0].codeb); + } + break; + + case 58: +#line 580 "parser.y" + { + if (notSkipping()) + { + if (currentMappedType -> convfromcode != NULL) + yyerror("%MappedType has more than one %ConvertFromTypeCode directive"); + + currentMappedType -> convfromcode = yyvsp[0].codeb; + } + } + break; + + case 59: +#line 589 "parser.y" + { + if (notSkipping()) + { + if (currentMappedType -> convtocode != NULL) + yyerror("%MappedType has more than one %ConvertToTypeCode directive"); + + currentMappedType -> convtocode = yyvsp[0].codeb; + } + } + break; + + case 60: +#line 600 "parser.y" + { + if (currentSpec -> genc) + yyerror("namespace definition not allowed in a C module"); + + if (notSkipping()) + { + classDef *ns; + + ns = newClass(currentSpec,namespace_iface,text2scopedName(yyvsp[0].text)); + + pushScope(ns); + + sectionFlags = 0; + } + } + break; + + case 61: +#line 614 "parser.y" + { + if (inMainModule()) + { + classDef *ns = currentScope(); + + if (!isUsedName(ns->iff->name)) + { + varDef *vd; + + for (vd = currentSpec->vars; vd != NULL; vd = vd->next) + if (vd->ecd == ns) + { + setIsUsedName(ns->iff->name); + break; + } + } + } + + if (notSkipping()) + popScope(); + } + break; + + case 64: +#line 641 "parser.y" + { + qualDef *qd; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == platform_qualifier) + yyerror("%Platforms has already been defined for this module"); + } + break; + + case 65: +#line 648 "parser.y" + { + qualDef *qd; + int nrneeded; + + /* + * Check that exactly one platform in the set was + * requested. + */ + + nrneeded = 0; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == platform_qualifier && isNeeded(qd)) + ++nrneeded; + + if (nrneeded > 1) + yyerror("No more than one of these %Platforms must be specified with the -t flag"); + } + break; + + case 68: +#line 672 "parser.y" + { + newQualifier(currentModule,-1,-1,yyvsp[0].text,platform_qualifier); + } + break; + + case 69: +#line 677 "parser.y" + { + newQualifier(currentModule,-1,-1,yyvsp[0].text,feature_qualifier); + } + break; + + case 70: +#line 682 "parser.y" + { + currentTimelineOrder = 0; + } + break; + + case 71: +#line 685 "parser.y" + { + qualDef *qd; + int nrneeded; + + /* + * Check that exactly one time slot in the set was + * requested. + */ + + nrneeded = 0; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == time_qualifier && isNeeded(qd)) + ++nrneeded; + + if (nrneeded > 1) + yyerror("At most one of this %Timeline must be specified with the -t flag"); + + currentModule -> nrtimelines++; + } + break; + + case 74: +#line 711 "parser.y" + { + newQualifier(currentModule,currentModule -> nrtimelines,currentTimelineOrder++,yyvsp[0].text,time_qualifier); + } + break; + + case 75: +#line 716 "parser.y" + { + if (skipStackPtr >= MAX_NESTED_IF) + yyerror("Internal error: increase the value of MAX_NESTED_IF"); + + /* Nested %Ifs are implicit logical ands. */ + + if (skipStackPtr > 0) + yyvsp[-1].boolean = (yyvsp[-1].boolean && skipStack[skipStackPtr - 1]); + + skipStack[skipStackPtr++] = yyvsp[-1].boolean; + } + break; + + case 76: +#line 729 "parser.y" + { + yyval.boolean = platOrFeature(yyvsp[0].text,FALSE); + } + break; + + case 77: +#line 732 "parser.y" + { + yyval.boolean = platOrFeature(yyvsp[0].text,TRUE); + } + break; + + case 78: +#line 735 "parser.y" + { + yyval.boolean = (platOrFeature(yyvsp[0].text,FALSE) || yyvsp[-2].boolean); + } + break; + + case 79: +#line 738 "parser.y" + { + yyval.boolean = (platOrFeature(yyvsp[0].text,TRUE) || yyvsp[-3].boolean); + } + break; + + case 81: +#line 744 "parser.y" + { + yyval.boolean = timePeriod(yyvsp[-2].text,yyvsp[0].text); + } + break; + + case 82: +#line 749 "parser.y" + { + if (skipStackPtr-- <= 0) + yyerror("Too many %End directives"); + } + break; + + case 83: +#line 755 "parser.y" + { + optFlag *of; + + if (yyvsp[0].optflags.nrFlags == 0) + yyerror("%License details not specified"); + + if ((of = findOptFlag(&yyvsp[0].optflags,"Type",string_flag)) == NULL) + yyerror("%License type not specified"); + + currentModule -> license = sipMalloc(sizeof (licenseDef)); + + currentModule -> license -> type = of -> fvalue.sval; + + currentModule -> license -> licensee = + ((of = findOptFlag(&yyvsp[0].optflags,"Licensee",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + + currentModule -> license -> timestamp = + ((of = findOptFlag(&yyvsp[0].optflags,"Timestamp",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + + currentModule -> license -> sig = + ((of = findOptFlag(&yyvsp[0].optflags,"Signature",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + } + break; + + case 84: +#line 782 "parser.y" + { + /* Check the module hasn't already been defined. */ + + moduleDef *mod; + + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + if (mod->fullname != NULL && strcmp(mod->fullname, yyvsp[-1].text) == 0) + yyerror("Module is already defined"); + + currentModule->fullname = yyvsp[-1].text; + + if ((currentModule->name = strrchr(yyvsp[-1].text, '.')) != NULL) + currentModule->name++; + else + currentModule->name = yyvsp[-1].text; + + currentModule -> version = yyvsp[0].number; + + if (currentSpec -> genc < 0) + currentSpec -> genc = yyvsp[-2].boolean; + else if (currentSpec -> genc != yyvsp[-2].boolean) + yyerror("Cannot mix C and C++ modules"); + } + break; + + case 85: +#line 807 "parser.y" + { + yyval.boolean = FALSE; + } + break; + + case 86: +#line 810 "parser.y" + { + yyval.boolean = TRUE; + } + break; + + case 88: +#line 816 "parser.y" + { + /* + * The grammar design is a bit broken and this is the + * easiest way to allow periods in module names. + */ + + char *cp; + + for (cp = yyvsp[0].text; *cp != '\0'; ++cp) + if (*cp != '.' && *cp != '_' && !isalnum(*cp)) + yyerror("Invalid character in module name"); + + yyval.text = yyvsp[0].text; + } + break; + + case 89: +#line 832 "parser.y" + { + yyval.number = -1; + } + break; + + case 91: +#line 838 "parser.y" + { + parseFile(NULL,yyvsp[0].text,NULL,FALSE); + } + break; + + case 92: +#line 843 "parser.y" + { + parseFile(NULL,yyvsp[0].text,NULL,TRUE); + } + break; + + case 93: +#line 848 "parser.y" + { + newImport(yyvsp[0].text); + } + break; + + case 94: +#line 853 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 95: +#line 856 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 96: +#line 861 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 97: +#line 864 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 98: +#line 869 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 99: +#line 872 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 100: +#line 877 "parser.y" + { + if (inMainModule()) + appendCodeBlock(¤tSpec -> copying,yyvsp[0].codeb); + } + break; + + case 101: +#line 883 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 102: +#line 888 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 103: +#line 893 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 104: +#line 898 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 106: +#line 904 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 107: +#line 909 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 108: +#line 914 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 109: +#line 919 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 110: +#line 924 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 111: +#line 929 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 112: +#line 934 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 113: +#line 939 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 114: +#line 944 "parser.y" + { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> preinitcode,yyvsp[0].codeb); + } + break; + + case 115: +#line 950 "parser.y" + { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> postinitcode,yyvsp[0].codeb); + } + break; + + case 116: +#line 956 "parser.y" + { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec->unitcode, yyvsp[0].codeb); + } + break; + + case 117: +#line 962 "parser.y" + { + /* + * This is a no-op and is retained for compatibility + * until the last use of it (by SIP v3) can be removed + * from PyQt. + */ + } + break; + + case 118: +#line 971 "parser.y" + { + if (inMainModule()) + appendCodeBlock(¤tSpec -> docs,yyvsp[0].codeb); + } + break; + + case 119: +#line 977 "parser.y" + { + appendCodeBlock(¤tSpec -> docs,yyvsp[0].codeb); + } + break; + + case 120: +#line 982 "parser.y" + { + if (inMainModule()) + yywarning("%Makefile is ignored, please use the -b flag instead"); + } + break; + + case 123: +#line 992 "parser.y" + { + yyval.codeb = yyvsp[-1].codeb; + + append(&yyval.codeb->frag, yyvsp[0].codeb->frag); + + free(yyvsp[0].codeb->frag); + free(yyvsp[0].codeb->filename); + free(yyvsp[0].codeb); + } + break; + + case 124: +#line 1003 "parser.y" + { + if (notSkipping()) + { + if (sectionFlags != 0 && (sectionFlags & ~(SECT_IS_PUBLIC | SECT_IS_PROT)) != 0) + yyerror("Class enums must be in the public or protected sections"); + + currentEnum = newEnum(currentSpec,currentModule,yyvsp[-1].text,&yyvsp[0].optflags,sectionFlags); + } + } + break; + + case 126: +#line 1014 "parser.y" + { + yyval.text = NULL; + } + break; + + case 127: +#line 1017 "parser.y" + { + yyval.text = yyvsp[0].text; + } + break; + + case 128: +#line 1022 "parser.y" + { + yyval.text = NULL; + } + break; + + case 129: +#line 1025 "parser.y" + { + yyval.text = yyvsp[0].text; + } + break; + + case 136: +#line 1040 "parser.y" + { + if (notSkipping()) + { + /* + * Note that we don't use the assigned value. + * This is a hangover from when enums where + * generated in Python. We can remove it when + * we have got around to updating all the .sip + * files. + */ + enumMemberDef *emd, **tail; + + emd = sipMalloc(sizeof (enumMemberDef)); + + emd -> pyname = cacheName(currentSpec, getPythonName(&yyvsp[-1].optflags, yyvsp[-3].text)); + emd -> cname = yyvsp[-3].text; + emd -> ed = currentEnum; + emd -> next = NULL; + + checkAttributes(currentSpec,emd -> ed -> ecd,emd -> pyname -> text,FALSE); + + /* Append to preserve the order. */ + for (tail = ¤tEnum->members; *tail != NULL; tail = &(*tail)->next) + ; + + *tail = emd; + + if (inMainModule()) + setIsUsedName(emd -> pyname); + } + } + break; + + case 141: +#line 1081 "parser.y" + { + yyval.valp = NULL; + } + break; + + case 142: +#line 1084 "parser.y" + { + yyval.valp = yyvsp[0].valp; + } + break; + + case 144: +#line 1090 "parser.y" + { + valueDef *vd; + + if (yyvsp[-2].valp -> vtype == string_value || yyvsp[0].valp -> vtype == string_value) + yyerror("Invalid binary operator for string"); + + /* Find the last value in the existing expression. */ + + for (vd = yyvsp[-2].valp; vd -> next != NULL; vd = vd -> next) + ; + + vd -> vbinop = yyvsp[-1].qchar; + vd -> next = yyvsp[0].valp; + + yyval.valp = yyvsp[-2].valp; + } + break; + + case 145: +#line 1108 "parser.y" + { + yyval.qchar = '-'; + } + break; + + case 146: +#line 1111 "parser.y" + { + yyval.qchar = '+'; + } + break; + + case 147: +#line 1114 "parser.y" + { + yyval.qchar = '*'; + } + break; + + case 148: +#line 1117 "parser.y" + { + yyval.qchar = '/'; + } + break; + + case 149: +#line 1120 "parser.y" + { + yyval.qchar = '&'; + } + break; + + case 150: +#line 1123 "parser.y" + { + yyval.qchar = '|'; + } + break; + + case 151: +#line 1128 "parser.y" + { + yyval.qchar = '\0'; + } + break; + + case 152: +#line 1131 "parser.y" + { + yyval.qchar = '!'; + } + break; + + case 153: +#line 1134 "parser.y" + { + yyval.qchar = '~'; + } + break; + + case 154: +#line 1137 "parser.y" + { + yyval.qchar = '-'; + } + break; + + case 155: +#line 1140 "parser.y" + { + yyval.qchar = '+'; + } + break; + + case 156: +#line 1145 "parser.y" + { + if (yyvsp[-1].qchar != '\0' && yyvsp[0].value.vtype == string_value) + yyerror("Invalid unary operator for string"); + + /* + * Convert the value to a simple expression on the + * heap. + */ + + yyval.valp = sipMalloc(sizeof (valueDef)); + + *yyval.valp = yyvsp[0].value; + yyval.valp -> vunop = yyvsp[-1].qchar; + yyval.valp -> vbinop = '\0'; + yyval.valp -> next = NULL; + } + break; + + case 158: +#line 1164 "parser.y" + { + if (currentSpec -> genc) + yyerror("Scoped names are not allowed in a C module"); + + appendScopedName(&yyvsp[-2].scpvalp,yyvsp[0].scpvalp); + } + break; + + case 159: +#line 1172 "parser.y" + { + yyval.scpvalp = text2scopePart(yyvsp[0].text); + } + break; + + case 160: +#line 1177 "parser.y" + { + /* + * We let the C++ compiler decide if the value is a + * valid one - no point in building a full C++ parser + * here. + */ + + yyval.value.vtype = scoped_value; + yyval.value.u.vscp = yyvsp[0].scpvalp; + } + break; + + case 161: +#line 1187 "parser.y" + { + fcallDef *fcd; + + fcd = sipMalloc(sizeof (fcallDef)); + *fcd = yyvsp[-1].fcall; + fcd -> type = yyvsp[-3].memArg; + + yyval.value.vtype = fcall_value; + yyval.value.u.fcd = fcd; + } + break; + + case 162: +#line 1197 "parser.y" + { + yyval.value.vtype = real_value; + yyval.value.u.vreal = yyvsp[0].real; + } + break; + + case 163: +#line 1201 "parser.y" + { + yyval.value.vtype = numeric_value; + yyval.value.u.vnum = yyvsp[0].number; + } + break; + + case 164: +#line 1205 "parser.y" + { + yyval.value.vtype = numeric_value; + yyval.value.u.vnum = 1; + } + break; + + case 165: +#line 1209 "parser.y" + { + yyval.value.vtype = numeric_value; + yyval.value.u.vnum = 0; + } + break; + + case 166: +#line 1213 "parser.y" + { + yyval.value.vtype = numeric_value; + yyval.value.u.vnum = 0; + } + break; + + case 167: +#line 1217 "parser.y" + { + yyval.value.vtype = string_value; + yyval.value.u.vstr = yyvsp[0].text; + } + break; + + case 168: +#line 1221 "parser.y" + { + yyval.value.vtype = qchar_value; + yyval.value.u.vqchar = yyvsp[0].qchar; + } + break; + + case 169: +#line 1227 "parser.y" + { + /* No values. */ + + yyval.fcall.nrArgs = 0; + } + break; + + case 170: +#line 1232 "parser.y" + { + /* The single or first expression. */ + + yyval.fcall.args[0] = yyvsp[0].valp; + yyval.fcall.nrArgs = 1; + } + break; + + case 171: +#line 1238 "parser.y" + { + /* Check that it wasn't ...(,expression...). */ + + if (yyval.fcall.nrArgs == 0) + yyerror("First argument to function call is missing"); + + /* Check there is room. */ + + if (yyvsp[-2].fcall.nrArgs == MAX_NR_ARGS) + yyerror("Too many arguments to function call"); + + yyval.fcall = yyvsp[-2].fcall; + + yyval.fcall.args[yyval.fcall.nrArgs] = yyvsp[0].valp; + yyval.fcall.nrArgs++; + } + break; + + case 172: +#line 1256 "parser.y" + { + if (notSkipping()) + newTypedef(currentSpec,currentModule,yyvsp[-1].text,&yyvsp[-2].memArg); + } + break; + + case 173: +#line 1260 "parser.y" + { + if (notSkipping()) + { + argDef ftype; + signatureDef *sig; + + /* Create the full signature on the heap. */ + sig = sipMalloc(sizeof (signatureDef)); + *sig = yyvsp[-2].signature; + sig -> result = yyvsp[-8].memArg; + + /* Create the full type. */ + ftype.atype = function_type; + ftype.argflags = 0; + ftype.nrderefs = yyvsp[-6].number; + ftype.defval = NULL; + ftype.u.sa = sig; + + newTypedef(currentSpec,currentModule,yyvsp[-5].text,&ftype); + } + } + break; + + case 174: +#line 1283 "parser.y" + { + if (notSkipping()) + { + classDef *cd; + + cd = newClass(currentSpec,class_iface,text2scopedName(yyvsp[0].text)); + + pushScope(cd); + + sectionFlags = SECT_IS_PUBLIC; + } + } + break; + + case 175: +#line 1294 "parser.y" + { + if (notSkipping()) + { + finishClass(currentSpec, currentModule, currentScope(), &yyvsp[-4].optflags); + popScope(); + } + } + break; + + case 176: +#line 1303 "parser.y" + { + if (currentSpec->genc) + yyerror("Class templates not allowed in a C module"); + + if (notSkipping()) + { + classTmplDef *tcd; + + /* + * Make sure there is room for the extra class + * name argument. + */ + if (yyvsp[-1].signature.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + tcd = sipMalloc(sizeof (classTmplDef)); + tcd->sig = yyvsp[-1].signature; + tcd->cd = yyvsp[0].klass; + tcd->next = currentSpec->classtemplates; + + currentSpec->classtemplates = tcd; + } + } + break; + + case 177: +#line 1328 "parser.y" + { + yyval.signature = yyvsp[-1].signature; + } + break; + + case 178: +#line 1333 "parser.y" + { + if (currentSpec -> genc) + yyerror("Class definition not allowed in a C module"); + + if (notSkipping()) + { + classDef *cd; + + cd = newClass(currentSpec, class_iface, scopeScopedName(yyvsp[0].scpvalp)); + + pushScope(cd); + + sectionFlags = SECT_IS_PRIVATE; + } + } + break; + + case 179: +#line 1347 "parser.y" + { + if (notSkipping()) + { + classDef *cd = currentScope(); + + /* + * See if the class was defined or just + * declared. + */ + if (yyvsp[-1].boolean) + { + if (yyvsp[-5].scpvalp->next != NULL) + yyerror("A scoped name cannot be given in a class definition"); + + } + else if (cd->supers != NULL) + yyerror("Class has super-classes but no definition"); + else + setIsOpaque(cd); + + finishClass(currentSpec, currentModule, cd, &yyvsp[-2].optflags); + popScope(); + + /* + * Check that external classes have only been + * declared at the global scope. + */ + if (isExternal(cd) && currentScope() != NULL) + yyerror("External classes can only be declared in the global scope"); + + yyval.klass = cd; + } + } + break; + + case 184: +#line 1390 "parser.y" + { + if (notSkipping()) + { + classDef *cd, *super; + + cd = currentScope(); + + super = findClass(currentSpec,class_iface,yyvsp[0].scpvalp); + + appendToClassList(&cd -> supers,super); + addToUsedList(&cd->iff->used, super->iff); + } + } + break; + + case 185: +#line 1405 "parser.y" + { + yyval.boolean = FALSE; + } + break; + + case 186: +#line 1408 "parser.y" + { + yyval.boolean = TRUE; + } + break; + + case 197: +#line 1425 "parser.y" + { + if (notSkipping()) + appendCodeBlock(¤tScope() -> cppcode,yyvsp[0].codeb); + } + break; + + case 198: +#line 1429 "parser.y" + { + if (notSkipping()) + appendCodeBlock(¤tScope() -> hdrcode,yyvsp[0].codeb); + } + break; + + case 199: +#line 1433 "parser.y" + { + if (currentScope()->travcode != NULL) + yyerror("%GCTraverseCode already given for class"); + + if (notSkipping()) + currentScope()->travcode = yyvsp[0].codeb; + } + break; + + case 200: +#line 1440 "parser.y" + { + if (currentScope()->clearcode != NULL) + yyerror("%GCClearCode already given for class"); + + if (notSkipping()) + currentScope()->clearcode = yyvsp[0].codeb; + } + break; + + case 201: +#line 1447 "parser.y" + { + if (currentScope()->readbufcode != NULL) + yyerror("%BIGetReadBufferCode already given for class"); + + if (notSkipping()) + currentScope()->readbufcode = yyvsp[0].codeb; + } + break; + + case 202: +#line 1454 "parser.y" + { + if (currentScope()->writebufcode != NULL) + yyerror("%BIGetWriteBufferCode already given for class"); + + if (notSkipping()) + currentScope()->writebufcode = yyvsp[0].codeb; + } + break; + + case 203: +#line 1461 "parser.y" + { + if (currentScope()->segcountcode != NULL) + yyerror("%BIGetSegCountCode already given for class"); + + if (notSkipping()) + currentScope()->segcountcode = yyvsp[0].codeb; + } + break; + + case 204: +#line 1468 "parser.y" + { + if (currentScope()->charbufcode != NULL) + yyerror("%BIGetCharBufferCode already given for class"); + + if (notSkipping()) + currentScope()->charbufcode = yyvsp[0].codeb; + } + break; + + case 208: +#line 1478 "parser.y" + { + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (cd -> convtosubcode != NULL) + yyerror("Class has more than one %ConvertToSubClassCode directive"); + + cd -> convtosubcode = yyvsp[0].codeb; + } + } + break; + + case 209: +#line 1489 "parser.y" + { + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (cd -> convtocode != NULL) + yyerror("Class has more than one %ConvertToTypeCode directive"); + + cd -> convtocode = yyvsp[0].codeb; + } + } + break; + + case 210: +#line 1500 "parser.y" + { + if (currentSpec -> genc) + yyerror("public section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PUBLIC | yyvsp[-1].number; + } + break; + + case 211: +#line 1507 "parser.y" + { + if (currentSpec -> genc) + yyerror("protected section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PROT | yyvsp[-1].number; + } + break; + + case 212: +#line 1514 "parser.y" + { + if (currentSpec -> genc) + yyerror("private section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PRIVATE | yyvsp[-1].number; + } + break; + + case 213: +#line 1521 "parser.y" + { + if (currentSpec -> genc) + yyerror("signals section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_SIGNAL; + } + break; + + case 214: +#line 1530 "parser.y" + { + yyval.number = 0; + } + break; + + case 215: +#line 1533 "parser.y" + { + yyval.number = SECT_IS_SLOT; + } + break; + + case 216: +#line 1538 "parser.y" + { + /* Note that we allow non-virtual dtors in C modules. */ + + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (strcmp(classBaseName(cd),yyvsp[-8].text) != 0) + yyerror("Destructor doesn't have the same name as its class"); + + if (isDtor(cd)) + yyerror("Destructor has already been defined"); + + if (currentSpec -> genc && yyvsp[-1].codeb == NULL) + yyerror("Destructor in C modules must include %MethodCode"); + + cd -> dealloccode = yyvsp[-1].codeb; + cd -> dtorcode = yyvsp[0].codeb; + cd -> dtorexceptions = yyvsp[-5].throwlist; + cd -> classflags |= sectionFlags; + + if (yyvsp[-4].number) + { + if (!yyvsp[-10].number) + yyerror("Abstract destructor must be virtual"); + + setIsAbstractClass(cd); + } + + /* + * The class has a shadow if we have a virtual dtor or some + * dtor code. + */ + if (yyvsp[-10].number || yyvsp[0].codeb != NULL) + { + if (currentSpec -> genc) + yyerror("Virtual destructor or %VirtualCatcherCode not allowed in a C module"); + + setHasShadow(cd); + } + + if (getReleaseGIL(&yyvsp[-3].optflags)) + setIsReleaseGILDtor(cd); + else if (getHoldGIL(&yyvsp[-3].optflags)) + setIsHoldGILDtor(cd); + } + } + break; + + case 217: +#line 1587 "parser.y" + {currentCtorIsExplicit = TRUE;} + break; + + case 220: +#line 1591 "parser.y" + { + /* Note that we allow ctors in C modules. */ + + if (notSkipping()) + { + if (currentSpec -> genc) + { + if (yyvsp[0].codeb == NULL && yyvsp[-6].signature.nrArgs != 0) + yyerror("Constructors with arguments in C modules must include %MethodCode"); + + if (currentCtorIsExplicit) + yyerror("Explicit constructors not allowed in a C module"); + } + + if ((sectionFlags & (SECT_IS_PUBLIC | SECT_IS_PROT | SECT_IS_PRIVATE)) == 0) + yyerror("Constructor must be in the public, private or protected sections"); + + newCtor(yyvsp[-8].text,sectionFlags,&yyvsp[-6].signature,&yyvsp[-3].optflags,yyvsp[0].codeb,yyvsp[-4].throwlist,yyvsp[-2].optsignature,currentCtorIsExplicit); + } + + free(yyvsp[-8].text); + + currentCtorIsExplicit = FALSE; + } + break; + + case 221: +#line 1617 "parser.y" + { + yyval.optsignature = NULL; + } + break; + + case 222: +#line 1620 "parser.y" + { + yyval.optsignature = sipMalloc(sizeof (signatureDef)); + + *yyval.optsignature = yyvsp[-2].signature; + } + break; + + case 223: +#line 1627 "parser.y" + { + yyval.optsignature = NULL; + } + break; + + case 224: +#line 1630 "parser.y" + { + yyval.optsignature = sipMalloc(sizeof (signatureDef)); + + *yyval.optsignature = yyvsp[-2].signature; + yyval.optsignature -> result = yyvsp[-4].memArg; + } + break; + + case 225: +#line 1638 "parser.y" + { + yyval.number = FALSE; + } + break; + + case 226: +#line 1641 "parser.y" + { + yyval.number = TRUE; + } + break; + + case 227: +#line 1646 "parser.y" + { + if (notSkipping()) + { + if (sectionFlags != 0 && (sectionFlags & (SECT_IS_PUBLIC | SECT_IS_PROT | SECT_IS_PRIVATE | SECT_IS_SLOT | SECT_IS_SIGNAL)) == 0) + yyerror("Class function must be in the public, private, protected, slot or signal sections"); + + yyvsp[-9].signature.result = yyvsp[-12].memArg; + + newFunction(currentSpec,currentModule, + sectionFlags,currentIsStatic, + currentOverIsVirt, + yyvsp[-11].text,&yyvsp[-9].signature,yyvsp[-7].number,yyvsp[-5].number,&yyvsp[-4].optflags,yyvsp[-1].codeb,yyvsp[0].codeb,yyvsp[-6].throwlist,yyvsp[-3].optsignature); + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + break; + + case 228: +#line 1663 "parser.y" + { + if (notSkipping()) + { + classDef *cd = currentScope(); + + /* Handle the unary '+' and '-' operators. */ + if ((cd != NULL && yyvsp[-9].signature.nrArgs == 0) || (cd == NULL && yyvsp[-9].signature.nrArgs == 1)) + { + if (strcmp(yyvsp[-11].text, "__add__") == 0) + yyvsp[-11].text = "__pos__"; + else if (strcmp(yyvsp[-11].text, "__sub__") == 0) + yyvsp[-11].text = "__neg__"; + } + + yyvsp[-9].signature.result = yyvsp[-13].memArg; + + newFunction(currentSpec,currentModule, + sectionFlags,currentIsStatic, + currentOverIsVirt, + yyvsp[-11].text,&yyvsp[-9].signature,yyvsp[-7].number,yyvsp[-5].number,&yyvsp[-4].optflags,yyvsp[-1].codeb,yyvsp[0].codeb,yyvsp[-6].throwlist,yyvsp[-3].optsignature); + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + break; + + case 229: +#line 1688 "parser.y" + { + classDef *scope = currentScope(); + + if (scope == NULL || yyvsp[-9].signature.nrArgs != 0) + yyerror("Operator casts must be specified in a class and have no arguments"); + + + if (notSkipping()) + { + char *sname; + + switch (yyvsp[-11].memArg.atype) + { + case defined_type: + sname = NULL; + break; + + case bool_type: + case cbool_type: + case short_type: + case ushort_type: + case int_type: + case cint_type: + case uint_type: + sname = "__int__"; + break; + + case long_type: + case ulong_type: + case longlong_type: + case ulonglong_type: + sname = "__long__"; + break; + + case float_type: + case cfloat_type: + case double_type: + case cdouble_type: + sname = "__float__"; + break; + + default: + yyerror("Unsupported operator cast"); + } + + if (sname != NULL) + { + yyvsp[-9].signature.result = yyvsp[-11].memArg; + + newFunction(currentSpec, currentModule, + sectionFlags, + currentIsStatic, + currentOverIsVirt, sname, + &yyvsp[-9].signature, yyvsp[-7].number, yyvsp[-5].number, &yyvsp[-4].optflags, yyvsp[-1].codeb, yyvsp[0].codeb, + yyvsp[-6].throwlist, yyvsp[-3].optsignature); + } + else + { + argList *al; + + /* Check it doesn't already exist. */ + for (al = scope->casts; al != NULL; al = al->next) + if (sameScopedName(yyvsp[-11].memArg.u.snd, al->arg.u.snd)) + yyerror("This operator cast has already been specified in this class"); + + al = sipMalloc(sizeof (argList)); + al->arg = yyvsp[-11].memArg; + al->next = scope->casts; + + scope->casts = al; + } + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + break; + + case 230: +#line 1766 "parser.y" + {yyval.text = "__add__";} + break; + + case 231: +#line 1767 "parser.y" + {yyval.text = "__sub__";} + break; + + case 232: +#line 1768 "parser.y" + {yyval.text = "__mul__";} + break; + + case 233: +#line 1769 "parser.y" + {yyval.text = "__div__";} + break; + + case 234: +#line 1770 "parser.y" + {yyval.text = "__mod__";} + break; + + case 235: +#line 1771 "parser.y" + {yyval.text = "__and__";} + break; + + case 236: +#line 1772 "parser.y" + {yyval.text = "__or__";} + break; + + case 237: +#line 1773 "parser.y" + {yyval.text = "__xor__";} + break; + + case 238: +#line 1774 "parser.y" + {yyval.text = "__lshift__";} + break; + + case 239: +#line 1775 "parser.y" + {yyval.text = "__rshift__";} + break; + + case 240: +#line 1776 "parser.y" + {yyval.text = "__iadd__";} + break; + + case 241: +#line 1777 "parser.y" + {yyval.text = "__isub__";} + break; + + case 242: +#line 1778 "parser.y" + {yyval.text = "__imul__";} + break; + + case 243: +#line 1779 "parser.y" + {yyval.text = "__idiv__";} + break; + + case 244: +#line 1780 "parser.y" + {yyval.text = "__imod__";} + break; + + case 245: +#line 1781 "parser.y" + {yyval.text = "__iand__";} + break; + + case 246: +#line 1782 "parser.y" + {yyval.text = "__ior__";} + break; + + case 247: +#line 1783 "parser.y" + {yyval.text = "__ixor__";} + break; + + case 248: +#line 1784 "parser.y" + {yyval.text = "__ilshift__";} + break; + + case 249: +#line 1785 "parser.y" + {yyval.text = "__irshift__";} + break; + + case 250: +#line 1786 "parser.y" + {yyval.text = "__invert__";} + break; + + case 251: +#line 1787 "parser.y" + {yyval.text = "__call__";} + break; + + case 252: +#line 1788 "parser.y" + {yyval.text = "__getitem__";} + break; + + case 253: +#line 1789 "parser.y" + {yyval.text = "__lt__";} + break; + + case 254: +#line 1790 "parser.y" + {yyval.text = "__le__";} + break; + + case 255: +#line 1791 "parser.y" + {yyval.text = "__eq__";} + break; + + case 256: +#line 1792 "parser.y" + {yyval.text = "__ne__";} + break; + + case 257: +#line 1793 "parser.y" + {yyval.text = "__gt__";} + break; + + case 258: +#line 1794 "parser.y" + {yyval.text = "__ge__";} + break; + + case 259: +#line 1797 "parser.y" + { + yyval.number = FALSE; + } + break; + + case 260: +#line 1800 "parser.y" + { + yyval.number = TRUE; + } + break; + + case 261: +#line 1805 "parser.y" + { + yyval.number = 0; + } + break; + + case 262: +#line 1808 "parser.y" + { + if (yyvsp[0].number != 0) + yyerror("Abstract virtual function '= 0' expected"); + + yyval.number = TRUE; + } + break; + + case 263: +#line 1816 "parser.y" + { + yyval.optflags.nrFlags = 0; + } + break; + + case 264: +#line 1819 "parser.y" + { + yyval.optflags = yyvsp[-1].optflags; + } + break; + + case 265: +#line 1825 "parser.y" + { + yyval.optflags.flags[0] = yyvsp[0].flag; + yyval.optflags.nrFlags = 1; + } + break; + + case 266: +#line 1829 "parser.y" + { + /* Check there is room. */ + + if (yyvsp[-2].optflags.nrFlags == MAX_NR_FLAGS) + yyerror("Too many optional flags"); + + yyval.optflags = yyvsp[-2].optflags; + + yyval.optflags.flags[yyval.optflags.nrFlags++] = yyvsp[0].flag; + } + break; + + case 267: +#line 1841 "parser.y" + { + yyval.flag.ftype = bool_flag; + yyval.flag.fname = yyvsp[0].text; + } + break; + + case 268: +#line 1845 "parser.y" + { + yyval.flag = yyvsp[0].flag; + yyval.flag.fname = yyvsp[-2].text; + } + break; + + case 269: +#line 1851 "parser.y" + { + yyval.flag.ftype = name_flag; + yyval.flag.fvalue.sval = yyvsp[0].text; + } + break; + + case 270: +#line 1855 "parser.y" + { + yyval.flag.ftype = string_flag; + yyval.flag.fvalue.sval = yyvsp[0].text; + } + break; + + case 271: +#line 1859 "parser.y" + { + yyval.flag.ftype = integer_flag; + yyval.flag.fvalue.ival = yyvsp[0].number; + } + break; + + case 272: +#line 1865 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 273: +#line 1868 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 274: +#line 1873 "parser.y" + { + yyval.codeb = NULL; + } + break; + + case 275: +#line 1876 "parser.y" + { + yyval.codeb = yyvsp[0].codeb; + } + break; + + case 276: +#line 1881 "parser.y" + { + int a, nrrxcon, nrrxdis, nrslotcon, nrslotdis, nrarray, nrarraysize; + + nrrxcon = nrrxdis = nrslotcon = nrslotdis = nrarray = nrarraysize = 0; + + for (a = 0; a < yyvsp[0].signature.nrArgs; ++a) + { + argDef *ad = &yyvsp[0].signature.args[a]; + + switch (ad -> atype) + { + case rxcon_type: + ++nrrxcon; + break; + + case rxdis_type: + ++nrrxdis; + break; + + case slotcon_type: + ++nrslotcon; + break; + + case slotdis_type: + ++nrslotdis; + break; + } + + if (isArray(ad)) + ++nrarray; + + if (isArraySize(ad)) + ++nrarraysize; + } + + if (nrrxcon != nrslotcon || nrrxcon > 1) + yyerror("SIP_RXOBJ_CON and SIP_SLOT_CON must both be given and at most once"); + + if (nrrxdis != nrslotdis || nrrxdis > 1) + yyerror("SIP_RXOBJ_DIS and SIP_SLOT_DIS must both be given and at most once"); + + if (nrarray != nrarraysize || nrarray > 1) + yyerror("/Array/ and /ArraySize/ must both be given and at most once"); + + yyval.signature = yyvsp[0].signature; + } + break; + + case 277: +#line 1929 "parser.y" + { + /* No arguments. */ + + yyval.signature.nrArgs = 0; + } + break; + + case 278: +#line 1934 "parser.y" + { + /* The single or first argument. */ + + yyval.signature.args[0] = yyvsp[0].memArg; + yyval.signature.nrArgs = 1; + } + break; + + case 279: +#line 1940 "parser.y" + { + /* Check that it wasn't ...(,arg...). */ + if (yyvsp[-2].signature.nrArgs == 0) + yyerror("First argument of the list is missing"); + + /* Check there is nothing after an ellipsis. */ + if (yyvsp[-2].signature.args[yyvsp[-2].signature.nrArgs - 1].atype == ellipsis_type) + yyerror("An ellipsis must be at the end of the argument list"); + + /* + * If this argument has no default value, then the + * previous one mustn't either. + */ + if (yyvsp[0].memArg.defval == NULL && yyvsp[-2].signature.args[yyvsp[-2].signature.nrArgs - 1].defval != NULL) + yyerror("Compulsory argument given after optional argument"); + + /* Check there is room. */ + if (yyvsp[-2].signature.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + yyval.signature = yyvsp[-2].signature; + + yyval.signature.args[yyval.signature.nrArgs] = yyvsp[0].memArg; + yyval.signature.nrArgs++; + } + break; + + case 280: +#line 1967 "parser.y" + { + yyval.memArg.atype = signal_type; + yyval.memArg.argflags = ARG_IS_CONST; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[-1].text; + yyval.memArg.defval = yyvsp[0].valp; + + currentSpec -> sigslots = TRUE; + } + break; + + case 281: +#line 1976 "parser.y" + { + yyval.memArg.atype = slot_type; + yyval.memArg.argflags = ARG_IS_CONST; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[-1].text; + yyval.memArg.defval = yyvsp[0].valp; + + currentSpec -> sigslots = TRUE; + } + break; + + case 282: +#line 1985 "parser.y" + { + yyval.memArg.atype = anyslot_type; + yyval.memArg.argflags = ARG_IS_CONST; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[-1].text; + yyval.memArg.defval = yyvsp[0].valp; + + currentSpec -> sigslots = TRUE; + } + break; + + case 283: +#line 1994 "parser.y" + { + yyval.memArg.atype = rxcon_type; + yyval.memArg.argflags = 0; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[0].text; + + currentSpec -> sigslots = TRUE; + } + break; + + case 284: +#line 2002 "parser.y" + { + yyval.memArg.atype = rxdis_type; + yyval.memArg.argflags = 0; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[0].text; + + currentSpec -> sigslots = TRUE; + } + break; + + case 285: +#line 2010 "parser.y" + { + yyval.memArg.atype = slotcon_type; + yyval.memArg.argflags = ARG_IS_CONST; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[0].text; + + yyvsp[-2].signature.result.atype = void_type; + yyvsp[-2].signature.result.argflags = 0; + yyvsp[-2].signature.result.nrderefs = 0; + + yyval.memArg.u.sa = sipMalloc(sizeof (signatureDef)); + *yyval.memArg.u.sa = yyvsp[-2].signature; + + currentSpec -> sigslots = TRUE; + } + break; + + case 286: +#line 2025 "parser.y" + { + yyval.memArg.atype = slotdis_type; + yyval.memArg.argflags = ARG_IS_CONST; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[0].text; + + yyvsp[-2].signature.result.atype = void_type; + yyvsp[-2].signature.result.argflags = 0; + yyvsp[-2].signature.result.nrderefs = 0; + + yyval.memArg.u.sa = sipMalloc(sizeof (signatureDef)); + *yyval.memArg.u.sa = yyvsp[-2].signature; + + currentSpec -> sigslots = TRUE; + } + break; + + case 287: +#line 2040 "parser.y" + { + yyval.memArg.atype = qobject_type; + yyval.memArg.argflags = 0; + yyval.memArg.nrderefs = 0; + yyval.memArg.name = yyvsp[0].text; + } + break; + + case 288: +#line 2046 "parser.y" + { + yyval.memArg = yyvsp[-1].memArg; + yyval.memArg.defval = yyvsp[0].valp; + } + break; + + case 289: +#line 2052 "parser.y" + {currentIsStatic = TRUE;} + break; + + case 294: +#line 2060 "parser.y" + {currentOverIsVirt = TRUE;} + break; + + case 297: +#line 2064 "parser.y" + { + if (notSkipping()) + { + /* Check the section. */ + + if (sectionFlags != 0) + { + if ((sectionFlags & SECT_IS_PUBLIC) == 0) + yyerror("Class variables must be in the public section"); + + if (!currentIsStatic && yyvsp[-2].codeb != NULL) + yyerror("%AccessCode cannot be specified for non-static class variables"); + } + + if (currentIsStatic && currentSpec -> genc) + yyerror("Cannot have static members in a C structure"); + + if (yyvsp[-1].codeb != NULL || yyvsp[0].codeb != NULL) + { + if (yyvsp[-2].codeb != NULL) + yyerror("Cannot mix %AccessCode and %GetCode or %SetCode"); + + if (currentScope() == NULL) + yyerror("Cannot specify %GetCode or %SetCode for global variables"); + } + + newVar(currentSpec,currentModule,yyvsp[-5].text,currentIsStatic,&yyvsp[-6].memArg,&yyvsp[-4].optflags,yyvsp[-2].codeb,yyvsp[-1].codeb,yyvsp[0].codeb); + } + + currentIsStatic = FALSE; + } + break; + + case 298: +#line 2097 "parser.y" + { + yyval.memArg = yyvsp[-2].memArg; + yyval.memArg.nrderefs = yyvsp[-1].number; + yyval.memArg.argflags = ARG_IS_CONST | yyvsp[0].number; + yyval.memArg.name = NULL; + } + break; + + case 299: +#line 2103 "parser.y" + { + yyval.memArg = yyvsp[-2].memArg; + yyval.memArg.nrderefs = yyvsp[-1].number; + yyval.memArg.argflags = yyvsp[0].number; + yyval.memArg.name = NULL; + } + break; + + case 300: +#line 2111 "parser.y" + { + yyval.memArg = yyvsp[-2].memArg; + yyval.memArg.name = yyvsp[-1].text; + + if (findOptFlag(&yyvsp[0].optflags,"AllowNone",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_ALLOW_NONE; + + if (findOptFlag(&yyvsp[0].optflags,"GetWrapper",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_GET_WRAPPER; + + if (findOptFlag(&yyvsp[0].optflags,"Array",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_ARRAY; + + if (findOptFlag(&yyvsp[0].optflags,"ArraySize",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_ARRAY_SIZE; + + if (findOptFlag(&yyvsp[0].optflags,"Transfer",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_XFERRED; + + if (findOptFlag(&yyvsp[0].optflags,"TransferThis",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_THIS_XFERRED; + + if (findOptFlag(&yyvsp[0].optflags,"TransferBack",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_XFERRED_BACK; + + if (findOptFlag(&yyvsp[0].optflags,"In",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_IN; + + if (findOptFlag(&yyvsp[0].optflags,"Out",bool_flag) != NULL) + yyval.memArg.argflags |= ARG_OUT; + + if (findOptFlag(&yyvsp[0].optflags,"Constrained",bool_flag) != NULL) + { + yyval.memArg.argflags |= ARG_CONSTRAINED; + + switch (yyval.memArg.atype) + { + case bool_type: + yyval.memArg.atype = cbool_type; + break; + + case int_type: + yyval.memArg.atype = cint_type; + break; + + case float_type: + yyval.memArg.atype = cfloat_type; + break; + + case double_type: + yyval.memArg.atype = cdouble_type; + break; + } + } + } + break; + + case 301: +#line 2168 "parser.y" + { + yyval.number = 0; + } + break; + + case 302: +#line 2171 "parser.y" + { + if (currentSpec -> genc) + yyerror("References not allowed in a C module"); + + yyval.number = ARG_IS_REF; + } + break; + + case 303: +#line 2179 "parser.y" + { + yyval.number = 0; + } + break; + + case 304: +#line 2182 "parser.y" + { + yyval.number = yyvsp[-1].number + 1; + } + break; + + case 305: +#line 2187 "parser.y" + { + yyval.memArg.atype = defined_type; + yyval.memArg.u.snd = yyvsp[0].scpvalp; + } + break; + + case 306: +#line 2191 "parser.y" + { + templateDef *td; + + td = sipMalloc(sizeof(templateDef)); + td -> fqname = yyvsp[-3].scpvalp; + td -> types = yyvsp[-1].signature; + + yyval.memArg.atype = template_type; + yyval.memArg.u.td = td; + } + break; + + case 307: +#line 2201 "parser.y" + { + /* In a C module all structures must be defined. */ + if (currentSpec -> genc) + { + yyval.memArg.atype = defined_type; + yyval.memArg.u.snd = yyvsp[0].scpvalp; + } + else + { + yyval.memArg.atype = struct_type; + yyval.memArg.u.sname = yyvsp[0].scpvalp; + } + } + break; + + case 308: +#line 2214 "parser.y" + { + yyval.memArg.atype = ushort_type; + } + break; + + case 309: +#line 2217 "parser.y" + { + yyval.memArg.atype = short_type; + } + break; + + case 310: +#line 2220 "parser.y" + { + yyval.memArg.atype = uint_type; + } + break; + + case 311: +#line 2223 "parser.y" + { + yyval.memArg.atype = uint_type; + } + break; + + case 312: +#line 2226 "parser.y" + { + yyval.memArg.atype = int_type; + } + break; + + case 313: +#line 2229 "parser.y" + { + yyval.memArg.atype = long_type; + } + break; + + case 314: +#line 2232 "parser.y" + { + yyval.memArg.atype = ulong_type; + } + break; + + case 315: +#line 2235 "parser.y" + { + yyval.memArg.atype = longlong_type; + } + break; + + case 316: +#line 2238 "parser.y" + { + yyval.memArg.atype = ulonglong_type; + } + break; + + case 317: +#line 2241 "parser.y" + { + yyval.memArg.atype = float_type; + } + break; + + case 318: +#line 2244 "parser.y" + { + yyval.memArg.atype = double_type; + } + break; + + case 319: +#line 2247 "parser.y" + { + yyval.memArg.atype = bool_type; + } + break; + + case 320: +#line 2250 "parser.y" + { + yyval.memArg.atype = sstring_type; + } + break; + + case 321: +#line 2253 "parser.y" + { + yyval.memArg.atype = ustring_type; + } + break; + + case 322: +#line 2256 "parser.y" + { + yyval.memArg.atype = string_type; + } + break; + + case 323: +#line 2259 "parser.y" + { + yyval.memArg.atype = wstring_type; + } + break; + + case 324: +#line 2262 "parser.y" + { + yyval.memArg.atype = void_type; + } + break; + + case 325: +#line 2265 "parser.y" + { + yyval.memArg.atype = pyobject_type; + } + break; + + case 326: +#line 2268 "parser.y" + { + yyval.memArg.atype = pytuple_type; + } + break; + + case 327: +#line 2271 "parser.y" + { + yyval.memArg.atype = pylist_type; + } + break; + + case 328: +#line 2274 "parser.y" + { + yyval.memArg.atype = pydict_type; + } + break; + + case 329: +#line 2277 "parser.y" + { + yyval.memArg.atype = pycallable_type; + } + break; + + case 330: +#line 2280 "parser.y" + { + yyval.memArg.atype = pyslice_type; + } + break; + + case 331: +#line 2283 "parser.y" + { + yyval.memArg.atype = pytype_type; + } + break; + + case 332: +#line 2286 "parser.y" + { + yyval.memArg.atype = ellipsis_type; + } + break; + + case 333: +#line 2291 "parser.y" + { + /* The single or first type. */ + + yyval.signature.args[0] = yyvsp[0].memArg; + yyval.signature.nrArgs = 1; + } + break; + + case 334: +#line 2297 "parser.y" + { + /* Check there is nothing after an ellipsis. */ + if (yyvsp[-2].signature.args[yyvsp[-2].signature.nrArgs - 1].atype == ellipsis_type) + yyerror("An ellipsis must be at the end of the argument list"); + + /* Check there is room. */ + if (yyvsp[-2].signature.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + yyval.signature = yyvsp[-2].signature; + + yyval.signature.args[yyval.signature.nrArgs] = yyvsp[0].memArg; + yyval.signature.nrArgs++; + } + break; + + case 335: +#line 2313 "parser.y" + { + yyval.throwlist = NULL; + } + break; + + case 336: +#line 2316 "parser.y" + { + if (currentSpec->genc) + yyerror("Exceptions not allowed in a C module"); + + if (notSkipping() && inMainModule()) + { + int e; + ifaceFileList **ifl; + + /* + * Make sure the exceptions' header files are + * included. We unconditionally mark them to + * be included in the current scope's header + * file to save us the effort of checking if + * they are being used with a protected method, + * a virtual or a signal. + */ + ifl = (currentScope() != NULL) ? ¤tScope()->iff->used : ¤tSpec->used; + + for (e = 0; e < yyvsp[-1].throwlist->nrArgs; ++e) + addToUsedList(ifl, yyvsp[-1].throwlist->args[e]->iff); + } + + yyval.throwlist = yyvsp[-1].throwlist; + } + break; + + case 337: +#line 2343 "parser.y" + { + /* Empty list so use a blank. */ + + yyval.throwlist = sipMalloc(sizeof (throwArgs)); + yyval.throwlist -> nrArgs = 0; + } + break; + + case 338: +#line 2349 "parser.y" + { + /* The only or first exception. */ + + yyval.throwlist = sipMalloc(sizeof (throwArgs)); + yyval.throwlist -> nrArgs = 1; + yyval.throwlist -> args[0] = findException(currentSpec, yyvsp[0].scpvalp, FALSE); + } + break; + + case 339: +#line 2356 "parser.y" + { + /* Check that it wasn't ...(,arg...). */ + + if (yyvsp[-2].throwlist -> nrArgs == 0) + yyerror("First exception of throw specifier is missing"); + + /* Check there is room. */ + + if (yyvsp[-2].throwlist -> nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + yyval.throwlist = yyvsp[-2].throwlist; + yyval.throwlist -> args[yyval.throwlist -> nrArgs++] = findException(currentSpec, yyvsp[0].scpvalp, FALSE); + } + break; + + + } + +/* Line 1010 of yacc.c. */ +#line 4727 "parser.c" + + yyvsp -= yylen; + yyssp -= yylen; + + + YY_STACK_PRINT (yyss, yyssp); + + *++yyvsp = yyval; + + + /* Now `shift' the result of the reduction. Determine what state + that goes to, based on the state we popped back to and the rule + number reduced by. */ + + yyn = yyr1[yyn]; + + yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; + if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) + yystate = yytable[yystate]; + else + yystate = yydefgoto[yyn - YYNTOKENS]; + + goto yynewstate; + + +/*------------------------------------. +| yyerrlab -- here on detecting error | +`------------------------------------*/ +yyerrlab: + /* If not already recovering from an error, report this error. */ + if (!yyerrstatus) + { + ++yynerrs; +#if YYERROR_VERBOSE + yyn = yypact[yystate]; + + if (YYPACT_NINF < yyn && yyn < YYLAST) + { + YYSIZE_T yysize = 0; + int yytype = YYTRANSLATE (yychar); + const char* yyprefix; + char *yymsg; + int yyx; + + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; + int yycount = 0; + + yyprefix = ", expecting "; + for (yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) + { + yysize += yystrlen (yyprefix) + yystrlen (yytname [yyx]); + yycount += 1; + if (yycount == 5) + { + yysize = 0; + break; + } + } + yysize += (sizeof ("syntax error, unexpected ") + + yystrlen (yytname[yytype])); + yymsg = (char *) YYSTACK_ALLOC (yysize); + if (yymsg != 0) + { + char *yyp = yystpcpy (yymsg, "syntax error, unexpected "); + yyp = yystpcpy (yyp, yytname[yytype]); + + if (yycount < 5) + { + yyprefix = ", expecting "; + for (yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR) + { + yyp = yystpcpy (yyp, yyprefix); + yyp = yystpcpy (yyp, yytname[yyx]); + yyprefix = " or "; + } + } + yyerror (yymsg); + YYSTACK_FREE (yymsg); + } + else + yyerror ("syntax error; also virtual memory exhausted"); + } + else +#endif /* YYERROR_VERBOSE */ + yyerror ("syntax error"); + } + + + + if (yyerrstatus == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + + if (yychar <= YYEOF) + { + /* If at end of input, pop the error token, + then the rest of the stack, then return failure. */ + if (yychar == YYEOF) + for (;;) + { + YYPOPSTACK; + if (yyssp == yyss) + YYABORT; + YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp); + yydestruct (yystos[*yyssp], yyvsp); + } + } + else + { + YYDSYMPRINTF ("Error: discarding", yytoken, &yylval, &yylloc); + yydestruct (yytoken, &yylval); + yychar = YYEMPTY; + + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + +#ifdef __GNUC__ + /* Pacify GCC when the user code never invokes YYERROR and the label + yyerrorlab therefore never appears in user code. */ + if (0) + goto yyerrorlab; +#endif + + yyvsp -= yylen; + yyssp -= yylen; + yystate = *yyssp; + goto yyerrlab1; + + +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + yyerrstatus = 3; /* Each real token shifted decrements this. */ + + for (;;) + { + yyn = yypact[yystate]; + if (yyn != YYPACT_NINF) + { + yyn += YYTERROR; + if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) + { + yyn = yytable[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yyssp == yyss) + YYABORT; + + YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp); + yydestruct (yystos[yystate], yyvsp); + YYPOPSTACK; + yystate = *yyssp; + YY_STACK_PRINT (yyss, yyssp); + } + + if (yyn == YYFINAL) + YYACCEPT; + + YYDPRINTF ((stderr, "Shifting error token, ")); + + *++yyvsp = yylval; + + + yystate = yyn; + goto yynewstate; + + +/*-------------------------------------. +| yyacceptlab -- YYACCEPT comes here. | +`-------------------------------------*/ +yyacceptlab: + yyresult = 0; + goto yyreturn; + +/*-----------------------------------. +| yyabortlab -- YYABORT comes here. | +`-----------------------------------*/ +yyabortlab: + yyresult = 1; + goto yyreturn; + +#ifndef yyoverflow +/*----------------------------------------------. +| yyoverflowlab -- parser overflow comes here. | +`----------------------------------------------*/ +yyoverflowlab: + yyerror ("parser stack overflow"); + yyresult = 2; + /* Fall through. */ +#endif + +yyreturn: +#ifndef yyoverflow + if (yyss != yyssa) + YYSTACK_FREE (yyss); +#endif + return yyresult; +} + + +#line 2372 "parser.y" + + + +/* + * Parse the specification. + */ +void parse(sipSpec *spec,FILE *fp,char *filename,stringList *tsl, + stringList *xfl) +{ + classTmplDef *tcd; + + /* Initialise the spec. */ + + spec -> modules = NULL; + spec -> namecache = NULL; + spec -> ifacefiles = NULL; + spec -> classes = NULL; + spec -> classtemplates = NULL; + spec -> proxies = NULL; + spec -> exceptions = NULL; + spec -> mappedtypes = NULL; + spec -> mappedtypetemplates = NULL; + spec -> qobjclass = -1; + spec -> enums = NULL; + spec -> vars = NULL; + spec -> othfuncs = NULL; + spec -> overs = NULL; + spec -> typedefs = NULL; + spec -> copying = NULL; + spec -> exphdrcode = NULL; + spec -> hdrcode = NULL; + spec -> cppcode = NULL; + spec -> docs = NULL; + spec -> preinitcode = NULL; + spec -> postinitcode = NULL; + spec -> unitcode = NULL; + spec -> used = NULL; + spec -> sigslots = FALSE; + spec -> genc = -1; + spec -> options = NULL; + + currentSpec = spec; + neededQualifiers = tsl; + excludedQualifiers = xfl; + currentModule = NULL; + currentMappedType = NULL; + currentOverIsVirt = FALSE; + currentCtorIsExplicit = FALSE; + currentIsStatic = FALSE; + previousFile = NULL; + skipStackPtr = 0; + currentScopeIdx = 0; + sectionFlags = 0; + + newModule(fp,filename); + spec -> module = currentModule; + + yyparse(); + + handleEOF(); + handleEOM(); + + /* + * Go through each template class and remove it from the list of + * classes. + */ + for (tcd = spec->classtemplates; tcd != NULL; tcd = tcd->next) + { + classDef **cdp; + + for (cdp = &spec->classes; *cdp != NULL; cdp = &(*cdp)->next) + if (*cdp == tcd->cd) + { + ifaceFileDef **ifdp; + + /* Remove the interface file as well. */ + for (ifdp = &spec->ifacefiles; *ifdp != NULL; ifdp = &(*ifdp)->next) + if (*ifdp == tcd->cd->iff) + { + *ifdp = (*ifdp)->next; + break; + } + + *cdp = (*cdp)->next; + break; + } + } +} + + +/* + * Tell the parser that a complete file has now been read. + */ +void parserEOF(char *name,parserContext *pc) +{ + previousFile = sipStrdup(name); + newContext = *pc; +} + + +/* + * Append a class definition to a class list if it doesn't already appear. + * Append is needed specifically for the list of super-classes because the + * order is important to Python. + */ +void appendToClassList(classList **clp,classDef *cd) +{ + classList *new; + + /* Find the end of the list. */ + + while (*clp != NULL) + { + if ((*clp) -> cd == cd) + return; + + clp = &(*clp) -> next; + } + + new = sipMalloc(sizeof (classList)); + + new -> cd = cd; + new -> next = NULL; + + *clp = new; +} + + +/* + * Create a new module for the current specification and make it current. + */ +static void newModule(FILE *fp,char *filename) +{ + moduleDef *newmod; + + parseFile(fp,filename,currentModule,FALSE); + + newmod = sipMalloc(sizeof (moduleDef)); + newmod -> fullname = NULL; + newmod -> name = NULL; + newmod -> version = -1; + newmod -> modflags = 0; + newmod -> modulenr = -1; + newmod -> file = filename; + newmod -> qualifiers = NULL; + newmod -> root.cd = NULL; + newmod -> root.child = NULL; + newmod -> nrtimelines = 0; + newmod -> nrclasses = 0; + newmod -> nrexceptions = 0; + newmod -> nrmappedtypes = 0; + newmod -> nrenums = 0; + newmod -> nrtypedefs = 0; + newmod -> nrvirthandlers = 0; + newmod -> virthandlers = NULL; + newmod -> license = NULL; + newmod -> allimports = NULL; + newmod -> imports = NULL; + newmod -> next = currentSpec -> modules; + + currentModule = currentSpec->modules = newmod; +} + + +/* + * Switch to parsing a new file. + */ +static void parseFile(FILE *fp,char *name,moduleDef *prevmod,int optional) +{ + parserContext pc; + + pc.ifdepth = skipStackPtr; + pc.prevmod = prevmod; + + setInputFile(fp,name,&pc,optional); +} + + +/* + * Find an interface file, or create a new one. + */ +ifaceFileDef *findIfaceFile(sipSpec *pt, moduleDef *mod, scopedNameDef *fqname, + ifaceFileType iftype, argDef *ad) +{ + ifaceFileDef *iff; + + /* See if the name is already used. */ + + for (iff = pt -> ifacefiles; iff != NULL; iff = iff -> next) + { + if (!sameScopedName(iff -> fqcname,fqname)) + continue; + + /* + * They must be the same type except that we allow a class if + * if we want an exception. This is because we allow classes + * to be used before they are defined. + */ + if (iff -> type != iftype) + if (iftype != exception_iface || iff -> type != class_iface) + yyerror("A class, exception, namespace or mapped type has already been defined with the same name"); + + /* Ignore an external class declared in another module. */ + if (iftype == class_iface && iff->module != mod) + { + classDef *cd; + + for (cd = pt->classes; cd != NULL; cd = cd->next) + if (cd->iff == iff) + break; + + if (cd != NULL && iff->module != NULL && isExternal(cd)) + continue; + } + + /* + * If this is a mapped type with the same name defined in a + * different module, then check that this type isn't the same + * as any of the mapped types defined in that module. + */ + if (iftype == mappedtype_iface && iff -> module != mod) + { + mappedTypeDef *mtd; + + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + { + if (mtd -> iff != iff) + continue; + + if (ad -> atype != template_type || + mtd -> type.atype != template_type || + sameBaseType(ad,&mtd -> type)) + yyerror("Mapped type has already been defined in another module"); + } + + /* + * If we got here then we have a mapped type based on + * an existing template, but with unique parameters. + * We don't want to use interface files from other + * modules, so skip this one. + */ + + continue; + } + + /* Ignore a namespace defined in another module. */ + if (iftype == namespace_iface && iff->module != mod) + continue; + + return iff; + } + + iff = sipMalloc(sizeof (ifaceFileDef)); + + iff -> name = cacheName(pt,scopedNameTail(fqname)); + iff -> type = iftype; + iff -> fqcname = fqname; + iff -> module = NULL; + iff -> used = NULL; + iff -> next = pt -> ifacefiles; + + pt -> ifacefiles = iff; + + return iff; +} + + +/* + * Find a class definition in a parse tree. + */ +static classDef *findClass(sipSpec *pt,ifaceFileType iftype, + scopedNameDef *fqname) +{ + return findClassWithInterface(pt, findIfaceFile(pt, currentModule, fqname, iftype, NULL)); +} + + +/* + * Find a class definition given an existing interface file. + */ +static classDef *findClassWithInterface(sipSpec *pt, ifaceFileDef *iff) +{ + classDef *cd; + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + if (cd -> iff == iff) + return cd; + + /* Create a new one. */ + cd = sipMalloc(sizeof (classDef)); + + cd -> iff = iff; + cd -> pyname = classBaseName(cd); + cd -> classnr = -1; + cd -> classflags = 0; + cd -> userflags = 0; + cd -> ecd = NULL; + cd -> dtorexceptions = NULL; + cd -> real = NULL; + cd -> node = NULL; + cd -> supers = NULL; + cd -> mro = NULL; + cd -> td = NULL; + cd -> ctors = NULL; + cd -> defctor = NULL; + cd -> dealloccode = NULL; + cd -> dtorcode = NULL; + cd -> members = NULL; + cd -> overs = NULL; + cd -> casts = NULL; + cd -> vmembers = NULL; + cd -> visible = NULL; + cd -> cppcode = NULL; + cd -> hdrcode = NULL; + cd -> convtosubcode = NULL; + cd -> subbase = NULL; + cd -> convtocode = NULL; + cd -> travcode = NULL; + cd -> clearcode = NULL; + cd -> readbufcode = NULL; + cd -> writebufcode = NULL; + cd -> segcountcode = NULL; + cd -> charbufcode = NULL; + cd -> next = pt -> classes; + + pt -> classes = cd; + + return cd; +} + + +/* + * Add an interface file to an interface file list if it isn't already there. + */ +ifaceFileList *addToUsedList(ifaceFileList **ifflp, ifaceFileDef *iff) +{ + ifaceFileList *iffl; + + while ((iffl = *ifflp) != NULL) + { + /* Don't bother if it is already there. */ + if (iffl -> iff == iff) + return iffl; + + ifflp = &iffl -> next; + } + + iffl = sipMalloc(sizeof (ifaceFileList)); + + iffl->iff = iff; + iffl->header = FALSE; + iffl->next = NULL; + + *ifflp = iffl; + + return iffl; +} + + +/* + * Find an undefined (or create a new) exception definition in a parse tree. + */ +static exceptionDef *findException(sipSpec *pt, scopedNameDef *fqname, int new) +{ + exceptionDef *xd, **tail; + ifaceFileDef *iff; + classDef *cd; + + iff = findIfaceFile(pt, currentModule, fqname, exception_iface, NULL); + + /* See if it is an existing one. */ + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + if (xd->iff == iff) + return xd; + + /* + * If it is an exception interface file then we have never seen this + * name before. We require that exceptions are defined before being + * used, but don't make the same requirement of classes (for reasons of + * backwards compatibility). Therefore the name must be reinterpreted + * as a (as yet undefined) class. + */ + if (new) + if (iff->type == exception_iface) + cd = NULL; + else + yyerror("There is already a class with the same name or the exception has been used before being defined"); + else + { + if (iff->type == exception_iface) + iff->type = class_iface; + + cd = findClassWithInterface(pt, iff); + } + + /* Create a new one. */ + xd = sipMalloc(sizeof (exceptionDef)); + + xd->exceptionnr = -1; + xd->iff = iff; + xd->pyname = NULL; + xd->cd = cd; + xd->bibase = NULL; + xd->base = NULL; + xd->hdrcode = NULL; + xd->raisecode = NULL; + xd->next = NULL; + + /* Append it to the list. */ + for (tail = &pt->exceptions; *tail != NULL; tail = &(*tail)->next) + ; + + *tail = xd; + + return xd; +} + + +/* + * Find an undefined (or create a new) class definition in a parse tree. + */ +static classDef *newClass(sipSpec *pt,ifaceFileType iftype, + scopedNameDef *fqname) +{ + int flags; + classDef *cd, *scope; + codeBlock *hdrcode; + + if (sectionFlags & SECT_IS_PRIVATE) + yyerror("Classes, structs and namespaces must be in the public or or protected sections"); + + flags = 0; + + if ((scope = currentScope()) != NULL) + { + if (sectionFlags & SECT_IS_PROT) + flags = CLASS_IS_PROTECTED; + + hdrcode = scope -> hdrcode; + } + else + hdrcode = NULL; + + if (pt -> genc) + { + /* C structs are always global types. */ + while (fqname -> next != NULL) + fqname = fqname -> next; + + scope = NULL; + } + + cd = findClass(pt,iftype,fqname); + + /* Check it hasn't already been defined. */ + if (iftype != namespace_iface && cd->iff->module != NULL) + yyerror("The struct/class has already been defined"); + + /* Complete the initialisation. */ + cd->classflags |= flags; + cd->ecd = scope; + cd->iff->module = currentModule; + + appendCodeBlock(&cd->hdrcode, hdrcode); + + /* See if it is a namespace extender. */ + if (iftype == namespace_iface) + { + classDef *ns; + + for (ns = pt->classes; ns != NULL; ns = ns->next) + { + if (ns == cd) + continue; + + if (ns->iff->type != namespace_iface) + continue; + + if (!sameScopedName(ns->iff->fqcname, fqname)) + continue; + + cd->real = ns; + break; + } + } + + return cd; +} + + +/* + * Tidy up after finishing a class definition. + */ +static void finishClass(sipSpec *pt, moduleDef *mod, classDef *cd, optFlags *of) +{ + char *pyname; + optFlag *flg; + + /* Get the Python name and see if it is different to the C++ name. */ + pyname = getPythonName(of, classBaseName(cd)); + + cd -> pyname = NULL; + checkAttributes(pt, cd->ecd, pyname, FALSE); + cd->pyname = pyname; + + if (cd->pyname != classBaseName(cd)) + setIsRenamedClass(cd); + + if ((flg = findOptFlag(of, "TypeFlags", integer_flag)) != NULL) + cd->userflags = flg->fvalue.ival; + + if (isOpaque(cd)) + { + if (findOptFlag(of, "External", bool_flag) != NULL) + setIsExternal(cd); + } + else + { + int seq_might, seq_not; + memberDef *md; + + if (findOptFlag(of, "NoDefaultCtors", bool_flag) != NULL) + setNoDefaultCtors(cd); + + if (cd -> ctors == NULL) + { + if (!noDefaultCtors(cd)) + { + /* Provide a default ctor. */ + + cd->ctors = sipMalloc(sizeof (ctorDef)); + + cd->ctors->ctorflags = SECT_IS_PUBLIC; + cd->ctors->pysig.nrArgs = 0; + cd->ctors->cppsig = &cd -> ctors -> pysig; + cd->ctors->exceptions = NULL; + cd->ctors->methodcode = NULL; + cd->ctors->prehook = NULL; + cd->ctors->posthook = NULL; + cd->ctors->next = NULL; + + cd->defctor = cd->ctors; + + setCanCreate(cd); + } + } + else if (cd -> defctor == NULL) + { + ctorDef *ct, *last = NULL; + + for (ct = cd -> ctors; ct != NULL; ct = ct -> next) + { + if (!isPublicCtor(ct)) + continue; + + if (ct -> pysig.nrArgs == 0 || ct -> pysig.args[0].defval != NULL) + { + cd -> defctor = ct; + break; + } + + if (last == NULL) + last = ct; + } + + /* The last resort is the first public ctor. */ + if (cd->defctor == NULL) + cd->defctor = last; + } + + if (findOptFlag(of,"Abstract",bool_flag) != NULL) + { + setIsAbstractClass(cd); + setIsIncomplete(cd); + resetCanCreate(cd); + } + + /* We assume a public dtor if nothing specific was provided. */ + if (!isDtor(cd)) + setIsPublicDtor(cd); + + if (findOptFlag(of, "DelayDtor", bool_flag) != NULL) + { + setIsDelayedDtor(cd); + setHasDelayedDtors(mod); + } + + /* + * There are subtle differences between the add and concat methods and + * the multiply and repeat methods. The number versions can have their + * operands swapped and may return NotImplemented. If the user has + * used the /Numeric/ annotation or there are other numeric operators + * then we use add/multiply. Otherwise, if there are indexing + * operators then we use concat/repeat. + */ + seq_might = seq_not = FALSE; + + for (md = cd -> members; md != NULL; md = md -> next) + switch (md -> slot) + { + case getitem_slot: + case setitem_slot: + case delitem_slot: + /* This might be a sequence. */ + seq_might = TRUE; + break; + + case sub_slot: + case isub_slot: + case div_slot: + case idiv_slot: + case mod_slot: + case imod_slot: + case pos_slot: + case neg_slot: + /* This is definately not a sequence. */ + seq_not = TRUE; + break; + } + + if (!seq_not && seq_might) + for (md = cd -> members; md != NULL; md = md -> next) + { + /* Ignore if the user has been explicit. */ + if (isNumeric(md)) + continue; + + switch (md -> slot) + { + case add_slot: + md -> slot = concat_slot; + break; + + case iadd_slot: + md -> slot = iconcat_slot; + break; + + case mul_slot: + md -> slot = repeat_slot; + break; + + case imul_slot: + md -> slot = irepeat_slot; + break; + } + } + } + + if (inMainModule()) + { + setIsUsedName(cd->iff->name); + setIsClassName(cd->iff->name); + } +} + + +/* + * Create a new mapped type. + */ +static mappedTypeDef *newMappedType(sipSpec *pt,argDef *ad) +{ + mappedTypeDef *mtd; + scopedNameDef *snd; + ifaceFileDef *iff; + + /* Check that the type is one we want to map. */ + switch (ad -> atype) + { + case defined_type: + snd = ad -> u.snd; + break; + + case template_type: + snd = ad -> u.td -> fqname; + break; + + case struct_type: + snd = ad -> u.sname; + break; + + default: + yyerror("Invalid type for %MappedType"); + } + + iff = findIfaceFile(pt, currentModule, snd, mappedtype_iface, ad); + + if (inMainModule()) + setIsUsedName(iff -> name); + + /* Check it hasn't already been defined. */ + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + if (mtd -> iff == iff) + { + /* + * We allow types based on the same template but with + * different arguments. + */ + + if (ad -> atype != template_type || + sameBaseType(ad,&mtd -> type)) + yyerror("Mapped type has already been defined in this module"); + } + + /* The module may not have been set yet. */ + iff -> module = currentModule; + + /* Create a new mapped type. */ + mtd = allocMappedType(ad); + + mtd -> iff = iff; + mtd -> next = pt -> mappedtypes; + + pt -> mappedtypes = mtd; + + return mtd; +} + + +/* + * Allocate, intialise and return a mapped type structure. + */ +mappedTypeDef *allocMappedType(argDef *type) +{ + mappedTypeDef *mtd; + + mtd = sipMalloc(sizeof (mappedTypeDef)); + + mtd->type = *type; + mtd->type.argflags = 0; + mtd->type.nrderefs = 0; + + mtd->mappednr = -1; + mtd->iff = NULL; + mtd->hdrcode = NULL; + mtd->convfromcode = NULL; + mtd->convtocode = NULL; + mtd->next = NULL; + + return mtd; +} + + +/* + * Create a new enum. + */ +static enumDef *newEnum(sipSpec *pt,moduleDef *mod,char *name,optFlags *of, + int flags) +{ + enumDef *ed; + classDef *escope = currentScope(); + + ed = sipMalloc(sizeof (enumDef)); + + if (name != NULL) + { + ed -> fqcname = text2scopedName(name); + ed -> pyname = cacheName(pt, getPythonName(of, name)); + + checkAttributes(pt, escope, ed->pyname->text, FALSE); + } + else + { + ed -> fqcname = NULL; + ed -> pyname = NULL; + } + + ed -> enumflags = flags; + ed -> enumnr = -1; + ed -> ecd = escope; + ed -> pcd = (flags & SECT_IS_PROT) ? escope : NULL; + ed -> module = mod; + ed -> members = NULL; + ed -> slots = NULL; + ed -> overs = NULL; + ed -> next = pt -> enums; + + if (name != NULL && strcmp(ed->pyname->text, name) != 0) + setIsRenamedEnum(ed); + + pt -> enums = ed; + + if (escope != NULL) + setHasEnums(escope); + + return ed; +} + + +/* + * Get the type values and (optionally) the type names for substitution in + * handwritten code. + */ +void appendTypeStrings(scopedNameDef *ename, signatureDef *patt, signatureDef *src, signatureDef *known, scopedNameDef **names, scopedNameDef **values) +{ + int a; + + for (a = 0; a < patt->nrArgs; ++a) + { + argDef *pad = &patt->args[a]; + + if (pad->atype == defined_type) + { + char *nam = NULL; + + /* + * If the type names are already known then check that + * this is one of them. + */ + if (known == NULL) + nam = scopedNameTail(pad->u.snd); + else if (pad->u.snd->next == NULL) + { + int k; + + for (k = 0; k < known->nrArgs; ++k) + if (strcmp(pad->u.snd->name, known->args[k].u.snd->name) == 0) + { + nam = pad->u.snd->name; + break; + } + } + + if (nam == NULL) + continue; + + /* Add the name. */ + appendScopedName(names, text2scopePart(nam)); + + /* Add the corresponding value. */ + appendScopedName(values, text2scopePart(getType(ename, &src->args[a]))); + } + else if (pad->atype == template_type) + { + argDef *sad = &src->args[a]; + + /* These checks shouldn't be necessary, but... */ + if (sad->atype == template_type && pad->u.td->types.nrArgs == sad->u.td->types.nrArgs) + appendTypeStrings(ename, &pad->u.td->types, &sad->u.td->types, known, names, values); + } + } +} + + +/* + * Convert a type to a string. We impose some limitations because I'm too lazy + * to handle everything that might be needed one day. + */ +static char *getType(scopedNameDef *ename, argDef *ad) +{ + if (ad->atype == defined_type) + return scopedNameToString(ad->u.snd); + + fatalScopedName(ename); + fatal(": unsupported type argument to template class instantiation\n"); + + return NULL; +} + + +/* + * Convert a scoped name to a string on the heap. + */ +static char *scopedNameToString(scopedNameDef *name) +{ + static const char scope_string[] = "::"; + size_t len; + scopedNameDef *snd; + char *s, *dp; + + /* Work out the length of buffer needed. */ + len = 0; + + for (snd = name; snd != NULL; snd = snd->next) + { + len += strlen(snd->name); + + if (snd->next != NULL) + len += strlen(scope_string); + } + + /* Allocate and populate the buffer. */ + dp = s = sipMalloc(len + 1); + + for (snd = name; snd != NULL; snd = snd->next) + { + strcpy(dp, snd->name); + dp += strlen(snd->name); + + if (snd->next != NULL) + { + strcpy(dp, scope_string); + dp += strlen(scope_string); + } + } + + return s; +} + + +/* + * Instantiate a class template. + */ +static void instantiateClassTemplate(sipSpec *pt, moduleDef *mod, classDef *scope, scopedNameDef *fqname, classTmplDef *tcd, templateDef *td) +{ + scopedNameDef *type_names, *type_values; + classDef *cd; + ctorDef *oct, **cttail; + memberDef *omd, **mdtail; + overDef *ood, **odtail; + argDef *ad; + ifaceFileList *iffl, **used; + + type_names = type_values = NULL; + appendTypeStrings(classFQCName(tcd->cd), &tcd->sig, &td->types, NULL, &type_names, &type_values); + + /* + * Add a mapping from the template name to the instantiated name. If + * we have got this far we know there is room for it. + */ + ad = &tcd->sig.args[tcd->sig.nrArgs++]; + ad->atype = defined_type; + ad->name = NULL; + ad->argflags = 0; + ad->nrderefs = 0; + ad->defval = NULL; + ad->u.snd = classFQCName(tcd->cd); + + appendScopedName(&type_names, text2scopePart(scopedNameTail(classFQCName(tcd->cd)))); + appendScopedName(&type_values, text2scopePart(scopedNameToString(fqname))); + + /* Create the new class. */ + cd = sipMalloc(sizeof (classDef)); + + /* Start with a shallow copy. */ + *cd = *tcd->cd; + + cd->pyname = scopedNameTail(fqname); + cd->td = td; + + /* Handle the interface file. */ + cd->iff = findIfaceFile(pt, mod, fqname, class_iface, NULL); + cd->iff->module = mod; + + /* Make a copy of the used list and add the enclosing scope. */ + used = &cd->iff->used; + + for (iffl = tcd->cd->iff->used; iffl != NULL; iffl = iffl->next) + addToUsedList(used, iffl->iff); + + if (scope != NULL) + addToUsedList(&cd->iff->used, scope->iff); + + if (inMainModule()) + { + setIsUsedName(cd->iff->name); + setIsClassName(cd->iff->name); + } + + cd->ecd = currentScope(); + + /* Handle the ctors. */ + cd->ctors = NULL; + cttail = &cd->ctors; + + for (oct = tcd->cd->ctors; oct != NULL; oct = oct->next) + { + ctorDef *nct = sipMalloc(sizeof (ctorDef)); + + /* Start with a shallow copy. */ + *nct = *oct; + + templateSignature(&nct->pysig, FALSE, tcd, td, cd); + + if (oct->cppsig == NULL) + nct->cppsig = NULL; + else if (oct->cppsig == &oct->pysig) + nct->cppsig = &nct->pysig; + else + { + nct->cppsig = sipMalloc(sizeof (signatureDef)); + + *nct->cppsig = *oct->cppsig; + + templateSignature(nct->cppsig, FALSE, tcd, td, cd); + } + + nct->methodcode = templateCode(pt, used, nct->methodcode, type_names, type_values); + + nct->next = NULL; + *cttail = nct; + cttail = &nct->next; + + /* Handle the default ctor. */ + if (tcd->cd->defctor == oct) + cd->defctor = nct; + } + + cd->dealloccode = templateCode(pt, used, cd->dealloccode, type_names, type_values); + cd->dtorcode = templateCode(pt, used, cd->dtorcode, type_names, type_values); + + /* Handle the members, ie. the common parts of overloads. */ + cd->members = NULL; + mdtail = &cd->members; + + for (omd = tcd->cd->members; omd != NULL; omd = omd->next) + { + memberDef *nmd = sipMalloc(sizeof (memberDef)); + + /* Start with a shallow copy. */ + *nmd = *omd; + + nmd->module = mod; + + nmd->next = NULL; + *mdtail = nmd; + mdtail = &nmd->next; + } + + /* Handle the overloads. */ + cd->overs = NULL; + odtail = &cd->overs; + + for (ood = tcd->cd->overs; ood != NULL; ood = ood->next) + { + overDef *nod = sipMalloc(sizeof (overDef)); + memberDef *nmd; + + /* Start with a shallow copy. */ + *nod = *ood; + + for (nmd = cd->members, omd = tcd->cd->members; omd != NULL; omd = omd->next, nmd = nmd->next) + if (omd == ood->common) + { + nod->common = nmd; + break; + } + + templateSignature(&nod->pysig, TRUE, tcd, td, cd); + + if (ood->cppsig == &ood->pysig) + nod->cppsig = &nod->pysig; + else + { + nod->cppsig = sipMalloc(sizeof (signatureDef)); + + *nod->cppsig = *ood->cppsig; + + templateSignature(nod->cppsig, TRUE, tcd, td, cd); + } + + nod->methodcode = templateCode(pt, used, nod->methodcode, type_names, type_values); + + /* Handle any virtual handler. */ + if (ood->virthandler != NULL) + { + nod->virthandler = sipMalloc(sizeof (virtHandlerDef)); + + /* Start with a shallow copy. */ + *nod->virthandler = *ood->virthandler; + + if (ood->virthandler->cppsig == &ood->pysig) + nod->virthandler->cppsig = &nod->pysig; + else + { + nod->virthandler->cppsig = sipMalloc(sizeof (signatureDef)); + + *nod->virthandler->cppsig = *ood->virthandler->cppsig; + + templateSignature(nod->virthandler->cppsig, TRUE, tcd, td, cd); + } + + nod->virthandler->module = mod; + nod->virthandler->virtcode = templateCode(pt, used, nod->virthandler->virtcode, type_names, type_values); + nod->virthandler->next = mod->virthandlers; + + mod->virthandlers = nod->virthandler; + } + + nod->next = NULL; + *odtail = nod; + odtail = &nod->next; + } + + cd->cppcode = templateCode(pt, used, cd->cppcode, type_names, type_values); + cd->hdrcode = templateCode(pt, used, cd->hdrcode, type_names, type_values); + cd->convtosubcode = templateCode(pt, used, cd->convtosubcode, type_names, type_values); + cd->convtocode = templateCode(pt, used, cd->convtocode, type_names, type_values); + cd->travcode = templateCode(pt, used, cd->travcode, type_names, type_values); + cd->clearcode = templateCode(pt, used, cd->clearcode, type_names, type_values); + cd->readbufcode = templateCode(pt, used, cd->readbufcode, type_names, type_values); + cd->writebufcode = templateCode(pt, used, cd->writebufcode, type_names, type_values); + cd->segcountcode = templateCode(pt, used, cd->segcountcode, type_names, type_values); + cd->charbufcode = templateCode(pt, used, cd->charbufcode, type_names, type_values); + cd->next = pt->classes; + + pt->classes = cd; + + tcd->sig.nrArgs--; + + freeScopedName(type_names); + freeScopedName(type_values); +} + + +/* + * Replace any template arguments in a signature. + */ +static void templateSignature(signatureDef *sd, int result, classTmplDef *tcd, templateDef *td, classDef *ncd) +{ + int a; + + if (result) + templateType(&sd->result, tcd, td, ncd); + + for (a = 0; a < sd->nrArgs; ++a) + templateType(&sd->args[a], tcd, td, ncd); +} + + +/* + * Replace any template arguments in a type. + */ +static void templateType(argDef *ad, classTmplDef *tcd, templateDef *td, classDef *ncd) +{ + int a; + char *name; + + /* Ignore if it isn't an unscoped name. */ + if (ad->atype != defined_type || ad->u.snd->next != NULL) + return; + + name = ad->u.snd->name; + + for (a = 0; a < tcd->sig.nrArgs - 1; ++a) + if (strcmp(name, scopedNameTail(tcd->sig.args[a].u.snd)) == 0) + { + ad->atype = td->types.args[a].atype; + + /* We take the constrained flag from the real type. */ + resetIsConstrained(ad); + + if (isConstrained(&td->types.args[a])) + setIsConstrained(ad); + + ad->u = td->types.args[a].u; + + return; + } + + /* Handle the class name itself. */ + if (strcmp(name, scopedNameTail(classFQCName(tcd->cd))) == 0) + { + ad->atype = class_type; + ad->u.cd = ncd; + } +} + + +/* + * Replace any template arguments in a literal code block. + */ +codeBlock *templateCode(sipSpec *pt, ifaceFileList **used, codeBlock *ocb, scopedNameDef *names, scopedNameDef *values) +{ + codeBlock *ncb = NULL, **tail = &ncb; + + while (ocb != NULL) + { + char *at = ocb->frag; + + do + { + char *first = NULL; + codeBlock *cb; + scopedNameDef *nam, *val, *nam_first, *val_first; + + /* + * Go through the rest of this fragment looking for + * each of the types and the name of the class itself. + */ + nam = names; + val = values; + + while (nam != NULL && val != NULL) + { + char *cp; + + if ((cp = strstr(at, nam->name)) != NULL) + if (first == NULL || first > cp) + { + nam_first = nam; + val_first = val; + first = cp; + } + + nam = nam->next; + val = val->next; + } + + /* Create the new fragment. */ + cb = sipMalloc(sizeof (codeBlock)); + + if (at == ocb->frag) + { + cb->filename = ocb->filename; + cb->linenr = ocb->linenr; + } + else + cb->filename = NULL; + + cb->next = NULL; + *tail = cb; + tail = &cb->next; + + /* See if anything was found. */ + if (first == NULL) + { + /* We can just point to this. */ + cb->frag = at; + + /* All done with this one. */ + at = NULL; + } + else + { + static char *gen_names[] = { + "sipForceConvertToTransfer_", + "sipForceConvertTo_", + "sipConvertFromTransfer_", + "sipConvertFrom_", + "sipClass_", + "sipEnum_", + "sipException_", + NULL + }; + + char *dp, *sp, **gn; + int genname = FALSE; + + /* + * If the context in which the text is used is + * in the name of a SIP generated object then + * translate any "::" scoping to "_". + */ + for (gn = gen_names; *gn != NULL; ++gn) + if (search_back(first, at, *gn)) + { + addUsedFromCode(pt, used, val_first->name); + genname = TRUE; + break; + } + + /* Fragment the fragment. */ + cb->frag = sipMalloc(first - at + strlen(val_first->name) + 1); + + strncpy(cb->frag, at, first - at); + + dp = &cb->frag[first - at]; + sp = val_first->name; + + if (genname) + { + char gch; + + while ((gch = *sp++) != '\0') + if (gch == ':' && *sp == ':') + { + *dp++ = '_'; + ++sp; + } + else + *dp++ = gch; + + *dp = '\0'; + } + else + strcpy(dp, sp); + + /* Move past the replaced text. */ + at = first + strlen(nam_first->name); + } + } + while (at != NULL && *at != '\0'); + + ocb = ocb->next; + } + + return ncb; +} + + +/* + * Return TRUE if the text at the end of a string matches the target string. + */ +static int search_back(const char *end, const char *start, const char *target) +{ + size_t tlen = strlen(target); + + if (start + tlen >= end) + return FALSE; + + return (strncmp(end - tlen, target, tlen) == 0); +} + + +/* + * Add any needed interface files based on handwritten code. + */ +static void addUsedFromCode(sipSpec *pt, ifaceFileList **used, const char *sname) +{ + ifaceFileDef *iff; + enumDef *ed; + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + { + if (iff->type != class_iface && iff->type != exception_iface) + continue; + + if (sameName(iff->fqcname, sname)) + { + addToUsedList(used, iff); + + return; + } + } + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + if (ed->ecd == NULL) + continue; + + if (sameName(ed->fqcname, sname)) + { + addToUsedList(used, ed->ecd->iff); + + return; + } + } +} + + +/* + * Compare a scoped name with its string equivalent. + */ +static int sameName(scopedNameDef *snd, const char *sname) +{ + while (snd != NULL && *sname != '\0') + { + const char *sp = snd->name; + + while (*sp != '\0' && *sname != ':' && *sname != '\0') + if (*sp++ != *sname++) + return FALSE; + + if (*sp != '\0' || (*sname != ':' && *sname != '\0')) + return FALSE; + + snd = snd->next; + + if (*sname == ':') + sname += 2; + } + + return (snd == NULL && *sname == '\0'); +} + + +/* + * Create a new typedef. + */ +static void newTypedef(sipSpec *pt,moduleDef *mod,char *name,argDef *type) +{ + typedefDef *td; + scopedNameDef *fqname = text2scopedName(name); + classDef *scope = currentScope(); + + /* See if we are instantiating a template class. */ + if (type->atype == template_type) + { + classTmplDef *tcd; + templateDef *td = type->u.td; + + for (tcd = pt->classtemplates; tcd != NULL; tcd = tcd->next) + if (sameScopedName(tcd->cd->iff->fqcname, td->fqname)) + { + if (!sameTemplateSignature(&tcd->sig, &td->types, FALSE)) + continue; + + instantiateClassTemplate(pt, mod, scope, fqname, tcd, td); + + /* All done. */ + return; + } + } + + /* Check it doesn't already exist. */ + for (td = pt -> typedefs; td != NULL; td = td -> next) + if (sameScopedName(td -> fqname,fqname)) + { + fatalScopedName(fqname); + fatal(" already defined\n"); + } + + td = sipMalloc(sizeof (typedefDef)); + + td -> fqname = fqname; + td -> ecd = scope; + td -> module = mod; + td -> type = *type; + td -> next = pt -> typedefs; + + mod -> nrtypedefs++; + + pt -> typedefs = td; +} + + +/* + * Return TRUE if the template signatures are the same. A deep comparison is + * used for mapped type templates where we want to recurse into any nested + * templates. + */ +int sameTemplateSignature(signatureDef *sd1, signatureDef *sd2, int deep) +{ + int a; + + if (sd1->nrArgs != sd2->nrArgs) + return FALSE; + + for (a = 0; a < sd1->nrArgs; ++a) + { + argDef *ad1 = &sd1->args[a]; + argDef *ad2 = &sd2->args[a]; + + /* + * If we are doing a shallow comparision (ie. for class + * templates) then a type name on the left hand side matches + * anything on the right hand side. + */ + if (ad1->atype == defined_type && !deep) + continue; + + /* + * For type names only compare the references and pointers, and + * do the same for any nested templates. + */ + if (ad1->atype == defined_type && ad2->atype == defined_type) + { + if (isReference(ad1) != isReference(ad2) || ad1->nrderefs != ad2->nrderefs) + return FALSE; + } + else if (ad1->atype == template_type && ad2->atype == template_type) + { + if (!sameTemplateSignature(&ad1->u.td->types, &ad2->u.td->types, deep)) + return FALSE; + } + else if (!sameBaseType(ad1, ad2)) + return FALSE; + } + + return TRUE; +} + + +/* + * Create a new variable. + */ +static void newVar(sipSpec *pt,moduleDef *mod,char *name,int isstatic, + argDef *type,optFlags *of,codeBlock *acode,codeBlock *gcode, + codeBlock *scode) +{ + varDef *var; + classDef *escope = currentScope(); + nameDef *nd = cacheName(pt,getPythonName(of,name)); + + if (inMainModule()) + setIsUsedName(nd); + + checkAttributes(pt,escope,nd -> text,FALSE); + + var = sipMalloc(sizeof (varDef)); + + var -> pyname = nd; + var -> fqcname = text2scopedName(name); + var -> ecd = escope; + var -> module = mod; + var -> varflags = 0; + var -> type = *type; + var -> accessfunc = acode; + var -> getcode = gcode; + var -> setcode = scode; + var -> next = pt -> vars; + + if (isstatic || (escope != NULL && escope->iff->type == namespace_iface)) + setIsStaticVar(var); + + pt -> vars = var; +} + + +/* + * Create a new ctor. + */ +static void newCtor(char *name,int sectFlags,signatureDef *args, + optFlags *optflgs,codeBlock *methodcode, + throwArgs *exceptions,signatureDef *cppsig,int explicit) +{ + ctorDef *ct, **ctp; + classDef *cd = currentScope(); + + /* Check the name of the constructor. */ + if (strcmp(classBaseName(cd),name) != 0) + yyerror("Constructor doesn't have the same name as its class"); + + /* Add to the list of constructors. */ + ct = sipMalloc(sizeof (ctorDef)); + + ct -> ctorflags = sectFlags; + ct -> pysig = *args; + ct -> cppsig = (cppsig != NULL ? cppsig : &ct -> pysig); + ct -> exceptions = exceptions; + ct -> methodcode = methodcode; + ct -> next = NULL; + + if (!isPrivateCtor(ct)) + setCanCreate(cd); + + if (isProtectedCtor(ct)) + setHasShadow(cd); + + if (explicit) + setIsExplicitCtor(ct); + + getHooks(optflgs,&ct -> prehook,&ct -> posthook); + + if (getReleaseGIL(optflgs)) + setIsReleaseGILCtor(ct); + else if (getHoldGIL(optflgs)) + setIsHoldGILCtor(ct); + + if (findOptFlag(optflgs,"NoDerived",bool_flag) != NULL) + { + if (cppsig != NULL) + yyerror("The /NoDerived/ annotation cannot be used with a C++ signature"); + + if (methodcode == NULL) + yyerror("The /NoDerived/ annotation must be used with %MethodCode"); + + ct->cppsig = NULL; + } + + if (findOptFlag(optflgs,"Default",bool_flag) != NULL) + { + if (cd -> defctor != NULL) + yyerror("A constructor with the /Default/ annotation has already been defined"); + + cd -> defctor = ct; + } + + /* Append to the list. */ + for (ctp = &cd->ctors; *ctp != NULL; ctp = &(*ctp)->next) + ; + + *ctp = ct; +} + + +/* + * Create a new function. + */ +static void newFunction(sipSpec *pt,moduleDef *mod,int sflags,int isstatic, + int isvirt,char *name,signatureDef *sig,int isconst, + int isabstract,optFlags *optflgs,codeBlock *methodcode, + codeBlock *vcode,throwArgs *exceptions, + signatureDef *cppsig) +{ + classDef *cd = currentScope(); + nameDef *pname; + int factory, xferback; + overDef *od, **odp, **headp; + optFlag *of; + virtHandlerDef *vhd; + + /* Extra checks for a C module. */ + if (pt -> genc) + { + if (cd != NULL) + yyerror("Function declaration not allowed in a struct in a C module"); + + if (isstatic) + yyerror("Static functions not allowed in a C module"); + + if (exceptions != NULL) + yyerror("Exceptions not allowed in a C module"); + } + + headp = (cd != NULL ? &cd -> overs : &pt -> overs); + + /* See if it is a factory method. */ + if (findOptFlag(optflgs,"Factory",bool_flag) != NULL) + factory = TRUE; + else + { + int a; + + factory = FALSE; + + /* Check /TransferThis/ wasn't specified. */ + if (cd == NULL || isstatic) + for (a = 0; a < sig -> nrArgs; ++a) + if (isThisTransferred(&sig -> args[a])) + yyerror("/TransferThis/ may only be specified in constructors and class methods"); + } + + /* See if the result is to be returned to Python ownership. */ + xferback = (findOptFlag(optflgs,"TransferBack",bool_flag) != NULL); + + if (factory && xferback) + yyerror("/TransferBack/ and /Factory/ cannot both be specified"); + + /* Use the C++ name if a Python name wasn't given. */ + pname = cacheName(pt, getPythonName(optflgs, name)); + + /* Create a new overload definition. */ + + od = sipMalloc(sizeof (overDef)); + + /* Set the overload flags. */ + + od -> overflags = sflags; + + if (factory) + setIsFactory(od); + + if (xferback) + setIsResultTransferredBack(od); + + if (isProtected(od)) + setHasShadow(cd); + + if ((isSlot(od) || isSignal(od)) && !isPrivate(od)) + { + if (isSignal(od)) + setHasShadow(cd); + + pt -> sigslots = TRUE; + } + + if (isSignal(od) && (methodcode != NULL || vcode != NULL)) + yyerror("Cannot provide code for signals"); + + if (isstatic) + { + if (isSignal(od)) + yyerror("Static functions cannot be signals"); + + if (isvirt) + yyerror("Static functions cannot be virtual"); + + setIsStatic(od); + } + + if (isconst) + setIsConst(od); + + if (isabstract) + { + if (sflags == 0) + yyerror("Non-class function specified as abstract"); + + setIsAbstract(od); + } + + if ((of = findOptFlag(optflgs,"AutoGen",opt_name_flag)) != NULL) + { + setIsAutoGen(od); + + if (of -> fvalue.sval != NULL) + { + qualDef *qd; + + if ((qd = findQualifier(of -> fvalue.sval)) == NULL || qd -> qtype != feature_qualifier) + yyerror("No such feature"); + + if (excludedFeature(excludedQualifiers,qd)) + resetIsAutoGen(od); + } + } + + if (isvirt) + { + if (isSignal(od) && !optNoEmitters(pt)) + yyerror("Virtual signals aren't supported"); + + setIsVirtual(od); + setHasShadow(cd); + + vhd = sipMalloc(sizeof (virtHandlerDef)); + + vhd -> virthandlernr = -1; + vhd -> vhflags = 0; + vhd -> pysig = &od -> pysig; + vhd -> cppsig = (cppsig != NULL ? cppsig : &od -> pysig); + vhd -> module = currentModule; + vhd -> virtcode = vcode; + vhd -> next = currentModule -> virthandlers; + + if (factory || xferback) + setIsTransferVH(vhd); + + currentModule -> virthandlers = vhd; + } + else + { + if (vcode != NULL) + yyerror("%VirtualCatcherCode provided for non-virtual function"); + + vhd = NULL; + } + + od -> cppname = name; + od -> pysig = *sig; + od -> cppsig = (cppsig != NULL ? cppsig : &od -> pysig); + od -> exceptions = exceptions; + od -> methodcode = methodcode; + od -> virthandler = vhd; + od -> common = findFunction(pt,mod,cd,pname,(methodcode != NULL),sig -> nrArgs); + + if (findOptFlag(optflgs,"Numeric",bool_flag) != NULL) + setIsNumeric(od -> common); + + /* Methods that run in new threads must be virtual. */ + if (findOptFlag(optflgs,"NewThread",bool_flag) != NULL) + { + argDef *res; + + if (!isvirt) + yyerror("/NewThread/ may only be specified for virtual functions"); + + /* + * This is an arbitary limitation to make the code generator + * slightly easier - laziness on my part. + */ + res = &od -> cppsig -> result; + + if (res -> atype != void_type || res -> nrderefs != 0) + yyerror("/NewThread/ may only be specified for void functions"); + + setIsNewThread(od); + } + + getHooks(optflgs,&od -> prehook,&od -> posthook); + + if (getReleaseGIL(optflgs)) + setIsReleaseGIL(od); + else if (getHoldGIL(optflgs)) + setIsHoldGIL(od); + + od -> next = NULL; + + /* Append to the list. */ + for (odp = headp; *odp != NULL; odp = &(*odp)->next) + ; + + *odp = od; +} + + +/* + * Return the Python name based on the C/C++ name and any /PyName/ annotation. + */ +static char *getPythonName(optFlags *optflgs, char *cname) +{ + char *pname; + optFlag *of; + + if ((of = findOptFlag(optflgs, "PyName", name_flag)) != NULL) + pname = of -> fvalue.sval; + else + pname = cname; + + return pname; +} + + +/* + * Cache a name in a module. + */ +static nameDef *cacheName(sipSpec *pt,char *name) +{ + nameDef *nd; + + /* See if it already exists. */ + for (nd = pt -> namecache; nd != NULL; nd = nd -> next) + if (strcmp(nd -> text,name) == 0) + return nd; + + /* Create a new one. */ + nd = sipMalloc(sizeof (nameDef)); + + nd -> nameflags = 0; + nd -> module = currentSpec -> module; + nd -> text = name; + nd -> next = pt -> namecache; + + pt -> namecache = nd; + + return nd; +} + + +/* + * Find (or create) an overloaded function name. + */ +static memberDef *findFunction(sipSpec *pt,moduleDef *mod,classDef *cd, + nameDef *pname,int hwcode,int nrargs) +{ + static struct slot_map { + char *name; /* The slot name. */ + slotType type; /* The corresponding type. */ + int needs_hwcode; /* If handwritten code is required. */ + int nrargs; /* Nr. of arguments. */ + } slot_table[] = { + {"__str__", str_slot, TRUE, 0}, + {"__unicode__", unicode_slot, TRUE, 0}, + {"__int__", int_slot, FALSE, 0}, + {"__long__", long_slot, FALSE, 0}, + {"__float__", float_slot, FALSE, 0}, + {"__len__", len_slot, TRUE, 0}, + {"__contains__", contains_slot, TRUE, 1}, + {"__add__", add_slot, FALSE, 1}, + {"__sub__", sub_slot, FALSE, 1}, + {"__mul__", mul_slot, FALSE, 1}, + {"__div__", div_slot, FALSE, 1}, + {"__mod__", mod_slot, FALSE, 1}, + {"__and__", and_slot, FALSE, 1}, + {"__or__", or_slot, FALSE, 1}, + {"__xor__", xor_slot, FALSE, 1}, + {"__lshift__", lshift_slot, FALSE, 1}, + {"__rshift__", rshift_slot, FALSE, 1}, + {"__iadd__", iadd_slot, FALSE, 1}, + {"__isub__", isub_slot, FALSE, 1}, + {"__imul__", imul_slot, FALSE, 1}, + {"__idiv__", idiv_slot, FALSE, 1}, + {"__imod__", imod_slot, FALSE, 1}, + {"__iand__", iand_slot, FALSE, 1}, + {"__ior__", ior_slot, FALSE, 1}, + {"__ixor__", ixor_slot, FALSE, 1}, + {"__ilshift__", ilshift_slot, FALSE, 1}, + {"__irshift__", irshift_slot, FALSE, 1}, + {"__invert__", invert_slot, FALSE, 0}, + {"__call__", call_slot, FALSE, -1}, + {"__getitem__", getitem_slot, FALSE, -1}, + {"__setitem__", setitem_slot, TRUE, -1}, + {"__delitem__", delitem_slot, TRUE, -1}, + {"__lt__", lt_slot, FALSE, 1}, + {"__le__", le_slot, FALSE, 1}, + {"__eq__", eq_slot, FALSE, 1}, + {"__ne__", ne_slot, FALSE, 1}, + {"__gt__", gt_slot, FALSE, 1}, + {"__ge__", ge_slot, FALSE, 1}, + {"__cmp__", cmp_slot, FALSE, 1}, + {"__nonzero__", nonzero_slot, TRUE, 0}, + {"__neg__", neg_slot, FALSE, 0}, + {"__pos__", pos_slot, FALSE, 0}, + {"__abs__", abs_slot, TRUE, 0}, + {"__repr__", repr_slot, TRUE, 0}, + {"__hash__", hash_slot, TRUE, 0}, + {NULL} + }; + + memberDef *md, **flist; + struct slot_map *sm; + slotType st; + + /* Get the slot type. */ + st = no_slot; + + for (sm = slot_table; sm -> name != NULL; ++sm) + if (strcmp(sm -> name,pname -> text) == 0) + { + if (sm -> needs_hwcode && !hwcode) + yyerror("This Python slot requires %MethodCode"); + + if (sm -> nrargs < 0) + { + int min_nr; + + /* These require a minimum number. */ + switch (sm -> type) + { + case getitem_slot: + case delitem_slot: + min_nr = 1; + break; + + case setitem_slot: + min_nr = 2; + break; + + default: + min_nr = 0; + } + + if (nrargs < min_nr) + yyerror("Insufficient number of arguments to Python slot"); + } + else if (cd == NULL) + { + /* Global operators need one extra argument. */ + if (sm -> nrargs + 1 != nrargs) + yyerror("Incorrect number of arguments to global operator"); + } + else if (sm -> nrargs != nrargs) + yyerror("Incorrect number of arguments to Python slot"); + + st = sm -> type; + + break; + } + + if (inMainModule()) + setIsUsedName(pname); + + /* Check there is no name clash. */ + checkAttributes(pt,cd,pname -> text,TRUE); + + /* See if it already exists. */ + flist = (cd != NULL ? &cd -> members : &pt -> othfuncs); + + for (md = *flist; md != NULL; md = md -> next) + if (md -> pyname == pname && md -> module == mod) + return md; + + /* Create a new one. */ + md = sipMalloc(sizeof (memberDef)); + + md -> pyname = pname; + md -> memberflags = 0; + md -> slot = st; + md -> module = mod; + md -> next = *flist; + + *flist = md; + + /* Global operators are a subset. */ + if (cd == NULL && st != no_slot && st != neg_slot && st != pos_slot && !isNumberSlot(md) && !isRichCompareSlot(md)) + yyerror("Global operators must be either numeric or comparison operators"); + + return md; +} + + +/* + * Search a set of flags for a particular one and check its type. + */ +static optFlag *findOptFlag(optFlags *flgs,char *name,flagType ft) +{ + int f; + + for (f = 0; f < flgs -> nrFlags; ++f) + { + optFlag *of = &flgs -> flags[f]; + + if (strcmp(of -> fname,name) == 0) + { + /* + * An optional name can look like a boolean or a name. + */ + + if (ft == opt_name_flag) + { + if (of -> ftype == bool_flag) + { + of -> ftype = opt_name_flag; + of -> fvalue.sval = NULL; + } + else if (of -> ftype == name_flag) + of -> ftype = opt_name_flag; + } + + if (ft != of -> ftype) + yyerror("Optional flag has a value of the wrong type"); + + return of; + } + } + + return NULL; +} + + +/* + * A name is going to be used as a Python attribute name within a Python scope + * (ie. a Python dictionary), so check against what we already know is going in + * the same scope in case there is a clash. + */ +static void checkAttributes(sipSpec *pt,classDef *pyscope,char *attr,int isfunc) +{ + enumDef *ed; + varDef *vd; + classDef *cd; + + /* Check the enums. */ + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + { + enumMemberDef *emd; + + if (ed -> ecd != pyscope || ed -> pyname == NULL) + continue; + + if (strcmp(ed->pyname->text, attr) == 0) + yyerror("There is already an enum in scope with the same Python name"); + + for (emd = ed -> members; emd != NULL; emd = emd -> next) + if (strcmp(emd -> pyname -> text, attr) == 0) + yyerror("There is already an enum member in scope with the same Python name"); + } + + /* Check the variables. */ + + for (vd = pt -> vars; vd != NULL; vd = vd -> next) + { + if (vd -> ecd != pyscope) + continue; + + if (strcmp(vd -> pyname -> text, attr) == 0) + yyerror("There is already a variable in scope with the same Python name"); + } + + /* + * Only check the members if this attribute isn't a member because we + * can handle members with the same name in the same scope. + */ + if (!isfunc) + { + memberDef *md, *membs; + + membs = (pyscope != NULL ? pyscope -> members : pt -> othfuncs); + + for (md = membs; md != NULL; md = md -> next) + { + overDef *od, *overs; + + if (strcmp(md -> pyname -> text, attr) != 0) + continue; + + /* Check for a conflict with all overloads. */ + + overs = (pyscope != NULL ? pyscope -> overs : pt -> overs); + + for (od = overs; od != NULL; od = od -> next) + { + if (od -> common != md) + continue; + + yyerror("There is already a function in scope with the same Python name"); + } + } + } + + /* Check the classes. */ + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + if (cd -> ecd != pyscope || cd -> pyname == NULL) + continue; + + if (strcmp(cd->pyname, attr) == 0 && !isExternal(cd)) + yyerror("There is already a class or namespace in scope with the same Python name"); + } + + /* Check the exceptions. */ + + if (pyscope == NULL) + { + exceptionDef *xd; + + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + if (xd->pyname != NULL && strcmp(xd->pyname, attr) == 0) + yyerror("There is already an exception with the same Python name"); + } +} + + +/* + * Append a code block to a list of them. Append is needed to give the + * specifier easy control over the order of the documentation. + */ +static void appendCodeBlock(codeBlock **headp,codeBlock *new) +{ + while (*headp != NULL) + headp = &(*headp) -> next; + + *headp = new; +} + + +/* + * Handle the end of a fully parsed a file. + */ +static void handleEOF() +{ + /* + * Check that the number of nested if's is the same as when we started + * the file. + */ + + if (skipStackPtr > newContext.ifdepth) + fatal("Too many %%If statements in %s\n",previousFile); + + if (skipStackPtr < newContext.ifdepth) + fatal("Too many %%End statements in %s\n",previousFile); +} + + +/* + * Handle the end of a fully parsed a module. + */ +static void handleEOM() +{ + /* Check it has been named. */ + + if (currentModule -> name == NULL) + fatal("No %%Module has been specified for module defined in %s\n",previousFile); + + /* The previous module is now current. */ + + currentModule = newContext.prevmod; +} + + +/* + * Find an existing qualifier. + */ +static qualDef *findQualifier(char *name) +{ + moduleDef *mod; + + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + { + qualDef *qd; + + for (qd = mod -> qualifiers; qd != NULL; qd = qd -> next) + if (strcmp(qd -> name,name) == 0) + return qd; + } + + return NULL; +} + + +/* + * Return a copy of a scoped name. + */ +scopedNameDef *copyScopedName(scopedNameDef *snd) +{ + scopedNameDef *head; + + head = NULL; + + while (snd != NULL) + { + appendScopedName(&head,text2scopePart(snd -> name)); + snd = snd -> next; + } + + return head; +} + + +/* + * Append a name to a list of scopes. + */ +void appendScopedName(scopedNameDef **headp,scopedNameDef *newsnd) +{ + while (*headp != NULL) + headp = &(*headp) -> next; + + *headp = newsnd; +} + + +/* + * Free a scoped name - but not the text itself. + */ +void freeScopedName(scopedNameDef *snd) +{ + while (snd != NULL) + { + scopedNameDef *next = snd -> next; + + free(snd); + + snd = next; + } +} + + +/* + * Convert a text string to a scope part structure. + */ +scopedNameDef *text2scopePart(char *text) +{ + scopedNameDef *snd; + + snd = sipMalloc(sizeof (scopedNameDef)); + + snd -> name = text; + snd -> next = NULL; + + return snd; +} + + +/* + * Convert a text string to a fully scoped name. + */ +static scopedNameDef *text2scopedName(char *text) +{ + return scopeScopedName(text2scopePart(text)); +} + + +/* + * Prepend any current scope to a scoped name. + */ +static scopedNameDef *scopeScopedName(scopedNameDef *name) +{ + classDef *cd = currentScope(); + scopedNameDef *snd; + + snd = (cd != NULL ? copyScopedName(cd->iff->fqcname) : NULL); + + appendScopedName(&snd, name); + + return snd; +} + + +/* + * Return a pointer to the tail part of a scoped name. + */ +char *scopedNameTail(scopedNameDef *snd) +{ + if (snd == NULL) + return NULL; + + while (snd -> next != NULL) + snd = snd -> next; + + return snd -> name; +} + + +/* + * Push the given scope onto the scope stack. + */ +static void pushScope(classDef *scope) +{ + if (currentScopeIdx >= MAX_NESTED_SCOPE) + fatal("Internal error: increase the value of MAX_NESTED_SCOPE\n"); + + scopeStack[currentScopeIdx] = scope; + sectFlagsStack[currentScopeIdx] = sectionFlags; + + ++currentScopeIdx; +} + + +/* + * Pop the scope stack. + */ +static void popScope(void) +{ + if (currentScopeIdx > 0) + sectionFlags = sectFlagsStack[--currentScopeIdx]; +} + + +/* + * Return non-zero if the current input should be parsed rather than be + * skipped. + */ +static int notSkipping() +{ + return (skipStackPtr == 0 ? TRUE : skipStack[skipStackPtr - 1]); +} + + +/* + * Return the value of an expression involving a time period. + */ +static int timePeriod(char *lname,char *uname) +{ + int this, line; + qualDef *qd, *lower, *upper; + moduleDef *mod; + + if (lname == NULL) + lower = NULL; + else if ((lower = findQualifier(lname)) == NULL || lower -> qtype != time_qualifier) + yyerror("Lower bound is not a time version"); + + if (uname == NULL) + upper = NULL; + else if ((upper = findQualifier(uname)) == NULL || upper -> qtype != time_qualifier) + yyerror("Upper bound is not a time version"); + + /* Sanity checks on the bounds. */ + + if (lower == NULL && upper == NULL) + yyerror("Lower and upper bounds cannot both be omitted"); + + if (lower != NULL && upper != NULL) + { + if (lower -> module != upper -> module || lower -> line != upper -> line) + yyerror("Lower and upper bounds are from different timelines"); + + if (lower == upper) + yyerror("Lower and upper bounds must be different"); + + if (lower -> order > upper -> order) + yyerror("Later version specified as lower bound"); + } + + /* Go through each slot in the relevant timeline. */ + + if (lower != NULL) + { + mod = lower -> module; + line = lower -> line; + } + else + { + mod = upper -> module; + line = upper -> line; + } + + this = FALSE; + + for (qd = mod -> qualifiers; qd != NULL; qd = qd -> next) + { + if (qd -> qtype != time_qualifier || qd -> line != line) + continue; + + if (lower != NULL && qd -> order < lower -> order) + continue; + + if (upper != NULL && qd -> order >= upper -> order) + continue; + + /* + * This is within the required range so if it is also needed + * then the expression is true. + */ + + if (isNeeded(qd)) + { + this = TRUE; + break; + } + } + + return this; +} + + +/* + * Return the value of an expression involving a single platform or feature. + */ +static int platOrFeature(char *name,int optnot) +{ + int this; + qualDef *qd; + + if ((qd = findQualifier(name)) == NULL || qd -> qtype == time_qualifier) + yyerror("No such platform or feature"); + + /* Assume this sub-expression is false. */ + + this = FALSE; + + if (qd -> qtype == feature_qualifier) + { + if (!excludedFeature(excludedQualifiers,qd)) + this = TRUE; + } + else if (isNeeded(qd)) + this = TRUE; + + if (optnot) + this = !this; + + return this; +} + + +/* + * Return TRUE if the given qualifier is excluded. + */ +int excludedFeature(stringList *xsl,qualDef *qd) +{ + while (xsl != NULL) + { + if (strcmp(qd -> name,xsl -> s) == 0) + return TRUE; + + xsl = xsl -> next; + } + + return FALSE; +} + + +/* + * Return TRUE if the given qualifier is needed. + */ +static int isNeeded(qualDef *qd) +{ + stringList *sl; + + for (sl = neededQualifiers; sl != NULL; sl = sl -> next) + if (strcmp(qd -> name,sl -> s) == 0) + return TRUE; + + return FALSE; +} + + +/* + * Return the current scope. currentScope() is only valid if notSkipping() + * returns non-zero. + */ +static classDef *currentScope(void) +{ + return (currentScopeIdx > 0 ? scopeStack[currentScopeIdx - 1] : NULL); +} + + +/* + * Create a new qualifier. + */ +static void newQualifier(moduleDef *mod,int line,int order,char *name,qualType qt) +{ + qualDef *qd; + + /* Check it doesn't already exist. */ + + if (findQualifier(name) != NULL) + yyerror("Version is already defined"); + + qd = sipMalloc(sizeof (qualDef)); + qd -> name = name; + qd -> qtype = qt; + qd -> module = mod; + qd -> line = line; + qd -> order = order; + qd -> next = mod -> qualifiers; + mod -> qualifiers = qd; +} + + +/* + * Create a new imported module. + */ +static void newImport(char *name) +{ + moduleDef *from, *mod; + moduleListDef *mld; + + /* Create a new module if it has already been imported. */ + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + if (strcmp(mod -> file,name) == 0) + break; + + from = currentModule; + + if (mod == NULL) + { + newModule(NULL,name); + mod = currentModule; + } + + /* Add the new import unless it has already been imported. */ + for (mld = from->imports; mld != NULL; mld = mld->next) + if (mld->module == mod) + return; + + mld = sipMalloc(sizeof (moduleListDef)); + mld -> module = mod; + mld -> next = from->imports; + + from->imports = mld; +} + + +/* + * Set up pointers to hook names. + */ +static void getHooks(optFlags *optflgs,char **pre,char **post) +{ + optFlag *of; + + if ((of = findOptFlag(optflgs,"PreHook",name_flag)) != NULL) + *pre = of -> fvalue.sval; + else + *pre = NULL; + + if ((of = findOptFlag(optflgs,"PostHook",name_flag)) != NULL) + *post = of -> fvalue.sval; + else + *post = NULL; +} + + +/* + * Get the /ReleaseGIL/ option flag. + */ +static int getReleaseGIL(optFlags *optflgs) +{ + return (findOptFlag(optflgs, "ReleaseGIL", bool_flag) != NULL); +} + + +/* + * Get the /HoldGIL/ option flag. + */ +static int getHoldGIL(optFlags *optflgs) +{ + return (findOptFlag(optflgs, "HoldGIL", bool_flag) != NULL); +} + + +/* + * Return TRUE if the QtNoEmitters option was specified. + */ +int optNoEmitters(sipSpec *pt) +{ + return optFind(pt, "QtNoEmitters"); +} + + +/* + * Return TRUE if the QtRegisterTypes option was specified. + */ +int optRegisterTypes(sipSpec *pt) +{ + return optFind(pt, "QtRegisterTypes"); +} + + +/* + * Return TRUE if the Qt4Q_OBJECT option was specified. + */ +int optQ_OBJECT4(sipSpec *pt) +{ + return optFind(pt, "Qt4Q_OBJECT"); +} + + +/* + * Return TRUE if a particular option was specified with %SIPOptions. + */ +static int optFind(sipSpec *pt, const char *opt) +{ + stringList *sl; + + for (sl = pt->options; sl != NULL; sl = sl->next) + if (strcmp(sl->s, opt) == 0) + return TRUE; + + return FALSE; +} + diff --git a/python/sip/sipgen/parser.h b/python/sip/sipgen/parser.h new file mode 100644 index 00000000..49cbce77 --- /dev/null +++ b/python/sip/sipgen/parser.h @@ -0,0 +1,268 @@ +/* A Bison parser, made by GNU Bison 1.875d. */ + +/* Skeleton parser for Yacc-like parsing with Bison, + Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. + + 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, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* As a special exception, when this file is copied by Bison into a + Bison output file, you may use that output file without restriction. + This special exception was added by the Free Software Foundation + in version 1.24 of Bison. */ + +/* Tokens. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + /* Put the tokens into the symbol table, so that GDB and other debuggers + know about them. */ + enum yytokentype { + TK_OPTIONS = 258, + TK_NOEMITTERS = 259, + TK_DOC = 260, + TK_EXPORTEDDOC = 261, + TK_MAKEFILE = 262, + TK_ACCESSCODE = 263, + TK_GETCODE = 264, + TK_SETCODE = 265, + TK_PREINITCODE = 266, + TK_POSTINITCODE = 267, + TK_UNITCODE = 268, + TK_MODCODE = 269, + TK_TYPECODE = 270, + TK_PREPYCODE = 271, + TK_COPYING = 272, + TK_MAPPEDTYPE = 273, + TK_CODELINE = 274, + TK_IF = 275, + TK_END = 276, + TK_NAME = 277, + TK_PATHNAME = 278, + TK_STRING = 279, + TK_VIRTUALCATCHERCODE = 280, + TK_TRAVERSECODE = 281, + TK_CLEARCODE = 282, + TK_READBUFFERCODE = 283, + TK_WRITEBUFFERCODE = 284, + TK_SEGCOUNTCODE = 285, + TK_CHARBUFFERCODE = 286, + TK_METHODCODE = 287, + TK_FROMTYPE = 288, + TK_TOTYPE = 289, + TK_TOSUBCLASS = 290, + TK_INCLUDE = 291, + TK_OPTINCLUDE = 292, + TK_IMPORT = 293, + TK_EXPHEADERCODE = 294, + TK_MODHEADERCODE = 295, + TK_TYPEHEADERCODE = 296, + TK_MODULE = 297, + TK_CMODULE = 298, + TK_CLASS = 299, + TK_STRUCT = 300, + TK_PUBLIC = 301, + TK_PROTECTED = 302, + TK_PRIVATE = 303, + TK_SIGNALS = 304, + TK_SLOTS = 305, + TK_BOOL = 306, + TK_SHORT = 307, + TK_INT = 308, + TK_LONG = 309, + TK_FLOAT = 310, + TK_DOUBLE = 311, + TK_CHAR = 312, + TK_WCHAR_T = 313, + TK_VOID = 314, + TK_PYOBJECT = 315, + TK_PYTUPLE = 316, + TK_PYLIST = 317, + TK_PYDICT = 318, + TK_PYCALLABLE = 319, + TK_PYSLICE = 320, + TK_PYTYPE = 321, + TK_VIRTUAL = 322, + TK_ENUM = 323, + TK_SIGNED = 324, + TK_UNSIGNED = 325, + TK_SCOPE = 326, + TK_LOGICAL_OR = 327, + TK_CONST = 328, + TK_STATIC = 329, + TK_SIPSIGNAL = 330, + TK_SIPSLOT = 331, + TK_SIPANYSLOT = 332, + TK_SIPRXCON = 333, + TK_SIPRXDIS = 334, + TK_SIPSLOTCON = 335, + TK_SIPSLOTDIS = 336, + TK_NUMBER = 337, + TK_REAL = 338, + TK_TYPEDEF = 339, + TK_NAMESPACE = 340, + TK_TIMELINE = 341, + TK_PLATFORMS = 342, + TK_FEATURE = 343, + TK_LICENSE = 344, + TK_QCHAR = 345, + TK_TRUE = 346, + TK_FALSE = 347, + TK_NULL = 348, + TK_OPERATOR = 349, + TK_THROW = 350, + TK_QOBJECT = 351, + TK_EXCEPTION = 352, + TK_RAISECODE = 353, + TK_EXPLICIT = 354, + TK_TEMPLATE = 355, + TK_ELLIPSIS = 356 + }; +#endif +#define TK_OPTIONS 258 +#define TK_NOEMITTERS 259 +#define TK_DOC 260 +#define TK_EXPORTEDDOC 261 +#define TK_MAKEFILE 262 +#define TK_ACCESSCODE 263 +#define TK_GETCODE 264 +#define TK_SETCODE 265 +#define TK_PREINITCODE 266 +#define TK_POSTINITCODE 267 +#define TK_UNITCODE 268 +#define TK_MODCODE 269 +#define TK_TYPECODE 270 +#define TK_PREPYCODE 271 +#define TK_COPYING 272 +#define TK_MAPPEDTYPE 273 +#define TK_CODELINE 274 +#define TK_IF 275 +#define TK_END 276 +#define TK_NAME 277 +#define TK_PATHNAME 278 +#define TK_STRING 279 +#define TK_VIRTUALCATCHERCODE 280 +#define TK_TRAVERSECODE 281 +#define TK_CLEARCODE 282 +#define TK_READBUFFERCODE 283 +#define TK_WRITEBUFFERCODE 284 +#define TK_SEGCOUNTCODE 285 +#define TK_CHARBUFFERCODE 286 +#define TK_METHODCODE 287 +#define TK_FROMTYPE 288 +#define TK_TOTYPE 289 +#define TK_TOSUBCLASS 290 +#define TK_INCLUDE 291 +#define TK_OPTINCLUDE 292 +#define TK_IMPORT 293 +#define TK_EXPHEADERCODE 294 +#define TK_MODHEADERCODE 295 +#define TK_TYPEHEADERCODE 296 +#define TK_MODULE 297 +#define TK_CMODULE 298 +#define TK_CLASS 299 +#define TK_STRUCT 300 +#define TK_PUBLIC 301 +#define TK_PROTECTED 302 +#define TK_PRIVATE 303 +#define TK_SIGNALS 304 +#define TK_SLOTS 305 +#define TK_BOOL 306 +#define TK_SHORT 307 +#define TK_INT 308 +#define TK_LONG 309 +#define TK_FLOAT 310 +#define TK_DOUBLE 311 +#define TK_CHAR 312 +#define TK_WCHAR_T 313 +#define TK_VOID 314 +#define TK_PYOBJECT 315 +#define TK_PYTUPLE 316 +#define TK_PYLIST 317 +#define TK_PYDICT 318 +#define TK_PYCALLABLE 319 +#define TK_PYSLICE 320 +#define TK_PYTYPE 321 +#define TK_VIRTUAL 322 +#define TK_ENUM 323 +#define TK_SIGNED 324 +#define TK_UNSIGNED 325 +#define TK_SCOPE 326 +#define TK_LOGICAL_OR 327 +#define TK_CONST 328 +#define TK_STATIC 329 +#define TK_SIPSIGNAL 330 +#define TK_SIPSLOT 331 +#define TK_SIPANYSLOT 332 +#define TK_SIPRXCON 333 +#define TK_SIPRXDIS 334 +#define TK_SIPSLOTCON 335 +#define TK_SIPSLOTDIS 336 +#define TK_NUMBER 337 +#define TK_REAL 338 +#define TK_TYPEDEF 339 +#define TK_NAMESPACE 340 +#define TK_TIMELINE 341 +#define TK_PLATFORMS 342 +#define TK_FEATURE 343 +#define TK_LICENSE 344 +#define TK_QCHAR 345 +#define TK_TRUE 346 +#define TK_FALSE 347 +#define TK_NULL 348 +#define TK_OPERATOR 349 +#define TK_THROW 350 +#define TK_QOBJECT 351 +#define TK_EXCEPTION 352 +#define TK_RAISECODE 353 +#define TK_EXPLICIT 354 +#define TK_TEMPLATE 355 +#define TK_ELLIPSIS 356 + + + + +#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) +#line 103 "parser.y" +typedef union YYSTYPE { + char qchar; + char *text; + long number; + double real; + argDef memArg; + signatureDef signature; + signatureDef *optsignature; + throwArgs *throwlist; + codeBlock *codeb; + valueDef value; + valueDef *valp; + optFlags optflags; + optFlag flag; + scopedNameDef *scpvalp; + fcallDef fcall; + int boolean; + exceptionDef exceptionbase; + classDef *klass; +} YYSTYPE; +/* Line 1285 of yacc.c. */ +#line 260 "parser.h" +# define yystype YYSTYPE /* obsolescent; will be withdrawn */ +# define YYSTYPE_IS_DECLARED 1 +# define YYSTYPE_IS_TRIVIAL 1 +#endif + +extern YYSTYPE yylval; + + + diff --git a/python/sip/sipgen/parser.y b/python/sip/sipgen/parser.y new file mode 100644 index 00000000..bba7d0c1 --- /dev/null +++ b/python/sip/sipgen/parser.y @@ -0,0 +1,4945 @@ +/* + * The SIP parser. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +%{ +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#include "sip.h" + + +#define MAX_NESTED_IF 10 +#define MAX_NESTED_SCOPE 10 + +#define inMainModule() (currentSpec -> module == currentModule) + + +static sipSpec *currentSpec; /* The current spec being parsed. */ +static stringList *neededQualifiers; /* The list of required qualifiers. */ +static stringList *excludedQualifiers; /* The list of excluded qualifiers. */ +static moduleDef *currentModule; /* The current module being parsed. */ +static mappedTypeDef *currentMappedType; /* The current mapped type. */ +static enumDef *currentEnum; /* The current enum being parsed. */ +static int sectionFlags; /* The current section flags. */ +static int currentOverIsVirt; /* Set if the overload is virtual. */ +static int currentCtorIsExplicit; /* Set if the ctor is explicit. */ +static int currentIsStatic; /* Set if the current is static. */ +static char *previousFile; /* The file just parsed. */ +static parserContext newContext; /* The new pending context. */ +static int skipStackPtr; /* The skip stack pointer. */ +static int skipStack[MAX_NESTED_IF]; /* Stack of skip flags. */ +static classDef *scopeStack[MAX_NESTED_SCOPE]; /* The scope stack. */ +static int sectFlagsStack[MAX_NESTED_SCOPE]; /* The section flags stack. */ +static int currentScopeIdx; /* The scope stack index. */ +static int currentTimelineOrder; /* The current timeline order. */ + + +static char *getPythonName(optFlags *optflgs, char *cname); +static nameDef *cacheName(sipSpec *,char *); +static classDef *findClass(sipSpec *,ifaceFileType,scopedNameDef *); +static classDef *findClassWithInterface(sipSpec *pt, ifaceFileDef *iff); +static classDef *newClass(sipSpec *,ifaceFileType,scopedNameDef *); +static void finishClass(sipSpec *,moduleDef *,classDef *,optFlags *); +static exceptionDef *findException(sipSpec *pt, scopedNameDef *fqname, int new); +static mappedTypeDef *newMappedType(sipSpec *,argDef *); +static enumDef *newEnum(sipSpec *,moduleDef *,char *,optFlags *,int); +static void instantiateClassTemplate(sipSpec *pt, moduleDef *mod, classDef *scope, scopedNameDef *fqname, classTmplDef *tcd, templateDef *td); +static void newTypedef(sipSpec *,moduleDef *,char *,argDef *); +static void newVar(sipSpec *,moduleDef *,char *,int,argDef *,optFlags *, + codeBlock *,codeBlock *,codeBlock *); +static void newCtor(char *,int,signatureDef *,optFlags *,codeBlock *, + throwArgs *,signatureDef *,int); +static void newFunction(sipSpec *,moduleDef *,int,int,int,char *, + signatureDef *,int,int,optFlags *,codeBlock *, + codeBlock *,throwArgs *,signatureDef *); +static optFlag *findOptFlag(optFlags *,char *,flagType); +static memberDef *findFunction(sipSpec *,moduleDef *,classDef *,nameDef *,int, + int); +static void checkAttributes(sipSpec *,classDef *,char *,int); +static void newModule(FILE *,char *); +static void appendCodeBlock(codeBlock **,codeBlock *); +static void parseFile(FILE *,char *,moduleDef *,int); +static void handleEOF(void); +static void handleEOM(void); +static qualDef *findQualifier(char *); +static scopedNameDef *text2scopedName(char *); +static scopedNameDef *scopeScopedName(scopedNameDef *name); +static void pushScope(classDef *); +static void popScope(void); +static classDef *currentScope(void); +static void newQualifier(moduleDef *,int,int,char *,qualType); +static void newImport(char *); +static void usedInMainModule(sipSpec *,ifaceFileDef *); +static int timePeriod(char *,char *); +static int platOrFeature(char *,int); +static int isNeeded(qualDef *); +static int notSkipping(void); +static void getHooks(optFlags *,char **,char **); +static int getReleaseGIL(optFlags *); +static int getHoldGIL(optFlags *); +static void templateSignature(signatureDef *sd, int result, classTmplDef *tcd, templateDef *td, classDef *ncd); +static void templateType(argDef *ad, classTmplDef *tcd, templateDef *td, classDef *ncd); +static int search_back(const char *end, const char *start, const char *target); +static char *getType(scopedNameDef *ename, argDef *ad); +static char *scopedNameToString(scopedNameDef *name); +static void addUsedFromCode(sipSpec *pt, ifaceFileList **used, const char *sname); +static int sameName(scopedNameDef *snd, const char *sname); +static int optFind(sipSpec *pt, const char *opt); +%} + +%union { + char qchar; + char *text; + long number; + double real; + argDef memArg; + signatureDef signature; + signatureDef *optsignature; + throwArgs *throwlist; + codeBlock *codeb; + valueDef value; + valueDef *valp; + optFlags optflags; + optFlag flag; + scopedNameDef *scpvalp; + fcallDef fcall; + int boolean; + exceptionDef exceptionbase; + classDef *klass; +} + +%token TK_OPTIONS +%token TK_NOEMITTERS +%token TK_DOC +%token TK_EXPORTEDDOC +%token TK_MAKEFILE +%token TK_ACCESSCODE +%token TK_GETCODE +%token TK_SETCODE +%token TK_PREINITCODE +%token TK_POSTINITCODE +%token TK_UNITCODE +%token TK_MODCODE +%token TK_TYPECODE +%token TK_PREPYCODE +%token TK_COPYING +%token TK_MAPPEDTYPE +%token <codeb> TK_CODELINE +%token TK_IF +%token TK_END +%token <text> TK_NAME +%token <text> TK_PATHNAME +%token <text> TK_STRING +%token TK_VIRTUALCATCHERCODE +%token TK_TRAVERSECODE +%token TK_CLEARCODE +%token TK_READBUFFERCODE +%token TK_WRITEBUFFERCODE +%token TK_SEGCOUNTCODE +%token TK_CHARBUFFERCODE +%token TK_METHODCODE +%token TK_FROMTYPE +%token TK_TOTYPE +%token TK_TOSUBCLASS +%token TK_INCLUDE +%token TK_OPTINCLUDE +%token TK_IMPORT +%token TK_EXPHEADERCODE +%token TK_MODHEADERCODE +%token TK_TYPEHEADERCODE +%token TK_MODULE +%token TK_CMODULE +%token TK_CLASS +%token TK_STRUCT +%token TK_PUBLIC +%token TK_PROTECTED +%token TK_PRIVATE +%token TK_SIGNALS +%token TK_SLOTS +%token TK_BOOL +%token TK_SHORT +%token TK_INT +%token TK_LONG +%token TK_FLOAT +%token TK_DOUBLE +%token TK_CHAR +%token TK_WCHAR_T +%token TK_VOID +%token TK_PYOBJECT +%token TK_PYTUPLE +%token TK_PYLIST +%token TK_PYDICT +%token TK_PYCALLABLE +%token TK_PYSLICE +%token TK_PYTYPE +%token TK_VIRTUAL +%token TK_ENUM +%token TK_SIGNED +%token TK_UNSIGNED +%token TK_SCOPE +%token TK_LOGICAL_OR +%token TK_CONST +%token TK_STATIC +%token TK_SIPSIGNAL +%token TK_SIPSLOT +%token TK_SIPANYSLOT +%token TK_SIPRXCON +%token TK_SIPRXDIS +%token TK_SIPSLOTCON +%token TK_SIPSLOTDIS +%token <number> TK_NUMBER +%token <real> TK_REAL +%token TK_TYPEDEF +%token TK_NAMESPACE +%token TK_TIMELINE +%token TK_PLATFORMS +%token TK_FEATURE +%token TK_LICENSE +%token <qchar> TK_QCHAR +%token TK_TRUE +%token TK_FALSE +%token TK_NULL +%token TK_OPERATOR +%token TK_THROW +%token TK_QOBJECT +%token TK_EXCEPTION +%token TK_RAISECODE +%token TK_EXPLICIT +%token TK_TEMPLATE +%token TK_ELLIPSIS + +%type <memArg> argvalue +%type <memArg> argtype +%type <memArg> cpptype +%type <memArg> basetype +%type <signature> template +%type <signature> arglist +%type <signature> rawarglist +%type <signature> cpptypelist +%type <optsignature> optsig +%type <optsignature> optctorsig +%type <throwlist> optexceptions +%type <throwlist> exceptionlist +%type <number> optslot +%type <number> optref +%type <number> optconst +%type <number> optvirtual +%type <number> optabstract +%type <number> deref +%type <number> optnumber +%type <value> simplevalue +%type <valp> value +%type <valp> expr +%type <valp> optassign +%type <codeb> optaccesscode +%type <codeb> optgetcode +%type <codeb> optsetcode +%type <codeb> exphdrcode +%type <codeb> modhdrcode +%type <codeb> typehdrcode +%type <codeb> opttypehdrcode +%type <codeb> travcode +%type <codeb> clearcode +%type <codeb> readbufcode +%type <codeb> writebufcode +%type <codeb> segcountcode +%type <codeb> charbufcode +%type <codeb> modcode +%type <codeb> typecode +%type <codeb> codeblock +%type <codeb> codelines +%type <codeb> virtualcatchercode +%type <codeb> methodcode +%type <codeb> raisecode +%type <text> operatorname +%type <text> optfilename +%type <text> optname +%type <text> modname +%type <optflags> optflags +%type <optflags> flaglist +%type <flag> flag +%type <flag> flagvalue +%type <qchar> optunop +%type <qchar> binop +%type <scpvalp> scopepart +%type <scpvalp> scopedname +%type <fcall> exprlist +%type <boolean> qualifiers +%type <boolean> oredqualifiers +%type <boolean> modlang +%type <boolean> optclassbody +%type <exceptionbase> baseexception +%type <klass> class + +%% + +specification: statement + | specification statement + ; + +statement: { + /* + * We don't do these in parserEOF() because the parser + * is reading ahead and that would be too early. + */ + + if (previousFile != NULL) + { + handleEOF(); + + if (newContext.prevmod != NULL) + handleEOM(); + + free(previousFile); + previousFile = NULL; + } + } modstatement + ; + +modstatement: module + | options + | noemitters + | copying + | include + | optinclude + | import + | timeline + | platforms + | feature + | license + | exphdrcode { + if (notSkipping()) + appendCodeBlock(¤tSpec->exphdrcode, $1); + } + | modhdrcode { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> hdrcode,$1); + } + | modcode { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> cppcode,$1); + } + | preinitcode + | postinitcode + | unitcode + | prepycode + | doc + | exporteddoc + | makefile + | mappedtype + | mappedtypetmpl + | nsstatement + ; + +nsstatement: ifstart + | ifend + | namespace + | struct + | class + | classtmpl + | exception + | typedef + | enum + | function + | variable + | typehdrcode { + if (notSkipping()) + { + classDef *scope = currentScope(); + + if (scope == NULL) + yyerror("%TypeHeaderCode can only be used in a namespace, class or mapped type"); + + appendCodeBlock(&scope->hdrcode, $1); + } + } + ; + +options: TK_OPTIONS '(' optionlist ')' + ; + +optionlist: TK_NAME { + appendString(¤tSpec->options, $1); + } + | optionlist ',' TK_NAME { + appendString(¤tSpec->options, $3); + } + ; + +noemitters: TK_NOEMITTERS { + if (notSkipping()) + { + yywarning("%SIPNoEmitters is deprecated, please use %SIPOptions instead"); + appendString(¤tSpec->options, "QtNoEmitters"); + } + } + ; + +exception: TK_EXCEPTION scopedname baseexception optflags '{' opttypehdrcode raisecode '}' ';' { + if (notSkipping()) + { + exceptionDef *xd; + char *pyname; + + if (currentSpec->genc) + yyerror("%Exception not allowed in a C module"); + + pyname = getPythonName(&$4, scopedNameTail($2)); + + checkAttributes(currentSpec, NULL, pyname, FALSE); + + xd = findException(currentSpec, $2, TRUE); + + if (xd->cd != NULL) + yyerror("%Exception name has already been seen as a class name - it must be defined before being used"); + + if (xd->iff->module != NULL) + yyerror("The %Exception has already been defined"); + + /* Complete the definition. */ + + xd->iff->module = currentModule; + xd->pyname = pyname; + xd->bibase = $3.bibase; + xd->base = $3.base; + xd->hdrcode = $6; + xd->raisecode = $7; + + if (xd->bibase != NULL || xd->base != NULL) + xd->exceptionnr = currentModule->nrexceptions++; + + if (inMainModule() && xd->base != NULL && xd->base->iff->module != currentModule) + addToUsedList(¤tSpec->used, xd->base->iff); + } + } + ; + +baseexception: { + $$.bibase = NULL; + $$.base = NULL; + } + | '(' scopedname ')' { + exceptionDef *xd; + + $$.bibase = NULL; + $$.base = NULL; + + /* See if it is a defined exception. */ + for (xd = currentSpec->exceptions; xd != NULL; xd = xd->next) + if (sameScopedName(xd->iff->fqcname, $2)) + { + $$.base = xd; + break; + } + + if (xd == NULL && $2->next == NULL && strncmp($2->name, "SIP_", 4) == 0) + { + /* See if it is a builtin exception. */ + + static char *builtins[] = { + "Exception", + "StopIteration", + "StandardError", + "ArithmeticError", + "LookupError", + "AssertionError", + "AttributeError", + "EOFError", + "FloatingPointError", + "EnvironmentError", + "IOError", + "OSError", + "ImportError", + "IndexError", + "KeyError", + "KeyboardInterrupt", + "MemoryError", + "NameError", + "OverflowError", + "RuntimeError", + "NotImplementedError", + "SyntaxError", + "IndentationError", + "TabError", + "ReferenceError", + "SystemError", + "SystemExit", + "TypeError", + "UnboundLocalError", + "UnicodeError", + "UnicodeEncodeError", + "UnicodeDecodeError", + "UnicodeTranslateError", + "ValueError", + "ZeroDivisionError", + "WindowsError", + "VMSError", + NULL + }; + + char **cp; + + for (cp = builtins; *cp != NULL; ++cp) + if (strcmp($2->name + 4, *cp) == 0) + { + $$.bibase = *cp; + break; + } + } + + if ($$.bibase == NULL && $$.base == NULL) + yyerror("Unknown exception base type"); + } + ; + +raisecode: TK_RAISECODE codeblock { + $$ = $2; + } + ; + +mappedtype: TK_MAPPEDTYPE basetype { + if (notSkipping()) + currentMappedType = newMappedType(currentSpec,&$2); + } mtdefinition + ; + +mappedtypetmpl: template TK_MAPPEDTYPE basetype { + int a; + + if (currentSpec->genc) + yyerror("%MappedType templates not allowed in a C module"); + + /* Check the template arguments are all just simple names. */ + for (a = 0; a < $1.nrArgs; ++a) + if ($1.args[a].atype != defined_type || $1.args[a].u.snd->next != NULL) + yyerror("%MappedType template arguments must be simple names"); + + if ($3.atype != template_type) + yyerror("%MappedType template must map a template type"); + + if (notSkipping()) + { + mappedTypeTmplDef *mtt; + + /* Check a template hasn't already been provided. */ + for (mtt = currentSpec->mappedtypetemplates; mtt != NULL; mtt = mtt->next) + if (sameScopedName(mtt->mt->type.u.td->fqname, $3.u.td->fqname) && sameTemplateSignature(&mtt->mt->type.u.td->types, &$3.u.td->types, TRUE)) + yyerror("%MappedType template for this type has already been defined"); + + $3.nrderefs = 0; + $3.argflags = 0; + + mtt = sipMalloc(sizeof (mappedTypeTmplDef)); + + mtt->sig = $1; + mtt->mt = allocMappedType(&$3); + mtt->next = currentSpec->mappedtypetemplates; + + currentSpec->mappedtypetemplates = mtt; + + currentMappedType = mtt->mt; + } + } mtdefinition + ; + +mtdefinition: '{' mtbody '}' ';' { + if (notSkipping()) + { + if (currentMappedType->convfromcode == NULL) + yyerror("%MappedType must have a %ConvertFromTypeCode directive"); + + if (currentMappedType->convtocode == NULL) + yyerror("%MappedType must have a %ConvertToTypeCode directive"); + + currentMappedType = NULL; + } + } + ; + +mtbody: mtline + | mtbody mtline + ; + +mtline: typehdrcode { + if (notSkipping()) + appendCodeBlock(¤tMappedType -> hdrcode,$1); + } + | TK_FROMTYPE codeblock { + if (notSkipping()) + { + if (currentMappedType -> convfromcode != NULL) + yyerror("%MappedType has more than one %ConvertFromTypeCode directive"); + + currentMappedType -> convfromcode = $2; + } + } + | TK_TOTYPE codeblock { + if (notSkipping()) + { + if (currentMappedType -> convtocode != NULL) + yyerror("%MappedType has more than one %ConvertToTypeCode directive"); + + currentMappedType -> convtocode = $2; + } + } + ; + +namespace: TK_NAMESPACE TK_NAME { + if (currentSpec -> genc) + yyerror("namespace definition not allowed in a C module"); + + if (notSkipping()) + { + classDef *ns; + + ns = newClass(currentSpec,namespace_iface,text2scopedName($2)); + + pushScope(ns); + + sectionFlags = 0; + } + } '{' nsbody '}' ';' { + if (inMainModule()) + { + classDef *ns = currentScope(); + + if (!isUsedName(ns->iff->name)) + { + varDef *vd; + + for (vd = currentSpec->vars; vd != NULL; vd = vd->next) + if (vd->ecd == ns) + { + setIsUsedName(ns->iff->name); + break; + } + } + } + + if (notSkipping()) + popScope(); + } + ; + +nsbody: nsstatement + | nsbody nsstatement + ; + +platforms: TK_PLATFORMS { + qualDef *qd; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == platform_qualifier) + yyerror("%Platforms has already been defined for this module"); + } + '{' platformlist '}' { + qualDef *qd; + int nrneeded; + + /* + * Check that exactly one platform in the set was + * requested. + */ + + nrneeded = 0; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == platform_qualifier && isNeeded(qd)) + ++nrneeded; + + if (nrneeded > 1) + yyerror("No more than one of these %Platforms must be specified with the -t flag"); + } + ; + +platformlist: platform + | platformlist platform + ; + +platform: TK_NAME { + newQualifier(currentModule,-1,-1,$1,platform_qualifier); + } + ; + +feature: TK_FEATURE TK_NAME { + newQualifier(currentModule,-1,-1,$2,feature_qualifier); + } + ; + +timeline: TK_TIMELINE { + currentTimelineOrder = 0; + } + '{' qualifierlist '}' { + qualDef *qd; + int nrneeded; + + /* + * Check that exactly one time slot in the set was + * requested. + */ + + nrneeded = 0; + + for (qd = currentModule -> qualifiers; qd != NULL; qd = qd -> next) + if (qd -> qtype == time_qualifier && isNeeded(qd)) + ++nrneeded; + + if (nrneeded > 1) + yyerror("At most one of this %Timeline must be specified with the -t flag"); + + currentModule -> nrtimelines++; + } + ; + +qualifierlist: qualifiername + | qualifierlist qualifiername + ; + +qualifiername: TK_NAME { + newQualifier(currentModule,currentModule -> nrtimelines,currentTimelineOrder++,$1,time_qualifier); + } + ; + +ifstart: TK_IF '(' qualifiers ')' { + if (skipStackPtr >= MAX_NESTED_IF) + yyerror("Internal error: increase the value of MAX_NESTED_IF"); + + /* Nested %Ifs are implicit logical ands. */ + + if (skipStackPtr > 0) + $3 = ($3 && skipStack[skipStackPtr - 1]); + + skipStack[skipStackPtr++] = $3; + } + ; + +oredqualifiers: TK_NAME { + $$ = platOrFeature($1,FALSE); + } + | '!' TK_NAME { + $$ = platOrFeature($2,TRUE); + } + | oredqualifiers TK_LOGICAL_OR TK_NAME { + $$ = (platOrFeature($3,FALSE) || $1); + } + | oredqualifiers TK_LOGICAL_OR '!' TK_NAME { + $$ = (platOrFeature($4,TRUE) || $1); + } + ; + +qualifiers: oredqualifiers + | optname '-' optname { + $$ = timePeriod($1,$3); + } + ; + +ifend: TK_END { + if (skipStackPtr-- <= 0) + yyerror("Too many %End directives"); + } + ; + +license: TK_LICENSE optflags { + optFlag *of; + + if ($2.nrFlags == 0) + yyerror("%License details not specified"); + + if ((of = findOptFlag(&$2,"Type",string_flag)) == NULL) + yyerror("%License type not specified"); + + currentModule -> license = sipMalloc(sizeof (licenseDef)); + + currentModule -> license -> type = of -> fvalue.sval; + + currentModule -> license -> licensee = + ((of = findOptFlag(&$2,"Licensee",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + + currentModule -> license -> timestamp = + ((of = findOptFlag(&$2,"Timestamp",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + + currentModule -> license -> sig = + ((of = findOptFlag(&$2,"Signature",string_flag)) != NULL) + ? of -> fvalue.sval : NULL; + } + ; + +module: modlang modname optnumber { + /* Check the module hasn't already been defined. */ + + moduleDef *mod; + + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + if (mod->fullname != NULL && strcmp(mod->fullname, $2) == 0) + yyerror("Module is already defined"); + + currentModule->fullname = $2; + + if ((currentModule->name = strrchr($2, '.')) != NULL) + currentModule->name++; + else + currentModule->name = $2; + + currentModule -> version = $3; + + if (currentSpec -> genc < 0) + currentSpec -> genc = $1; + else if (currentSpec -> genc != $1) + yyerror("Cannot mix C and C++ modules"); + } + ; + +modlang: TK_MODULE { + $$ = FALSE; + } + | TK_CMODULE { + $$ = TRUE; + } + ; + +modname: TK_NAME + | TK_PATHNAME { + /* + * The grammar design is a bit broken and this is the + * easiest way to allow periods in module names. + */ + + char *cp; + + for (cp = $1; *cp != '\0'; ++cp) + if (*cp != '.' && *cp != '_' && !isalnum(*cp)) + yyerror("Invalid character in module name"); + + $$ = $1; + } + ; + +optnumber: { + $$ = -1; + } + | TK_NUMBER + ; + +include: TK_INCLUDE TK_PATHNAME { + parseFile(NULL,$2,NULL,FALSE); + } + ; + +optinclude: TK_OPTINCLUDE TK_PATHNAME { + parseFile(NULL,$2,NULL,TRUE); + } + ; + +import: TK_IMPORT TK_PATHNAME { + newImport($2); + } + ; + +optaccesscode: { + $$ = NULL; + } + | TK_ACCESSCODE codeblock { + $$ = $2; + } + ; + +optgetcode: { + $$ = NULL; + } + | TK_GETCODE codeblock { + $$ = $2; + } + ; + +optsetcode: { + $$ = NULL; + } + | TK_SETCODE codeblock { + $$ = $2; + } + ; + +copying: TK_COPYING codeblock { + if (inMainModule()) + appendCodeBlock(¤tSpec -> copying,$2); + } + ; + +exphdrcode: TK_EXPHEADERCODE codeblock { + $$ = $2; + } + ; + +modhdrcode: TK_MODHEADERCODE codeblock { + $$ = $2; + } + ; + +typehdrcode: TK_TYPEHEADERCODE codeblock { + $$ = $2; + } + ; + +opttypehdrcode: { + $$ = NULL; + } + | typehdrcode + ; + +travcode: TK_TRAVERSECODE codeblock { + $$ = $2; + } + ; + +clearcode: TK_CLEARCODE codeblock { + $$ = $2; + } + ; + +readbufcode: TK_READBUFFERCODE codeblock { + $$ = $2; + } + ; + +writebufcode: TK_WRITEBUFFERCODE codeblock { + $$ = $2; + } + ; + +segcountcode: TK_SEGCOUNTCODE codeblock { + $$ = $2; + } + ; + +charbufcode: TK_CHARBUFFERCODE codeblock { + $$ = $2; + } + ; + +modcode: TK_MODCODE codeblock { + $$ = $2; + } + ; + +typecode: TK_TYPECODE codeblock { + $$ = $2; + } + ; + +preinitcode: TK_PREINITCODE codeblock { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> preinitcode,$2); + } + ; + +postinitcode: TK_POSTINITCODE codeblock { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec -> postinitcode,$2); + } + ; + +unitcode: TK_UNITCODE codeblock { + if (notSkipping() && inMainModule()) + appendCodeBlock(¤tSpec->unitcode, $2); + } + ; + +prepycode: TK_PREPYCODE codeblock { + /* + * This is a no-op and is retained for compatibility + * until the last use of it (by SIP v3) can be removed + * from PyQt. + */ + } + ; + +doc: TK_DOC codeblock { + if (inMainModule()) + appendCodeBlock(¤tSpec -> docs,$2); + } + ; + +exporteddoc: TK_EXPORTEDDOC codeblock { + appendCodeBlock(¤tSpec -> docs,$2); + } + ; + +makefile: TK_MAKEFILE TK_PATHNAME optfilename codeblock { + if (inMainModule()) + yywarning("%Makefile is ignored, please use the -b flag instead"); + } + ; + +codeblock: codelines TK_END + ; + +codelines: TK_CODELINE + | codelines TK_CODELINE { + $$ = $1; + + append(&$$->frag, $2->frag); + + free($2->frag); + free($2->filename); + free($2); + } + ; + +enum: TK_ENUM optname optflags { + if (notSkipping()) + { + if (sectionFlags != 0 && (sectionFlags & ~(SECT_IS_PUBLIC | SECT_IS_PROT)) != 0) + yyerror("Class enums must be in the public or protected sections"); + + currentEnum = newEnum(currentSpec,currentModule,$2,&$3,sectionFlags); + } + } '{' optenumbody '}' ';' + ; + +optfilename: { + $$ = NULL; + } + | TK_PATHNAME { + $$ = $1; + } + ; + +optname: { + $$ = NULL; + } + | TK_NAME { + $$ = $1; + } + ; + +optenumbody: + | enumbody + ; + +enumbody: enumline + | enumbody enumline + ; + +enumline: ifstart + | ifend + | TK_NAME optenumassign optflags optcomma { + if (notSkipping()) + { + /* + * Note that we don't use the assigned value. + * This is a hangover from when enums where + * generated in Python. We can remove it when + * we have got around to updating all the .sip + * files. + */ + enumMemberDef *emd, **tail; + + emd = sipMalloc(sizeof (enumMemberDef)); + + emd -> pyname = cacheName(currentSpec, getPythonName(&$3, $1)); + emd -> cname = $1; + emd -> ed = currentEnum; + emd -> next = NULL; + + checkAttributes(currentSpec,emd -> ed -> ecd,emd -> pyname -> text,FALSE); + + /* Append to preserve the order. */ + for (tail = ¤tEnum->members; *tail != NULL; tail = &(*tail)->next) + ; + + *tail = emd; + + if (inMainModule()) + setIsUsedName(emd -> pyname); + } + } + ; + +optcomma: + | ',' + ; + +optenumassign: + | '=' value + ; + +optassign: { + $$ = NULL; + } + | '=' expr { + $$ = $2; + } + ; + +expr: value + | expr binop value { + valueDef *vd; + + if ($1 -> vtype == string_value || $3 -> vtype == string_value) + yyerror("Invalid binary operator for string"); + + /* Find the last value in the existing expression. */ + + for (vd = $1; vd -> next != NULL; vd = vd -> next) + ; + + vd -> vbinop = $2; + vd -> next = $3; + + $$ = $1; + } + ; + +binop: '-' { + $$ = '-'; + } + | '+' { + $$ = '+'; + } + | '*' { + $$ = '*'; + } + | '/' { + $$ = '/'; + } + | '&' { + $$ = '&'; + } + | '|' { + $$ = '|'; + } + ; + +optunop: { + $$ = '\0'; + } + | '!' { + $$ = '!'; + } + | '~' { + $$ = '~'; + } + | '-' { + $$ = '-'; + } + | '+' { + $$ = '+'; + } + ; + +value: optunop simplevalue { + if ($1 != '\0' && $2.vtype == string_value) + yyerror("Invalid unary operator for string"); + + /* + * Convert the value to a simple expression on the + * heap. + */ + + $$ = sipMalloc(sizeof (valueDef)); + + *$$ = $2; + $$ -> vunop = $1; + $$ -> vbinop = '\0'; + $$ -> next = NULL; + } + ; + +scopedname: scopepart + | scopedname TK_SCOPE scopepart { + if (currentSpec -> genc) + yyerror("Scoped names are not allowed in a C module"); + + appendScopedName(&$1,$3); + } + ; + +scopepart: TK_NAME { + $$ = text2scopePart($1); + } + ; + +simplevalue: scopedname { + /* + * We let the C++ compiler decide if the value is a + * valid one - no point in building a full C++ parser + * here. + */ + + $$.vtype = scoped_value; + $$.u.vscp = $1; + } + | basetype '(' exprlist ')' { + fcallDef *fcd; + + fcd = sipMalloc(sizeof (fcallDef)); + *fcd = $3; + fcd -> type = $1; + + $$.vtype = fcall_value; + $$.u.fcd = fcd; + } + | TK_REAL { + $$.vtype = real_value; + $$.u.vreal = $1; + } + | TK_NUMBER { + $$.vtype = numeric_value; + $$.u.vnum = $1; + } + | TK_TRUE { + $$.vtype = numeric_value; + $$.u.vnum = 1; + } + | TK_FALSE { + $$.vtype = numeric_value; + $$.u.vnum = 0; + } + | TK_NULL { + $$.vtype = numeric_value; + $$.u.vnum = 0; + } + | TK_STRING { + $$.vtype = string_value; + $$.u.vstr = $1; + } + | TK_QCHAR { + $$.vtype = qchar_value; + $$.u.vqchar = $1; + } + ; + +exprlist: { + /* No values. */ + + $$.nrArgs = 0; + } + | expr { + /* The single or first expression. */ + + $$.args[0] = $1; + $$.nrArgs = 1; + } + | exprlist ',' expr { + /* Check that it wasn't ...(,expression...). */ + + if ($$.nrArgs == 0) + yyerror("First argument to function call is missing"); + + /* Check there is room. */ + + if ($1.nrArgs == MAX_NR_ARGS) + yyerror("Too many arguments to function call"); + + $$ = $1; + + $$.args[$$.nrArgs] = $3; + $$.nrArgs++; + } + ; + +typedef: TK_TYPEDEF cpptype TK_NAME ';' { + if (notSkipping()) + newTypedef(currentSpec,currentModule,$3,&$2); + } + | TK_TYPEDEF cpptype '(' deref TK_NAME ')' '(' cpptypelist ')' ';' { + if (notSkipping()) + { + argDef ftype; + signatureDef *sig; + + /* Create the full signature on the heap. */ + sig = sipMalloc(sizeof (signatureDef)); + *sig = $8; + sig -> result = $2; + + /* Create the full type. */ + ftype.atype = function_type; + ftype.argflags = 0; + ftype.nrderefs = $4; + ftype.defval = NULL; + ftype.u.sa = sig; + + newTypedef(currentSpec,currentModule,$5,&ftype); + } + } + ; + +struct: TK_STRUCT TK_NAME { + if (notSkipping()) + { + classDef *cd; + + cd = newClass(currentSpec,class_iface,text2scopedName($2)); + + pushScope(cd); + + sectionFlags = SECT_IS_PUBLIC; + } + } optflags '{' classbody '}' ';' { + if (notSkipping()) + { + finishClass(currentSpec, currentModule, currentScope(), &$4); + popScope(); + } + } + ; + +classtmpl: template class { + if (currentSpec->genc) + yyerror("Class templates not allowed in a C module"); + + if (notSkipping()) + { + classTmplDef *tcd; + + /* + * Make sure there is room for the extra class + * name argument. + */ + if ($1.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + tcd = sipMalloc(sizeof (classTmplDef)); + tcd->sig = $1; + tcd->cd = $2; + tcd->next = currentSpec->classtemplates; + + currentSpec->classtemplates = tcd; + } + } + ; + +template: TK_TEMPLATE '<' cpptypelist '>' { + $$ = $3; + } + ; + +class: TK_CLASS scopedname { + if (currentSpec -> genc) + yyerror("Class definition not allowed in a C module"); + + if (notSkipping()) + { + classDef *cd; + + cd = newClass(currentSpec, class_iface, scopeScopedName($2)); + + pushScope(cd); + + sectionFlags = SECT_IS_PRIVATE; + } + } superclasses optflags optclassbody ';' { + if (notSkipping()) + { + classDef *cd = currentScope(); + + /* + * See if the class was defined or just + * declared. + */ + if ($6) + { + if ($2->next != NULL) + yyerror("A scoped name cannot be given in a class definition"); + + } + else if (cd->supers != NULL) + yyerror("Class has super-classes but no definition"); + else + setIsOpaque(cd); + + finishClass(currentSpec, currentModule, cd, &$5); + popScope(); + + /* + * Check that external classes have only been + * declared at the global scope. + */ + if (isExternal(cd) && currentScope() != NULL) + yyerror("External classes can only be declared in the global scope"); + + $$ = cd; + } + } + ; + +superclasses: + | ':' superlist + ; + +superlist: superclass + | superlist ',' superclass + ; + +superclass: scopedname { + if (notSkipping()) + { + classDef *cd, *super; + + cd = currentScope(); + + super = findClass(currentSpec,class_iface,$1); + + appendToClassList(&cd -> supers,super); + addToUsedList(&cd->iff->used, super->iff); + } + } + ; + +optclassbody: { + $$ = FALSE; + } + | '{' classbody '}' { + $$ = TRUE; + } + ; + +classbody: classline + | classbody classline + ; + +classline: ifstart + | ifend + | namespace + | struct + | class + | exception + | typedef + | enum + | typecode { + if (notSkipping()) + appendCodeBlock(¤tScope() -> cppcode,$1); + } + | typehdrcode { + if (notSkipping()) + appendCodeBlock(¤tScope() -> hdrcode,$1); + } + | travcode { + if (currentScope()->travcode != NULL) + yyerror("%GCTraverseCode already given for class"); + + if (notSkipping()) + currentScope()->travcode = $1; + } + | clearcode { + if (currentScope()->clearcode != NULL) + yyerror("%GCClearCode already given for class"); + + if (notSkipping()) + currentScope()->clearcode = $1; + } + | readbufcode { + if (currentScope()->readbufcode != NULL) + yyerror("%BIGetReadBufferCode already given for class"); + + if (notSkipping()) + currentScope()->readbufcode = $1; + } + | writebufcode { + if (currentScope()->writebufcode != NULL) + yyerror("%BIGetWriteBufferCode already given for class"); + + if (notSkipping()) + currentScope()->writebufcode = $1; + } + | segcountcode { + if (currentScope()->segcountcode != NULL) + yyerror("%BIGetSegCountCode already given for class"); + + if (notSkipping()) + currentScope()->segcountcode = $1; + } + | charbufcode { + if (currentScope()->charbufcode != NULL) + yyerror("%BIGetCharBufferCode already given for class"); + + if (notSkipping()) + currentScope()->charbufcode = $1; + } + | ctor + | dtor + | varmember + | TK_TOSUBCLASS codeblock { + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (cd -> convtosubcode != NULL) + yyerror("Class has more than one %ConvertToSubClassCode directive"); + + cd -> convtosubcode = $2; + } + } + | TK_TOTYPE codeblock { + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (cd -> convtocode != NULL) + yyerror("Class has more than one %ConvertToTypeCode directive"); + + cd -> convtocode = $2; + } + } + | TK_PUBLIC optslot ':' { + if (currentSpec -> genc) + yyerror("public section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PUBLIC | $2; + } + | TK_PROTECTED optslot ':' { + if (currentSpec -> genc) + yyerror("protected section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PROT | $2; + } + | TK_PRIVATE optslot ':' { + if (currentSpec -> genc) + yyerror("private section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_PRIVATE | $2; + } + | TK_SIGNALS ':' { + if (currentSpec -> genc) + yyerror("signals section not allowed in a C module"); + + if (notSkipping()) + sectionFlags = SECT_IS_SIGNAL; + } + ; + +optslot: { + $$ = 0; + } + | TK_SLOTS { + $$ = SECT_IS_SLOT; + } + ; + +dtor: optvirtual '~' TK_NAME '(' ')' optexceptions optabstract optflags ';' methodcode virtualcatchercode { + /* Note that we allow non-virtual dtors in C modules. */ + + if (notSkipping()) + { + classDef *cd = currentScope(); + + if (strcmp(classBaseName(cd),$3) != 0) + yyerror("Destructor doesn't have the same name as its class"); + + if (isDtor(cd)) + yyerror("Destructor has already been defined"); + + if (currentSpec -> genc && $10 == NULL) + yyerror("Destructor in C modules must include %MethodCode"); + + cd -> dealloccode = $10; + cd -> dtorcode = $11; + cd -> dtorexceptions = $6; + cd -> classflags |= sectionFlags; + + if ($7) + { + if (!$1) + yyerror("Abstract destructor must be virtual"); + + setIsAbstractClass(cd); + } + + /* + * The class has a shadow if we have a virtual dtor or some + * dtor code. + */ + if ($1 || $11 != NULL) + { + if (currentSpec -> genc) + yyerror("Virtual destructor or %VirtualCatcherCode not allowed in a C module"); + + setHasShadow(cd); + } + + if (getReleaseGIL(&$8)) + setIsReleaseGILDtor(cd); + else if (getHoldGIL(&$8)) + setIsHoldGILDtor(cd); + } + } + ; + +ctor: TK_EXPLICIT {currentCtorIsExplicit = TRUE;} simplector + | simplector + ; + +simplector: TK_NAME '(' arglist ')' optexceptions optflags optctorsig ';' methodcode { + /* Note that we allow ctors in C modules. */ + + if (notSkipping()) + { + if (currentSpec -> genc) + { + if ($9 == NULL && $3.nrArgs != 0) + yyerror("Constructors with arguments in C modules must include %MethodCode"); + + if (currentCtorIsExplicit) + yyerror("Explicit constructors not allowed in a C module"); + } + + if ((sectionFlags & (SECT_IS_PUBLIC | SECT_IS_PROT | SECT_IS_PRIVATE)) == 0) + yyerror("Constructor must be in the public, private or protected sections"); + + newCtor($1,sectionFlags,&$3,&$6,$9,$5,$7,currentCtorIsExplicit); + } + + free($1); + + currentCtorIsExplicit = FALSE; + } + ; + +optctorsig: { + $$ = NULL; + } + | '[' '(' arglist ')' ']' { + $$ = sipMalloc(sizeof (signatureDef)); + + *$$ = $3; + } + ; + +optsig: { + $$ = NULL; + } + | '[' cpptype '(' arglist ')' ']' { + $$ = sipMalloc(sizeof (signatureDef)); + + *$$ = $4; + $$ -> result = $2; + } + ; + +optvirtual: { + $$ = FALSE; + } + | TK_VIRTUAL { + $$ = TRUE; + } + ; + +function: cpptype TK_NAME '(' arglist ')' optconst optexceptions optabstract optflags optsig ';' methodcode virtualcatchercode { + if (notSkipping()) + { + if (sectionFlags != 0 && (sectionFlags & (SECT_IS_PUBLIC | SECT_IS_PROT | SECT_IS_PRIVATE | SECT_IS_SLOT | SECT_IS_SIGNAL)) == 0) + yyerror("Class function must be in the public, private, protected, slot or signal sections"); + + $4.result = $1; + + newFunction(currentSpec,currentModule, + sectionFlags,currentIsStatic, + currentOverIsVirt, + $2,&$4,$6,$8,&$9,$12,$13,$7,$10); + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + | cpptype TK_OPERATOR operatorname '(' arglist ')' optconst optexceptions optabstract optflags optsig ';' methodcode virtualcatchercode { + if (notSkipping()) + { + classDef *cd = currentScope(); + + /* Handle the unary '+' and '-' operators. */ + if ((cd != NULL && $5.nrArgs == 0) || (cd == NULL && $5.nrArgs == 1)) + { + if (strcmp($3, "__add__") == 0) + $3 = "__pos__"; + else if (strcmp($3, "__sub__") == 0) + $3 = "__neg__"; + } + + $5.result = $1; + + newFunction(currentSpec,currentModule, + sectionFlags,currentIsStatic, + currentOverIsVirt, + $3,&$5,$7,$9,&$10,$13,$14,$8,$11); + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + | TK_OPERATOR cpptype '(' arglist ')' optconst optexceptions optabstract optflags optsig ';' methodcode virtualcatchercode { + classDef *scope = currentScope(); + + if (scope == NULL || $4.nrArgs != 0) + yyerror("Operator casts must be specified in a class and have no arguments"); + + + if (notSkipping()) + { + char *sname; + + switch ($2.atype) + { + case defined_type: + sname = NULL; + break; + + case bool_type: + case cbool_type: + case short_type: + case ushort_type: + case int_type: + case cint_type: + case uint_type: + sname = "__int__"; + break; + + case long_type: + case ulong_type: + case longlong_type: + case ulonglong_type: + sname = "__long__"; + break; + + case float_type: + case cfloat_type: + case double_type: + case cdouble_type: + sname = "__float__"; + break; + + default: + yyerror("Unsupported operator cast"); + } + + if (sname != NULL) + { + $4.result = $2; + + newFunction(currentSpec, currentModule, + sectionFlags, + currentIsStatic, + currentOverIsVirt, sname, + &$4, $6, $8, &$9, $12, $13, + $7, $10); + } + else + { + argList *al; + + /* Check it doesn't already exist. */ + for (al = scope->casts; al != NULL; al = al->next) + if (sameScopedName($2.u.snd, al->arg.u.snd)) + yyerror("This operator cast has already been specified in this class"); + + al = sipMalloc(sizeof (argList)); + al->arg = $2; + al->next = scope->casts; + + scope->casts = al; + } + } + + currentIsStatic = FALSE; + currentOverIsVirt = FALSE; + } + ; + +operatorname: '+' {$$ = "__add__";} + | '-' {$$ = "__sub__";} + | '*' {$$ = "__mul__";} + | '/' {$$ = "__div__";} + | '%' {$$ = "__mod__";} + | '&' {$$ = "__and__";} + | '|' {$$ = "__or__";} + | '^' {$$ = "__xor__";} + | '<' '<' {$$ = "__lshift__";} + | '>' '>' {$$ = "__rshift__";} + | '+' '=' {$$ = "__iadd__";} + | '-' '=' {$$ = "__isub__";} + | '*' '=' {$$ = "__imul__";} + | '/' '=' {$$ = "__idiv__";} + | '%' '=' {$$ = "__imod__";} + | '&' '=' {$$ = "__iand__";} + | '|' '=' {$$ = "__ior__";} + | '^' '=' {$$ = "__ixor__";} + | '<' '<' '=' {$$ = "__ilshift__";} + | '>' '>' '=' {$$ = "__irshift__";} + | '~' {$$ = "__invert__";} + | '(' ')' {$$ = "__call__";} + | '[' ']' {$$ = "__getitem__";} + | '<' {$$ = "__lt__";} + | '<' '=' {$$ = "__le__";} + | '=' '=' {$$ = "__eq__";} + | '!' '=' {$$ = "__ne__";} + | '>' {$$ = "__gt__";} + | '>' '=' {$$ = "__ge__";} + ; + +optconst: { + $$ = FALSE; + } + | TK_CONST { + $$ = TRUE; + } + ; + +optabstract: { + $$ = 0; + } + | '=' TK_NUMBER { + if ($2 != 0) + yyerror("Abstract virtual function '= 0' expected"); + + $$ = TRUE; + } + ; + +optflags: { + $$.nrFlags = 0; + } + | '/' flaglist '/' { + $$ = $2; + } + ; + + +flaglist: flag { + $$.flags[0] = $1; + $$.nrFlags = 1; + } + | flaglist ',' flag { + /* Check there is room. */ + + if ($1.nrFlags == MAX_NR_FLAGS) + yyerror("Too many optional flags"); + + $$ = $1; + + $$.flags[$$.nrFlags++] = $3; + } + ; + +flag: TK_NAME { + $$.ftype = bool_flag; + $$.fname = $1; + } + | TK_NAME '=' flagvalue { + $$ = $3; + $$.fname = $1; + } + ; + +flagvalue: TK_NAME { + $$.ftype = name_flag; + $$.fvalue.sval = $1; + } + | TK_STRING { + $$.ftype = string_flag; + $$.fvalue.sval = $1; + } + | TK_NUMBER { + $$.ftype = integer_flag; + $$.fvalue.ival = $1; + } + ; + +methodcode: { + $$ = NULL; + } + | TK_METHODCODE codeblock { + $$ = $2; + } + ; + +virtualcatchercode: { + $$ = NULL; + } + | TK_VIRTUALCATCHERCODE codeblock { + $$ = $2; + } + ; + +arglist: rawarglist { + int a, nrrxcon, nrrxdis, nrslotcon, nrslotdis, nrarray, nrarraysize; + + nrrxcon = nrrxdis = nrslotcon = nrslotdis = nrarray = nrarraysize = 0; + + for (a = 0; a < $1.nrArgs; ++a) + { + argDef *ad = &$1.args[a]; + + switch (ad -> atype) + { + case rxcon_type: + ++nrrxcon; + break; + + case rxdis_type: + ++nrrxdis; + break; + + case slotcon_type: + ++nrslotcon; + break; + + case slotdis_type: + ++nrslotdis; + break; + } + + if (isArray(ad)) + ++nrarray; + + if (isArraySize(ad)) + ++nrarraysize; + } + + if (nrrxcon != nrslotcon || nrrxcon > 1) + yyerror("SIP_RXOBJ_CON and SIP_SLOT_CON must both be given and at most once"); + + if (nrrxdis != nrslotdis || nrrxdis > 1) + yyerror("SIP_RXOBJ_DIS and SIP_SLOT_DIS must both be given and at most once"); + + if (nrarray != nrarraysize || nrarray > 1) + yyerror("/Array/ and /ArraySize/ must both be given and at most once"); + + $$ = $1; + } + ; + +rawarglist: { + /* No arguments. */ + + $$.nrArgs = 0; + } + | argvalue { + /* The single or first argument. */ + + $$.args[0] = $1; + $$.nrArgs = 1; + } + | rawarglist ',' argvalue { + /* Check that it wasn't ...(,arg...). */ + if ($1.nrArgs == 0) + yyerror("First argument of the list is missing"); + + /* Check there is nothing after an ellipsis. */ + if ($1.args[$1.nrArgs - 1].atype == ellipsis_type) + yyerror("An ellipsis must be at the end of the argument list"); + + /* + * If this argument has no default value, then the + * previous one mustn't either. + */ + if ($3.defval == NULL && $1.args[$1.nrArgs - 1].defval != NULL) + yyerror("Compulsory argument given after optional argument"); + + /* Check there is room. */ + if ($1.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + $$ = $1; + + $$.args[$$.nrArgs] = $3; + $$.nrArgs++; + } + ; + +argvalue: TK_SIPSIGNAL optname optassign { + $$.atype = signal_type; + $$.argflags = ARG_IS_CONST; + $$.nrderefs = 0; + $$.name = $2; + $$.defval = $3; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPSLOT optname optassign { + $$.atype = slot_type; + $$.argflags = ARG_IS_CONST; + $$.nrderefs = 0; + $$.name = $2; + $$.defval = $3; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPANYSLOT optname optassign { + $$.atype = anyslot_type; + $$.argflags = ARG_IS_CONST; + $$.nrderefs = 0; + $$.name = $2; + $$.defval = $3; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPRXCON optname { + $$.atype = rxcon_type; + $$.argflags = 0; + $$.nrderefs = 0; + $$.name = $2; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPRXDIS optname { + $$.atype = rxdis_type; + $$.argflags = 0; + $$.nrderefs = 0; + $$.name = $2; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPSLOTCON '(' arglist ')' optname { + $$.atype = slotcon_type; + $$.argflags = ARG_IS_CONST; + $$.nrderefs = 0; + $$.name = $5; + + $3.result.atype = void_type; + $3.result.argflags = 0; + $3.result.nrderefs = 0; + + $$.u.sa = sipMalloc(sizeof (signatureDef)); + *$$.u.sa = $3; + + currentSpec -> sigslots = TRUE; + } + | TK_SIPSLOTDIS '(' arglist ')' optname { + $$.atype = slotdis_type; + $$.argflags = ARG_IS_CONST; + $$.nrderefs = 0; + $$.name = $5; + + $3.result.atype = void_type; + $3.result.argflags = 0; + $3.result.nrderefs = 0; + + $$.u.sa = sipMalloc(sizeof (signatureDef)); + *$$.u.sa = $3; + + currentSpec -> sigslots = TRUE; + } + | TK_QOBJECT optname { + $$.atype = qobject_type; + $$.argflags = 0; + $$.nrderefs = 0; + $$.name = $2; + } + | argtype optassign { + $$ = $1; + $$.defval = $2; + } + ; + +varmember: TK_STATIC {currentIsStatic = TRUE;} varmem + | varmem + ; + +varmem: member + | variable + ; + +member: TK_VIRTUAL {currentOverIsVirt = TRUE;} function + | function + ; + +variable: cpptype TK_NAME optflags ';' optaccesscode optgetcode optsetcode { + if (notSkipping()) + { + /* Check the section. */ + + if (sectionFlags != 0) + { + if ((sectionFlags & SECT_IS_PUBLIC) == 0) + yyerror("Class variables must be in the public section"); + + if (!currentIsStatic && $5 != NULL) + yyerror("%AccessCode cannot be specified for non-static class variables"); + } + + if (currentIsStatic && currentSpec -> genc) + yyerror("Cannot have static members in a C structure"); + + if ($6 != NULL || $7 != NULL) + { + if ($5 != NULL) + yyerror("Cannot mix %AccessCode and %GetCode or %SetCode"); + + if (currentScope() == NULL) + yyerror("Cannot specify %GetCode or %SetCode for global variables"); + } + + newVar(currentSpec,currentModule,$2,currentIsStatic,&$1,&$3,$5,$6,$7); + } + + currentIsStatic = FALSE; + } + ; + +cpptype: TK_CONST basetype deref optref { + $$ = $2; + $$.nrderefs = $3; + $$.argflags = ARG_IS_CONST | $4; + $$.name = NULL; + } + | basetype deref optref { + $$ = $1; + $$.nrderefs = $2; + $$.argflags = $3; + $$.name = NULL; + } + ; + +argtype: cpptype optname optflags { + $$ = $1; + $$.name = $2; + + if (findOptFlag(&$3,"AllowNone",bool_flag) != NULL) + $$.argflags |= ARG_ALLOW_NONE; + + if (findOptFlag(&$3,"GetWrapper",bool_flag) != NULL) + $$.argflags |= ARG_GET_WRAPPER; + + if (findOptFlag(&$3,"Array",bool_flag) != NULL) + $$.argflags |= ARG_ARRAY; + + if (findOptFlag(&$3,"ArraySize",bool_flag) != NULL) + $$.argflags |= ARG_ARRAY_SIZE; + + if (findOptFlag(&$3,"Transfer",bool_flag) != NULL) + $$.argflags |= ARG_XFERRED; + + if (findOptFlag(&$3,"TransferThis",bool_flag) != NULL) + $$.argflags |= ARG_THIS_XFERRED; + + if (findOptFlag(&$3,"TransferBack",bool_flag) != NULL) + $$.argflags |= ARG_XFERRED_BACK; + + if (findOptFlag(&$3,"In",bool_flag) != NULL) + $$.argflags |= ARG_IN; + + if (findOptFlag(&$3,"Out",bool_flag) != NULL) + $$.argflags |= ARG_OUT; + + if (findOptFlag(&$3,"Constrained",bool_flag) != NULL) + { + $$.argflags |= ARG_CONSTRAINED; + + switch ($$.atype) + { + case bool_type: + $$.atype = cbool_type; + break; + + case int_type: + $$.atype = cint_type; + break; + + case float_type: + $$.atype = cfloat_type; + break; + + case double_type: + $$.atype = cdouble_type; + break; + } + } + } + ; + +optref: { + $$ = 0; + } + | '&' { + if (currentSpec -> genc) + yyerror("References not allowed in a C module"); + + $$ = ARG_IS_REF; + } + ; + +deref: { + $$ = 0; + } + | deref '*' { + $$ = $1 + 1; + } + ; + +basetype: scopedname { + $$.atype = defined_type; + $$.u.snd = $1; + } + | scopedname '<' cpptypelist '>' { + templateDef *td; + + td = sipMalloc(sizeof(templateDef)); + td -> fqname = $1; + td -> types = $3; + + $$.atype = template_type; + $$.u.td = td; + } + | TK_STRUCT scopedname { + /* In a C module all structures must be defined. */ + if (currentSpec -> genc) + { + $$.atype = defined_type; + $$.u.snd = $2; + } + else + { + $$.atype = struct_type; + $$.u.sname = $2; + } + } + | TK_UNSIGNED TK_SHORT { + $$.atype = ushort_type; + } + | TK_SHORT { + $$.atype = short_type; + } + | TK_UNSIGNED { + $$.atype = uint_type; + } + | TK_UNSIGNED TK_INT { + $$.atype = uint_type; + } + | TK_INT { + $$.atype = int_type; + } + | TK_LONG { + $$.atype = long_type; + } + | TK_UNSIGNED TK_LONG { + $$.atype = ulong_type; + } + | TK_LONG TK_LONG { + $$.atype = longlong_type; + } + | TK_UNSIGNED TK_LONG TK_LONG { + $$.atype = ulonglong_type; + } + | TK_FLOAT { + $$.atype = float_type; + } + | TK_DOUBLE { + $$.atype = double_type; + } + | TK_BOOL { + $$.atype = bool_type; + } + | TK_SIGNED TK_CHAR { + $$.atype = sstring_type; + } + | TK_UNSIGNED TK_CHAR { + $$.atype = ustring_type; + } + | TK_CHAR { + $$.atype = string_type; + } + | TK_WCHAR_T { + $$.atype = wstring_type; + } + | TK_VOID { + $$.atype = void_type; + } + | TK_PYOBJECT { + $$.atype = pyobject_type; + } + | TK_PYTUPLE { + $$.atype = pytuple_type; + } + | TK_PYLIST { + $$.atype = pylist_type; + } + | TK_PYDICT { + $$.atype = pydict_type; + } + | TK_PYCALLABLE { + $$.atype = pycallable_type; + } + | TK_PYSLICE { + $$.atype = pyslice_type; + } + | TK_PYTYPE { + $$.atype = pytype_type; + } + | TK_ELLIPSIS { + $$.atype = ellipsis_type; + } + ; + +cpptypelist: cpptype { + /* The single or first type. */ + + $$.args[0] = $1; + $$.nrArgs = 1; + } + | cpptypelist ',' cpptype { + /* Check there is nothing after an ellipsis. */ + if ($1.args[$1.nrArgs - 1].atype == ellipsis_type) + yyerror("An ellipsis must be at the end of the argument list"); + + /* Check there is room. */ + if ($1.nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + $$ = $1; + + $$.args[$$.nrArgs] = $3; + $$.nrArgs++; + } + ; + +optexceptions: { + $$ = NULL; + } + | TK_THROW '(' exceptionlist ')' { + if (currentSpec->genc) + yyerror("Exceptions not allowed in a C module"); + + if (notSkipping() && inMainModule()) + { + int e; + ifaceFileList **ifl; + + /* + * Make sure the exceptions' header files are + * included. We unconditionally mark them to + * be included in the current scope's header + * file to save us the effort of checking if + * they are being used with a protected method, + * a virtual or a signal. + */ + ifl = (currentScope() != NULL) ? ¤tScope()->iff->used : ¤tSpec->used; + + for (e = 0; e < $3->nrArgs; ++e) + addToUsedList(ifl, $3->args[e]->iff); + } + + $$ = $3; + } + ; + +exceptionlist: { + /* Empty list so use a blank. */ + + $$ = sipMalloc(sizeof (throwArgs)); + $$ -> nrArgs = 0; + } + | scopedname { + /* The only or first exception. */ + + $$ = sipMalloc(sizeof (throwArgs)); + $$ -> nrArgs = 1; + $$ -> args[0] = findException(currentSpec, $1, FALSE); + } + | exceptionlist ',' scopedname { + /* Check that it wasn't ...(,arg...). */ + + if ($1 -> nrArgs == 0) + yyerror("First exception of throw specifier is missing"); + + /* Check there is room. */ + + if ($1 -> nrArgs == MAX_NR_ARGS) + yyerror("Internal error - increase the value of MAX_NR_ARGS"); + + $$ = $1; + $$ -> args[$$ -> nrArgs++] = findException(currentSpec, $3, FALSE); + } + ; + +%% + + +/* + * Parse the specification. + */ +void parse(sipSpec *spec,FILE *fp,char *filename,stringList *tsl, + stringList *xfl) +{ + classTmplDef *tcd; + + /* Initialise the spec. */ + + spec -> modules = NULL; + spec -> namecache = NULL; + spec -> ifacefiles = NULL; + spec -> classes = NULL; + spec -> classtemplates = NULL; + spec -> proxies = NULL; + spec -> exceptions = NULL; + spec -> mappedtypes = NULL; + spec -> mappedtypetemplates = NULL; + spec -> qobjclass = -1; + spec -> enums = NULL; + spec -> vars = NULL; + spec -> othfuncs = NULL; + spec -> overs = NULL; + spec -> typedefs = NULL; + spec -> copying = NULL; + spec -> exphdrcode = NULL; + spec -> hdrcode = NULL; + spec -> cppcode = NULL; + spec -> docs = NULL; + spec -> preinitcode = NULL; + spec -> postinitcode = NULL; + spec -> unitcode = NULL; + spec -> used = NULL; + spec -> sigslots = FALSE; + spec -> genc = -1; + spec -> options = NULL; + + currentSpec = spec; + neededQualifiers = tsl; + excludedQualifiers = xfl; + currentModule = NULL; + currentMappedType = NULL; + currentOverIsVirt = FALSE; + currentCtorIsExplicit = FALSE; + currentIsStatic = FALSE; + previousFile = NULL; + skipStackPtr = 0; + currentScopeIdx = 0; + sectionFlags = 0; + + newModule(fp,filename); + spec -> module = currentModule; + + yyparse(); + + handleEOF(); + handleEOM(); + + /* + * Go through each template class and remove it from the list of + * classes. + */ + for (tcd = spec->classtemplates; tcd != NULL; tcd = tcd->next) + { + classDef **cdp; + + for (cdp = &spec->classes; *cdp != NULL; cdp = &(*cdp)->next) + if (*cdp == tcd->cd) + { + ifaceFileDef **ifdp; + + /* Remove the interface file as well. */ + for (ifdp = &spec->ifacefiles; *ifdp != NULL; ifdp = &(*ifdp)->next) + if (*ifdp == tcd->cd->iff) + { + *ifdp = (*ifdp)->next; + break; + } + + *cdp = (*cdp)->next; + break; + } + } +} + + +/* + * Tell the parser that a complete file has now been read. + */ +void parserEOF(char *name,parserContext *pc) +{ + previousFile = sipStrdup(name); + newContext = *pc; +} + + +/* + * Append a class definition to a class list if it doesn't already appear. + * Append is needed specifically for the list of super-classes because the + * order is important to Python. + */ +void appendToClassList(classList **clp,classDef *cd) +{ + classList *new; + + /* Find the end of the list. */ + + while (*clp != NULL) + { + if ((*clp) -> cd == cd) + return; + + clp = &(*clp) -> next; + } + + new = sipMalloc(sizeof (classList)); + + new -> cd = cd; + new -> next = NULL; + + *clp = new; +} + + +/* + * Create a new module for the current specification and make it current. + */ +static void newModule(FILE *fp,char *filename) +{ + moduleDef *newmod; + + parseFile(fp,filename,currentModule,FALSE); + + newmod = sipMalloc(sizeof (moduleDef)); + newmod -> fullname = NULL; + newmod -> name = NULL; + newmod -> version = -1; + newmod -> modflags = 0; + newmod -> modulenr = -1; + newmod -> file = filename; + newmod -> qualifiers = NULL; + newmod -> root.cd = NULL; + newmod -> root.child = NULL; + newmod -> nrtimelines = 0; + newmod -> nrclasses = 0; + newmod -> nrexceptions = 0; + newmod -> nrmappedtypes = 0; + newmod -> nrenums = 0; + newmod -> nrtypedefs = 0; + newmod -> nrvirthandlers = 0; + newmod -> virthandlers = NULL; + newmod -> license = NULL; + newmod -> allimports = NULL; + newmod -> imports = NULL; + newmod -> next = currentSpec -> modules; + + currentModule = currentSpec->modules = newmod; +} + + +/* + * Switch to parsing a new file. + */ +static void parseFile(FILE *fp,char *name,moduleDef *prevmod,int optional) +{ + parserContext pc; + + pc.ifdepth = skipStackPtr; + pc.prevmod = prevmod; + + setInputFile(fp,name,&pc,optional); +} + + +/* + * Find an interface file, or create a new one. + */ +ifaceFileDef *findIfaceFile(sipSpec *pt, moduleDef *mod, scopedNameDef *fqname, + ifaceFileType iftype, argDef *ad) +{ + ifaceFileDef *iff; + + /* See if the name is already used. */ + + for (iff = pt -> ifacefiles; iff != NULL; iff = iff -> next) + { + if (!sameScopedName(iff -> fqcname,fqname)) + continue; + + /* + * They must be the same type except that we allow a class if + * if we want an exception. This is because we allow classes + * to be used before they are defined. + */ + if (iff -> type != iftype) + if (iftype != exception_iface || iff -> type != class_iface) + yyerror("A class, exception, namespace or mapped type has already been defined with the same name"); + + /* Ignore an external class declared in another module. */ + if (iftype == class_iface && iff->module != mod) + { + classDef *cd; + + for (cd = pt->classes; cd != NULL; cd = cd->next) + if (cd->iff == iff) + break; + + if (cd != NULL && iff->module != NULL && isExternal(cd)) + continue; + } + + /* + * If this is a mapped type with the same name defined in a + * different module, then check that this type isn't the same + * as any of the mapped types defined in that module. + */ + if (iftype == mappedtype_iface && iff -> module != mod) + { + mappedTypeDef *mtd; + + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + { + if (mtd -> iff != iff) + continue; + + if (ad -> atype != template_type || + mtd -> type.atype != template_type || + sameBaseType(ad,&mtd -> type)) + yyerror("Mapped type has already been defined in another module"); + } + + /* + * If we got here then we have a mapped type based on + * an existing template, but with unique parameters. + * We don't want to use interface files from other + * modules, so skip this one. + */ + + continue; + } + + /* Ignore a namespace defined in another module. */ + if (iftype == namespace_iface && iff->module != mod) + continue; + + return iff; + } + + iff = sipMalloc(sizeof (ifaceFileDef)); + + iff -> name = cacheName(pt,scopedNameTail(fqname)); + iff -> type = iftype; + iff -> fqcname = fqname; + iff -> module = NULL; + iff -> used = NULL; + iff -> next = pt -> ifacefiles; + + pt -> ifacefiles = iff; + + return iff; +} + + +/* + * Find a class definition in a parse tree. + */ +static classDef *findClass(sipSpec *pt,ifaceFileType iftype, + scopedNameDef *fqname) +{ + return findClassWithInterface(pt, findIfaceFile(pt, currentModule, fqname, iftype, NULL)); +} + + +/* + * Find a class definition given an existing interface file. + */ +static classDef *findClassWithInterface(sipSpec *pt, ifaceFileDef *iff) +{ + classDef *cd; + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + if (cd -> iff == iff) + return cd; + + /* Create a new one. */ + cd = sipMalloc(sizeof (classDef)); + + cd -> iff = iff; + cd -> pyname = classBaseName(cd); + cd -> classnr = -1; + cd -> classflags = 0; + cd -> userflags = 0; + cd -> ecd = NULL; + cd -> dtorexceptions = NULL; + cd -> real = NULL; + cd -> node = NULL; + cd -> supers = NULL; + cd -> mro = NULL; + cd -> td = NULL; + cd -> ctors = NULL; + cd -> defctor = NULL; + cd -> dealloccode = NULL; + cd -> dtorcode = NULL; + cd -> members = NULL; + cd -> overs = NULL; + cd -> casts = NULL; + cd -> vmembers = NULL; + cd -> visible = NULL; + cd -> cppcode = NULL; + cd -> hdrcode = NULL; + cd -> convtosubcode = NULL; + cd -> subbase = NULL; + cd -> convtocode = NULL; + cd -> travcode = NULL; + cd -> clearcode = NULL; + cd -> readbufcode = NULL; + cd -> writebufcode = NULL; + cd -> segcountcode = NULL; + cd -> charbufcode = NULL; + cd -> next = pt -> classes; + + pt -> classes = cd; + + return cd; +} + + +/* + * Add an interface file to an interface file list if it isn't already there. + */ +ifaceFileList *addToUsedList(ifaceFileList **ifflp, ifaceFileDef *iff) +{ + ifaceFileList *iffl; + + while ((iffl = *ifflp) != NULL) + { + /* Don't bother if it is already there. */ + if (iffl -> iff == iff) + return iffl; + + ifflp = &iffl -> next; + } + + iffl = sipMalloc(sizeof (ifaceFileList)); + + iffl->iff = iff; + iffl->header = FALSE; + iffl->next = NULL; + + *ifflp = iffl; + + return iffl; +} + + +/* + * Find an undefined (or create a new) exception definition in a parse tree. + */ +static exceptionDef *findException(sipSpec *pt, scopedNameDef *fqname, int new) +{ + exceptionDef *xd, **tail; + ifaceFileDef *iff; + classDef *cd; + + iff = findIfaceFile(pt, currentModule, fqname, exception_iface, NULL); + + /* See if it is an existing one. */ + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + if (xd->iff == iff) + return xd; + + /* + * If it is an exception interface file then we have never seen this + * name before. We require that exceptions are defined before being + * used, but don't make the same requirement of classes (for reasons of + * backwards compatibility). Therefore the name must be reinterpreted + * as a (as yet undefined) class. + */ + if (new) + if (iff->type == exception_iface) + cd = NULL; + else + yyerror("There is already a class with the same name or the exception has been used before being defined"); + else + { + if (iff->type == exception_iface) + iff->type = class_iface; + + cd = findClassWithInterface(pt, iff); + } + + /* Create a new one. */ + xd = sipMalloc(sizeof (exceptionDef)); + + xd->exceptionnr = -1; + xd->iff = iff; + xd->pyname = NULL; + xd->cd = cd; + xd->bibase = NULL; + xd->base = NULL; + xd->hdrcode = NULL; + xd->raisecode = NULL; + xd->next = NULL; + + /* Append it to the list. */ + for (tail = &pt->exceptions; *tail != NULL; tail = &(*tail)->next) + ; + + *tail = xd; + + return xd; +} + + +/* + * Find an undefined (or create a new) class definition in a parse tree. + */ +static classDef *newClass(sipSpec *pt,ifaceFileType iftype, + scopedNameDef *fqname) +{ + int flags; + classDef *cd, *scope; + codeBlock *hdrcode; + + if (sectionFlags & SECT_IS_PRIVATE) + yyerror("Classes, structs and namespaces must be in the public or or protected sections"); + + flags = 0; + + if ((scope = currentScope()) != NULL) + { + if (sectionFlags & SECT_IS_PROT) + flags = CLASS_IS_PROTECTED; + + hdrcode = scope -> hdrcode; + } + else + hdrcode = NULL; + + if (pt -> genc) + { + /* C structs are always global types. */ + while (fqname -> next != NULL) + fqname = fqname -> next; + + scope = NULL; + } + + cd = findClass(pt,iftype,fqname); + + /* Check it hasn't already been defined. */ + if (iftype != namespace_iface && cd->iff->module != NULL) + yyerror("The struct/class has already been defined"); + + /* Complete the initialisation. */ + cd->classflags |= flags; + cd->ecd = scope; + cd->iff->module = currentModule; + + appendCodeBlock(&cd->hdrcode, hdrcode); + + /* See if it is a namespace extender. */ + if (iftype == namespace_iface) + { + classDef *ns; + + for (ns = pt->classes; ns != NULL; ns = ns->next) + { + if (ns == cd) + continue; + + if (ns->iff->type != namespace_iface) + continue; + + if (!sameScopedName(ns->iff->fqcname, fqname)) + continue; + + cd->real = ns; + break; + } + } + + return cd; +} + + +/* + * Tidy up after finishing a class definition. + */ +static void finishClass(sipSpec *pt, moduleDef *mod, classDef *cd, optFlags *of) +{ + char *pyname; + optFlag *flg; + + /* Get the Python name and see if it is different to the C++ name. */ + pyname = getPythonName(of, classBaseName(cd)); + + cd -> pyname = NULL; + checkAttributes(pt, cd->ecd, pyname, FALSE); + cd->pyname = pyname; + + if (cd->pyname != classBaseName(cd)) + setIsRenamedClass(cd); + + if ((flg = findOptFlag(of, "TypeFlags", integer_flag)) != NULL) + cd->userflags = flg->fvalue.ival; + + if (isOpaque(cd)) + { + if (findOptFlag(of, "External", bool_flag) != NULL) + setIsExternal(cd); + } + else + { + int seq_might, seq_not; + memberDef *md; + + if (findOptFlag(of, "NoDefaultCtors", bool_flag) != NULL) + setNoDefaultCtors(cd); + + if (cd -> ctors == NULL) + { + if (!noDefaultCtors(cd)) + { + /* Provide a default ctor. */ + + cd->ctors = sipMalloc(sizeof (ctorDef)); + + cd->ctors->ctorflags = SECT_IS_PUBLIC; + cd->ctors->pysig.nrArgs = 0; + cd->ctors->cppsig = &cd -> ctors -> pysig; + cd->ctors->exceptions = NULL; + cd->ctors->methodcode = NULL; + cd->ctors->prehook = NULL; + cd->ctors->posthook = NULL; + cd->ctors->next = NULL; + + cd->defctor = cd->ctors; + + setCanCreate(cd); + } + } + else if (cd -> defctor == NULL) + { + ctorDef *ct, *last = NULL; + + for (ct = cd -> ctors; ct != NULL; ct = ct -> next) + { + if (!isPublicCtor(ct)) + continue; + + if (ct -> pysig.nrArgs == 0 || ct -> pysig.args[0].defval != NULL) + { + cd -> defctor = ct; + break; + } + + if (last == NULL) + last = ct; + } + + /* The last resort is the first public ctor. */ + if (cd->defctor == NULL) + cd->defctor = last; + } + + if (findOptFlag(of,"Abstract",bool_flag) != NULL) + { + setIsAbstractClass(cd); + setIsIncomplete(cd); + resetCanCreate(cd); + } + + /* We assume a public dtor if nothing specific was provided. */ + if (!isDtor(cd)) + setIsPublicDtor(cd); + + if (findOptFlag(of, "DelayDtor", bool_flag) != NULL) + { + setIsDelayedDtor(cd); + setHasDelayedDtors(mod); + } + + /* + * There are subtle differences between the add and concat methods and + * the multiply and repeat methods. The number versions can have their + * operands swapped and may return NotImplemented. If the user has + * used the /Numeric/ annotation or there are other numeric operators + * then we use add/multiply. Otherwise, if there are indexing + * operators then we use concat/repeat. + */ + seq_might = seq_not = FALSE; + + for (md = cd -> members; md != NULL; md = md -> next) + switch (md -> slot) + { + case getitem_slot: + case setitem_slot: + case delitem_slot: + /* This might be a sequence. */ + seq_might = TRUE; + break; + + case sub_slot: + case isub_slot: + case div_slot: + case idiv_slot: + case mod_slot: + case imod_slot: + case pos_slot: + case neg_slot: + /* This is definately not a sequence. */ + seq_not = TRUE; + break; + } + + if (!seq_not && seq_might) + for (md = cd -> members; md != NULL; md = md -> next) + { + /* Ignore if the user has been explicit. */ + if (isNumeric(md)) + continue; + + switch (md -> slot) + { + case add_slot: + md -> slot = concat_slot; + break; + + case iadd_slot: + md -> slot = iconcat_slot; + break; + + case mul_slot: + md -> slot = repeat_slot; + break; + + case imul_slot: + md -> slot = irepeat_slot; + break; + } + } + } + + if (inMainModule()) + { + setIsUsedName(cd->iff->name); + setIsClassName(cd->iff->name); + } +} + + +/* + * Create a new mapped type. + */ +static mappedTypeDef *newMappedType(sipSpec *pt,argDef *ad) +{ + mappedTypeDef *mtd; + scopedNameDef *snd; + ifaceFileDef *iff; + + /* Check that the type is one we want to map. */ + switch (ad -> atype) + { + case defined_type: + snd = ad -> u.snd; + break; + + case template_type: + snd = ad -> u.td -> fqname; + break; + + case struct_type: + snd = ad -> u.sname; + break; + + default: + yyerror("Invalid type for %MappedType"); + } + + iff = findIfaceFile(pt, currentModule, snd, mappedtype_iface, ad); + + if (inMainModule()) + setIsUsedName(iff -> name); + + /* Check it hasn't already been defined. */ + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + if (mtd -> iff == iff) + { + /* + * We allow types based on the same template but with + * different arguments. + */ + + if (ad -> atype != template_type || + sameBaseType(ad,&mtd -> type)) + yyerror("Mapped type has already been defined in this module"); + } + + /* The module may not have been set yet. */ + iff -> module = currentModule; + + /* Create a new mapped type. */ + mtd = allocMappedType(ad); + + mtd -> iff = iff; + mtd -> next = pt -> mappedtypes; + + pt -> mappedtypes = mtd; + + return mtd; +} + + +/* + * Allocate, intialise and return a mapped type structure. + */ +mappedTypeDef *allocMappedType(argDef *type) +{ + mappedTypeDef *mtd; + + mtd = sipMalloc(sizeof (mappedTypeDef)); + + mtd->type = *type; + mtd->type.argflags = 0; + mtd->type.nrderefs = 0; + + mtd->mappednr = -1; + mtd->iff = NULL; + mtd->hdrcode = NULL; + mtd->convfromcode = NULL; + mtd->convtocode = NULL; + mtd->next = NULL; + + return mtd; +} + + +/* + * Create a new enum. + */ +static enumDef *newEnum(sipSpec *pt,moduleDef *mod,char *name,optFlags *of, + int flags) +{ + enumDef *ed; + classDef *escope = currentScope(); + + ed = sipMalloc(sizeof (enumDef)); + + if (name != NULL) + { + ed -> fqcname = text2scopedName(name); + ed -> pyname = cacheName(pt, getPythonName(of, name)); + + checkAttributes(pt, escope, ed->pyname->text, FALSE); + } + else + { + ed -> fqcname = NULL; + ed -> pyname = NULL; + } + + ed -> enumflags = flags; + ed -> enumnr = -1; + ed -> ecd = escope; + ed -> pcd = (flags & SECT_IS_PROT) ? escope : NULL; + ed -> module = mod; + ed -> members = NULL; + ed -> slots = NULL; + ed -> overs = NULL; + ed -> next = pt -> enums; + + if (name != NULL && strcmp(ed->pyname->text, name) != 0) + setIsRenamedEnum(ed); + + pt -> enums = ed; + + if (escope != NULL) + setHasEnums(escope); + + return ed; +} + + +/* + * Get the type values and (optionally) the type names for substitution in + * handwritten code. + */ +void appendTypeStrings(scopedNameDef *ename, signatureDef *patt, signatureDef *src, signatureDef *known, scopedNameDef **names, scopedNameDef **values) +{ + int a; + + for (a = 0; a < patt->nrArgs; ++a) + { + argDef *pad = &patt->args[a]; + + if (pad->atype == defined_type) + { + char *nam = NULL; + + /* + * If the type names are already known then check that + * this is one of them. + */ + if (known == NULL) + nam = scopedNameTail(pad->u.snd); + else if (pad->u.snd->next == NULL) + { + int k; + + for (k = 0; k < known->nrArgs; ++k) + if (strcmp(pad->u.snd->name, known->args[k].u.snd->name) == 0) + { + nam = pad->u.snd->name; + break; + } + } + + if (nam == NULL) + continue; + + /* Add the name. */ + appendScopedName(names, text2scopePart(nam)); + + /* Add the corresponding value. */ + appendScopedName(values, text2scopePart(getType(ename, &src->args[a]))); + } + else if (pad->atype == template_type) + { + argDef *sad = &src->args[a]; + + /* These checks shouldn't be necessary, but... */ + if (sad->atype == template_type && pad->u.td->types.nrArgs == sad->u.td->types.nrArgs) + appendTypeStrings(ename, &pad->u.td->types, &sad->u.td->types, known, names, values); + } + } +} + + +/* + * Convert a type to a string. We impose some limitations because I'm too lazy + * to handle everything that might be needed one day. + */ +static char *getType(scopedNameDef *ename, argDef *ad) +{ + if (ad->atype == defined_type) + return scopedNameToString(ad->u.snd); + + fatalScopedName(ename); + fatal(": unsupported type argument to template class instantiation\n"); + + return NULL; +} + + +/* + * Convert a scoped name to a string on the heap. + */ +static char *scopedNameToString(scopedNameDef *name) +{ + static const char scope_string[] = "::"; + size_t len; + scopedNameDef *snd; + char *s, *dp; + + /* Work out the length of buffer needed. */ + len = 0; + + for (snd = name; snd != NULL; snd = snd->next) + { + len += strlen(snd->name); + + if (snd->next != NULL) + len += strlen(scope_string); + } + + /* Allocate and populate the buffer. */ + dp = s = sipMalloc(len + 1); + + for (snd = name; snd != NULL; snd = snd->next) + { + strcpy(dp, snd->name); + dp += strlen(snd->name); + + if (snd->next != NULL) + { + strcpy(dp, scope_string); + dp += strlen(scope_string); + } + } + + return s; +} + + +/* + * Instantiate a class template. + */ +static void instantiateClassTemplate(sipSpec *pt, moduleDef *mod, classDef *scope, scopedNameDef *fqname, classTmplDef *tcd, templateDef *td) +{ + scopedNameDef *type_names, *type_values; + classDef *cd; + ctorDef *oct, **cttail; + memberDef *omd, **mdtail; + overDef *ood, **odtail; + argDef *ad; + ifaceFileList *iffl, **used; + + type_names = type_values = NULL; + appendTypeStrings(classFQCName(tcd->cd), &tcd->sig, &td->types, NULL, &type_names, &type_values); + + /* + * Add a mapping from the template name to the instantiated name. If + * we have got this far we know there is room for it. + */ + ad = &tcd->sig.args[tcd->sig.nrArgs++]; + ad->atype = defined_type; + ad->name = NULL; + ad->argflags = 0; + ad->nrderefs = 0; + ad->defval = NULL; + ad->u.snd = classFQCName(tcd->cd); + + appendScopedName(&type_names, text2scopePart(scopedNameTail(classFQCName(tcd->cd)))); + appendScopedName(&type_values, text2scopePart(scopedNameToString(fqname))); + + /* Create the new class. */ + cd = sipMalloc(sizeof (classDef)); + + /* Start with a shallow copy. */ + *cd = *tcd->cd; + + cd->pyname = scopedNameTail(fqname); + cd->td = td; + + /* Handle the interface file. */ + cd->iff = findIfaceFile(pt, mod, fqname, class_iface, NULL); + cd->iff->module = mod; + + /* Make a copy of the used list and add the enclosing scope. */ + used = &cd->iff->used; + + for (iffl = tcd->cd->iff->used; iffl != NULL; iffl = iffl->next) + addToUsedList(used, iffl->iff); + + if (scope != NULL) + addToUsedList(&cd->iff->used, scope->iff); + + if (inMainModule()) + { + setIsUsedName(cd->iff->name); + setIsClassName(cd->iff->name); + } + + cd->ecd = currentScope(); + + /* Handle the ctors. */ + cd->ctors = NULL; + cttail = &cd->ctors; + + for (oct = tcd->cd->ctors; oct != NULL; oct = oct->next) + { + ctorDef *nct = sipMalloc(sizeof (ctorDef)); + + /* Start with a shallow copy. */ + *nct = *oct; + + templateSignature(&nct->pysig, FALSE, tcd, td, cd); + + if (oct->cppsig == NULL) + nct->cppsig = NULL; + else if (oct->cppsig == &oct->pysig) + nct->cppsig = &nct->pysig; + else + { + nct->cppsig = sipMalloc(sizeof (signatureDef)); + + *nct->cppsig = *oct->cppsig; + + templateSignature(nct->cppsig, FALSE, tcd, td, cd); + } + + nct->methodcode = templateCode(pt, used, nct->methodcode, type_names, type_values); + + nct->next = NULL; + *cttail = nct; + cttail = &nct->next; + + /* Handle the default ctor. */ + if (tcd->cd->defctor == oct) + cd->defctor = nct; + } + + cd->dealloccode = templateCode(pt, used, cd->dealloccode, type_names, type_values); + cd->dtorcode = templateCode(pt, used, cd->dtorcode, type_names, type_values); + + /* Handle the members, ie. the common parts of overloads. */ + cd->members = NULL; + mdtail = &cd->members; + + for (omd = tcd->cd->members; omd != NULL; omd = omd->next) + { + memberDef *nmd = sipMalloc(sizeof (memberDef)); + + /* Start with a shallow copy. */ + *nmd = *omd; + + nmd->module = mod; + + nmd->next = NULL; + *mdtail = nmd; + mdtail = &nmd->next; + } + + /* Handle the overloads. */ + cd->overs = NULL; + odtail = &cd->overs; + + for (ood = tcd->cd->overs; ood != NULL; ood = ood->next) + { + overDef *nod = sipMalloc(sizeof (overDef)); + memberDef *nmd; + + /* Start with a shallow copy. */ + *nod = *ood; + + for (nmd = cd->members, omd = tcd->cd->members; omd != NULL; omd = omd->next, nmd = nmd->next) + if (omd == ood->common) + { + nod->common = nmd; + break; + } + + templateSignature(&nod->pysig, TRUE, tcd, td, cd); + + if (ood->cppsig == &ood->pysig) + nod->cppsig = &nod->pysig; + else + { + nod->cppsig = sipMalloc(sizeof (signatureDef)); + + *nod->cppsig = *ood->cppsig; + + templateSignature(nod->cppsig, TRUE, tcd, td, cd); + } + + nod->methodcode = templateCode(pt, used, nod->methodcode, type_names, type_values); + + /* Handle any virtual handler. */ + if (ood->virthandler != NULL) + { + nod->virthandler = sipMalloc(sizeof (virtHandlerDef)); + + /* Start with a shallow copy. */ + *nod->virthandler = *ood->virthandler; + + if (ood->virthandler->cppsig == &ood->pysig) + nod->virthandler->cppsig = &nod->pysig; + else + { + nod->virthandler->cppsig = sipMalloc(sizeof (signatureDef)); + + *nod->virthandler->cppsig = *ood->virthandler->cppsig; + + templateSignature(nod->virthandler->cppsig, TRUE, tcd, td, cd); + } + + nod->virthandler->module = mod; + nod->virthandler->virtcode = templateCode(pt, used, nod->virthandler->virtcode, type_names, type_values); + nod->virthandler->next = mod->virthandlers; + + mod->virthandlers = nod->virthandler; + } + + nod->next = NULL; + *odtail = nod; + odtail = &nod->next; + } + + cd->cppcode = templateCode(pt, used, cd->cppcode, type_names, type_values); + cd->hdrcode = templateCode(pt, used, cd->hdrcode, type_names, type_values); + cd->convtosubcode = templateCode(pt, used, cd->convtosubcode, type_names, type_values); + cd->convtocode = templateCode(pt, used, cd->convtocode, type_names, type_values); + cd->travcode = templateCode(pt, used, cd->travcode, type_names, type_values); + cd->clearcode = templateCode(pt, used, cd->clearcode, type_names, type_values); + cd->readbufcode = templateCode(pt, used, cd->readbufcode, type_names, type_values); + cd->writebufcode = templateCode(pt, used, cd->writebufcode, type_names, type_values); + cd->segcountcode = templateCode(pt, used, cd->segcountcode, type_names, type_values); + cd->charbufcode = templateCode(pt, used, cd->charbufcode, type_names, type_values); + cd->next = pt->classes; + + pt->classes = cd; + + tcd->sig.nrArgs--; + + freeScopedName(type_names); + freeScopedName(type_values); +} + + +/* + * Replace any template arguments in a signature. + */ +static void templateSignature(signatureDef *sd, int result, classTmplDef *tcd, templateDef *td, classDef *ncd) +{ + int a; + + if (result) + templateType(&sd->result, tcd, td, ncd); + + for (a = 0; a < sd->nrArgs; ++a) + templateType(&sd->args[a], tcd, td, ncd); +} + + +/* + * Replace any template arguments in a type. + */ +static void templateType(argDef *ad, classTmplDef *tcd, templateDef *td, classDef *ncd) +{ + int a; + char *name; + + /* Ignore if it isn't an unscoped name. */ + if (ad->atype != defined_type || ad->u.snd->next != NULL) + return; + + name = ad->u.snd->name; + + for (a = 0; a < tcd->sig.nrArgs - 1; ++a) + if (strcmp(name, scopedNameTail(tcd->sig.args[a].u.snd)) == 0) + { + ad->atype = td->types.args[a].atype; + + /* We take the constrained flag from the real type. */ + resetIsConstrained(ad); + + if (isConstrained(&td->types.args[a])) + setIsConstrained(ad); + + ad->u = td->types.args[a].u; + + return; + } + + /* Handle the class name itself. */ + if (strcmp(name, scopedNameTail(classFQCName(tcd->cd))) == 0) + { + ad->atype = class_type; + ad->u.cd = ncd; + } +} + + +/* + * Replace any template arguments in a literal code block. + */ +codeBlock *templateCode(sipSpec *pt, ifaceFileList **used, codeBlock *ocb, scopedNameDef *names, scopedNameDef *values) +{ + codeBlock *ncb = NULL, **tail = &ncb; + + while (ocb != NULL) + { + char *at = ocb->frag; + + do + { + char *first = NULL; + codeBlock *cb; + scopedNameDef *nam, *val, *nam_first, *val_first; + + /* + * Go through the rest of this fragment looking for + * each of the types and the name of the class itself. + */ + nam = names; + val = values; + + while (nam != NULL && val != NULL) + { + char *cp; + + if ((cp = strstr(at, nam->name)) != NULL) + if (first == NULL || first > cp) + { + nam_first = nam; + val_first = val; + first = cp; + } + + nam = nam->next; + val = val->next; + } + + /* Create the new fragment. */ + cb = sipMalloc(sizeof (codeBlock)); + + if (at == ocb->frag) + { + cb->filename = ocb->filename; + cb->linenr = ocb->linenr; + } + else + cb->filename = NULL; + + cb->next = NULL; + *tail = cb; + tail = &cb->next; + + /* See if anything was found. */ + if (first == NULL) + { + /* We can just point to this. */ + cb->frag = at; + + /* All done with this one. */ + at = NULL; + } + else + { + static char *gen_names[] = { + "sipForceConvertToTransfer_", + "sipForceConvertTo_", + "sipConvertFromTransfer_", + "sipConvertFrom_", + "sipClass_", + "sipEnum_", + "sipException_", + NULL + }; + + char *dp, *sp, **gn; + int genname = FALSE; + + /* + * If the context in which the text is used is + * in the name of a SIP generated object then + * translate any "::" scoping to "_". + */ + for (gn = gen_names; *gn != NULL; ++gn) + if (search_back(first, at, *gn)) + { + addUsedFromCode(pt, used, val_first->name); + genname = TRUE; + break; + } + + /* Fragment the fragment. */ + cb->frag = sipMalloc(first - at + strlen(val_first->name) + 1); + + strncpy(cb->frag, at, first - at); + + dp = &cb->frag[first - at]; + sp = val_first->name; + + if (genname) + { + char gch; + + while ((gch = *sp++) != '\0') + if (gch == ':' && *sp == ':') + { + *dp++ = '_'; + ++sp; + } + else + *dp++ = gch; + + *dp = '\0'; + } + else + strcpy(dp, sp); + + /* Move past the replaced text. */ + at = first + strlen(nam_first->name); + } + } + while (at != NULL && *at != '\0'); + + ocb = ocb->next; + } + + return ncb; +} + + +/* + * Return TRUE if the text at the end of a string matches the target string. + */ +static int search_back(const char *end, const char *start, const char *target) +{ + size_t tlen = strlen(target); + + if (start + tlen >= end) + return FALSE; + + return (strncmp(end - tlen, target, tlen) == 0); +} + + +/* + * Add any needed interface files based on handwritten code. + */ +static void addUsedFromCode(sipSpec *pt, ifaceFileList **used, const char *sname) +{ + ifaceFileDef *iff; + enumDef *ed; + + for (iff = pt->ifacefiles; iff != NULL; iff = iff->next) + { + if (iff->type != class_iface && iff->type != exception_iface) + continue; + + if (sameName(iff->fqcname, sname)) + { + addToUsedList(used, iff); + + return; + } + } + + for (ed = pt->enums; ed != NULL; ed = ed->next) + { + if (ed->ecd == NULL) + continue; + + if (sameName(ed->fqcname, sname)) + { + addToUsedList(used, ed->ecd->iff); + + return; + } + } +} + + +/* + * Compare a scoped name with its string equivalent. + */ +static int sameName(scopedNameDef *snd, const char *sname) +{ + while (snd != NULL && *sname != '\0') + { + const char *sp = snd->name; + + while (*sp != '\0' && *sname != ':' && *sname != '\0') + if (*sp++ != *sname++) + return FALSE; + + if (*sp != '\0' || (*sname != ':' && *sname != '\0')) + return FALSE; + + snd = snd->next; + + if (*sname == ':') + sname += 2; + } + + return (snd == NULL && *sname == '\0'); +} + + +/* + * Create a new typedef. + */ +static void newTypedef(sipSpec *pt,moduleDef *mod,char *name,argDef *type) +{ + typedefDef *td; + scopedNameDef *fqname = text2scopedName(name); + classDef *scope = currentScope(); + + /* See if we are instantiating a template class. */ + if (type->atype == template_type) + { + classTmplDef *tcd; + templateDef *td = type->u.td; + + for (tcd = pt->classtemplates; tcd != NULL; tcd = tcd->next) + if (sameScopedName(tcd->cd->iff->fqcname, td->fqname)) + { + if (!sameTemplateSignature(&tcd->sig, &td->types, FALSE)) + continue; + + instantiateClassTemplate(pt, mod, scope, fqname, tcd, td); + + /* All done. */ + return; + } + } + + /* Check it doesn't already exist. */ + for (td = pt -> typedefs; td != NULL; td = td -> next) + if (sameScopedName(td -> fqname,fqname)) + { + fatalScopedName(fqname); + fatal(" already defined\n"); + } + + td = sipMalloc(sizeof (typedefDef)); + + td -> fqname = fqname; + td -> ecd = scope; + td -> module = mod; + td -> type = *type; + td -> next = pt -> typedefs; + + mod -> nrtypedefs++; + + pt -> typedefs = td; +} + + +/* + * Return TRUE if the template signatures are the same. A deep comparison is + * used for mapped type templates where we want to recurse into any nested + * templates. + */ +int sameTemplateSignature(signatureDef *sd1, signatureDef *sd2, int deep) +{ + int a; + + if (sd1->nrArgs != sd2->nrArgs) + return FALSE; + + for (a = 0; a < sd1->nrArgs; ++a) + { + argDef *ad1 = &sd1->args[a]; + argDef *ad2 = &sd2->args[a]; + + /* + * If we are doing a shallow comparision (ie. for class + * templates) then a type name on the left hand side matches + * anything on the right hand side. + */ + if (ad1->atype == defined_type && !deep) + continue; + + /* + * For type names only compare the references and pointers, and + * do the same for any nested templates. + */ + if (ad1->atype == defined_type && ad2->atype == defined_type) + { + if (isReference(ad1) != isReference(ad2) || ad1->nrderefs != ad2->nrderefs) + return FALSE; + } + else if (ad1->atype == template_type && ad2->atype == template_type) + { + if (!sameTemplateSignature(&ad1->u.td->types, &ad2->u.td->types, deep)) + return FALSE; + } + else if (!sameBaseType(ad1, ad2)) + return FALSE; + } + + return TRUE; +} + + +/* + * Create a new variable. + */ +static void newVar(sipSpec *pt,moduleDef *mod,char *name,int isstatic, + argDef *type,optFlags *of,codeBlock *acode,codeBlock *gcode, + codeBlock *scode) +{ + varDef *var; + classDef *escope = currentScope(); + nameDef *nd = cacheName(pt,getPythonName(of,name)); + + if (inMainModule()) + setIsUsedName(nd); + + checkAttributes(pt,escope,nd -> text,FALSE); + + var = sipMalloc(sizeof (varDef)); + + var -> pyname = nd; + var -> fqcname = text2scopedName(name); + var -> ecd = escope; + var -> module = mod; + var -> varflags = 0; + var -> type = *type; + var -> accessfunc = acode; + var -> getcode = gcode; + var -> setcode = scode; + var -> next = pt -> vars; + + if (isstatic || (escope != NULL && escope->iff->type == namespace_iface)) + setIsStaticVar(var); + + pt -> vars = var; +} + + +/* + * Create a new ctor. + */ +static void newCtor(char *name,int sectFlags,signatureDef *args, + optFlags *optflgs,codeBlock *methodcode, + throwArgs *exceptions,signatureDef *cppsig,int explicit) +{ + ctorDef *ct, **ctp; + classDef *cd = currentScope(); + + /* Check the name of the constructor. */ + if (strcmp(classBaseName(cd),name) != 0) + yyerror("Constructor doesn't have the same name as its class"); + + /* Add to the list of constructors. */ + ct = sipMalloc(sizeof (ctorDef)); + + ct -> ctorflags = sectFlags; + ct -> pysig = *args; + ct -> cppsig = (cppsig != NULL ? cppsig : &ct -> pysig); + ct -> exceptions = exceptions; + ct -> methodcode = methodcode; + ct -> next = NULL; + + if (!isPrivateCtor(ct)) + setCanCreate(cd); + + if (isProtectedCtor(ct)) + setHasShadow(cd); + + if (explicit) + setIsExplicitCtor(ct); + + getHooks(optflgs,&ct -> prehook,&ct -> posthook); + + if (getReleaseGIL(optflgs)) + setIsReleaseGILCtor(ct); + else if (getHoldGIL(optflgs)) + setIsHoldGILCtor(ct); + + if (findOptFlag(optflgs,"NoDerived",bool_flag) != NULL) + { + if (cppsig != NULL) + yyerror("The /NoDerived/ annotation cannot be used with a C++ signature"); + + if (methodcode == NULL) + yyerror("The /NoDerived/ annotation must be used with %MethodCode"); + + ct->cppsig = NULL; + } + + if (findOptFlag(optflgs,"Default",bool_flag) != NULL) + { + if (cd -> defctor != NULL) + yyerror("A constructor with the /Default/ annotation has already been defined"); + + cd -> defctor = ct; + } + + /* Append to the list. */ + for (ctp = &cd->ctors; *ctp != NULL; ctp = &(*ctp)->next) + ; + + *ctp = ct; +} + + +/* + * Create a new function. + */ +static void newFunction(sipSpec *pt,moduleDef *mod,int sflags,int isstatic, + int isvirt,char *name,signatureDef *sig,int isconst, + int isabstract,optFlags *optflgs,codeBlock *methodcode, + codeBlock *vcode,throwArgs *exceptions, + signatureDef *cppsig) +{ + classDef *cd = currentScope(); + nameDef *pname; + int factory, xferback; + overDef *od, **odp, **headp; + optFlag *of; + virtHandlerDef *vhd; + + /* Extra checks for a C module. */ + if (pt -> genc) + { + if (cd != NULL) + yyerror("Function declaration not allowed in a struct in a C module"); + + if (isstatic) + yyerror("Static functions not allowed in a C module"); + + if (exceptions != NULL) + yyerror("Exceptions not allowed in a C module"); + } + + headp = (cd != NULL ? &cd -> overs : &pt -> overs); + + /* See if it is a factory method. */ + if (findOptFlag(optflgs,"Factory",bool_flag) != NULL) + factory = TRUE; + else + { + int a; + + factory = FALSE; + + /* Check /TransferThis/ wasn't specified. */ + if (cd == NULL || isstatic) + for (a = 0; a < sig -> nrArgs; ++a) + if (isThisTransferred(&sig -> args[a])) + yyerror("/TransferThis/ may only be specified in constructors and class methods"); + } + + /* See if the result is to be returned to Python ownership. */ + xferback = (findOptFlag(optflgs,"TransferBack",bool_flag) != NULL); + + if (factory && xferback) + yyerror("/TransferBack/ and /Factory/ cannot both be specified"); + + /* Use the C++ name if a Python name wasn't given. */ + pname = cacheName(pt, getPythonName(optflgs, name)); + + /* Create a new overload definition. */ + + od = sipMalloc(sizeof (overDef)); + + /* Set the overload flags. */ + + od -> overflags = sflags; + + if (factory) + setIsFactory(od); + + if (xferback) + setIsResultTransferredBack(od); + + if (isProtected(od)) + setHasShadow(cd); + + if ((isSlot(od) || isSignal(od)) && !isPrivate(od)) + { + if (isSignal(od)) + setHasShadow(cd); + + pt -> sigslots = TRUE; + } + + if (isSignal(od) && (methodcode != NULL || vcode != NULL)) + yyerror("Cannot provide code for signals"); + + if (isstatic) + { + if (isSignal(od)) + yyerror("Static functions cannot be signals"); + + if (isvirt) + yyerror("Static functions cannot be virtual"); + + setIsStatic(od); + } + + if (isconst) + setIsConst(od); + + if (isabstract) + { + if (sflags == 0) + yyerror("Non-class function specified as abstract"); + + setIsAbstract(od); + } + + if ((of = findOptFlag(optflgs,"AutoGen",opt_name_flag)) != NULL) + { + setIsAutoGen(od); + + if (of -> fvalue.sval != NULL) + { + qualDef *qd; + + if ((qd = findQualifier(of -> fvalue.sval)) == NULL || qd -> qtype != feature_qualifier) + yyerror("No such feature"); + + if (excludedFeature(excludedQualifiers,qd)) + resetIsAutoGen(od); + } + } + + if (isvirt) + { + if (isSignal(od) && !optNoEmitters(pt)) + yyerror("Virtual signals aren't supported"); + + setIsVirtual(od); + setHasShadow(cd); + + vhd = sipMalloc(sizeof (virtHandlerDef)); + + vhd -> virthandlernr = -1; + vhd -> vhflags = 0; + vhd -> pysig = &od -> pysig; + vhd -> cppsig = (cppsig != NULL ? cppsig : &od -> pysig); + vhd -> module = currentModule; + vhd -> virtcode = vcode; + vhd -> next = currentModule -> virthandlers; + + if (factory || xferback) + setIsTransferVH(vhd); + + currentModule -> virthandlers = vhd; + } + else + { + if (vcode != NULL) + yyerror("%VirtualCatcherCode provided for non-virtual function"); + + vhd = NULL; + } + + od -> cppname = name; + od -> pysig = *sig; + od -> cppsig = (cppsig != NULL ? cppsig : &od -> pysig); + od -> exceptions = exceptions; + od -> methodcode = methodcode; + od -> virthandler = vhd; + od -> common = findFunction(pt,mod,cd,pname,(methodcode != NULL),sig -> nrArgs); + + if (findOptFlag(optflgs,"Numeric",bool_flag) != NULL) + setIsNumeric(od -> common); + + /* Methods that run in new threads must be virtual. */ + if (findOptFlag(optflgs,"NewThread",bool_flag) != NULL) + { + argDef *res; + + if (!isvirt) + yyerror("/NewThread/ may only be specified for virtual functions"); + + /* + * This is an arbitary limitation to make the code generator + * slightly easier - laziness on my part. + */ + res = &od -> cppsig -> result; + + if (res -> atype != void_type || res -> nrderefs != 0) + yyerror("/NewThread/ may only be specified for void functions"); + + setIsNewThread(od); + } + + getHooks(optflgs,&od -> prehook,&od -> posthook); + + if (getReleaseGIL(optflgs)) + setIsReleaseGIL(od); + else if (getHoldGIL(optflgs)) + setIsHoldGIL(od); + + od -> next = NULL; + + /* Append to the list. */ + for (odp = headp; *odp != NULL; odp = &(*odp)->next) + ; + + *odp = od; +} + + +/* + * Return the Python name based on the C/C++ name and any /PyName/ annotation. + */ +static char *getPythonName(optFlags *optflgs, char *cname) +{ + char *pname; + optFlag *of; + + if ((of = findOptFlag(optflgs, "PyName", name_flag)) != NULL) + pname = of -> fvalue.sval; + else + pname = cname; + + return pname; +} + + +/* + * Cache a name in a module. + */ +static nameDef *cacheName(sipSpec *pt,char *name) +{ + nameDef *nd; + + /* See if it already exists. */ + for (nd = pt -> namecache; nd != NULL; nd = nd -> next) + if (strcmp(nd -> text,name) == 0) + return nd; + + /* Create a new one. */ + nd = sipMalloc(sizeof (nameDef)); + + nd -> nameflags = 0; + nd -> module = currentSpec -> module; + nd -> text = name; + nd -> next = pt -> namecache; + + pt -> namecache = nd; + + return nd; +} + + +/* + * Find (or create) an overloaded function name. + */ +static memberDef *findFunction(sipSpec *pt,moduleDef *mod,classDef *cd, + nameDef *pname,int hwcode,int nrargs) +{ + static struct slot_map { + char *name; /* The slot name. */ + slotType type; /* The corresponding type. */ + int needs_hwcode; /* If handwritten code is required. */ + int nrargs; /* Nr. of arguments. */ + } slot_table[] = { + {"__str__", str_slot, TRUE, 0}, + {"__unicode__", unicode_slot, TRUE, 0}, + {"__int__", int_slot, FALSE, 0}, + {"__long__", long_slot, FALSE, 0}, + {"__float__", float_slot, FALSE, 0}, + {"__len__", len_slot, TRUE, 0}, + {"__contains__", contains_slot, TRUE, 1}, + {"__add__", add_slot, FALSE, 1}, + {"__sub__", sub_slot, FALSE, 1}, + {"__mul__", mul_slot, FALSE, 1}, + {"__div__", div_slot, FALSE, 1}, + {"__mod__", mod_slot, FALSE, 1}, + {"__and__", and_slot, FALSE, 1}, + {"__or__", or_slot, FALSE, 1}, + {"__xor__", xor_slot, FALSE, 1}, + {"__lshift__", lshift_slot, FALSE, 1}, + {"__rshift__", rshift_slot, FALSE, 1}, + {"__iadd__", iadd_slot, FALSE, 1}, + {"__isub__", isub_slot, FALSE, 1}, + {"__imul__", imul_slot, FALSE, 1}, + {"__idiv__", idiv_slot, FALSE, 1}, + {"__imod__", imod_slot, FALSE, 1}, + {"__iand__", iand_slot, FALSE, 1}, + {"__ior__", ior_slot, FALSE, 1}, + {"__ixor__", ixor_slot, FALSE, 1}, + {"__ilshift__", ilshift_slot, FALSE, 1}, + {"__irshift__", irshift_slot, FALSE, 1}, + {"__invert__", invert_slot, FALSE, 0}, + {"__call__", call_slot, FALSE, -1}, + {"__getitem__", getitem_slot, FALSE, -1}, + {"__setitem__", setitem_slot, TRUE, -1}, + {"__delitem__", delitem_slot, TRUE, -1}, + {"__lt__", lt_slot, FALSE, 1}, + {"__le__", le_slot, FALSE, 1}, + {"__eq__", eq_slot, FALSE, 1}, + {"__ne__", ne_slot, FALSE, 1}, + {"__gt__", gt_slot, FALSE, 1}, + {"__ge__", ge_slot, FALSE, 1}, + {"__cmp__", cmp_slot, FALSE, 1}, + {"__nonzero__", nonzero_slot, TRUE, 0}, + {"__neg__", neg_slot, FALSE, 0}, + {"__pos__", pos_slot, FALSE, 0}, + {"__abs__", abs_slot, TRUE, 0}, + {"__repr__", repr_slot, TRUE, 0}, + {"__hash__", hash_slot, TRUE, 0}, + {NULL} + }; + + memberDef *md, **flist; + struct slot_map *sm; + slotType st; + + /* Get the slot type. */ + st = no_slot; + + for (sm = slot_table; sm -> name != NULL; ++sm) + if (strcmp(sm -> name,pname -> text) == 0) + { + if (sm -> needs_hwcode && !hwcode) + yyerror("This Python slot requires %MethodCode"); + + if (sm -> nrargs < 0) + { + int min_nr; + + /* These require a minimum number. */ + switch (sm -> type) + { + case getitem_slot: + case delitem_slot: + min_nr = 1; + break; + + case setitem_slot: + min_nr = 2; + break; + + default: + min_nr = 0; + } + + if (nrargs < min_nr) + yyerror("Insufficient number of arguments to Python slot"); + } + else if (cd == NULL) + { + /* Global operators need one extra argument. */ + if (sm -> nrargs + 1 != nrargs) + yyerror("Incorrect number of arguments to global operator"); + } + else if (sm -> nrargs != nrargs) + yyerror("Incorrect number of arguments to Python slot"); + + st = sm -> type; + + break; + } + + if (inMainModule()) + setIsUsedName(pname); + + /* Check there is no name clash. */ + checkAttributes(pt,cd,pname -> text,TRUE); + + /* See if it already exists. */ + flist = (cd != NULL ? &cd -> members : &pt -> othfuncs); + + for (md = *flist; md != NULL; md = md -> next) + if (md -> pyname == pname && md -> module == mod) + return md; + + /* Create a new one. */ + md = sipMalloc(sizeof (memberDef)); + + md -> pyname = pname; + md -> memberflags = 0; + md -> slot = st; + md -> module = mod; + md -> next = *flist; + + *flist = md; + + /* Global operators are a subset. */ + if (cd == NULL && st != no_slot && st != neg_slot && st != pos_slot && !isNumberSlot(md) && !isRichCompareSlot(md)) + yyerror("Global operators must be either numeric or comparison operators"); + + return md; +} + + +/* + * Search a set of flags for a particular one and check its type. + */ +static optFlag *findOptFlag(optFlags *flgs,char *name,flagType ft) +{ + int f; + + for (f = 0; f < flgs -> nrFlags; ++f) + { + optFlag *of = &flgs -> flags[f]; + + if (strcmp(of -> fname,name) == 0) + { + /* + * An optional name can look like a boolean or a name. + */ + + if (ft == opt_name_flag) + { + if (of -> ftype == bool_flag) + { + of -> ftype = opt_name_flag; + of -> fvalue.sval = NULL; + } + else if (of -> ftype == name_flag) + of -> ftype = opt_name_flag; + } + + if (ft != of -> ftype) + yyerror("Optional flag has a value of the wrong type"); + + return of; + } + } + + return NULL; +} + + +/* + * A name is going to be used as a Python attribute name within a Python scope + * (ie. a Python dictionary), so check against what we already know is going in + * the same scope in case there is a clash. + */ +static void checkAttributes(sipSpec *pt,classDef *pyscope,char *attr,int isfunc) +{ + enumDef *ed; + varDef *vd; + classDef *cd; + + /* Check the enums. */ + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + { + enumMemberDef *emd; + + if (ed -> ecd != pyscope || ed -> pyname == NULL) + continue; + + if (strcmp(ed->pyname->text, attr) == 0) + yyerror("There is already an enum in scope with the same Python name"); + + for (emd = ed -> members; emd != NULL; emd = emd -> next) + if (strcmp(emd -> pyname -> text, attr) == 0) + yyerror("There is already an enum member in scope with the same Python name"); + } + + /* Check the variables. */ + + for (vd = pt -> vars; vd != NULL; vd = vd -> next) + { + if (vd -> ecd != pyscope) + continue; + + if (strcmp(vd -> pyname -> text, attr) == 0) + yyerror("There is already a variable in scope with the same Python name"); + } + + /* + * Only check the members if this attribute isn't a member because we + * can handle members with the same name in the same scope. + */ + if (!isfunc) + { + memberDef *md, *membs; + + membs = (pyscope != NULL ? pyscope -> members : pt -> othfuncs); + + for (md = membs; md != NULL; md = md -> next) + { + overDef *od, *overs; + + if (strcmp(md -> pyname -> text, attr) != 0) + continue; + + /* Check for a conflict with all overloads. */ + + overs = (pyscope != NULL ? pyscope -> overs : pt -> overs); + + for (od = overs; od != NULL; od = od -> next) + { + if (od -> common != md) + continue; + + yyerror("There is already a function in scope with the same Python name"); + } + } + } + + /* Check the classes. */ + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + if (cd -> ecd != pyscope || cd -> pyname == NULL) + continue; + + if (strcmp(cd->pyname, attr) == 0 && !isExternal(cd)) + yyerror("There is already a class or namespace in scope with the same Python name"); + } + + /* Check the exceptions. */ + + if (pyscope == NULL) + { + exceptionDef *xd; + + for (xd = pt->exceptions; xd != NULL; xd = xd->next) + if (xd->pyname != NULL && strcmp(xd->pyname, attr) == 0) + yyerror("There is already an exception with the same Python name"); + } +} + + +/* + * Append a code block to a list of them. Append is needed to give the + * specifier easy control over the order of the documentation. + */ +static void appendCodeBlock(codeBlock **headp,codeBlock *new) +{ + while (*headp != NULL) + headp = &(*headp) -> next; + + *headp = new; +} + + +/* + * Handle the end of a fully parsed a file. + */ +static void handleEOF() +{ + /* + * Check that the number of nested if's is the same as when we started + * the file. + */ + + if (skipStackPtr > newContext.ifdepth) + fatal("Too many %%If statements in %s\n",previousFile); + + if (skipStackPtr < newContext.ifdepth) + fatal("Too many %%End statements in %s\n",previousFile); +} + + +/* + * Handle the end of a fully parsed a module. + */ +static void handleEOM() +{ + /* Check it has been named. */ + + if (currentModule -> name == NULL) + fatal("No %%Module has been specified for module defined in %s\n",previousFile); + + /* The previous module is now current. */ + + currentModule = newContext.prevmod; +} + + +/* + * Find an existing qualifier. + */ +static qualDef *findQualifier(char *name) +{ + moduleDef *mod; + + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + { + qualDef *qd; + + for (qd = mod -> qualifiers; qd != NULL; qd = qd -> next) + if (strcmp(qd -> name,name) == 0) + return qd; + } + + return NULL; +} + + +/* + * Return a copy of a scoped name. + */ +scopedNameDef *copyScopedName(scopedNameDef *snd) +{ + scopedNameDef *head; + + head = NULL; + + while (snd != NULL) + { + appendScopedName(&head,text2scopePart(snd -> name)); + snd = snd -> next; + } + + return head; +} + + +/* + * Append a name to a list of scopes. + */ +void appendScopedName(scopedNameDef **headp,scopedNameDef *newsnd) +{ + while (*headp != NULL) + headp = &(*headp) -> next; + + *headp = newsnd; +} + + +/* + * Free a scoped name - but not the text itself. + */ +void freeScopedName(scopedNameDef *snd) +{ + while (snd != NULL) + { + scopedNameDef *next = snd -> next; + + free(snd); + + snd = next; + } +} + + +/* + * Convert a text string to a scope part structure. + */ +scopedNameDef *text2scopePart(char *text) +{ + scopedNameDef *snd; + + snd = sipMalloc(sizeof (scopedNameDef)); + + snd -> name = text; + snd -> next = NULL; + + return snd; +} + + +/* + * Convert a text string to a fully scoped name. + */ +static scopedNameDef *text2scopedName(char *text) +{ + return scopeScopedName(text2scopePart(text)); +} + + +/* + * Prepend any current scope to a scoped name. + */ +static scopedNameDef *scopeScopedName(scopedNameDef *name) +{ + classDef *cd = currentScope(); + scopedNameDef *snd; + + snd = (cd != NULL ? copyScopedName(cd->iff->fqcname) : NULL); + + appendScopedName(&snd, name); + + return snd; +} + + +/* + * Return a pointer to the tail part of a scoped name. + */ +char *scopedNameTail(scopedNameDef *snd) +{ + if (snd == NULL) + return NULL; + + while (snd -> next != NULL) + snd = snd -> next; + + return snd -> name; +} + + +/* + * Push the given scope onto the scope stack. + */ +static void pushScope(classDef *scope) +{ + if (currentScopeIdx >= MAX_NESTED_SCOPE) + fatal("Internal error: increase the value of MAX_NESTED_SCOPE\n"); + + scopeStack[currentScopeIdx] = scope; + sectFlagsStack[currentScopeIdx] = sectionFlags; + + ++currentScopeIdx; +} + + +/* + * Pop the scope stack. + */ +static void popScope(void) +{ + if (currentScopeIdx > 0) + sectionFlags = sectFlagsStack[--currentScopeIdx]; +} + + +/* + * Return non-zero if the current input should be parsed rather than be + * skipped. + */ +static int notSkipping() +{ + return (skipStackPtr == 0 ? TRUE : skipStack[skipStackPtr - 1]); +} + + +/* + * Return the value of an expression involving a time period. + */ +static int timePeriod(char *lname,char *uname) +{ + int this, line; + qualDef *qd, *lower, *upper; + moduleDef *mod; + + if (lname == NULL) + lower = NULL; + else if ((lower = findQualifier(lname)) == NULL || lower -> qtype != time_qualifier) + yyerror("Lower bound is not a time version"); + + if (uname == NULL) + upper = NULL; + else if ((upper = findQualifier(uname)) == NULL || upper -> qtype != time_qualifier) + yyerror("Upper bound is not a time version"); + + /* Sanity checks on the bounds. */ + + if (lower == NULL && upper == NULL) + yyerror("Lower and upper bounds cannot both be omitted"); + + if (lower != NULL && upper != NULL) + { + if (lower -> module != upper -> module || lower -> line != upper -> line) + yyerror("Lower and upper bounds are from different timelines"); + + if (lower == upper) + yyerror("Lower and upper bounds must be different"); + + if (lower -> order > upper -> order) + yyerror("Later version specified as lower bound"); + } + + /* Go through each slot in the relevant timeline. */ + + if (lower != NULL) + { + mod = lower -> module; + line = lower -> line; + } + else + { + mod = upper -> module; + line = upper -> line; + } + + this = FALSE; + + for (qd = mod -> qualifiers; qd != NULL; qd = qd -> next) + { + if (qd -> qtype != time_qualifier || qd -> line != line) + continue; + + if (lower != NULL && qd -> order < lower -> order) + continue; + + if (upper != NULL && qd -> order >= upper -> order) + continue; + + /* + * This is within the required range so if it is also needed + * then the expression is true. + */ + + if (isNeeded(qd)) + { + this = TRUE; + break; + } + } + + return this; +} + + +/* + * Return the value of an expression involving a single platform or feature. + */ +static int platOrFeature(char *name,int optnot) +{ + int this; + qualDef *qd; + + if ((qd = findQualifier(name)) == NULL || qd -> qtype == time_qualifier) + yyerror("No such platform or feature"); + + /* Assume this sub-expression is false. */ + + this = FALSE; + + if (qd -> qtype == feature_qualifier) + { + if (!excludedFeature(excludedQualifiers,qd)) + this = TRUE; + } + else if (isNeeded(qd)) + this = TRUE; + + if (optnot) + this = !this; + + return this; +} + + +/* + * Return TRUE if the given qualifier is excluded. + */ +int excludedFeature(stringList *xsl,qualDef *qd) +{ + while (xsl != NULL) + { + if (strcmp(qd -> name,xsl -> s) == 0) + return TRUE; + + xsl = xsl -> next; + } + + return FALSE; +} + + +/* + * Return TRUE if the given qualifier is needed. + */ +static int isNeeded(qualDef *qd) +{ + stringList *sl; + + for (sl = neededQualifiers; sl != NULL; sl = sl -> next) + if (strcmp(qd -> name,sl -> s) == 0) + return TRUE; + + return FALSE; +} + + +/* + * Return the current scope. currentScope() is only valid if notSkipping() + * returns non-zero. + */ +static classDef *currentScope(void) +{ + return (currentScopeIdx > 0 ? scopeStack[currentScopeIdx - 1] : NULL); +} + + +/* + * Create a new qualifier. + */ +static void newQualifier(moduleDef *mod,int line,int order,char *name,qualType qt) +{ + qualDef *qd; + + /* Check it doesn't already exist. */ + + if (findQualifier(name) != NULL) + yyerror("Version is already defined"); + + qd = sipMalloc(sizeof (qualDef)); + qd -> name = name; + qd -> qtype = qt; + qd -> module = mod; + qd -> line = line; + qd -> order = order; + qd -> next = mod -> qualifiers; + mod -> qualifiers = qd; +} + + +/* + * Create a new imported module. + */ +static void newImport(char *name) +{ + moduleDef *from, *mod; + moduleListDef *mld; + + /* Create a new module if it has already been imported. */ + for (mod = currentSpec -> modules; mod != NULL; mod = mod -> next) + if (strcmp(mod -> file,name) == 0) + break; + + from = currentModule; + + if (mod == NULL) + { + newModule(NULL,name); + mod = currentModule; + } + + /* Add the new import unless it has already been imported. */ + for (mld = from->imports; mld != NULL; mld = mld->next) + if (mld->module == mod) + return; + + mld = sipMalloc(sizeof (moduleListDef)); + mld -> module = mod; + mld -> next = from->imports; + + from->imports = mld; +} + + +/* + * Set up pointers to hook names. + */ +static void getHooks(optFlags *optflgs,char **pre,char **post) +{ + optFlag *of; + + if ((of = findOptFlag(optflgs,"PreHook",name_flag)) != NULL) + *pre = of -> fvalue.sval; + else + *pre = NULL; + + if ((of = findOptFlag(optflgs,"PostHook",name_flag)) != NULL) + *post = of -> fvalue.sval; + else + *post = NULL; +} + + +/* + * Get the /ReleaseGIL/ option flag. + */ +static int getReleaseGIL(optFlags *optflgs) +{ + return (findOptFlag(optflgs, "ReleaseGIL", bool_flag) != NULL); +} + + +/* + * Get the /HoldGIL/ option flag. + */ +static int getHoldGIL(optFlags *optflgs) +{ + return (findOptFlag(optflgs, "HoldGIL", bool_flag) != NULL); +} + + +/* + * Return TRUE if the QtNoEmitters option was specified. + */ +int optNoEmitters(sipSpec *pt) +{ + return optFind(pt, "QtNoEmitters"); +} + + +/* + * Return TRUE if the QtRegisterTypes option was specified. + */ +int optRegisterTypes(sipSpec *pt) +{ + return optFind(pt, "QtRegisterTypes"); +} + + +/* + * Return TRUE if the Qt4Q_OBJECT option was specified. + */ +int optQ_OBJECT4(sipSpec *pt) +{ + return optFind(pt, "Qt4Q_OBJECT"); +} + + +/* + * Return TRUE if a particular option was specified with %SIPOptions. + */ +static int optFind(sipSpec *pt, const char *opt) +{ + stringList *sl; + + for (sl = pt->options; sl != NULL; sl = sl->next) + if (strcmp(sl->s, opt) == 0) + return TRUE; + + return FALSE; +} diff --git a/python/sip/sipgen/sip.h b/python/sip/sipgen/sip.h new file mode 100644 index 00000000..f7fa46c3 --- /dev/null +++ b/python/sip/sipgen/sip.h @@ -0,0 +1,1027 @@ +/* + * The main header file for SIP. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#ifndef SIP_H +#define SIP_H + +#include <stdio.h> +#include <sys/types.h> + + +#ifdef TRUE +#undef TRUE +#endif + +#ifdef FALSE +#undef FALSE +#endif + +#define TRUE 1 +#define FALSE 0 + + +#define DEFAULT_OFILE_EXT ".o" /* Default object file extension. */ + +#define MAX_NR_ARGS 20 /* Max. nr. args. to a function or template. */ + + +/* For convenience. */ + +#define classBaseName(cd) ((cd)->iff->name->text) +#define classFQCName(cd) ((cd)->iff->fqcname) + + +/* Handle module flags. */ + +#define MOD_HAS_DELAYED_DTORS 0x0001 /* It has a class with a delayed dtor. */ + +#define hasDelayedDtors(m) ((m)->modflags & MOD_HAS_DELAYED_DTORS) +#define setHasDelayedDtors(m) ((m)->modflags |= MOD_HAS_DELAYED_DTORS) + + +/* Handle section flags. */ + +#define SECT_IS_PUBLIC 0x01 /* It is public. */ +#define SECT_IS_PROT 0x02 /* It is protected. */ +#define SECT_IS_PRIVATE 0x04 /* It is private. */ +#define SECT_IS_SLOT 0x08 /* It is a slot. */ +#define SECT_IS_SIGNAL 0x10 /* It is a signal. */ +#define SECT_MASK 0x1f /* The mask of all flags. */ + + +/* Handle class flags. These are combined with the section flags. */ + +#define CLASS_HAS_ENUMS 0x00000100 /* It has enums. */ +#define CLASS_HAS_SIGSLOTS 0x00000200 /* It has signals or slots. */ +#define CLASS_IS_ABSTRACT 0x00000400 /* It is an abstract class. */ +#define CLASS_HAS_SHADOW 0x00000800 /* It is has a shadow class. */ +#define CLASS_IS_OPAQUE 0x00001000 /* It is opaque. */ +#define CLASS_HAS_VAR_HANDLERS 0x00002000 /* It has variable handlers. */ +#define CLASS_DTOR_RELEASE_GIL 0x00004000 /* The dtor releases the GIL. */ +#define CLASS_IS_PROTECTED 0x00008000 /* It is protected. */ +#define CLASS_IS_PROTECTED_SAV 0x00010000 /* It is protected (saved). */ +#define CLASS_IS_RENAMED 0x00020000 /* It has a different Python name. */ +#define CLASS_IS_INCOMPLETE 0x00040000 /* The specification is incomplete. */ +#define CLASS_CAN_CREATE 0x00080000 /* It has usable ctors. */ +#define CLASS_IS_EXTERNAL 0x00100000 /* It is external. */ +#define CLASS_IS_DELAYED_DTOR 0x00200000 /* The dtor is delayed. */ +#define CLASS_NO_DEFAULT_CTORS 0x00400000 /* Don't create default ctors. */ +#define CLASS_QOBJECT_SUB 0x00800000 /* It is derived from QObject. */ +#define CLASS_DTOR_HOLD_GIL 0x01000000 /* The dtor holds the GIL. */ + +#define hasEnums(cd) ((cd)->classflags & CLASS_HAS_ENUMS) +#define setHasEnums(cd) ((cd)->classflags |= CLASS_HAS_ENUMS) +#define hasSigSlots(cd) ((cd)->classflags & CLASS_HAS_SIGSLOTS) +#define setHasSigSlots(cd) ((cd)->classflags |= CLASS_HAS_SIGSLOTS) +#define isAbstractClass(cd) ((cd)->classflags & CLASS_IS_ABSTRACT) +#define setIsAbstractClass(cd) ((cd)->classflags |= CLASS_IS_ABSTRACT) +#define hasShadow(cd) ((cd)->classflags & CLASS_HAS_SHADOW) +#define setHasShadow(cd) ((cd)->classflags |= CLASS_HAS_SHADOW) +#define resetHasShadow(cd) ((cd)->classflags &= ~CLASS_HAS_SHADOW) +#define isOpaque(cd) ((cd)->classflags & CLASS_IS_OPAQUE) +#define setIsOpaque(cd) ((cd)->classflags |= CLASS_IS_OPAQUE) +#define hasVarHandlers(cd) ((cd)->classflags & CLASS_HAS_VAR_HANDLERS) +#define setHasVarHandlers(cd) ((cd)->classflags |= CLASS_HAS_VAR_HANDLERS) +#define isProtectedClass(cd) ((cd)->classflags & CLASS_IS_PROTECTED) +#define setIsProtectedClass(cd) ((cd)->classflags |= CLASS_IS_PROTECTED) +#define resetIsProtectedClass(cd) ((cd)->classflags &= ~CLASS_IS_PROTECTED) +#define wasProtectedClass(cd) ((cd)->classflags & CLASS_IS_PROTECTED_SAV) +#define setWasProtectedClass(cd) ((cd)->classflags |= CLASS_IS_PROTECTED_SAV) +#define resetWasProtectedClass(cd) ((cd)->classflags &= ~CLASS_IS_PROTECTED_SAV) +#define isReleaseGILDtor(c) ((cd)->classflags & CLASS_DTOR_RELEASE_GIL) +#define setIsReleaseGILDtor(c) ((cd)->classflags |= CLASS_DTOR_RELEASE_GIL) +#define isRenamedClass(cd) ((cd)->classflags & CLASS_IS_RENAMED) +#define setIsRenamedClass(cd) ((cd)->classflags |= CLASS_IS_RENAMED) +#define isIncomplete(cd) ((cd)->classflags & CLASS_IS_INCOMPLETE) +#define setIsIncomplete(cd) ((cd)->classflags |= CLASS_IS_INCOMPLETE) +#define canCreate(cd) ((cd)->classflags & CLASS_CAN_CREATE) +#define setCanCreate(cd) ((cd)->classflags |= CLASS_CAN_CREATE) +#define resetCanCreate(cd) ((cd)->classflags &= ~CLASS_CAN_CREATE) +#define isExternal(cd) ((cd)->classflags & CLASS_IS_EXTERNAL) +#define setIsExternal(cd) ((cd)->classflags |= CLASS_IS_EXTERNAL) +#define isDelayedDtor(cd) ((cd)->classflags & CLASS_IS_DELAYED_DTOR) +#define setIsDelayedDtor(cd) ((cd)->classflags |= CLASS_IS_DELAYED_DTOR) +#define noDefaultCtors(cd) ((cd)->classflags & CLASS_NO_DEFAULT_CTORS) +#define setNoDefaultCtors(cd) ((cd)->classflags |= CLASS_NO_DEFAULT_CTORS) +#define isQObjectSubClass(cd) ((cd)->classflags & CLASS_QOBJECT_SUB) +#define setIsQObjectSubClass(cd) ((cd)->classflags |= CLASS_QOBJECT_SUB) +#define isHoldGILDtor(c) ((cd)->classflags & CLASS_DTOR_HOLD_GIL) +#define setIsHoldGILDtor(c) ((cd)->classflags |= CLASS_DTOR_HOLD_GIL) + +#define isPublicDtor(cd) ((cd)->classflags & SECT_IS_PUBLIC) +#define setIsPublicDtor(cd) ((cd)->classflags |= SECT_IS_PUBLIC) +#define isProtectedDtor(cd) ((cd)->classflags & SECT_IS_PROT) +#define isPrivateDtor(cd) ((cd)->classflags & SECT_IS_PRIVATE) + +#define isDtor(cd) ((cd)->classflags & (SECT_IS_PUBLIC | SECT_IS_PROT | SECT_IS_PRIVATE)) + + +/* Handle ctor flags. These are combined with the section flags. */ + +#define CTOR_RELEASE_GIL 0x00000100 /* The ctor releases the GIL. */ +#define CTOR_EXPLICIT 0x00000200 /* The ctor is explicit. */ +#define CTOR_CAST 0x00000400 /* The ctor is a cast. */ +#define CTOR_HOLD_GIL 0x00000800 /* The ctor holds the GIL. */ + +#define isPublicCtor(c) ((c)->ctorflags & SECT_IS_PUBLIC) +#define setIsPublicCtor(c) ((c)->ctorflags |= SECT_IS_PUBLIC) +#define isProtectedCtor(c) ((c)->ctorflags & SECT_IS_PROT) +#define setIsProtectedCtor(c) ((c)->ctorflags |= SECT_IS_PROT) +#define isPrivateCtor(c) ((c)->ctorflags & SECT_IS_PRIVATE) +#define setIsPrivateCtor(c) ((c)->ctorflags |= SECT_IS_PRIVATE) + +#define isReleaseGILCtor(c) ((c)->ctorflags & CTOR_RELEASE_GIL) +#define setIsReleaseGILCtor(c) ((c)->ctorflags |= CTOR_RELEASE_GIL) +#define isExplicitCtor(c) ((c)->ctorflags & CTOR_EXPLICIT) +#define setIsExplicitCtor(c) ((c)->ctorflags |= CTOR_EXPLICIT) +#define isCastCtor(c) ((c)->ctorflags & CTOR_CAST) +#define isHoldGILCtor(c) ((c)->ctorflags & CTOR_HOLD_GIL) +#define setIsHoldGILCtor(c) ((c)->ctorflags |= CTOR_HOLD_GIL) + + +/* Handle member flags. */ + +#define MEMBR_NUMERIC 0x0001 /* It is a numeric slot. */ + +#define isNumeric(m) ((m)->memberflags & MEMBR_NUMERIC) +#define setIsNumeric(m) ((m)->memberflags |= MEMBR_NUMERIC) + + +/* Handle enum flags. These are combined with the section flags. */ + +#define ENUM_WAS_PROT 0x00000100 /* It was defined as protected. */ +#define ENUM_IS_RENAMED 0x00000200 /* It has been renamed. */ + +#define isProtectedEnum(e) ((e)->enumflags & SECT_IS_PROT) +#define setIsProtectedEnum(e) ((e)->enumflags |= SECT_IS_PROT) +#define resetIsProtectedEnum(e) ((e)->enumflags &= ~SECT_IS_PROT) + +#define wasProtectedEnum(e) ((e)->enumflags & ENUM_WAS_PROT) +#define setWasProtectedEnum(e) ((e)->enumflags |= ENUM_WAS_PROT) +#define resetWasProtectedEnum(e) ((e)->enumflags &= ~ENUM_WAS_PROT) +#define isRenamedEnum(e) ((e)->enumflags & ENUM_IS_RENAMED) +#define setIsRenamedEnum(e) ((e)->enumflags |= ENUM_IS_RENAMED) + + +/* Handle hierarchy flags. */ + +#define HIER_IS_DUPLICATE 0x0001 /* It is a super class duplicate. */ +#define HIER_HAS_DUPLICATE 0x0002 /* It has a super class duplicate. */ + +#define isDuplicateSuper(m) ((m)->mroflags & HIER_IS_DUPLICATE) +#define setIsDuplicateSuper(m) ((m)->mroflags |= HIER_IS_DUPLICATE) +#define hasDuplicateSuper(m) ((m)->mroflags & HIER_HAS_DUPLICATE) +#define setHasDuplicateSuper(m) ((m)->mroflags |= HIER_HAS_DUPLICATE) + + +/* Handle overload flags. These are combined with the section flags. */ + +#define OVER_IS_VIRTUAL 0x00000100 /* It is virtual. */ +#define OVER_IS_ABSTRACT 0x00000200 /* It is abstract. */ +#define OVER_IS_CONST 0x00000400 /* It is a const function. */ +#define OVER_IS_STATIC 0x00000800 /* It is a static function. */ +#define OVER_IS_AUTOGEN 0x00001000 /* It is auto-generated. */ +#define OVER_IS_NEW_THREAD 0x00002000 /* It is in a new thread. */ +#define OVER_IS_FACTORY 0x00004000 /* It is a factory method. */ +#define OVER_XFERRED_BACK 0x00008000 /* Ownership is transferred back. */ +#define OVER_RELEASE_GIL 0x00010000 /* The function releases the GIL. */ +#define OVER_IS_VIRTUAL_REIMP 0x00020000 /* It is a re-implementation of a virtual. */ +#define OVER_DONT_DEREF_SELF 0x00040000 /* For comparison operators, don't dereference self. */ +#define OVER_HOLD_GIL 0x00080000 /* The function holds the GIL. */ + +#define isPublic(o) ((o)->overflags & SECT_IS_PUBLIC) +#define setIsPublic(o) ((o)->overflags |= SECT_IS_PUBLIC) +#define isProtected(o) ((o)->overflags & SECT_IS_PROT) +#define setIsProtected(o) ((o)->overflags |= SECT_IS_PROT) +#define isPrivate(o) ((o)->overflags & SECT_IS_PRIVATE) +#define setIsPrivate(o) ((o)->overflags |= SECT_IS_PRIVATE) +#define isSlot(o) ((o)->overflags & SECT_IS_SLOT) +#define setIsSlot(o) ((o)->overflags |= SECT_IS_SLOT) +#define isSignal(o) ((o)->overflags & SECT_IS_SIGNAL) +#define setIsSignal(o) ((o)->overflags |= SECT_IS_SIGNAL) + +#define isVirtual(o) ((o)->overflags & OVER_IS_VIRTUAL) +#define setIsVirtual(o) ((o)->overflags |= OVER_IS_VIRTUAL) +#define isAbstract(o) ((o)->overflags & OVER_IS_ABSTRACT) +#define setIsAbstract(o) ((o)->overflags |= OVER_IS_ABSTRACT) +#define isConst(o) ((o)->overflags & OVER_IS_CONST) +#define setIsConst(o) ((o)->overflags |= OVER_IS_CONST) +#define isStatic(o) ((o)->overflags & OVER_IS_STATIC) +#define setIsStatic(o) ((o)->overflags |= OVER_IS_STATIC) +#define isAutoGen(o) ((o)->overflags & OVER_IS_AUTOGEN) +#define setIsAutoGen(o) ((o)->overflags |= OVER_IS_AUTOGEN) +#define resetIsAutoGen(o) ((o)->overflags &= ~OVER_IS_AUTOGEN) +#define isNewThread(o) ((o)->overflags & OVER_IS_NEW_THREAD) +#define setIsNewThread(o) ((o)->overflags |= OVER_IS_NEW_THREAD) +#define isFactory(o) ((o)->overflags & OVER_IS_FACTORY) +#define setIsFactory(o) ((o)->overflags |= OVER_IS_FACTORY) +#define isResultTransferredBack(o) ((o)->overflags & OVER_XFERRED_BACK) +#define setIsResultTransferredBack(o) ((o)->overflags |= OVER_XFERRED_BACK) +#define isReleaseGIL(o) ((o)->overflags & OVER_RELEASE_GIL) +#define setIsReleaseGIL(o) ((o)->overflags |= OVER_RELEASE_GIL) +#define isVirtualReimp(o) ((o)->overflags & OVER_IS_VIRTUAL_REIMP) +#define setIsVirtualReimp(o) ((o)->overflags |= OVER_IS_VIRTUAL_REIMP) +#define dontDerefSelf(o) ((o)->overflags & OVER_DONT_DEREF_SELF) +#define setDontDerefSelf(o) ((o)->overflags |= OVER_DONT_DEREF_SELF) +#define isHoldGIL(o) ((o)->overflags & OVER_HOLD_GIL) +#define setIsHoldGIL(o) ((o)->overflags |= OVER_HOLD_GIL) + + +/* Handle variable flags. */ + +#define VAR_IS_STATIC 0x01 /* It is a static variable. */ +#define VAR_NEEDS_HANDLER 0x02 /* It the variable needs a handler. */ + +#define isStaticVar(v) ((v)->varflags & VAR_IS_STATIC) +#define setIsStaticVar(v) ((v)->varflags |= VAR_IS_STATIC) +#define needsHandler(v) ((v)->varflags & VAR_NEEDS_HANDLER) +#define setNeedsHandler(v) ((v)->varflags |= VAR_NEEDS_HANDLER) + + +/* Handle argument flags. */ + +#define ARG_IS_REF 0x0001 /* It is a reference. */ +#define ARG_IS_CONST 0x0002 /* It is a const. */ +#define ARG_XFERRED 0x0004 /* Ownership is transferred. */ +#define ARG_THIS_XFERRED 0x0008 /* Ownership of this is transferred. */ +#define ARG_XFERRED_BACK 0x0010 /* Ownership is transferred back. */ +#define ARG_ARRAY 0x0020 /* Used as an array. */ +#define ARG_ARRAY_SIZE 0x0040 /* Used as an array size. */ +#define ARG_ALLOW_NONE 0x0080 /* Allow None as a value. */ +#define ARG_GET_WRAPPER 0x0100 /* Get the wrapper object. */ +#define ARG_IN 0x0200 /* It passes an argument. */ +#define ARG_OUT 0x0400 /* It returns a result. */ +#define ARG_CONSTRAINED 0x0800 /* Suppress type conversion. */ + +#define isReference(a) ((a)->argflags & ARG_IS_REF) +#define setIsReference(a) ((a)-> argflags |= ARG_IS_REF) +#define resetIsReference(a) ((a)->argflags &= ~ARG_IS_REF) +#define isConstArg(a) ((a)->argflags & ARG_IS_CONST) +#define setIsConstArg(a) ((a)->argflags |= ARG_IS_CONST) +#define resetIsConstArg(a) ((a)->argflags &= ~ARG_IS_CONST) +#define isTransferred(a) ((a)->argflags & ARG_XFERRED) +#define setIsTransferred(a) ((a)->argflags |= ARG_XFERRED) +#define isThisTransferred(a) ((a)->argflags & ARG_THIS_XFERRED) +#define setIsThisTransferred(a) ((a)->argflags |= ARG_THIS_XFERRED) +#define isTransferredBack(a) ((a)->argflags & ARG_XFERRED_BACK) +#define setIsTransferredBack(a) ((a)->argflags |= ARG_XFERRED_BACK) +#define isArray(a) ((a)->argflags & ARG_ARRAY) +#define setArray(a) ((a)->argflags |= ARG_ARRAY) +#define isArraySize(a) ((a)->argflags & ARG_ARRAY_SIZE) +#define setArraySize(a) ((a)->argflags |= ARG_ARRAY_SIZE) +#define isAllowNone(a) ((a)->argflags & ARG_ALLOW_NONE) +#define setAllowNone(a) ((a)->argflags |= ARG_ALLOW_NONE) +#define isGetWrapper(a) ((a)->argflags & ARG_GET_WRAPPER) +#define setGetWrapper(a) ((a)->argflags |= ARG_GET_WRAPPER) +#define isInArg(a) ((a)->argflags & ARG_IN) +#define setIsInArg(a) ((a)->argflags |= ARG_IN) +#define isOutArg(a) ((a)->argflags & ARG_OUT) +#define setIsOutArg(a) ((a)->argflags |= ARG_OUT) +#define isConstrained(a) ((a)->argflags & ARG_CONSTRAINED) +#define setIsConstrained(a) ((a)->argflags |= ARG_CONSTRAINED) +#define resetIsConstrained(a) ((a)->argflags &= ~ARG_CONSTRAINED) + + +/* Handle name flags. */ + +#define NAME_IS_USED 0x01 /* It is used in the main module. */ +#define NAME_IS_CLASS 0x02 /* It is the name of a class. */ + +#define isUsedName(n) ((n)->nameflags & NAME_IS_USED) +#define setIsUsedName(n) ((n)->nameflags |= NAME_IS_USED) +#define resetIsUsedName(n) ((n)->nameflags &= ~NAME_IS_USED) +#define isClassName(n) ((n)->nameflags & NAME_IS_CLASS) +#define setIsClassName(n) ((n)->nameflags |= NAME_IS_CLASS) +#define resetIsClassName(n) ((n)->nameflags &= ~NAME_IS_CLASS) + + +/* Handle virtual handler flags. */ + +#define VH_IS_DUPLICATE 0x01 /* It is a duplicate. */ +#define VH_TRANSFERS 0x02 /* It transfers ownership of the result. */ + +#define isDuplicateVH(vh) ((vh)->vhflags & VH_IS_DUPLICATE) +#define setIsDuplicateVH(vh) ((vh)->vhflags |= VH_IS_DUPLICATE) +#define resetIsDuplicateVH(vh) ((vh)->vhflags &= ~VH_IS_DUPLICATE) +#define isTransferVH(vh) ((vh)->vhflags & VH_TRANSFERS) +#define setIsTransferVH(vh) ((vh)->vhflags |= VH_TRANSFERS) + + +/* Slot types. */ + +typedef enum { + str_slot, + unicode_slot, + int_slot, + long_slot, + float_slot, + len_slot, + contains_slot, + add_slot, + concat_slot, + sub_slot, + mul_slot, + repeat_slot, + div_slot, + mod_slot, + and_slot, + or_slot, + xor_slot, + lshift_slot, + rshift_slot, + iadd_slot, + iconcat_slot, + isub_slot, + imul_slot, + irepeat_slot, + idiv_slot, + imod_slot, + iand_slot, + ior_slot, + ixor_slot, + ilshift_slot, + irshift_slot, + invert_slot, + call_slot, + getitem_slot, + setitem_slot, + delitem_slot, + lt_slot, + le_slot, + eq_slot, + ne_slot, + gt_slot, + ge_slot, + cmp_slot, + nonzero_slot, + neg_slot, + pos_slot, + abs_slot, + repr_slot, + hash_slot, + no_slot +} slotType; + + +/* + * Argument types. Always add new ones at the end because the numeric values + * can appear in generated code. + */ +typedef enum { + no_type, + defined_type, + class_type, + struct_type, + void_type, + enum_type, + template_type, + signal_type, + slot_type, + rxcon_type, + rxdis_type, + slotcon_type, + slotdis_type, + ustring_type, + string_type, + short_type, + ushort_type, + cint_type, + int_type, + uint_type, + long_type, + ulong_type, + float_type, + cfloat_type, + double_type, + cdouble_type, + bool_type, + mapped_type, + pyobject_type, + pytuple_type, + pylist_type, + pydict_type, + pycallable_type, + pyslice_type, + qobject_type, + function_type, + pytype_type, + ellipsis_type, + longlong_type, + ulonglong_type, + anyslot_type, + cbool_type, + sstring_type, + wstring_type +} argType; + + +/* Value types. */ + +typedef enum { + qchar_value, + string_value, + numeric_value, + real_value, + scoped_value, + fcall_value +} valueType; + + +/* Version types. */ + +typedef enum { + time_qualifier, + platform_qualifier, + feature_qualifier +} qualType; + + +/* Interface file types. */ + +typedef enum { + exception_iface, + mappedtype_iface, + namespace_iface, + class_iface +} ifaceFileType; + + +/* A software license. */ + +typedef struct { + char *type; /* The license type. */ + char *licensee; /* The licensee. */ + char *timestamp; /* The timestamp. */ + char *sig; /* The signature. */ +} licenseDef; + + +/* A version qualifier. */ + +typedef struct _qualDef { + char *name; /* The qualifier name. */ + qualType qtype; /* The qualifier type. */ + struct _moduleDef *module; /* The defining module. */ + int line; /* Timeline if it is a time. */ + int order; /* Order if it is a time. */ + struct _qualDef *next; /* Next in the list. */ +} qualDef; + + +/* A scoped name. */ + +typedef struct _scopedNameDef { + char *name; /* The name. */ + struct _scopedNameDef *next; /* Next in the scope list. */ +} scopedNameDef; + + +/* A name. */ + +typedef struct _nameDef { + int nameflags; /* The name flags. */ + struct _moduleDef *module; /* The main module. */ + char *text; /* The text of the name. */ + struct _nameDef *next; /* Next in the list. */ +} nameDef; + + +/* + * A node in the tree of classes used to determine the order in which the + * classes need to be created. + */ + +typedef struct _nodeDef { + int ordered; /* Set if in order. */ + struct _classDef *cd; /* The class. */ + struct _nodeDef *parent; /* The parent. */ + struct _nodeDef *child; /* The first child. */ + struct _nodeDef *next; /* The next sibling. */ +} nodeDef; + + +/* A module definition. */ + +typedef struct _moduleDef { + char *fullname; /* The full module name. */ + char *name; /* The module base name. */ + int version; /* The module version. */ + int modflags; /* The module flags. */ + int modulenr; /* The module number. */ + char *file; /* The filename. */ + qualDef *qualifiers; /* The list of qualifiers. */ + nodeDef root; /* Root of class tree. */ + int nrtimelines; /* The nr. of timelines. */ + int nrclasses; /* The nr. of classes. */ + int nrexceptions; /* The nr. of exceptions. */ + int nrmappedtypes; /* The nr. of mapped types. */ + int nrenums; /* The nr. of named enums. */ + int nrtypedefs; /* The nr. of typedefs. */ + int nrvirthandlers; /* The nr. of virtual handlers. */ + struct _virtHandlerDef *virthandlers; /* The virtual handlers. */ + licenseDef *license; /* The software license. */ + struct _moduleListDef *allimports; /* The list of all imports. */ + struct _moduleListDef *imports; /* The list of direct imports. */ + struct _moduleDef *next; /* Next in the list. */ +} moduleDef; + + +/* An entry in a linked module list. */ + +typedef struct _moduleListDef { + moduleDef *module; /* The module itself. */ + struct _moduleListDef *next; /* The next in the list. */ +} moduleListDef; + + +/* A literal code block. */ + +typedef struct _codeBlock { + char *frag; /* The code itself. */ + char *filename; /* The original file. */ + int linenr; /* The line in the file. */ + struct _codeBlock *next; /* Next in the list. */ +} codeBlock; + + +/* The arguments to a throw specifier. */ + +typedef struct _throwArgs { + int nrArgs; /* The number of arguments. */ + struct _exceptionDef *args[MAX_NR_ARGS]; /* The arguments. */ +} throwArgs; + + +/* An exception. */ + +typedef struct _exceptionDef { + int exceptionnr; /* The exception number. */ + struct _ifaceFileDef *iff; /* The interface file. */ + char *pyname; /* The exception Python name. */ + struct _classDef *cd; /* The exception class. */ + char *bibase; /* The builtin base exception. */ + struct _exceptionDef *base; /* The defined base exception. */ + codeBlock *hdrcode; /* Optional header code. */ + codeBlock *raisecode; /* Raise exception code. */ + struct _exceptionDef *next; /* The next in the list. */ +} exceptionDef; + + +/* A value. */ + +typedef struct _valueDef { + valueType vtype; /* The type. */ + char vunop; /* Any unary operator. */ + char vbinop; /* Any binary operator. */ + union { + char vqchar; /* Quoted character value. */ + long vnum; /* Numeric value. */ + double vreal; /* Real value. */ + char *vstr; /* String value. */ + scopedNameDef *vscp; /* Scoped value. */ + struct _fcallDef *fcd; /* Function call. */ + } u; + struct _valueDef *next; /* Next in the expression. */ +} valueDef; + + +/* A member function argument (or result). */ + +typedef struct { + argType atype; /* The type. */ + char *name; /* The name. */ + int argflags; /* The argument flags. */ + int nrderefs; /* Nr. of dereferences. */ + valueDef *defval; /* The default value. */ + union { + struct _signatureDef *sa; /* If it is a function. */ + struct _templateDef *td; /* If it is a template. */ + struct _scopedNameDef *snd; /* If it is a defined type. */ + struct _classDef *cd; /* If it is a class. */ + struct _enumDef *ed; /* If it is an enum. */ + struct _scopedNameDef *sname; /* If it is a struct. */ + struct _mappedTypeDef *mtd; /* If it is a mapped type. */ + } u; +} argDef; + + + +/* An entry in a linked argument list. */ +typedef struct _argList { + argDef arg; /* The argument itself. */ + struct _argList *next; /* Next in the list. */ +} argList; + + +/* A function call. */ + +typedef struct _fcallDef { + argDef type; /* The type. */ + int nrArgs; /* The number of arguments. */ + struct _valueDef *args[MAX_NR_ARGS]; /* The arguments. */ +} fcallDef; + + +/* An interface file definition. */ + +typedef struct _ifaceFileDef { + nameDef *name; /* The name. */ + ifaceFileType type; /* Interface file type. */ + scopedNameDef *fqcname; /* The fully qualified C++ name. */ + moduleDef *module; /* The owning module. */ + struct _ifaceFileList *used; /* Interface files used. */ + struct _ifaceFileDef *next; /* Next in the list. */ +} ifaceFileDef; + + +/* An entry in a linked interface file list. */ + +typedef struct _ifaceFileList { + ifaceFileDef *iff; /* The interface file itself. */ + int header; /* If needed in the .h file. */ + struct _ifaceFileList *next; /* Next in the list. */ +} ifaceFileList; + + +/* A mapped type. */ + +typedef struct _mappedTypeDef { + argDef type; /* The type being mapped. */ + int mappednr; /* The mapped type number. */ + ifaceFileDef *iff; /* The interface file. */ + codeBlock *hdrcode; /* Header code. */ + codeBlock *convfromcode; /* Convert from C++ code. */ + codeBlock *convtocode; /* Convert to C++ code. */ + struct _mappedTypeDef *next; /* Next in the list. */ +} mappedTypeDef; + + +/* A function signature. */ + +typedef struct _signatureDef { + argDef result; /* The result. */ + int nrArgs; /* The number of arguments. */ + argDef args[MAX_NR_ARGS]; /* The arguments. */ +} signatureDef; + + +/* A list of function signatures. */ + +typedef struct _signatureList { + struct _signatureDef *sd; /* The signature. */ + struct _signatureList *next; /* Next in the list. */ +} signatureList; + + +/* A template type. */ + +typedef struct _templateDef { + scopedNameDef *fqname; /* The name. */ + signatureDef types; /* The types. */ +} templateDef; + + +/* A list of virtual handlers. */ + +typedef struct _virtHandlerDef { + int virthandlernr; /* The nr. of the virtual handler. */ + int vhflags; /* The virtual handler flags. */ + signatureDef *pysig; /* The Python signature. */ + signatureDef *cppsig; /* The C++ signature. */ + struct _moduleDef *module; /* The defining module. */ + codeBlock *virtcode; /* Virtual handler code. */ + struct _virtHandlerDef *next; /* Next in the list. */ +} virtHandlerDef; + + +/* A typedef definition. */ + +typedef struct _typedefDef { + scopedNameDef *fqname; /* The fully qualified name. */ + struct _classDef *ecd; /* The enclosing class. */ + moduleDef *module; /* The owning module. */ + argDef type; /* The actual type. */ + struct _typedefDef *next; /* Next in the list. */ +} typedefDef; + + +/* A variable definition. */ + +typedef struct _varDef { + nameDef *pyname; /* The variable Python name. */ + scopedNameDef *fqcname; /* The fully qualified C/C++ name. */ + struct _classDef *ecd; /* The enclosing class. */ + moduleDef *module; /* The owning module. */ + int varflags; /* The variable flags. */ + argDef type; /* The actual type. */ + codeBlock *accessfunc; /* The access function. */ + codeBlock *getcode; /* The get code. */ + codeBlock *setcode; /* The set code. */ + struct _varDef *next; /* Next in the list. */ +} varDef; + + +/* An overloaded member function definition. */ + +typedef struct _overDef { + char *cppname; /* The C++ name. */ + int overflags; /* The overload flags. */ + struct _memberDef *common; /* Common parts. */ + signatureDef pysig; /* The Python signature. */ + signatureDef *cppsig; /* The C++ signature. */ + throwArgs *exceptions; /* The exceptions. */ + codeBlock *methodcode; /* Method code. */ + virtHandlerDef *virthandler; /* The virtual handler. */ + char *prehook; /* The pre-hook name. */ + char *posthook; /* The post-hook name. */ + struct _overDef *next; /* Next in the list. */ +} overDef; + + +/* An overloaded constructor definition. */ + +typedef struct _ctorDef { + int ctorflags; /* The ctor flags. */ + signatureDef pysig; /* The Python signature. */ + signatureDef *cppsig; /* The C++ signature, NULL if /NoDerived/. */ + throwArgs *exceptions; /* The exceptions. */ + codeBlock *methodcode; /* Method code. */ + char *prehook; /* The pre-hook name. */ + char *posthook; /* The post-hook name. */ + struct _ctorDef *next; /* Next in the list. */ +} ctorDef; + + +/* An enumerated type member definition. */ + +typedef struct _enumMemberDef { + nameDef *pyname; /* The Python name. */ + char *cname; /* The C/C++ name. */ + struct _enumDef *ed; /* The enclosing enum. */ + struct _enumMemberDef *next; /* Next in the list. */ +} enumMemberDef; + + +/* An enumerated type definition. */ + +typedef struct _enumDef { + int enumflags; /* The enum flags. */ + scopedNameDef *fqcname; /* The name (may be NULL). */ + nameDef *pyname; /* The Python name (may be NULL). */ + int enumnr; /* The enum number. */ + struct _classDef *ecd; /* The enclosing class. */ + struct _classDef *pcd; /* The publishing class. */ + moduleDef *module; /* The owning module. */ + enumMemberDef *members; /* The list of members. */ + struct _memberDef *slots; /* The list of slots. */ + struct _overDef *overs; /* The list of slot overloads. */ + struct _enumDef *next; /* Next in the list. */ +} enumDef; + + +/* An member function definition. */ + +typedef struct _memberDef { + nameDef *pyname; /* The Python name. */ + int memberflags; /* The member flags. */ + slotType slot; /* The slot type. */ + moduleDef *module; /* The owning module. */ + struct _memberDef *next; /* Next in the list. */ +} memberDef; + + +/* A list of visible member functions. */ + +typedef struct _visibleList { + memberDef *m; /* The member definition. */ + struct _classDef *cd; /* The class. */ + struct _visibleList *next; /* Next in the list. */ +} visibleList; + + +/* An entry in a linked class list. */ + +typedef struct _classList { + struct _classDef *cd; /* The class itself. */ + struct _classList *next; /* Next in the list. */ +} classList; + + +/* A virtual overload definition. */ + +typedef struct _virtOverDef { + overDef o; /* The overload. */ + struct _classDef *scope; /* The overload scope. */ + struct _virtOverDef *next; /* Next in the list. */ +} virtOverDef; + + +/* A class that appears in a class's hierarchy. */ + +typedef struct _mroDef { + struct _classDef *cd; /* The class. */ + int mroflags; /* The hierarchy flags. */ + struct _mroDef *next; /* The next in the list. */ +} mroDef; + + +/* A class definition. */ + +typedef struct _classDef { + int classflags; /* The class flags. */ + int userflags; /* The user type flags. */ + int classnr; /* The class number. */ + char *pyname; /* The Python name. */ + ifaceFileDef *iff; /* The interface file. */ + struct _classDef *ecd; /* The enclosing scope. */ + struct _classDef *real; /* The real class if this is a proxy or extender. */ + nodeDef *node; /* Position in class tree. */ + classList *supers; /* The parent classes. */ + mroDef *mro; /* The super-class hierarchy. */ + templateDef *td; /* The instantiated template. */ + ctorDef *ctors; /* The constructors. */ + ctorDef *defctor; /* The default ctor. */ + codeBlock *dealloccode; /* Handwritten dealloc code. */ + codeBlock *dtorcode; /* Handwritten dtor code. */ + throwArgs *dtorexceptions; /* The dtor exceptions. */ + memberDef *members; /* The member functions. */ + overDef *overs; /* The overloads. */ + argList *casts; /* The operator casts. */ + virtOverDef *vmembers; /* The virtual members. */ + visibleList *visible; /* The visible members. */ + codeBlock *cppcode; /* Class C++ code. */ + codeBlock *hdrcode; /* Class header code. */ + codeBlock *convtosubcode; /* Convert to sub C++ code. */ + struct _classDef *subbase; /* Sub-class base class. */ + codeBlock *convtocode; /* Convert to C++ code. */ + codeBlock *travcode; /* Traverse code. */ + codeBlock *clearcode; /* Clear code. */ + codeBlock *readbufcode; /* Read buffer code. */ + codeBlock *writebufcode; /* Write buffer code. */ + codeBlock *segcountcode; /* Segment count code. */ + codeBlock *charbufcode; /* Character buffer code. */ + struct _classDef *next; /* Next in the list. */ +} classDef; + + +/* A class template definition. */ + +typedef struct _classTmplDef { + signatureDef sig; /* The template arguments. */ + classDef *cd; /* The class itself. */ + struct _classTmplDef *next; /* The next in the list. */ +} classTmplDef; + + +/* A mapped type template definition. */ + +typedef struct _mappedTypeTmplDef { + signatureDef sig; /* The template arguments. */ + mappedTypeDef *mt; /* The mapped type itself. */ + struct _mappedTypeTmplDef *next; /* The next in the list. */ +} mappedTypeTmplDef; + + +/* The parse tree corresponding to the specification file. */ + +typedef struct { + moduleDef *module; /* This module. */ + moduleDef *modules; /* The list of modules. */ + nameDef *namecache; /* The name cache. */ + ifaceFileDef *ifacefiles; /* The list of interface files. */ + classDef *classes; /* The list of classes. */ + classTmplDef *classtemplates; /* The list of class templates. */ + classDef *proxies; /* The list of proxy classes. */ + exceptionDef *exceptions; /* The list of exceptions. */ + mappedTypeDef *mappedtypes; /* The mapped types. */ + mappedTypeTmplDef *mappedtypetemplates; /* The list of mapped type templates. */ + int qobjclass; /* QObject class, -1 if none. */ + enumDef *enums; /* List of enums. */ + varDef *vars; /* List of variables. */ + memberDef *othfuncs; /* List of other functions. */ + overDef *overs; /* Global overloads. */ + typedefDef *typedefs; /* List of typedefs. */ + codeBlock *copying; /* Software license. */ + codeBlock *exphdrcode; /* Exported header code. */ + codeBlock *hdrcode; /* Header code. */ + codeBlock *cppcode; /* Global C++ code. */ + codeBlock *docs; /* Documentation. */ + codeBlock *preinitcode; /* Pre-initialisation code. */ + codeBlock *postinitcode; /* Post-initialisation code. */ + codeBlock *unitcode; /* Compilation unit code. */ + ifaceFileList *used; /* Interface files used. */ + int sigslots; /* Set if signals or slots are used. */ + int genc; /* Set if we are generating C code. */ + struct _stringList *options; /* The list of options. */ +} sipSpec; + + +/* A list of strings. */ + +typedef struct _stringList { + const char *s; /* The string. */ + struct _stringList *next; /* The next in the list. */ +} stringList; + + +/* File specific context information for the parser. */ + +typedef struct _parserContext { + int ifdepth; /* The depth of nested if's. */ + moduleDef *prevmod; /* The previous module. */ +} parserContext; + + +extern char *sipVersion; /* The version of SIP. */ +extern stringList *includeDirList; /* The include directory list for SIP files. */ + + +void parse(sipSpec *,FILE *,char *,stringList *,stringList *); +void parserEOF(char *,parserContext *); +void transform(sipSpec *); +void generateCode(sipSpec *,char *,char *,char *,char *,int,int,int,int,stringList *); +void generateAPI(sipSpec *pt, const char *apiFile); +void generateXML(sipSpec *pt, const char *xmlFile); +void generateExpression(valueDef *vd, FILE *fp); +void warning(char *,...); +void fatal(char *,...); +void fatalScopedName(scopedNameDef *); +void setInputFile(FILE *,char *,parserContext *,int); +void *sipMalloc(size_t); +char *sipStrdup(char *); +char *concat(const char *, ...); +void append(char **,char *); +ifaceFileList *addToUsedList(ifaceFileList **, ifaceFileDef *); +int excludedFeature(stringList *,qualDef *); +int sameSignature(signatureDef *,signatureDef *,int); +int sameTemplateSignature(signatureDef *sd1, signatureDef *sd2, int deep); +int sameScopedName(scopedNameDef *,scopedNameDef *); +int sameBaseType(argDef *,argDef *); +char *scopedNameTail(scopedNameDef *); +scopedNameDef *text2scopePart(char *); +scopedNameDef *copyScopedName(scopedNameDef *); +void appendScopedName(scopedNameDef **,scopedNameDef *); +void freeScopedName(scopedNameDef *); +void appendToClassList(classList **,classDef *); +void prcode(FILE *fp, const char *fmt, ...); +void prOverloadName(FILE *fp, overDef *od); +void prScopedPythonName(FILE *fp, classDef *scope, const char *pyname); +void prOverloadDecl(FILE *fp, overDef *od, int defval); +int isIntReturnSlot(memberDef *md); +int isLongReturnSlot(memberDef *md); +int isVoidReturnSlot(memberDef *md); +int isNumberSlot(memberDef *md); +int isRichCompareSlot(memberDef *md); +mappedTypeDef *allocMappedType(argDef *type); +void appendString(stringList **headp, const char *s); +void appendTypeStrings(scopedNameDef *ename, signatureDef *patt, signatureDef *src, signatureDef *known, scopedNameDef **names, scopedNameDef **values); +codeBlock *templateCode(sipSpec *pt, ifaceFileList **used, codeBlock *ocb, scopedNameDef *names, scopedNameDef *values); +ifaceFileDef *findIfaceFile(sipSpec *pt, moduleDef *mod, scopedNameDef *fqname, ifaceFileType iftype, argDef *ad); +int optNoEmitters(sipSpec *pt); +int optRegisterTypes(sipSpec *pt); +int optQ_OBJECT4(sipSpec *pt); +void yywarning(char *); + + +/* These are only here because bison publically references them. */ + +/* Represent a set of option flags. */ + +#define MAX_NR_FLAGS 5 + +typedef enum { + bool_flag, + string_flag, + name_flag, + opt_name_flag, + integer_flag +} flagType; + +typedef struct { + char *fname; /* The flag name. */ + flagType ftype; /* The flag type. */ + union { /* The flag value. */ + char *sval; /* A string value. */ + long ival; /* An integer value. */ + } fvalue; +} optFlag; + +typedef struct { + int nrFlags; /* The number of flags. */ + optFlag flags[MAX_NR_FLAGS]; /* Each flag. */ +} optFlags; + +#endif diff --git a/python/sip/sipgen/sipgen.sbf b/python/sip/sipgen/sipgen.sbf new file mode 100644 index 00000000..911b216b --- /dev/null +++ b/python/sip/sipgen/sipgen.sbf @@ -0,0 +1,16 @@ +# This is the build file for the code generator. +# +# Copyright (c) 2007 +# Riverbank Computing Limited <info@riverbankcomputing.co.uk> +# +# This file is part of SIP. +# +# This copy of SIP is licensed for use under the terms of the SIP License +# Agreement. See the file LICENSE for more details. +# +# SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +target = sip +sources = main.c transform.c gencode.c export.c heap.c parser.c lexer.c +headers = sip.h parser.h diff --git a/python/sip/sipgen/transform.c b/python/sip/sipgen/transform.c new file mode 100644 index 00000000..036a124b --- /dev/null +++ b/python/sip/sipgen/transform.c @@ -0,0 +1,2856 @@ +/* + * The parse tree transformation module for SIP. + * + * Copyright (c) 2007 + * Riverbank Computing Limited <info@riverbankcomputing.co.uk> + * + * This file is part of SIP. + * + * This copy of SIP is licensed for use under the terms of the SIP License + * Agreement. See the file LICENSE for more details. + * + * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + +#include <stddef.h> +#include <string.h> +#include <stdlib.h> + +#include "sip.h" + + +static int samePythonSignature(signatureDef *sd1, signatureDef *sd2); +static int nextSignificantArg(signatureDef *sd, int a); +static int sameArgType(argDef *a1, argDef *a2, int strict); +static int supportedType(classDef *,overDef *,argDef *,int); +static int sameOverload(overDef *od1,overDef *od2); +static int sameVirtualHandler(virtHandlerDef *vhd1,virtHandlerDef *vhd2); +static int isSubClass(classDef *cc,classDef *pc); +static void setAllImports(sipSpec *pt, moduleDef *mod); +static void addUniqueModule(moduleDef *mod, moduleDef *imp); +static void ensureInput(classDef *,overDef *,argDef *); +static void defaultInput(argDef *); +static void defaultOutput(classDef *,overDef *,argDef *); +static void assignClassNrs(sipSpec *,moduleDef *,nodeDef *); +static void assignEnumNrs(sipSpec *pt); +static void positionClass(classDef *); +static void addNodeToParent(nodeDef *,classDef *); +static void addAutoOverload(sipSpec *,classDef *,overDef *); +static void ifaceFileIsUsed(sipSpec *, ifaceFileDef *, argDef *); +static void ifaceFilesAreUsed(sipSpec *, ifaceFileDef *, overDef *); +static void ifaceFilesAreUsedByMethod(sipSpec *, classDef *, memberDef *); +static void ifaceFilesAreUsedFromOther(sipSpec *pt, signatureDef *sd); +static void scopeDefaultValue(sipSpec *,classDef *,argDef *); +static void setHierarchy(sipSpec *,classDef *,classDef *,classList **); +static void transformCtors(sipSpec *,classDef *); +static void transformCasts(sipSpec *,classDef *); +static void addDefaultCopyCtor(classDef *); +static void transformOverloads(sipSpec *,classDef *,overDef *); +static void transformVariableList(sipSpec *); +static void transformMappedTypes(sipSpec *); +static void getVisibleMembers(sipSpec *,classDef *); +static void getVirtuals(sipSpec *pt,classDef *cd); +static void getClassVirtuals(classDef *,classDef *); +static void transformTypedefs(sipSpec *pt); +static void resolveMappedTypeTypes(sipSpec *,mappedTypeDef *); +static void resolveCtorTypes(sipSpec *,classDef *,ctorDef *); +static void resolveFuncTypes(sipSpec *,moduleDef *,classDef *,overDef *); +static void resolvePySigTypes(sipSpec *,moduleDef *,classDef *,overDef *,signatureDef *,int); +static void resolveVariableType(sipSpec *,varDef *); +static void fatalNoDefinedType(scopedNameDef *); +static void getBaseType(sipSpec *,moduleDef *,classDef *,argDef *); +static void searchScope(sipSpec *,classDef *,scopedNameDef *,argDef *); +static void searchMappedTypes(sipSpec *,scopedNameDef *,argDef *); +static void searchTypedefs(sipSpec *,scopedNameDef *,argDef *); +static void searchEnums(sipSpec *,scopedNameDef *,argDef *); +static void searchClasses(sipSpec *,moduleDef *mod,scopedNameDef *,argDef *); +static void appendToMRO(mroDef *,mroDef ***,classDef *); +static void moveClassCasts(sipSpec *pt, classDef *cd); +static void moveGlobalSlot(sipSpec *pt, memberDef *gmd); +static void filterVirtualHandlers(moduleDef *mod); +static ifaceFileDef *getIfaceFile(argDef *ad); +static mappedTypeDef *instantiateMappedTypeTemplate(sipSpec *pt, moduleDef *mod, mappedTypeTmplDef *mtt, argDef *type); +static classDef *getProxy(sipSpec *pt, classDef *cd); + + +/* + * Transform the parse tree. + */ + +void transform(sipSpec *pt) +{ + moduleDef *mod; + moduleListDef *mld; + classDef *cd, *rev, **tail; + classList *newl; + overDef *od; + mappedTypeDef *mtd; + virtHandlerDef *vhd; + int nr; + + if (pt -> module -> name == NULL) + fatal("No %%Module has been specified for the module\n"); + + /* + * The class list has the main module's classes at the front and the + * ones from the module at the most nested %Import at the end. This + * affects some of the following algorithms, eg. when assigning class + * numbers. We have to have consistency whenever a module is used. To + * achieve this we reverse the order of the classes. + */ + rev = NULL; + cd = pt -> classes; + + while (cd != NULL) + { + classDef *next = cd -> next; + + cd -> next = rev; + rev = cd; + + /* + * Mark any QObject class. This flag will ripple through all derived + * classes when we set the hierarchy. + */ + if (strcmp(classBaseName(cd), "QObject") == 0) + setIsQObjectSubClass(cd); + + cd = next; + } + + pt -> classes = rev; + + /* Build the list of all imports for each module. */ + for (mod = pt->modules; mod != NULL; mod = mod->next) + setAllImports(pt, mod); + + /* Check each class has been defined. */ + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + if (cd -> iff -> module == NULL) + { + fatalScopedName(classFQCName(cd)); + fatal(" has not been defined\n"); + } + + /* + * Set the super-class hierarchy for each class and re-order the list + * of classes so that no class appears before a super class or an + * enclosing scope class. + */ + newl = NULL; + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + setHierarchy(pt,cd,cd,&newl); + + /* Replace the old list with the new one. */ + tail = &pt -> classes; + + while (newl != NULL) + { + classList *cl = newl; + + *tail = cl -> cd; + tail = &cl -> cd -> next; + + newl = cl -> next; + free(cl); + } + + *tail = NULL; + + /* Transform typedefs, variables and global functions. */ + transformTypedefs(pt); + transformVariableList(pt); + transformOverloads(pt,NULL,pt -> overs); + + /* Transform class ctors, functions and casts. */ + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + transformCtors(pt,cd); + + if (!pt -> genc) + { + transformOverloads(pt,cd,cd -> overs); + transformCasts(pt, cd); + } + } + + /* Transform mapped types based on templates. */ + transformMappedTypes(pt); + + /* Handle default ctors now that the argument types are resolved. */ + if (!pt -> genc) + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + if (!noDefaultCtors(cd) && !isOpaque(cd) && cd->iff->type != namespace_iface) + addDefaultCopyCtor(cd); + + /* + * Go through each class and add it to it's defining module's tree of + * classes. The tree reflects the namespace hierarchy. + */ + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + addNodeToParent(&cd -> iff -> module -> root,cd); + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + positionClass(cd); + + /* Assign module specific class numbers for all modules. */ + for (mod = pt->modules; mod != NULL; mod = mod->next) + assignClassNrs(pt, mod, &mod->root); + + /* Assign module specific enum numbers for all enums. */ + assignEnumNrs(pt); + + /* Add any automatically generated methods. */ + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + for (od = cd -> overs; od != NULL; od = od -> next) + if (isAutoGen(od)) + addAutoOverload(pt,cd,od); + + /* Allocate mapped types numbers. */ + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + mtd -> mappednr = mtd -> iff -> module -> nrmappedtypes++; + + /* + * Move casts and slots around to their correct classes (if in the same + * module) or create proxies for them (if cross-module). + */ + if (!pt -> genc) + { + memberDef *md; + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + if (cd->iff->module == pt->module) + moveClassCasts(pt, cd); + + for (md = pt->othfuncs; md != NULL; md = md->next) + if (md->slot != no_slot && md->module == pt->module) + moveGlobalSlot(pt, md); + } + + /* Generate the different class views. */ + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + ifaceFileDef *iff = cd -> iff; + + if (iff -> type == class_iface) + { + /* Get the list of visible member functions. */ + getVisibleMembers(pt,cd); + + /* Get the virtual members. */ + if (hasShadow(cd)) + getVirtuals(pt,cd); + } + else if (iff -> type == namespace_iface && iff -> module == pt -> module) + { + memberDef *md; + + for (md = cd -> members; md != NULL; md = md -> next) + ifaceFilesAreUsedByMethod(pt, cd, md); + } + } + + /* + * In case there are any global functions that need external interface + * files. + */ + for (od = pt -> overs; od != NULL; od = od -> next) + if (od->common->module == pt->module) + ifaceFilesAreUsedFromOther(pt, &od->pysig); + + /* + * Remove redundant virtual handlers. It's important that earlier, + * ie. those at the deepest level of %Import, are done first. + */ + nr = 0; + + for (mld = pt->module->allimports; mld != NULL; mld = mld->next) + { + mld->module->modulenr = nr++; + filterVirtualHandlers(mld->module); + } + + pt->module->modulenr = nr; + filterVirtualHandlers(pt->module); + + /* + * Make sure we have the interface files for all types from other modules + * that are used in virtual handlers implemented in this module. + */ + for (vhd = pt->module->virthandlers; vhd != NULL; vhd = vhd->next) + if (!isDuplicateVH(vhd)) + ifaceFilesAreUsedFromOther(pt, vhd->cppsig); + + /* Update proxies with some information from the real classes. */ + for (cd = pt->proxies; cd != NULL; cd = cd->next) + cd->classnr = cd->real->classnr; +} + + +/* + * Set the list of all imports for a module. The list is ordered so that a + * module appears before any module that imports it. + */ +static void setAllImports(sipSpec *pt, moduleDef *mod) +{ + moduleListDef *mld; + + /* + * Handle the trivial case where there are no imports, or the list has + * already been done. + */ + if (mod->imports == NULL || mod->allimports != NULL) + return; + + /* Make sure all the direct imports are done first. */ + for (mld = mod->imports; mld != NULL; mld = mld->next) + setAllImports(pt, mld->module); + + /* + * Now build the list from our direct imports lists but ignoring + * duplicates. + */ + for (mld = mod->imports; mld != NULL; mld = mld->next) + { + moduleListDef *amld; + + for (amld = mld->module->allimports; amld != NULL; amld = amld->next) + addUniqueModule(mod, amld->module); + + addUniqueModule(mod, mld->module); + } +} + + +/* + * Append a module to the list of all imported modules if it isn't already + * there. + */ +static void addUniqueModule(moduleDef *mod, moduleDef *imp) +{ + moduleListDef **tail; + + for (tail = &mod->allimports; *tail != NULL; tail = &(*tail)->next) + if ((*tail)->module == imp) + return; + + *tail = sipMalloc(sizeof (moduleListDef)); + + (*tail)->module = imp; + (*tail)->next = NULL; +} + + +/* + * Move any class casts to its correct class, or publish as a ctor extender. + */ +static void moveClassCasts(sipSpec *pt, classDef *cd) +{ + argList *al; + + for (al = cd->casts; al != NULL; al = al->next) + { + classDef *dcd = al->arg.u.cd; + ctorDef *ct, **ctp; + argDef *ad; + + /* + * If the destination class is in a different module then use + * a proxy. + */ + if (dcd->iff->module != pt->module) + dcd = getProxy(pt, dcd); + + /* Create the new ctor. */ + ct = sipMalloc(sizeof (ctorDef)); + + ct->ctorflags = SECT_IS_PUBLIC | CTOR_CAST; + ct->cppsig = &ct->pysig; + ct->exceptions = NULL; + ct->methodcode = NULL; + ct->prehook = NULL; + ct->posthook = NULL; + ct->next = NULL; + + /* Add the source class as the only argument. */ + ad = &ct->pysig.args[0]; + + ad->atype = class_type; + ad->name = NULL; + ad->argflags = ARG_IN | (al->arg.argflags & (ARG_IS_REF | ARG_IS_CONST)); + ad->nrderefs = al->arg.nrderefs; + ad->defval = NULL; + ad->u.cd = cd; + + ifaceFileIsUsed(pt, dcd->iff, ad); + + ct->pysig.nrArgs = 1; + + /* Append it to the list. */ + for (ctp = &dcd->ctors; *ctp != NULL; ctp = &(*ctp)->next) + if (sameSignature(&(*ctp)->pysig, &ct->pysig, FALSE)) + { + fatal("operator "); + fatalScopedName(classFQCName(dcd)); + fatal("::"); + fatalScopedName(classFQCName(dcd)); + fatal("("); + fatalScopedName(classFQCName(cd)); + fatal(") already defined\n"); + } + + *ctp = ct; + } +} + + +/* + * If possible, move a global slot to its correct class. + */ +static void moveGlobalSlot(sipSpec *pt, memberDef *gmd) +{ + overDef **odp = &pt->overs, *od; + + while ((od = *odp) != NULL) + { + int second; + argDef *arg0, *arg1; + memberDef *md, **mdhead; + overDef **odhead; + moduleDef *mod; + nameDef *nd; + + if (od->common != gmd) + { + odp = &od->next; + continue; + } + + /* + * We know that the slot has the right number of arguments, but the + * first or second one needs to be a class or enum defined in the same + * module. Otherwise we leave it as it is and publish it as a slot + * extender. + */ + arg0 = &od->pysig.args[0]; + arg1 = &od->pysig.args[1]; + + second = FALSE; + nd = NULL; + + if (arg0->atype == class_type) + { + mdhead = &arg0->u.cd->members; + odhead = &arg0->u.cd->overs; + mod = arg0->u.cd->iff->module; + } + else if (arg0->atype == enum_type) + { + mdhead = &arg0->u.ed->slots; + odhead = &arg0->u.ed->overs; + mod = arg0->u.ed->module; + nd = arg0->u.ed->pyname; + } + else if (arg1->atype == class_type) + { + mdhead = &arg1->u.cd->members; + odhead = &arg1->u.cd->overs; + mod = arg1->u.cd->iff->module; + second = TRUE; + } + else if (arg1->atype == enum_type) + { + mdhead = &arg1->u.ed->slots; + odhead = &arg1->u.ed->overs; + mod = arg1->u.ed->module; + nd = arg1->u.ed->pyname; + second = TRUE; + } + else + { + fatal("One of the arguments of "); + prOverloadName(stderr, od); + fatal(" must be a class or enum\n"); + } + + /* + * For rich comparisons the first argument must be a class or + * an enum. For cross-module slots then it may only be a + * class. (This latter limitation is artificial, but is + * unlikely to be a problem in practice.) + */ + if (isRichCompareSlot(gmd)) + { + if (second) + { + fatal("The first argument of "); + prOverloadName(stderr, od); + fatal(" must be a class or enum\n"); + } + + if (mod != gmd->module && arg0->atype == enum_type) + { + fatal("The first argument of "); + prOverloadName(stderr, od); + fatal(" must be a class\n"); + } + } + + if (mod != gmd->module) + { + if (isRichCompareSlot(gmd)) + { + classDef *pcd = getProxy(pt, arg0->u.cd); + memberDef *pmd; + overDef *pod; + + /* Create a new proxy member if needed. */ + for (pmd = pcd->members; pmd != NULL; pmd = pmd->next) + if (pmd->slot == gmd->slot) + break; + + if (pmd == NULL) + { + pmd = sipMalloc(sizeof (memberDef)); + + pmd->pyname = gmd->pyname; + pmd->memberflags = 0; + pmd->slot = gmd->slot; + pmd->module = mod; + pmd->next = pcd->members; + + pcd->members = pmd; + } + + /* Add the proxy overload. */ + pod = sipMalloc(sizeof (overDef)); + + *pod = *od; + pod->common = pmd; + pod->next = pcd->overs; + + pcd->overs = pod; + + /* Remove the first argument. */ + pod->pysig.args[0] = pod->pysig.args[1]; + pod->pysig.nrArgs = 1; + + /* Remove from the list. */ + *odp = od->next; + } + else + odp = &od->next; + + continue; + } + + /* Remove from the list. */ + *odp = od->next; + + /* + * The only time we need the name of an enum is when it has + * slots. + */ + if (nd != NULL) + setIsUsedName(nd); + + /* See if there is already a member or create a new one. */ + for (md = *mdhead; md != NULL; md = md->next) + if (md->slot == gmd->slot) + break; + + if (md == NULL) + { + md = sipMalloc(sizeof (memberDef)); + + *md = *gmd; + + md->module = mod; + md->next = *mdhead; + + *mdhead = md; + } + + /* Move the overload. */ + setIsPublic(od); + od->common = md; + od->next = *odhead; + + *odhead = od; + + /* Remove the first argument of comparison operators. */ + if (isRichCompareSlot(md)) + { + /* Remember if the argument was a pointer. */ + if (arg0->nrderefs > 0) + setDontDerefSelf(od); + + *arg0 = *arg1; + od->pysig.nrArgs = 1; + } + } +} + + +/* + * Create a proxy for a class if it doesn't already exist. Proxies are used as + * containers for cross-module extenders. + */ +static classDef *getProxy(sipSpec *pt, classDef *cd) +{ + classDef *pcd; + + for (pcd = pt->proxies; pcd != NULL; pcd = pcd->next) + if (pcd->iff == cd->iff) + return pcd; + + pcd = sipMalloc(sizeof (classDef)); + + pcd->classflags = 0; + pcd->userflags = 0; + pcd->classnr = -1; + pcd->pyname = cd->pyname; + pcd->iff = cd->iff; + pcd->ecd = cd->ecd; + pcd->real = cd; + pcd->node = NULL; + pcd->supers = cd->supers; + pcd->mro = cd->mro; + pcd->td = NULL; + pcd->ctors = NULL; + pcd->defctor = NULL; + pcd->dealloccode = NULL; + pcd->dtorcode = NULL; + pcd->dtorexceptions = NULL; + pcd->members = NULL; + pcd->overs = NULL; + pcd->casts = NULL; + pcd->vmembers = NULL; + pcd->visible = NULL; + pcd->cppcode = NULL; + pcd->hdrcode = NULL; + pcd->convtosubcode = NULL; + pcd->subbase = NULL; + pcd->convtocode = NULL; + pcd->travcode = NULL; + pcd->clearcode = NULL; + pcd->readbufcode = NULL; + pcd->writebufcode = NULL; + pcd->segcountcode = NULL; + pcd->charbufcode = NULL; + pcd->next = pt->proxies; + + pt->proxies = pcd; + + return pcd; +} + + +/* + * Go through the virtual handlers filtering those that can duplicate earlier + * ones. Make sure each virtual is numbered within its module, and according + * to their position in the list (ignoring duplicates). + */ +static void filterVirtualHandlers(moduleDef *mod) +{ + virtHandlerDef *vhd; + + for (vhd = mod->virthandlers; vhd != NULL; vhd = vhd->next) + { + virtHandlerDef *best, *best_thismod, *hd; + + best = best_thismod = NULL; + + /* + * If this has handwritten code then we will want to use it. + * Otherwise, look for a handler in earlier modules. + */ + if (vhd->virtcode == NULL) + { + moduleListDef *mld; + + for (mld = mod->allimports; mld != NULL && mld->module != mod; mld = mld->next) + { + for (hd = mld->module->virthandlers; hd != NULL; hd = hd->next) + if (sameVirtualHandler(vhd, hd)) + { + best = hd; + break; + } + + /* + * No need to check later modules as this will either be the + * right one, or a duplicate of the right one. + */ + if (best != NULL) + break; + } + } + + /* + * Find the best candidate in this module in case we want to give it + * our handwritten code. + */ + for (hd = mod->virthandlers; hd != vhd; hd = hd->next) + if (sameVirtualHandler(vhd, hd)) + { + best_thismod = hd; + break; + } + + /* + * We don't use this one if it doesn't have virtual code and there is + * an alternative, or if it does have virtual code and there is already + * an alternative in the same module which doesn't have virtual code. + */ + if ((vhd->virtcode == NULL && (best != NULL || best_thismod != NULL)) || + (vhd->virtcode != NULL && best_thismod != NULL && best_thismod->virtcode == NULL)) + { + virtHandlerDef *saved; + + /* + * If the alternative is in the same module and we have virtual + * code then give it to the alternative. Note that there is a bug + * here. If there are three handlers, the first without code and + * the second and third with code then which code is transfered to + * the first is down to luck. We should really only transfer code + * to methods that are known to be re-implementations - just having + * the same signature isn't enough. + */ + if (best_thismod != NULL) + { + if (best_thismod->virtcode == NULL && vhd->virtcode != NULL) + { + best_thismod->virtcode = vhd->virtcode; + resetIsDuplicateVH(best_thismod); + } + + best = best_thismod; + } + + /* Use the better one in place of this one. */ + saved = vhd->next; + *vhd = *best; + setIsDuplicateVH(vhd); + vhd->next = saved; + } + else + vhd->virthandlernr = mod->nrvirthandlers++; + } +} + + +/* + * Add an overload that is automatically generated (typically by Qt's moc). + */ +static void addAutoOverload(sipSpec *pt,classDef *autocd,overDef *autood) +{ + classDef *cd; + + /* Find every class that has this one in its hierarchy. */ + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + mroDef *mro; + + if (cd == autocd) + continue; + + for (mro = cd -> mro; mro != NULL; mro = mro -> next) + if (mro -> cd == autocd) + { + memberDef *md; + overDef *od; + + /* Another overload may already exist. */ + + for (md = cd -> members; md != NULL; md = md -> next) + if (md -> pyname == autood -> common -> pyname) + break; + + if (md == NULL) + { + md = sipMalloc(sizeof (memberDef)); + + md -> pyname = autood -> common -> pyname; + md -> memberflags = autood -> common -> memberflags; + md -> slot = autood -> common -> slot; + md -> module = cd -> iff -> module; + md -> next = cd -> members; + cd -> members = md; + } + + od = sipMalloc(sizeof (overDef)); + + *od = *autood; + od -> common = md; + od -> next = cd -> overs; + cd -> overs = od; + + resetIsAutoGen(od); + + if (cd -> iff -> module == pt -> module) + setIsUsedName(md -> pyname); + + break; + } + } +} + + +/* + * Set the complete hierarchy for a class. + */ +static void setHierarchy(sipSpec *pt,classDef *base,classDef *cd, + classList **head) +{ + mroDef **tailp = &cd -> mro; + + /* See if it has already been done. */ + if (cd -> mro != NULL) + return; + + if (cd -> ecd != NULL) + setHierarchy(pt,base,cd -> ecd,head); + + if (cd -> iff -> type == class_iface) + { + classList *cl; + + /* The first thing is itself. */ + appendToMRO(cd -> mro,&tailp,cd); + + if (cd -> convtosubcode != NULL) + cd -> subbase = cd; + + /* Now do it's superclasses. */ + for (cl = cd -> supers; cl != NULL; cl = cl -> next) + { + mroDef *mro; + + /* + * Make sure the super-class's hierarchy has been done. + */ + setHierarchy(pt,base,cl -> cd,head); + + /* Append the super-classes hierarchy. */ + for (mro = cl -> cd -> mro; mro != NULL; mro = mro -> next) + { + appendToMRO(cd -> mro,&tailp,mro -> cd); + + /* + * If the super-class is a QObject sub-class then this one is + * as well. + */ + if (isQObjectSubClass(mro->cd)) + setIsQObjectSubClass(cd); + + /* + * If the super-class has a shadow then this one should have + * one as well. + */ + if (hasShadow(mro->cd)) + setHasShadow(cd); + + /* + * Ensure that the sub-class base class is the furthest up the + * hierarchy. + */ + if (mro->cd->subbase != NULL) + cd->subbase = mro->cd->subbase; + } + } + } + + /* + * We can't have a shadow if the specification is incomplete, there is + * a private dtor, there are no none-private ctors or there are private + * abstract methods. + */ + if (isIncomplete(cd) || isPrivateDtor(cd) || !canCreate(cd)) + resetHasShadow(cd); + else + { + overDef *od; + + /* + * Note that we should be able to provide better support for + * abstract private methods than we do at the moment. + */ + for (od = cd->overs; od != NULL; od = od->next) + if (isAbstract(od) && isPrivate(od)) + { + resetHasShadow(cd); + + /* + * It also means we cannot create an instance + * from Python. + */ + resetCanCreate(cd); + + break; + } + } + + /* Add it to the new list. */ + appendToClassList(head,cd); +} + + +/* + * Append a class definition to an mro list + */ +static void appendToMRO(mroDef *head,mroDef ***tailp,classDef *cd) +{ + mroDef *mro, *new; + + new = sipMalloc(sizeof (mroDef)); + + new -> cd = cd; + new -> mroflags = 0; + new -> next = NULL; + + /* See if it is a duplicate. */ + + for (mro = head; mro != NULL; mro = mro -> next) + if (mro -> cd == cd) + { + setIsDuplicateSuper(new); + + if (!isDuplicateSuper(mro)) + setHasDuplicateSuper(mro); + + break; + } + + /* Append to the list and update the tail pointer. */ + **tailp = new; + *tailp = &new -> next; +} + + +/* + * Get the base types for all typedefs. + */ +static void transformTypedefs(sipSpec *pt) +{ + typedefDef *td; + + for (td = pt -> typedefs; td != NULL; td = td -> next) + getBaseType(pt, td->module, td -> ecd, &td -> type); +} + + +/* + * Transform the data types for mapped types based on a template. + */ +static void transformMappedTypes(sipSpec *pt) +{ + mappedTypeDef *mt; + + for (mt = pt -> mappedtypes; mt != NULL; mt = mt -> next) + { + /* Nothing to do if this isn't template based. */ + + if (mt -> type.atype == template_type) + resolveMappedTypeTypes(pt,mt); + } +} + + +/* + * Transform the data types for a list of ctors. + */ +static void transformCtors(sipSpec *pt, classDef *cd) +{ + ctorDef *ct; + + for (ct = cd->ctors; ct != NULL; ct = ct->next) + { + ctorDef *prev; + + resolveCtorTypes(pt, cd, ct); + + /* + * Now check that the Python signature doesn't conflict with an + * earlier one. + */ + for (prev = cd->ctors; prev != ct; prev = prev->next) + if (samePythonSignature(&prev->pysig, &ct->pysig)) + { + fatalScopedName(classFQCName(cd)); + fatal(" has ctors with the same Python signature\n"); + } + } +} + + +/* + * Transform the data type for a list of casts. + */ +static void transformCasts(sipSpec *pt, classDef *cd) +{ + argList *al; + + for (al = cd->casts; al != NULL; al = al->next) + { + getBaseType(pt, cd->iff->module, cd, &al->arg); + + if (al->arg.atype != class_type) + { + fatalScopedName(classFQCName(cd)); + fatal(" operator cast must be to a class\n"); + } + } +} + + +/* + * Add a default copy ctor is required. + */ +static void addDefaultCopyCtor(classDef *cd) +{ + ctorDef *copyct; + mroDef *mro; + + /* See if there is a private copy ctor in the hierarchy. */ + + copyct = NULL; + + for (mro = cd -> mro; mro != NULL; mro = mro -> next) + { + ctorDef *ct; + + if (isDuplicateSuper(mro)) + continue; + + for (ct = mro -> cd -> ctors; ct != NULL; ct = ct -> next) + { + argDef *ad = &ct -> pysig.args[0]; + + /* See if is a copy ctor. */ + if (ct -> pysig.nrArgs != 1 || ad -> nrderefs != 0 || + !isReference(ad) || ad -> atype != class_type || + ad -> u.cd != mro -> cd) + continue; + + /* Stop now if the copy ctor is private. */ + if (isPrivateCtor(ct)) + return; + + /* + * Remember if it's in the class we are dealing with. + */ + if (mro == cd -> mro) + copyct = ct; + + break; + } + } + + if (copyct == NULL) + { + ctorDef **tailp; + + /* Create a default public copy ctor. */ + + copyct = sipMalloc(sizeof (ctorDef)); + + copyct -> ctorflags = SECT_IS_PUBLIC; + copyct -> pysig.nrArgs = 1; + copyct -> pysig.args[0].name = "other"; + copyct -> pysig.args[0].atype = class_type; + copyct -> pysig.args[0].u.cd = cd; + copyct -> pysig.args[0].argflags = (ARG_IS_REF | ARG_IS_CONST | ARG_IN); + copyct -> pysig.args[0].nrderefs = 0; + copyct -> pysig.args[0].defval = NULL; + + copyct -> cppsig = ©ct -> pysig; + copyct -> exceptions = NULL; + copyct -> methodcode = NULL; + copyct -> prehook = NULL; + copyct -> posthook = NULL; + copyct -> next = NULL; + + /* Append it to the list. */ + for (tailp = &cd -> ctors; *tailp != NULL; tailp = &(*tailp) -> next) + ; + + *tailp = copyct; + } +} + + +/* + * Transform the data types for a list of overloads. + */ +static void transformOverloads(sipSpec *pt, classDef *scope, overDef *overs) +{ + overDef *od; + + for (od = overs; od != NULL; od = od -> next) + { + overDef *prev; + + resolveFuncTypes(pt, od->common->module, scope, od); + + /* + * Now check that the Python signature doesn't conflict with an + * earlier one. + */ + for (prev = overs; prev != od; prev = prev->next) + { + if (prev->common != od->common) + continue; + + if (samePythonSignature(&prev->pysig, &od->pysig)) + { + if (scope != NULL) + { + fatalScopedName(classFQCName(scope)); + fatal("::"); + } + + fatal("%s() has overloaded functions with the same Python signature\n", od->common->pyname->text); + } + } + } +} + + +/* + * Transform the data types for the variables. + */ +static void transformVariableList(sipSpec *pt) +{ + varDef *vd; + + for (vd = pt -> vars; vd != NULL; vd = vd -> next) + resolveVariableType(pt,vd); +} + + +/* + * Set the list of visible member functions for a class. + */ +static void getVisibleMembers(sipSpec *pt,classDef *cd) +{ + mroDef *mro; + + cd -> visible = NULL; + + for (mro = cd -> mro; mro != NULL; mro = mro -> next) + { + memberDef *md; + classDef *mrocd; + + if (isDuplicateSuper(mro)) + continue; + + mrocd = mro -> cd; + + /* + * If the base class is in the main module, see if it needs to + * publish any protected enums. + */ + if (cd -> iff -> module == pt -> module) + { + enumDef *ed; + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + { + /* Skip unless we are the publisher. */ + if (ed -> pcd != mrocd) + continue; + + /* + * If we are not in the main module then the + * base class must take over as the publisher. + */ + if (mrocd -> iff -> module != pt -> module) + ed -> pcd = cd; + } + } + + for (md = mrocd -> members; md != NULL; md = md -> next) + { + visibleList *vl; + + /* + * See if it is already in the list. This has the desired side + * effect of eliminating any functions that have an implementation + * closer to this class in the hierarchy. This is the only reason + * to define private functions. + */ + for (vl = cd->visible; vl != NULL; vl = vl->next) + if (vl->m->pyname == md->pyname) + break; + + /* See if it is a new member function. */ + + if (vl == NULL) + { + overDef *od; + + vl = sipMalloc(sizeof (visibleList)); + + vl -> m = md; + vl -> cd = mrocd; + vl -> next = cd -> visible; + + addToUsedList(&cd->iff->used, mrocd->iff); + + cd -> visible = vl; + + for (od = mrocd -> overs; od != NULL; od = od -> next) + if (od -> common == md) + { + if (isAbstract(od)) + setIsAbstractClass(cd); + + ifaceFilesAreUsed(pt, cd->iff, od); + + /* See if we need the name. */ + if (cd->iff->module != pt->module) + continue; + + if (isProtected(od) || (isSignal(od) && !optNoEmitters(pt))) + setIsUsedName(md->pyname); + } + } + } + } +} + + +/* + * Get all the virtuals for a particular class. + */ +static void getVirtuals(sipSpec *pt,classDef *cd) +{ + mroDef *mro; + virtOverDef *vod; + + for (mro = cd -> mro; mro != NULL; mro = mro -> next) + { + if (isDuplicateSuper(mro)) + continue; + + getClassVirtuals(cd,mro -> cd); + } + + /* + * Identify any re-implementations of virtuals. We have to do this for + * all classes, not just those in the main module. + */ + for (vod = cd -> vmembers; vod != NULL; vod = vod -> next) + { + overDef *od; + + for (od = cd->overs; od != NULL; od = od->next) + { + if (isVirtual(od)) + continue; + + if (strcmp(vod->o.cppname, od->cppname) == 0 && sameOverload(&vod->o, od)) + { + setIsVirtualReimp(od); + break; + } + } + + /* + * If this class is defined in the main module make sure we get + * the API files for all the visible virtuals. + */ + if (cd->iff->module == pt->module) + { + /* Make sure we get the name. */ + setIsUsedName(vod -> o.common -> pyname); + + ifaceFilesAreUsed(pt, cd->iff, &vod -> o); + } + } +} + + +/* + * Get the list of visible virtual functions for a class. + */ +static void getClassVirtuals(classDef *base,classDef *cd) +{ + overDef *od; + + for (od = cd -> overs; od != NULL; od = od -> next) + { + virtOverDef **tailp, *vod; + + if (!isVirtual(od) || isPrivate(od)) + continue; + + /* + * See if a virtual of this name and signature is already in + * the list. + */ + for (tailp = &base -> vmembers; (vod = *tailp) != NULL; tailp = &vod -> next) + if (strcmp(vod -> o.cppname,od -> cppname) == 0 && sameOverload(&vod -> o,od)) + break; + + if (vod == NULL) + { + /* + * See if there is a non-virtual reimplementation + * nearer in the class hierarchy. + */ + + mroDef *mro; + classDef *scope = NULL; + overDef *eod; + + for (mro = base -> mro; mro -> cd != cd; mro = mro -> next) + { + if (isDuplicateSuper(mro)) + continue; + + /* + * Ignore classes that are on a different + * branch of the class hierarchy. + */ + if (!isSubClass(mro -> cd,cd)) + continue; + + for (eod = mro -> cd -> overs; eod != NULL; eod = eod -> next) + if (strcmp(eod -> cppname,od -> cppname) == 0 && sameSignature(eod -> cppsig,od -> cppsig,TRUE) && isConst(eod) == isConst(od) && !isAbstract(eod)) + { + scope = mro -> cd; + break; + } + + if (scope != NULL) + break; + } + + vod = sipMalloc(sizeof (virtOverDef)); + + vod -> o = *od; + vod -> scope = (scope != NULL ? scope : cd); + vod -> next = NULL; + + *tailp = vod; + + /* + * If there was a nearer reimplementation then we use + * its protection and abstract flags. + */ + if (scope != NULL) + { + vod -> o.overflags &= ~(SECT_MASK | OVER_IS_ABSTRACT); + vod -> o.overflags |= (SECT_MASK | OVER_IS_ABSTRACT) & eod -> overflags; + } + } + } +} + + +/* + * Return TRUE is a class is derived from another. + */ +static int isSubClass(classDef *cc,classDef *pc) +{ + mroDef *mro; + + /* + * In other words, does the parent class appear in the child class's + * MRO list. + */ + for (mro = cc -> mro; mro != NULL; mro = mro -> next) + if (mro -> cd == pc) + return TRUE; + + return FALSE; +} + + +/* + * Resolve the types of a mapped type based on a template. + */ +static void resolveMappedTypeTypes(sipSpec *pt,mappedTypeDef *mt) +{ + int a; + templateDef *td = mt -> type.u.td; + + for (a = 0; a < td -> types.nrArgs; ++a) + { + getBaseType(pt, mt->iff->module, NULL, &td->types.args[a]); + + ifaceFileIsUsed(pt, mt->iff, &td->types.args[a]); + } +} + + +/* + * Resolve the types of a ctor. + */ +static void resolveCtorTypes(sipSpec *pt,classDef *scope,ctorDef *ct) +{ + int a; + + /* Handle any C++ signature. */ + if (ct->cppsig != NULL && ct->cppsig != &ct->pysig) + for (a = 0; a < ct -> cppsig -> nrArgs; ++a) + getBaseType(pt, scope->iff->module, scope, &ct->cppsig->args[a]); + + /* Handle the Python signature. */ + for (a = 0; a < ct -> pysig.nrArgs; ++a) + { + argDef *ad = &ct -> pysig.args[a]; + + getBaseType(pt, scope->iff->module, scope, ad); + + if (!supportedType(scope,NULL,ad,FALSE) && (ct -> cppsig == &ct -> pysig || ct -> methodcode == NULL)) + { + fatalScopedName(classFQCName(scope)); + fatal(" unsupported ctor argument type - provide %%MethodCode and a C++ signature\n"); + } + + ifaceFileIsUsed(pt, scope->iff, ad); + scopeDefaultValue(pt,scope,ad); + } +} + + +/* + * Resolve the types of a function. + */ +static void resolveFuncTypes(sipSpec *pt, moduleDef *mod, classDef *scope, overDef *od) +{ + argDef *res; + + /* Handle any C++ signature. */ + if (od -> cppsig != &od -> pysig) + { + int a; + + getBaseType(pt,mod, scope, &od->cppsig->result); + + for (a = 0; a < od -> cppsig -> nrArgs; ++a) + getBaseType(pt, mod, scope, &od->cppsig->args[a]); + } + + /* Handle the Python signature. */ + resolvePySigTypes(pt, mod, scope, od, &od->pysig,isSignal(od)); + + /* These slots must return int. */ + res = &od -> pysig.result; + + if (isIntReturnSlot(od->common)) + if (res -> atype != int_type || res -> nrderefs != 0 || + isReference(res) || isConstArg(res)) + fatal("%s slots must return int\n",od -> common -> pyname -> text); + + /* These slots must return void. */ + if (isVoidReturnSlot(od -> common)) + if (res -> atype != void_type || res -> nrderefs != 0 || + isReference(res) || isConstArg(res)) + fatal("%s slots must return void\n",od -> common -> pyname -> text); + + /* These slots must return long. */ + if (isLongReturnSlot(od->common)) + if (res->atype != long_type || res->nrderefs != 0 || + isReference(res) || isConstArg(res)) + fatal("%s slots must return long\n", od->common->pyname->text); +} + + +/* + * Resolve the types of a Python signature. + */ +static void resolvePySigTypes(sipSpec *pt, moduleDef *mod, classDef *scope, + overDef *od, signatureDef *pysig, int issignal) +{ + int a; + argDef *res = &pysig -> result; + + if (res -> atype != void_type || res -> nrderefs != 0) + { + if (issignal) + { + if (scope != NULL) + { + fatalScopedName(classFQCName(scope)); + fatal("::"); + } + + fatal("%s() signals must return void\n",od -> cppname); + } + + getBaseType(pt, mod, scope, res); + + /* Results must be simple. */ + if (!supportedType(scope,od,res,FALSE) && (od -> cppsig == &od -> pysig || od -> methodcode == NULL)) + { + if (scope != NULL) + { + fatalScopedName(classFQCName(scope)); + fatal("::"); + } + + fatal("%s() unsupported function return type - provide %%MethodCode and a %s signature\n",od -> cppname,(pt -> genc ? "C" : "C++")); + } + } + + for (a = 0; a < pysig -> nrArgs; ++a) + { + argDef *ad = &pysig -> args[a]; + + getBaseType(pt, mod, scope, ad); + + if (ad -> atype == slotcon_type) + resolvePySigTypes(pt, mod, scope, od, ad->u.sa, TRUE); + + /* + * Note signal arguments are restricted in their types because we don't + * (yet) support handwritten code for them. + */ + if (issignal) + { + if (!supportedType(scope,od,ad,FALSE)) + { + if (scope != NULL) + { + fatalScopedName(classFQCName(scope)); + fatal("::"); + } + + fatal("%s() unsupported signal argument type\n"); + } + } + else if (!supportedType(scope,od,ad,TRUE) && (od -> cppsig == &od -> pysig || od -> methodcode == NULL || (isVirtual(od) && od -> virthandler -> virtcode == NULL))) + { + if (scope != NULL) + { + fatalScopedName(classFQCName(scope)); + fatal("::"); + } + + if (isVirtual(od)) + fatal("%s() unsupported function argument type - provide %%Method code, a valid %%VirtualCatcherCode and a valid C++ signature\n",od -> cppname); + + fatal("%s() unsupported function argument type - provide %%Method code and a valid %s signature\n",od -> cppname,(pt -> genc ? "C" : "C++")); + } + + if (scope != NULL) + scopeDefaultValue(pt,scope,ad); + } +} + + +/* + * Resolve the type of a variable. + */ +static void resolveVariableType(sipSpec *pt,varDef *vd) +{ + int bad = TRUE; + argDef *vtype = &vd -> type; + + getBaseType(pt, vd->module, vd->ecd, vtype); + + switch (vtype -> atype) + { + case mapped_type: + case class_type: + /* Class, Class & and Class * are supported. */ + + if (vtype -> nrderefs <= 1) + bad = FALSE; + break; + + case sstring_type: + case ustring_type: + case string_type: + case wstring_type: + /* + * (signed/unsigned) char, (signed/unsigned) char *, wchar_t, wchar_t * + * are supported. + */ + + if (!isReference(vtype) && vtype -> nrderefs <= 1) + bad = FALSE; + break; + + case cfloat_type: + case float_type: + case cdouble_type: + case double_type: + case enum_type: + case bool_type: + case cbool_type: + case ushort_type: + case short_type: + case uint_type: + case cint_type: + case int_type: + case ulong_type: + case long_type: + case ulonglong_type: + case longlong_type: + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + /* These are supported without pointers or references. */ + + if (!isReference(vtype) && vtype -> nrderefs == 0) + bad = FALSE; + break; + + case struct_type: + case void_type: + /* A simple pointer is supported. */ + + if (!isReference(vtype) && vtype -> nrderefs == 1) + bad = FALSE; + break; + } + + if (bad) + { + fatalScopedName(vd -> fqcname); + fatal(" has an unsupported type\n"); + } + + if (vtype -> atype != class_type && vd -> accessfunc != NULL) + { + fatalScopedName(vd -> fqcname); + fatal(" has %%AccessCode but isn't a class instance\n"); + } + + if (vd -> ecd != NULL) + ifaceFileIsUsed(pt, vd->ecd->iff, vtype); + else + ifaceFileIsUsed(pt, NULL, vtype); + + /* + * Instance variables or static class variables (unless they are + * constants) need a handler. + */ + if (vd -> ecd != NULL && vd -> accessfunc == NULL && + (!isStaticVar(vd) || vtype -> nrderefs != 0 || !isConstArg(vtype))) + { + setNeedsHandler(vd); + setHasVarHandlers(vd -> ecd); + } +} + + +/* + * See if a type is supported by the generated code. + */ +static int supportedType(classDef *cd,overDef *od,argDef *ad,int outputs) +{ + switch (ad -> atype) + { + case anyslot_type: + /* + * This must be an input, and must also have handwritten code. + */ + + ensureInput(cd,od,ad); + return FALSE; + + case signal_type: + case slot_type: + case rxcon_type: + case rxdis_type: + case slotcon_type: + case slotdis_type: + case qobject_type: + case ellipsis_type: + /* These can only appear in argument lists without * or &. */ + + ensureInput(cd,od,ad); + return TRUE; + + case sstring_type: + case ustring_type: + case string_type: + case wstring_type: + if (isReference(ad)) + { + if (outputs && ad -> nrderefs <= 1) + { + defaultOutput(cd,od,ad); + return TRUE; + } + } + else if (ad -> nrderefs == 0) + { + ensureInput(cd,od,ad); + return TRUE; + } + else if (ad -> nrderefs == 1) + { + if (outputs) + defaultInput(ad); + else + ensureInput(cd,od,ad); + + return TRUE; + } + else if (ad -> nrderefs == 2 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + + break; + + case cfloat_type: + case float_type: + case cdouble_type: + case double_type: + case enum_type: + case bool_type: + case cbool_type: + case ushort_type: + case short_type: + case uint_type: + case cint_type: + case int_type: + case ulong_type: + case long_type: + case ulonglong_type: + case longlong_type: + case pyobject_type: + case pytuple_type: + case pylist_type: + case pydict_type: + case pycallable_type: + case pyslice_type: + case pytype_type: + if (isReference(ad)) + { + if (ad -> nrderefs == 0 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + } + else if (ad -> nrderefs == 0) + { + ensureInput(cd,od,ad); + return TRUE; + } + else if (ad -> nrderefs == 1 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + + break; + + case mapped_type: + case class_type: + if (isReference(ad)) + { + if (ad -> nrderefs == 0) + { + defaultInput(ad); + return TRUE; + } + else if (ad -> nrderefs == 1 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + } + else if (ad -> nrderefs == 0) + { + ensureInput(cd,od,ad); + return TRUE; + } + else if (ad -> nrderefs == 1) + { + if (outputs) + defaultInput(ad); + else + ensureInput(cd,od,ad); + + return TRUE; + } + else if (ad -> nrderefs == 2 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + + break; + + case struct_type: + case void_type: + if (isReference(ad)) + { + if (ad -> nrderefs == 1 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + } + else if (ad -> nrderefs == 1) + { + ensureInput(cd,od,ad); + return TRUE; + } + else if (ad -> nrderefs == 2 && outputs) + { + defaultOutput(cd,od,ad); + return TRUE; + } + + break; + } + + /* Unsupported if we got this far. */ + return FALSE; +} + + +/* + * Ensure the direction of an argument is an input. + */ +static void ensureInput(classDef *cd,overDef *od,argDef *ad) +{ + if (isOutArg(ad)) + { + if (cd != NULL) + { + fatalScopedName(classFQCName(cd)); + fatal("::"); + } + + if (od != NULL) + fatal("%s",od -> cppname); + + fatal("() invalid argument type for /Out/\n"); + } + + setIsInArg(ad); +} + + +/* + * Default the direction of an argument to an input. + */ +static void defaultInput(argDef *ad) +{ + if (!isInArg(ad) && !isOutArg(ad)) + setIsInArg(ad); +} + + +/* + * Default the direction of an argument to an output unless the argument is + * const. + */ +static void defaultOutput(classDef *cd,overDef *od,argDef *ad) +{ + if (isOutArg(ad)) + { + if (isConstArg(ad)) + { + if (cd != NULL) + { + fatalScopedName(classFQCName(cd)); + fatal("::"); + } + + if (od != NULL) + fatal("%s",od -> cppname); + + fatal("() const argument cannot have /Out/ specified\n"); + } + } + else if (!isInArg(ad)) + if (isConstArg(ad)) + setIsInArg(ad); + else + setIsOutArg(ad); +} + + +/* + * Put a scoped name to stderr. + */ +void fatalScopedName(scopedNameDef *snd) +{ + while (snd != NULL) + { + fatal("%s",snd -> name); + + snd = snd -> next; + + if (snd != NULL) + fatal("::"); + } +} + + +/* + * Compare two overloads and return TRUE if they are the same. + */ +static int sameOverload(overDef *od1,overDef *od2) +{ + /* They must both be const, or both not. */ + if (isConst(od1) != isConst(od2)) + return FALSE; + + return sameSignature(&od1 -> pysig,&od2 -> pysig,TRUE); +} + + +/* + * Compare two virtual handlers and return TRUE if they are the same. + */ +static int sameVirtualHandler(virtHandlerDef *vhd1,virtHandlerDef *vhd2) +{ + if (isTransferVH(vhd1) != isTransferVH(vhd2)) + return FALSE; + + if (!sameArgType(&vhd1->pysig->result, &vhd2->pysig->result, TRUE)) + return FALSE; + + if (!sameSignature(vhd1->pysig, vhd2->pysig, TRUE)) + return FALSE; + + if (vhd1->pysig == vhd1->cppsig && vhd2->pysig == vhd2->cppsig) + return TRUE; + + if (!sameArgType(&vhd1->cppsig->result, &vhd2->cppsig->result, TRUE)) + return FALSE; + + return sameSignature(vhd1->cppsig, vhd2->cppsig, TRUE); +} + + +/* + * Compare two signatures and return TRUE if they are the same. + */ +int sameSignature(signatureDef *sd1,signatureDef *sd2,int strict) +{ + int a; + + if (strict) + { + /* The number of arguments must be the same. */ + if (sd1 -> nrArgs != sd2 -> nrArgs) + return FALSE; + } + else + { + int na1, na2; + + /* We only count the compulsory arguments. */ + na1 = 0; + + for (a = 0; a < sd1 -> nrArgs; ++a) + { + if (sd1 -> args[a].defval != NULL) + break; + + ++na1; + } + + na2 = 0; + + for (a = 0; a < sd2 -> nrArgs; ++a) + { + if (sd2 -> args[a].defval != NULL) + break; + + ++na2; + } + + if (na1 != na2) + return FALSE; + } + + /* The arguments must be the same. */ + for (a = 0; a < sd1 -> nrArgs; ++a) + { + if (!strict && sd1 -> args[a].defval != NULL) + break; + + if (!sameArgType(&sd1 -> args[a],&sd2 -> args[a],strict)) + return FALSE; + } + + /* Must be the same if we've got this far. */ + return TRUE; +} + + +#define pyAsString(t) ((t) == ustring_type || (t) == sstring_type || \ + (t) == string_type) +#define pyAsFloat(t) ((t) == cfloat_type || (t) == float_type || \ + (t) == cdouble_type || (t) == double_type) +#define pyAsInt(t) ((t) == cint_type || (t) == bool_type || \ + (t) == short_type || (t) == ushort_type || \ + (t) == int_type || (t) == uint_type) +#define pyAsLong(t) ((t) == long_type || (t) == longlong_type) +#define pyAsULong(t) ((t) == ulong_type || (t) == ulonglong_type) +#define pyAsAuto(t) ((t) == bool_type || \ + (t) == short_type || (t) == ushort_type || \ + (t) == int_type || (t) == uint_type || \ + (t) == float_type || (t) == double_type) + +/* + * Compare two argument types and return TRUE if they are the same. "strict" + * means as C++ would see it, rather than Python. + */ +static int sameArgType(argDef *a1, argDef *a2, int strict) +{ + /* The references must be the same. */ + if (isReference(a1) != isReference(a2) || a1->nrderefs != a2->nrderefs) + return FALSE; + + if (strict) + { + /* The const should be the same. */ + if (isConstArg(a1) != isConstArg(a2)) + return FALSE; + + return sameBaseType(a1,a2); + } + + /* Python will see all these as strings. */ + if (pyAsString(a1->atype) && pyAsString(a2->atype)) + return TRUE; + + /* Python will see all these as floats. */ + if (pyAsFloat(a1->atype) && pyAsFloat(a2->atype)) + return TRUE; + + /* Python will see all these as ints. */ + if (pyAsInt(a1->atype) && pyAsInt(a2->atype)) + return TRUE; + + /* Python will see all these as longs. */ + if (pyAsLong(a1->atype) && pyAsLong(a2->atype)) + return TRUE; + + /* Python will see all these as unsigned longs. */ + if (pyAsULong(a1->atype) && pyAsULong(a2->atype)) + return TRUE; + + /* Python will automatically convert between these. */ + if (pyAsAuto(a1->atype) && pyAsAuto(a2->atype)) + return TRUE; + + /* All the special cases have been handled. */ + return sameBaseType(a1, a2); +} + + +/* + * Compare two basic types and return TRUE if they are the same. + */ + +int sameBaseType(argDef *a1,argDef *a2) +{ + /* The types must be the same. */ + + if (a1 -> atype != a2 ->atype) + return FALSE; + + switch (a1 -> atype) + { + case class_type: + if (a1 -> u.cd != a2 -> u.cd) + return FALSE; + + break; + + case enum_type: + if (a1 -> u.ed != a2 -> u.ed) + return FALSE; + + break; + + case slotcon_type: + case slotdis_type: + if (!sameSignature(a1 -> u.sa,a2 -> u.sa,TRUE)) + return FALSE; + + break; + + case template_type: + { + int a; + templateDef *td1, *td2; + + td1 = a1 -> u.td; + td2 = a2 -> u.td; + + if (!sameScopedName(td1 -> fqname,td2 -> fqname) != 0 || + td1 -> types.nrArgs != td2 -> types.nrArgs) + return FALSE; + + for (a = 0; a < td1 -> types.nrArgs; ++a) + if (!sameBaseType(&td1 -> types.args[a],&td2 -> types.args[a])) + return FALSE; + + break; + } + + case struct_type: + if (!sameScopedName(a1 -> u.sname,a2 -> u.sname) != 0) + return FALSE; + + break; + + case defined_type: + if (!sameScopedName(a1 -> u.snd,a2 -> u.snd)) + return FALSE; + + break; + + case mapped_type: + if (a1 -> u.mtd != a2 -> u.mtd) + return FALSE; + + break; + } + + /* Must be the same if we've got this far. */ + + return TRUE; +} + + +/* + * See if two Python signatures are the same as far as Python is concerned. + */ +static int samePythonSignature(signatureDef *sd1, signatureDef *sd2) +{ + int a1, a2; + + a1 = a2 = -1; + + for (;;) + { + a1 = nextSignificantArg(sd1, a1); + a2 = nextSignificantArg(sd2, a2); + + if (a1 < 0 || a2 < 0) + break; + + if (!sameArgType(&sd1->args[a1], &sd2->args[a2], FALSE)) + return FALSE; + } + + return (a1 < 0 && a2 < 0); + +} + + +/* + * Return the next significant argument from a Python signature (ie. one that + * is not optional or an output only argument. Return -1 if there isn't one. + */ +static int nextSignificantArg(signatureDef *sd, int a) +{ + while (++a < sd->nrArgs) + { + if (sd->args[a].defval != NULL) + break; + + if (isInArg(&sd->args[a])) + return a; + } + + return -1; +} + + +/* + * Return TRUE if two scoped names are the same. + */ + +int sameScopedName(scopedNameDef *snd1,scopedNameDef *snd2) +{ + while (snd1 != NULL && snd2 != NULL && strcmp(snd1 -> name,snd2 -> name) == 0) + { + snd1 = snd1 -> next; + snd2 = snd2 -> next; + } + + return (snd1 == NULL && snd2 == NULL); +} + + +/* + * Add an explicit scope to the default value of an argument if possible. + */ + +static void scopeDefaultValue(sipSpec *pt,classDef *cd,argDef *ad) +{ + valueDef *vd, **tailp, *newvd; + + /* + * We do a quick check to see if we need to do anything. This means + * we can limit the times we need to copy the default value. It needs + * to be copied because it will be shared by class versions that have + * been created on the fly and it may need to be scoped differently for + * each of those versions. + */ + + for (vd = ad -> defval; vd != NULL; vd = vd -> next) + if (vd -> vtype == scoped_value && vd -> u.vscp -> next == NULL) + break; + + if (vd == NULL) + return; + + /* + * It's not certain that we will do anything, but we assume we will and + * start copying. + */ + + newvd = NULL; + tailp = &newvd; + + for (vd = ad -> defval; vd != NULL; vd = vd -> next) + { + mroDef *mro; + scopedNameDef *origname; + valueDef *new; + + /* Make the copy. */ + + new = sipMalloc(sizeof (valueDef)); + + *new = *vd; + *tailp = new; + tailp = &new -> next; + + /* + * Skip this part of the expression if it isn't a named value + * or it already has a scope. + */ + + if (vd -> vtype != scoped_value || vd -> u.vscp -> next != NULL) + continue; + + /* + * Search the class hierarchy for an enum value with the same + * name. If we don't find one, leave it as it is (the compiler + * will find out if this is a problem). + */ + + origname = vd -> u.vscp; + + for (mro = cd -> mro; mro != NULL; mro = mro -> next) + { + enumDef *ed; + + if (isDuplicateSuper(mro)) + continue; + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + { + enumMemberDef *emd; + + if (ed -> ecd != mro -> cd) + continue; + + for (emd = ed -> members; emd != NULL; emd = emd -> next) + if (strcmp(emd -> cname,origname -> name) == 0) + { + scopedNameDef *snd; + + /* + * Take the scope from the + * class that the enum was + * defined in. + */ + + snd = copyScopedName(mro -> cd -> iff -> fqcname); + appendScopedName(&snd,origname); + + new -> u.vscp = snd; + + /* Nothing more to do. */ + + break; + } + + if (emd != NULL) + break; + } + + if (ed != NULL) + break; + } + } + + ad -> defval = newvd; +} + + +/* + * Make sure a type is a base type. + */ +static void getBaseType(sipSpec *pt, moduleDef *mod, classDef *defscope, argDef *type) +{ + /* Loop until we've got to a base type. */ + while (type -> atype == defined_type) + { + scopedNameDef *snd = type -> u.snd; + + type -> atype = no_type; + + if (defscope != NULL) + searchScope(pt,defscope,snd,type); + + if (type -> atype == no_type) + searchMappedTypes(pt,snd,type); + + if (type -> atype == no_type) + searchTypedefs(pt,snd,type); + + if (type -> atype == no_type) + searchEnums(pt,snd,type); + + if (type -> atype == no_type) + searchClasses(pt, mod, snd, type); + + if (type -> atype == no_type) + fatalNoDefinedType(snd); + } + + /* Get the base type of any slot arguments. */ + if (type -> atype == slotcon_type || type -> atype == slotdis_type) + { + int sa; + + for (sa = 0; sa < type -> u.sa -> nrArgs; ++sa) + getBaseType(pt, mod, defscope, &type->u.sa->args[sa]); + } + + /* See if the type refers to an instantiated template. */ + if (type->atype == template_type) + { + classDef *cd; + + for (cd = pt->classes; cd != NULL; cd = cd->next) + if (cd->td != NULL && + sameScopedName(cd->td->fqname, type->u.td->fqname) && + sameSignature(&cd->td->types, &type->u.td->types, TRUE)) + { + type->atype = class_type; + type->u.cd = cd; + + break; + } + } + + /* Replace the base type if it has been mapped. */ + if (type -> atype == struct_type || type -> atype == template_type) + { + searchMappedTypes(pt,NULL,type); + + /* + * If we still have a template then see if we need to + * automatically instantiate it. + */ + if (type->atype == template_type) + { + mappedTypeTmplDef *mtt; + + for (mtt = pt->mappedtypetemplates; mtt != NULL; mtt = mtt->next) + if (sameScopedName(type->u.td->fqname, mtt->mt->type.u.td->fqname) && sameTemplateSignature(&type->u.td->types, &mtt->mt->type.u.td->types, TRUE)) + { + type->u.mtd = instantiateMappedTypeTemplate(pt, mod, mtt, type); + type->atype = mapped_type; + + break; + } + } + } +} + + +/* + * Instantiate a mapped type template and return it. + */ +static mappedTypeDef *instantiateMappedTypeTemplate(sipSpec *pt, moduleDef *mod, mappedTypeTmplDef *mtt, argDef *type) +{ + scopedNameDef *type_names, *type_values; + mappedTypeDef *mtd; + + type_names = type_values = NULL; + appendTypeStrings(type->u.td->fqname, &mtt->mt->type.u.td->types, &type->u.td->types, &mtt->sig, &type_names, &type_values); + + mtd = allocMappedType(type); + + mtd->iff = findIfaceFile(pt, mod, type->u.td->fqname, mappedtype_iface, type); + mtd->iff->module = mod; + + mtd->hdrcode = templateCode(pt, &mtd->iff->used, mtt->mt->hdrcode, type_names, type_values); + mtd->convfromcode = templateCode(pt, &mtd->iff->used, mtt->mt->convfromcode, type_names, type_values); + mtd->convtocode = templateCode(pt, &mtd->iff->used, mtt->mt->convtocode, type_names, type_values); + + mtd->next = pt->mappedtypes; + pt->mappedtypes = mtd; + + if (type_names != NULL) + freeScopedName(type_names); + + if (type_values != NULL) + freeScopedName(type_values); + + return mtd; +} + + +/* + * Search for a name in a scope and return the corresponding type. + */ + +static void searchScope(sipSpec *pt,classDef *scope,scopedNameDef *snd, + argDef *ad) +{ + scopedNameDef *tmpsnd = NULL; + mroDef *mro; + + for (mro = scope -> mro; mro != NULL; mro = mro -> next) + { + if (isDuplicateSuper(mro)) + continue; + + /* Append the name to the scope and see if it exists. */ + + tmpsnd = copyScopedName(classFQCName(mro -> cd)); + appendScopedName(&tmpsnd,copyScopedName(snd)); + + searchMappedTypes(pt,tmpsnd,ad); + + if (ad -> atype != no_type) + break; + + searchTypedefs(pt,tmpsnd,ad); + + if (ad -> atype != no_type) + break; + + searchEnums(pt,tmpsnd,ad); + + if (ad -> atype != no_type) + break; + + searchClasses(pt, mro->cd->iff->module, tmpsnd, ad); + + if (ad -> atype != no_type) + break; + + freeScopedName(tmpsnd); + tmpsnd = NULL; + } + + if (tmpsnd != NULL) + freeScopedName(tmpsnd); +} + + +/* + * Search the mapped types for a name and return the type. + */ + +static void searchMappedTypes(sipSpec *pt,scopedNameDef *snd,argDef *ad) +{ + mappedTypeDef *mtd; + scopedNameDef *oname; + + /* Patch back to defined types so we can use sameBaseType(). */ + if (snd != NULL) + { + oname = ad -> u.snd; + ad -> u.snd = snd; + ad -> atype = defined_type; + } + + for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next) + if (sameBaseType(ad,&mtd -> type)) + { + /* Copy the type. */ + ad -> atype = mapped_type; + ad -> u.mtd = mtd; + + return; + } + + /* Restore because we didn't find anything. */ + if (snd != NULL) + { + ad -> u.snd = oname; + ad -> atype = no_type; + } +} + + +/* + * Search the typedefs for a name and return the type. + */ + +static void searchTypedefs(sipSpec *pt,scopedNameDef *snd,argDef *ad) +{ + typedefDef *td; + + for (td = pt -> typedefs; td != NULL; td = td -> next) + if (sameScopedName(td -> fqname,snd)) + { + /* Copy the type. */ + + ad -> atype = td -> type.atype; + ad -> argflags |= td -> type.argflags; + ad -> nrderefs += td -> type.nrderefs; + ad -> u = td -> type.u; + + break; + } +} + + +/* + * Search the enums for a name and return the type. + */ + +static void searchEnums(sipSpec *pt,scopedNameDef *snd,argDef *ad) +{ + enumDef *ed; + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + { + if (ed -> fqcname == NULL) + continue; + + if (sameScopedName(ed -> fqcname,snd)) + { + ad -> atype = enum_type; + ad -> u.ed = ed; + + break; + } + } +} + + +/* + * Search the classes for one with a particular name and return it as a type. + */ +static void searchClasses(sipSpec *pt, moduleDef *mod, scopedNameDef *cname, argDef *ad) +{ + classDef *cd; + + for (cd = pt -> classes; cd != NULL; cd = cd -> next) + { + /* + * Ignore an external class unless it was declared in the same + * context (ie. module) as the name is being used. + */ + if (isExternal(cd) && cd->iff->module != mod) + continue; + + if (sameScopedName(classFQCName(cd), cname)) + { + ad -> atype = class_type; + ad -> u.cd = cd; + + break; + } + } +} + + +/* + * Print an error message describing an undefined type to stderr and terminate. + */ + +static void fatalNoDefinedType(scopedNameDef *snd) +{ + fatalScopedName(snd); + fatal(" is undefined\n"); +} + + +/* + * Make sure all external interface files for all other functions of a module + * are used. + */ +static void ifaceFilesAreUsedFromOther(sipSpec *pt, signatureDef *sd) +{ + int a; + ifaceFileDef *iff; + + if ((iff = getIfaceFile(&sd->result)) != NULL && iff->module != pt->module) + addToUsedList(&pt->used, iff); + + for (a = 0; a < sd->nrArgs; ++a) + if ((iff = getIfaceFile(&sd->args[a])) != NULL && iff->module != pt->module) + addToUsedList(&pt->used, iff); +} + + +/* + * Make sure all interface files for all overloads of a method are used. + */ +static void ifaceFilesAreUsedByMethod(sipSpec *pt, classDef *cd, memberDef *md) +{ + overDef *od; + + for (od = cd -> overs; od != NULL; od = od -> next) + if (od -> common == md) + ifaceFilesAreUsed(pt, cd->iff, od); +} + + +/* + * Make sure all interface files for a signature are used. + */ +static void ifaceFilesAreUsed(sipSpec *pt, ifaceFileDef *iff, overDef *od) +{ + int a; + + ifaceFileIsUsed(pt, iff, &od->pysig.result); + + for (a = 0; a < od->pysig.nrArgs; ++a) + ifaceFileIsUsed(pt, iff, &od->pysig.args[a]); + + if (od->cppsig != &od->pysig) + { + ifaceFileIsUsed(pt, iff, &od->cppsig->result); + + for (a = 0; a < od->cppsig->nrArgs; ++a) + ifaceFileIsUsed(pt, iff, &od->cppsig->args[a]); + } +} + + +/* + * If a type has an interface file then add it to the appropriate list of used + * interface files so that the header file is #included in the generated code. + */ +static void ifaceFileIsUsed(sipSpec *pt, ifaceFileDef *iff, argDef *ad) +{ + ifaceFileDef *usediff; + + if ((usediff = getIfaceFile(ad)) != NULL && usediff != iff) + { + ifaceFileList *iffl, **used; + + used = (iff != NULL ? &iff->used : &pt->used); + + iffl = addToUsedList(used, usediff); + + /* + * If the type is a protected enum then its scoping shadow + * class is needed in the generated header file. + */ + if (ad->atype == enum_type && isProtectedEnum(ad->u.ed)) + iffl->header = TRUE; + } +} + + +/* + * Return the interface file for a type, or NULL if it doesn't have one. + */ +static ifaceFileDef *getIfaceFile(argDef *ad) +{ + ifaceFileDef *iff; + + switch (ad -> atype) + { + case class_type: + iff = ad -> u.cd -> iff; + break; + + case mapped_type: + iff = ad -> u.mtd -> iff; + break; + + case enum_type: + if (ad -> u.ed -> fqcname != NULL && ad -> u.ed -> ecd != NULL) + { + iff = ad -> u.ed -> ecd -> iff; + break; + } + + /* Drop through. */ + + default: + iff = NULL; + } + + return iff; +} + + +/* + * Position a class so that it is after all its super-classes. + */ +static void positionClass(classDef *cd) +{ + classList *cl; + + /* See if it has already been done. */ + if (cd -> node -> ordered) + return; + + for (cl = cd -> supers; cl != NULL; cl = cl -> next) + { + nodeDef **ndp, *nd1, *nd2, *rp; + + /* Ignore super-classes from different modules. */ + if (cl -> cd -> iff -> module != cd -> iff -> module) + continue; + + /* Make sure the super-class is positioned. */ + positionClass(cl -> cd); + + /* + * Find ancestors of the two that are siblings (ie. they have a + * common parent). + */ + rp = &cd -> iff -> module -> root; + + for (nd1 = cd -> node; nd1 != rp; nd1 = nd1 -> parent) + { + for (nd2 = cl -> cd -> node; nd2 != rp; nd2 = nd2 -> parent) + if (nd1 -> parent == nd2 -> parent) + break; + + if (nd2 != rp) + break; + } + + /* + * The first node must appear after the second in the common + * parent's list of children. + */ + for (ndp = &nd1 -> parent -> child; *ndp != NULL; ndp = &(*ndp) -> next) + { + nodeDef *nd = *ndp; + + if (nd == nd2) + break; + + if (nd == nd1) + { + /* Remove this one from the list. */ + *ndp = nd -> next; + + /* Find the super-class ancestor. */ + while (*ndp != nd2) + ndp = &(*ndp) -> next; + + /* + * Put this one back after the super-class + * ancestor. + */ + nd -> next = (*ndp) -> next; + (*ndp) -> next = nd; + + break; + } + } + } + + cd -> node -> ordered = TRUE; +} + + +/* + * Make sure a class is in the namespace tree. + */ +static void addNodeToParent(nodeDef *root,classDef *cd) +{ + nodeDef *nd, *parent; + + /* Skip classes already in the tree. */ + if (cd -> node != NULL) + return; + + /* Add this child to the parent. */ + nd = sipMalloc(sizeof (nodeDef)); + + nd -> ordered = FALSE; + nd -> cd = cd; + nd -> child = NULL; + + /* Get the address of the parent node. */ + if (cd -> ecd == NULL) + parent = root; + else + { + /* Make sure the parent is in the tree. */ + addNodeToParent(root,cd -> ecd); + + parent = cd -> ecd -> node; + } + + nd -> parent = parent; + + /* Insert this at the head of the parent's children. */ + nd -> next = parent -> child; + parent -> child = nd; + + /* Remember where we are in the tree. */ + cd -> node = nd; +} + + +/* + * Assign the module specific class number for a class and all it's children. + */ +static void assignClassNrs(sipSpec *pt,moduleDef *mod,nodeDef *nd) +{ + classDef *cd; + nodeDef *cnd; + + /* Assign the class if it's not the root. */ + if ((cd = nd -> cd) != NULL) + { + cd -> classnr = mod -> nrclasses++; + + /* + * If we find a class defined in the main module called QObject, assume + * it's Qt. + */ + if (mod == pt -> module && strcmp(classBaseName(cd),"QObject") == 0) + pt -> qobjclass = cd -> classnr; + } + + /* Assign all it's children. */ + for (cnd = nd -> child; cnd != NULL; cnd = cnd -> next) + assignClassNrs(pt,mod,cnd); +} + + +/* + * Assign the module specific enum number for all named enums. + */ +static void assignEnumNrs(sipSpec *pt) +{ + enumDef *ed; + + for (ed = pt -> enums; ed != NULL; ed = ed -> next) + if (ed -> fqcname != NULL) + ed -> enumnr = ed -> module -> nrenums++; +} |