Просмотр исходного кода

ncd: move NCDMethodIndex from NCDInterpreter into NCDModuleIndex

ambrop7 13 лет назад
Родитель
Сommit
153681af18

+ 6 - 7
ncd/NCDInterpProcess.c

@@ -161,13 +161,12 @@ fail:
     return 0;
 }
 
-int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
+int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index)
 {
     ASSERT(process)
     ASSERT(string_index)
     ASSERT(pdb)
     ASSERT(module_index)
-    ASSERT(method_index)
     
     NCDBlock *block = NCDProcess_Block(process);
     
@@ -247,9 +246,9 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIn
                 goto loop_fail2;
             }
             
-            e->binding.method_name_id = NCDMethodIndex_GetMethodNameId(method_index, NCDStatement_RegCmdName(s));
+            e->binding.method_name_id = NCDModuleIndex_GetMethodNameId(module_index, NCDStatement_RegCmdName(s));
             if (e->binding.method_name_id == -1) {
-                BLog(BLOG_ERROR, "NCDMethodIndex_GetMethodNameId failed");
+                BLog(BLOG_ERROR, "NCDModuleIndex_GetMethodNameId failed");
                 goto loop_fail3;
             }
         } else {
@@ -368,16 +367,16 @@ const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpPro
     return o->stmts[i].binding.simple_module;
 }
 
-const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDMethodIndex *method_index)
+const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDModuleIndex *module_index)
 {
     DebugObject_Access(&o->d_obj);
     ASSERT(i >= 0)
     ASSERT(i < o->num_stmts)
     ASSERT(o->stmts[i].objnames)
     ASSERT(obj_type >= 0)
-    ASSERT(method_index)
+    ASSERT(module_index)
     
-    return NCDMethodIndex_GetMethodModule(method_index, obj_type, o->stmts[i].binding.method_name_id);
+    return NCDModuleIndex_GetMethodModule(module_index, obj_type, o->stmts[i].binding.method_name_id);
 }
 
 int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)

+ 2 - 3
ncd/NCDInterpProcess.h

@@ -40,7 +40,6 @@
 #include <ncd/NCDModule.h>
 #include <ncd/NCDModuleIndex.h>
 #include <ncd/NCDStringIndex.h>
-#include <ncd/NCDMethodIndex.h>
 
 struct NCDInterpProcess__stmt {
     NCD_string_id_t name;
@@ -80,13 +79,13 @@ typedef struct {
     DebugObject d_obj;
 } NCDInterpProcess;
 
-int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index) WARN_UNUSED;
+int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index) WARN_UNUSED;
 void NCDInterpProcess_Free (NCDInterpProcess *o);
 int NCDInterpProcess_FindStatement (NCDInterpProcess *o, int from_index, NCD_string_id_t name);
 const char * NCDInterpProcess_StatementCmdName (NCDInterpProcess *o, int i, NCDStringIndex *string_index);
 void NCDInterpProcess_StatementObjNames (NCDInterpProcess *o, int i, const NCD_string_id_t **out_objnames, size_t *out_num_objnames);
 const struct NCDModule * NCDInterpProcess_StatementGetSimpleModule (NCDInterpProcess *o, int i);
-const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDMethodIndex *method_index);
+const struct NCDModule * NCDInterpProcess_StatementGetMethodModule (NCDInterpProcess *o, int i, NCD_string_id_t obj_type, NCDModuleIndex *module_index);
 int NCDInterpProcess_CopyStatementArgs (NCDInterpProcess *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog) WARN_UNUSED;
 void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int alloc_size);
 int NCDInterpProcess_PreallocSize (NCDInterpProcess *o);

+ 2 - 3
ncd/NCDInterpProg.c

@@ -41,7 +41,7 @@
 #include "NCDInterpProg_hash.h"
 #include <structure/CHash_impl.h>
 
-int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
+int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index)
 {
     ASSERT(prog)
     ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE))
@@ -49,7 +49,6 @@ int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *stri
     ASSERT(string_index)
     ASSERT(pdb)
     ASSERT(module_index)
