compiler/passes/normalize.cpp
// Insert an init function into every module
Insert an init function into every module
Insert an init function into every module
forv_Vec(ModuleSymbol, mod, allModules) {
    SET_LINENO(mod);
    mod->initFn          = new FnSymbol(astr("chpl__init_", mod->name));
    mod->initFn->retType = dtVoid;
    mod->initFn->addFlag(FLAG_MODULE_INIT);
    mod->initFn->addFlag(FLAG_INSERT_LINE_FILE_INFO);
     move module-level statements into module's init function
    for_alist(stmt, mod->block->body) {
      if (stmt->isModuleDefinition() == false) {
        if (FnSymbol* deinitFn = toModuleDeinitFn(mod, stmt)) {
          mod->deinitFn = deinitFn;  the rest is in handleModuleDeinitFn()
        } else {
          mod->initFn->insertAtTail(stmt->remove());
        }
      }
    }
    mod->block->insertAtHead(new DefExpr(mod->initFn));
    handleModuleDeinitFn(mod);
     If the module has the EXPORT_INIT flag then
     propagate it to the module's init function
    if (mod->hasFlag(FLAG_EXPORT_INIT) == true ||
        (fLibraryCompile == true && mod->modTag == MOD_USER)) {
      mod->initFn->addFlag(FLAG_EXPORT);
      mod->initFn->addFlag(FLAG_LOCAL_ARGS);
    }
  }
forv_Vec ( ModuleSymbol , mod , allModules ) { SET_LINENO ( mod ) ; mod -> initFn = new FnSymbol ( astr ( " " , mod -> name ) ) ; mod -> initFn -> retType = dtVoid ; mod -> initFn -> addFlag ( FLAG_MODULE_INIT ) ; mod -> initFn -> addFlag ( FLAG_INSERT_LINE_FILE_INFO ) ; for_alist ( stmt , mod -> block -> body ) { if ( stmt -> isModuleDefinition ( ) == false ) { if ( FnSymbol * deinitFn = toModuleDeinitFn ( mod , stmt ) ) { mod -> deinitFn = deinitFn ; } else { mod -> initFn -> insertAtTail ( stmt -> remove ( ) ) ; } } } mod -> block -> insertAtHead ( new DefExpr ( mod -> initFn ) ) ; handleModuleDeinitFn ( mod ) ; if ( mod -> hasFlag ( FLAG_EXPORT_INIT ) == true || ( fLibraryCompile == true && mod -> modTag == MOD_USER ) ) { mod -> initFn -> addFlag ( FLAG_EXPORT ) ; mod -> initFn -> addFlag ( FLAG_LOCAL_ARGS ) ; } }
Insert an init function into every module
Insert an init function into every module
// // move module-level statements into module's init function // // the rest is in handleModuleDeinitFn() // // If the module has the EXPORT_INIT flag then // propagate it to the module's init function //