1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/* $Header$ */
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
#include "nickle.h"
static void MarkFuncCode (void *object)
{
FuncCodePtr fc = object;
MemReference (fc->base.type);
MemReference (fc->base.args);
MemReference (fc->base.name);
MemReference (fc->base.previous);
MemReference (fc->base.func);
MemReference (fc->base.doc);
MemReference (fc->code);
MemReference (fc->body.obj);
MemReference (fc->body.dynamics);
MemReference (fc->staticInit.obj);
MemReference (fc->staticInit.dynamics);
MemReference (fc->statics);
}
DataType FuncCodeType = { MarkFuncCode, 0, "FuncCodeType" };
static Bool
HasVarargs (ArgType *args)
{
while (args)
{
if (args->varargs)
return True;
args = args->next;
}
return False;
}
CodePtr
NewFuncCode (Type *type, ArgType *args, ExprPtr code, Value doc)
{
ENTER ();
CodePtr fc;
fc = ALLOCATE (&FuncCodeType, sizeof (FuncCode));
fc->base.builtin = False;
fc->base.type = type;
fc->base.argc = 0;
fc->base.varargs = HasVarargs (args);
fc->base.args = args;
fc->base.name = 0;
fc->base.previous = 0;
fc->base.func = fc;
fc->base.doc = doc;
fc->func.code = code;
fc->func.body.dynamics = 0;
fc->func.body.obj = 0;
fc->func.staticInit.obj = 0;
fc->func.staticInit.dynamics = 0;
fc->func.statics = 0;
fc->func.inStaticInit = False;
fc->func.inGlobalInit = False;
RETURN (fc);
}
static void MarkBuiltinCode (void *object)
{
BuiltinCodePtr bc = object;
MemReference (bc->base.type);
MemReference (bc->base.args);
MemReference (bc->base.name);
MemReference (bc->base.previous);
MemReference (bc->base.func);
MemReference (bc->base.doc);
}
DataType BuiltinCodeType = { MarkBuiltinCode, 0, "BuiltinCodeType" };
CodePtr
NewBuiltinCode (Type *type, ArgType *args, int argc,
BuiltinFunc builtin, Bool needsNext, char *doc)
{
ENTER ();
CodePtr bc;
bc = ALLOCATE (&BuiltinCodeType, sizeof (BuiltinCode));
bc->base.builtin = True;
bc->base.type = type;
bc->base.argc = argc;
bc->base.varargs = HasVarargs (args);
bc->base.args = args;
bc->base.name = 0;
bc->base.previous = 0;
bc->base.func = 0;
bc->base.doc = doc ? NewStrString (doc) : Void;
bc->builtin.needsNext = needsNext;
bc->builtin.b = builtin;
RETURN (bc);
}
static void
FuncMark (void *object)
{
Func *f = object;
MemReference (f->code);
MemReference (f->staticLink);
MemReference (f->statics);
}
void printCode (Value f, CodePtr code, int level);
static Bool
FuncPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
Bool nest = False;
if (format == 'v')
nest = True;
PrettyCode (f, av->func.code, 0, class_undef, publish_private, 0, nest);
return True;
}
ValueRep FuncRep = {
{ FuncMark, 0, "FuncRep" }, /* base */
rep_func, /* tag */
{ /* binary */
0,
0,
0,
0,
0,
0,
0,
ValueEqual,
0,
0,
},
{ /* unary */
0,
0,
0,
},
0,
0,
FuncPrint,
0,
};
Value
NewFunc (CodePtr code, FramePtr staticLink)
{
ENTER ();
Value ret;
ret = ALLOCATE (&FuncRep.data, sizeof (Func));
ret->func.code = code;
ret->func.staticLink = staticLink;
ret->func.statics = 0;
/*
* Create the box containing static variables for this closure
*/
if (!code->base.builtin && code->func.statics)
ret->func.statics = NewTypedBox (False, code->func.statics);
RETURN (ret);
}
|