-    ASSERT(method_index)
     
     if (NCDProgram_NumElems(prog) > INT_MAX) {
         BLog(BLOG_ERROR, "too many processes");
@@ -81,7 +80,7 @@ int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *stri
             goto fail2;
         }
         
-        if (!NCDInterpProcess_Init(&e->iprocess, p, string_index, pdb, module_index, method_index)) {
+        if (!NCDInterpProcess_Init(&e->iprocess, p, string_index, pdb, module_index)) {
             BLog(BLOG_ERROR, "NCDInterpProcess_Init failed");
             goto fail2;
         }

+ 1 - 1
ncd/NCDInterpProg.h

@@ -56,7 +56,7 @@ typedef struct {
     DebugObject d_obj;
 } NCDInterpProg;
 
-int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index) WARN_UNUSED;
+int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index) WARN_UNUSED;
 void NCDInterpProg_Free (NCDInterpProg *o);
 NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, NCD_string_id_t name);
 

+ 4 - 16
ncd/NCDInterpreter.c

@@ -155,21 +155,15 @@ int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterp
         goto fail0;
     }
     
-    // init method index
-    if (!NCDMethodIndex_Init(&o->method_index, &o->string_index)) {
-        BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
-        goto fail1;
-    }
-    
     // init module index
-    if (!NCDModuleIndex_Init(&o->mindex)) {
+    if (!NCDModuleIndex_Init(&o->mindex, &o->string_index)) {
         BLog(BLOG_ERROR, "NCDModuleIndex_Init failed");
         goto fail2;
     }
     
     // add module groups to index and allocate string id's for base_type's
     for (const struct NCDModuleGroup **g = ncd_modules; *g; g++) {
-        if (!NCDModuleIndex_AddGroup(&o->mindex, *g, &o->method_index)) {
+        if (!NCDModuleIndex_AddGroup(&o->mindex, *g)) {
             BLog(BLOG_ERROR, "NCDModuleIndex_AddGroup failed");
             goto fail3;
         }
@@ -191,7 +185,7 @@ int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterp
     }
     
     // init interp program
-    if (!NCDInterpProg_Init(&o->iprogram, &o->program, &o->string_index, &o->placeholder_db, &o->mindex, &o->method_index)) {
+    if (!NCDInterpProg_Init(&o->iprogram, &o->program, &o->string_index, &o->placeholder_db, &o->mindex)) {
         BLog(BLOG_ERROR, "NCDInterpProg_Init failed");
         goto fail5;
     }
@@ -298,9 +292,6 @@ fail3:
     // free module index
     NCDModuleIndex_Free(&o->mindex);
 fail2:
-    // free method index
-    NCDMethodIndex_Free(&o->method_index);
-fail1:
     // free string index
     NCDStringIndex_Free(&o->string_index);
 fail0:
@@ -347,9 +338,6 @@ void NCDInterpreter_Free (NCDInterpreter *o)
     // free module index
     NCDModuleIndex_Free(&o->mindex);
     
-    // free method index
-    NCDMethodIndex_Free(&o->method_index);
-    
     // free string index
     NCDStringIndex_Free(&o->string_index);
 }
@@ -942,7 +930,7 @@ void process_advance (struct process *p)
         method_context = NCDObject_MethodUser(&object);
         
         // find module based on type of object
-        module = NCDInterpProcess_StatementGetMethodModule(p->iprocess, p->ap, object_type, &p->interp->method_index);
+        module = NCDInterpProcess_StatementGetMethodModule(p->iprocess, p->ap, object_type, &p->interp->mindex);
         
         if (!module) {
             const char *type_str = NCDStringIndex_Value(&p->interp->string_index, object_type);

+ 0 - 4
ncd/NCDInterpreter.h

@@ -37,7 +37,6 @@
 #include <system/BTime.h>
 #include <system/BReactor.h>
 #include <ncd/NCDStringIndex.h>
-#include <ncd/NCDMethodIndex.h>
 #include <ncd/NCDModuleIndex.h>
 #include <ncd/NCDAst.h>
 #include <ncd/NCDPlaceholderDb.h>
@@ -98,9 +97,6 @@ typedef struct {
     // string index
     NCDStringIndex string_index;
 
-    // method index
-    NCDMethodIndex method_index;
-
     // module index
     NCDModuleIndex mindex;
 

+ 31 - 4
ncd/NCDModuleIndex.c

@@ -109,8 +109,10 @@ static int add_method (char *type, const struct NCDModule *module, NCDMethodInde
     return 1;
 }
 
-int NCDModuleIndex_Init (NCDModuleIndex *o)
+int NCDModuleIndex_Init (NCDModuleIndex *o, NCDStringIndex *string_index)
 {
+    ASSERT(string_index)
+    
     // allocate modules array
     if (!(o->modules = BAllocArray(NCDMODULEINDEX_MAX_MODULES, sizeof(o->modules[0])))) {
         BLog(BLOG_ERROR, "BAllocArray failed");
@@ -129,9 +131,17 @@ int NCDModuleIndex_Init (NCDModuleIndex *o)
     // init base types tree
     BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
     
+    // init method index
+    if (!NCDMethodIndex_Init(&o->method_index, string_index)) {
+        BLog(BLOG_ERROR, "NCDMethodIndex_Init failed");
+        goto fail2;
+    }
+    
     DebugObject_Init(&o->d_obj);
     return 1;
     
+fail2:
+    NCDModuleIndex__MHash_Free(&o->modules_hash);
 fail1:
     BFree(o->modules);
 fail0:
@@ -149,6 +159,9 @@ void NCDModuleIndex_Free (NCDModuleIndex *o)
         free(bt);
     }
     
+    // free method index
+    NCDMethodIndex_Free(&o->method_index);
+    
     // free modules hash
     NCDModuleIndex__MHash_Free(&o->modules_hash);
     
@@ -156,11 +169,10 @@ void NCDModuleIndex_Free (NCDModuleIndex *o)
     BFree(o->modules);
 }
 
-int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, NCDMethodIndex *method_index)
+int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group)
 {
     DebugObject_Access(&o->d_obj);
     ASSERT(group)
-    ASSERT(method_index)
     
     for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
         if (find_module(o, nm->type)) {
@@ -208,7 +220,7 @@ int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *gro
         
         o->num_modules++;
         
-        if (!add_method(m->type, nm, method_index)) {
+        if (!add_method(m->type, nm, &o->method_index)) {
             BLog(BLOG_ERROR, "failed to add method to method index");
             return 0;
         }
@@ -236,3 +248,18 @@ const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const cha
     
     return m->module;
 }
+
+int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name)
+{
+    DebugObject_Access(&o->d_obj);
+    ASSERT(method_name)
+    
+    return NCDMethodIndex_GetMethodNameId(&o->method_index, method_name);
+}
+
+const struct NCDModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    return NCDMethodIndex_GetMethodModule(&o->method_index, obj_type, method_name_id);
+}

+ 5 - 2
ncd/NCDModuleIndex.h

@@ -65,12 +65,15 @@ typedef struct {
     int num_modules;
     NCDModuleIndex__MHash modules_hash;
     BAVL base_types_tree;
+    NCDMethodIndex method_index;
     DebugObject d_obj;
 } NCDModuleIndex;
 
-int NCDModuleIndex_Init (NCDModuleIndex *o) WARN_UNUSED;
+int NCDModuleIndex_Init (NCDModuleIndex *o, NCDStringIndex *string_index) WARN_UNUSED;
 void NCDModuleIndex_Free (NCDModuleIndex *o);
-int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group, NCDMethodIndex *method_index) WARN_UNUSED;
+int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group) WARN_UNUSED;
 const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type);
+int NCDModuleIndex_GetMethodNameId (NCDModuleIndex *o, const char *method_name);
+const struct NCDModule * NCDModuleIndex_GetMethodModule (NCDModuleIndex *o, NCD_string_id_t obj_type, int method_name_id);
 
 #endif