📄 gencode.c
字号:
"\n" ); if (cd != NULL) { prcode(fp,"static PyObject *meth_%C_%s(PyObject *,PyObject *sipArgs)\n" ,classFQCName(cd),md -> pyname -> text); od = cd -> overs; } else { 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""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) && 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) && 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -