[R] Functioning and source code location of .Primitive()

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon May 24 11:02:23 CEST 2010


On Mon, May 24, 2010 at 9:03 AM, Stefano Baraldo
<stefano.baraldo at gmail.com> wrote:
> Hi all.
> I began examining the R source code in the last few days, for a better understanding of its structure. I read the manuals and digged deeply (maybe not enough?) into the source code, but I couldn't totally understand how .Primitive() works. Its effects, if I understand well, are quite clear to me (calling the C compiled counterpart of a "primitive" function), but where is its definition? The only things I was able to find were:
> -in src/library/base/R/zzz.R, its creation in the .ArgsEnv environment, but with NULL body!
> -in src/main/names.c, the association of .Primitive() to the C function do_primitive, which I cannot find anywhere else in the source code.
> Does any of you know where should I look?

 First, I reckon you should be looking on the r-dev mailing list! :)

 Secondly, if you are using a Unix machine with GNU utils, 'grep -nr'
is your friend:

$ grep -nr do_primitive .
./src/gnuwin32/Rdll.hide:864: do_primitive
./src/include/Internal.h:365:SEXP do_primitive(SEXP, SEXP, SEXP, SEXP);
./src/main/names.c:117:{".Primitive",	do_primitive,	0,	1,	1,	{PP_FUNCALL,
PREC_FN,	  0}},
./src/main/names.c:1008:SEXP attribute_hidden do_primitive(SEXP call,
SEXP op, SEXP args, SEXP env)

  - so I'm not sure why you didn't spot the do_primitive definition on
line 1008:

SEXP attribute_hidden do_primitive(SEXP call, SEXP op, SEXP args, SEXP env)
{
    SEXP name;
    int i;
    checkArity(op, args);
    name = CAR(args);
    if (!isString(name) || length(name) < 1 ||

etc etc.

 If you are going to do a lot of this kind of nosing around, it's
worth looking at an IDE that can help by tracking where function
definitions are. For example, emacs with the etags system. Tutorial
here:

http://www.linuxjournal.com/article/2821?page=0,1

Barry



More information about the R-help mailing list