Skip to content

Commit 94e24b0

Browse files
committed
nCompile automated inclusion (optionally) of needed units
1 parent 13a9e5e commit 94e24b0

7 files changed

Lines changed: 247 additions & 80 deletions

File tree

nCompiler/R/NC_Compile.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ nCompile_nClass <- function(NC,
4646
control
4747
)
4848
is_predefined <- !isFALSE(NCinternals(NC)$predefined)
49+
gather_needed_units <- isTRUE(controlFull$always_include_units)
50+
needed_units <- list()
4951
if(is_predefined) {
5052
predefined_dir <- NCinternals(NC)$predefined
5153
# predefined can be character, quoted expression, or function.
@@ -63,6 +65,7 @@ nCompile_nClass <- function(NC,
6365
"It should give the directory path of the predefined nClass. ",
6466
"The classname argument to nClass gives the base for filenames in that directory.")
6567
regular_filename <- NCinternals(NC)$cpp_classname
68+
if(gather_needed_units) needed_units <- NCinternals(NC)$compileInfo$needed_units
6669
}
6770
if(is_predefined && isFALSE(controlFull$generate_predefined)) {
6871
RcppPacket <- loadRcppPacket(predefined_dir, regular_filename)
@@ -89,6 +92,10 @@ nCompile_nClass <- function(NC,
8992
# Now add interface calls if necessary for this live compilation, having
9093
# kept them out of the written packet code.
9194
cppDef$buildGenericInterface(interfaceCalls=TRUE, interface=FALSE)
95+
# To do: check that there aren't any detected needed units that are not in the compileInfo$needed_units
96+
# because for a predefined, needed units must be provided manually by compileInfo.
97+
} else {
98+
if(gather_needed_units) needed_units <- NC_Compiler$gather_needed_units()
9299
}
93100

94101
##
@@ -101,8 +108,10 @@ nCompile_nClass <- function(NC,
101108
return(NC_Compiler)
102109
}
103110

104-
if(stopAfterCppDef) return(cppDef)
105-
111+
if(stopAfterCppDef) {
112+
if(gather_needed_units) return(list(cppDef = cppDef, needed_units = needed_units))
113+
else return(cppDef)
114+
}
106115
# We might deprecate from here onward.
107116
# Then nCompile_nClass would only be called via nCompile
108117
filebase <- controlFull$filename

nCompiler/R/NC_CompilerClass.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,43 @@ NC_CompilerClass <- R6::R6Class(
115115
NCgenerator)
116116
setupMethodSymbolTables()
117117
}
118+
},
119+
gather_needed_units = function() {
120+
needed_nClasses1 <- nCompile_gather_needed_nClasses(cppDef, self$symbolTable)
121+
needed_nClasses2 <- lapply(NFcompilers,
122+
\(x) x$gather_needed_nClasses()) |>
123+
unlist(recursive = FALSE) |> unique()
124+
needed_nFunctions <- lapply(NFcompilers,
125+
\(x) x$gather_needed_nFunctions()) |>
126+
unlist(recursive = FALSE) |> unique()
127+
list(
128+
needed_nClasses = unique(c(needed_nClasses1, needed_nClasses2)),
129+
needed_nFunctions = needed_nFunctions
130+
)
118131
}
119132
)
120133
)
134+
135+
nCompile_gather_needed_nClasses <- function(cppDef,
136+
symTab,
137+
NF_Compiler = NULL) {
138+
# Collect nClass generators needed by this symbol table
139+
new_needed <- list()
140+
for(i in seq_along(symTab$symbols)) {
141+
if(inherits(symTab$symbols[[i]], "symbolNC")) {
142+
new_needed[[length(new_needed) + 1]] <-
143+
symTab$symbols[[i]]$nClassGenerator
144+
}
145+
}
146+
# For an nFunction, collection nClass generators identified
147+
# from processing the code.
148+
if(!is.null(NF_Compiler)) {
149+
auxEnv_needed_nClasses <- NF_Compiler$auxEnv$needed_nClasses
150+
if(length(auxEnv_needed_nClasses)) {
151+
bool_NCgen <- lapply(auxEnv_needed_nClasses, isNCgenerator) |> unlist()
152+
new_needed <- c(new_needed,
153+
auxEnv_needed_nClasses[bool_NCgen])
154+
}
155+
}
156+
unique(new_needed)
157+
}

nCompiler/R/NF_Compile.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ nCompile_nFunction <- function(NF,
5858
if(is.null(compileInfo)) compileInfo <- NFinternals(NF)$compileInfo
5959

6060
is_predefined <- !isFALSE(NFinternals(NF)$predefined)
61+
gather_needed_units <- isTRUE(controlFull$always_include_units)
62+
needed_units <- list()
6163
if(is_predefined) {
6264
predefined_dir <- NFinternals(NF)$predefined
6365
# predefined can be character, quoted expression, or function.
@@ -75,6 +77,7 @@ nCompile_nFunction <- function(NF,
7577
"It should give the directory path of the predefined nFunction. ",
7678
"The name argument to nFunction gives the base for filenames in that directory.")
7779
regular_filename <- NFinternals(NF)$cpp_code_name
80+
if(gather_needed_units) needed_units <- NFinternals(NF)$compileInfo$needed_units
7881
}
7982
if(is_predefined && isFALSE(controlFull$generate_predefined)) {
8083
RcppPacket <- loadRcppPacket(predefined_dir, regular_filename)
@@ -98,6 +101,8 @@ nCompile_nFunction <- function(NF,
98101
predefined_gen_dir <- predefined_dir
99102
RcppPacket <- cppDefs_2_RcppPacket(NF_Compiler$cppDef)
100103
saveRcppPacket(RcppPacket, predefined_dir, regular_filename)
104+
} else {
105+
if(gather_needed_units) needed_units <- NF_Compiler$gather_needed_units()
101106
}
102107
stageName <- 'makeRcppPacket'
103108
if (logging) logBeforeStage(stageName)
@@ -106,8 +111,10 @@ nCompile_nFunction <- function(NF,
106111

107112
cppDef <- NF_Compiler$cppDef
108113
}
109-
if(stopAfterCppDef) return(cppDef)
110-
114+
if(stopAfterCppDef) {
115+
if(gather_needed_units) return(list(needed_units = needed_units, cppDef = cppDef))
116+
else return(cppDef)
117+
}
111118
# We might deprecate from here down and make all usages start from nCompile.
112119

113120
stop("Entering deprecated portion of nCompile_nFunction. Check what is going on.")

nCompiler/R/NF_CompilerClass.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,29 @@ NF_CompilerClass <- R6::R6Class(
139139
doKeywords,
140140
.nCompilerProject,
141141
initialTypeInferenceOnly)
142+
},
143+
gather_needed_units = function() {
144+
list(
145+
needed_nClasses = self$gather_needed_nClasses(),
146+
needed_nFunctions = self$gather_needed_nFunctions()
147+
)
148+
},
149+
gather_needed_nClasses = function() {
150+
nCompile_gather_needed_nClasses(cppDef, self$symbolTable, self)
151+
},
152+
gather_needed_nFunctions = function() {
153+
nCompile_gather_needed_nFunctions(cppDef, self$symbolTable)
142154
}
143155
)
144156
)
145157

158+
nCompile_gather_needed_nFunctions <- function(cppDef,
159+
NF_Compiler) {
160+
lapply(NF_Compiler$auxEnv$needed_nFunctions,
161+
function(x)
162+
nGet(x[[1]], where = x[[2]])) |> unique()
163+
}
164+
146165
processNFstages <- function(NFcompiler,
147166
control = list(),
148167
sourceObj = NULL,

nCompiler/R/cppDefs_nFunction.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ cpp_include_needed_nClasses <- function(cppDef,
199199
}
200200
}
201201
new_Hincludes <- unique(new_Hincludes)
202-
cppDef$Hincludes <- c(cppDef$Hincludes, new_Hincludes)
202+
cppDef$Hincludes <- unique(c(cppDef$Hincludes, new_Hincludes))
203203
invisible(NULL)
204204
}
205205

0 commit comments

Comments
 (0)