computeGlobalConstants: Find the top-level/global variables that are constant

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/constants.R

Description

This function searches the translation unit for non-local or top-level C/C++ variables and determines whether they are constant (i.e. declared with the qualifier const). The function then generates C code which can be run and in turn outputs R code to define R variables with the same values as these C level constants. This works for simple types.

Usage

1
2
3
4
5
computeGlobalConstants(tu = NULL, gvars = getGlobalVariables(tu, files),
                        files = character(),
                         defs = DefinitionContainer(tu),
                           varsOnly = FALSE, symbolic = FALSE,
                             access = c("public", "protected"))

Arguments

tu

the translation unit node/parser, obtained by a call to parseTU

gvars

the global variable nodes. In some cases, these will already have been computed and then one only needs to pass these and not files.

files

a character vector identifying the names of the files mentioned in the TU nodes in which we are interested, i.e. those in which the variables of interest are declared/defined. If gvars is specified, this is not used.

defs

the table of resolved nodes and data types and routines. This is a mutable object of class DefinitionContainer and we typically have just one of these for each translation unit and we reuse it across many of these functions which process the TU nodes further and resolve the different types.

varsOnly

a logical value which, if TRUE merely returns the indices of the elements in gvars which are constant. In general, the caller will want the generated code to calculate these values and so the default of FALSE is appropriate.

symbolic

a logical value indicating whether we want the values of the constants to be returned as values or symbolically in terms of other constant variables, e.g. int x = a + b; as 7 if a = 3 and b = 4 or as the expression a + b

access

a character vector containing any of “public”, “protected” and “private” which is used to filter the constants returned. This is relevant only for C++ code. This specifies whether we want public, protected and/or private global variables.

Value

An object of S3 class ComputeConstants. This is a list with two elements:

cmds

a character vector with names giving the names of the global variables and entries giving C/C++ code to calculate the constant value or an NA

filenames

a character vector that parallels cmds and gives the names of the header file in which the variable is declared. This is used when we emit the C code to compute the values of these constants as R values as we need to include the header files to ensure the constants are defined and accessible to our generated code. As with all TU files, we don't have the full path name but merely the base name of the file and some more computations are warranted to get the proper access to these files for the #include in the generated code.

Author(s)

Duncan Temple Lang

References

The GCC compiler suite

See Also

parseTU computeGlobalEnumConstants getCppDefines

Examples

1
2
3
4
5
  tu = parseTU(system.file("examples", "globals", "globals.c.tu", package = "RGCCTranslationUnit"))
  g = computeGlobalConstants(tu)
  names(g)
  names(g$cmds)
  g$filenames

omegahat/RGCCTranslationUnit documentation built on May 24, 2019, 1:53 p.m.