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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
% slirpc++.sl: Supports generation of S-Lang wrappers for C++ {{{
%
% This file is part of SLIRP, the (Sl)ang (I)nte(r)face (P)ackage.
%
% Copyright (c) 2003-2006 Massachusetts Institute of Technology
% Copyright (C) 2002 Michael S. Noble <mnoble@space.mit.edu> }}}
require("slirpmaps");
% Front matter: type/variable/forward declarations, etc {{{
private define parse_cplusplus_block();
private define activate_cpp_support()
{
add_tokens(Single_Char_Tokens, "~ ( ) , [ ] < +");
_macros["static"] = EMPTY;
SC.funcprefix = "^[~]?[a-zA-Z]+";
SC.cplusplus = 1;
}
% }}}
% Overloaded type mnemonics % {{{
define map_args_to_type_abbrevs(args)
{
variable inputs = where( struct_map(Integer_Type, args, "marshal"));
args = args[inputs];
variable types = struct_map(String_Type, args, "typeid");
types = array_map(String_Type, &sltype_abbrev, types);
variable refs = where(types == "R");
if (length(refs)) {
variable refd_types = struct_map(Struct_Type, args[refs], "aux");
refd_types = struct_map(String_Type, refd_types, "typeid");
refd_types = array_map(String_Type, &sltype_abbrev, refd_types);
refd_types = array_map(String_Type, &sltype_ptr_abbrev, refd_types);
types[refs] = refd_types;
}
return types;
}
define make_dispatch_table_entry(overloaded_funcs)
{
variable entry = EMPTY;
foreach(overloaded_funcs.head) {
variable f = ();
entry = sprintf("\n { %s, (char*)\"%s\", %d },", f.name,
f.value, strlen(f.value));
SC.dispatch_table = [ SC.dispatch_table, entry ];
}
}
% }}}
% Code emission {{{
define emit_destructor(class)
{
!if (length(class.ancestors)) {
variable proto = sprintf("void %s%s_delete(void *o)",
SC.wrapper_prefix, class.name);
SC.interface.prototypes[proto] = EMPTY;
!if (SC.cfront)
emit("static ");
emit("%s {delete (%s *)o;}\n", proto, class.name);
}
}
define emit_dispatch_table()
{
!if (length(SC.dispatch_table)) return;
emit("typedef struct _Overloaded_Func {\t%s\n"+
" void (*func) (void);\n"+
" char *signature;\n"+
" int nargs;\n"+
"} Overloaded_Func; %s\n", FOLD_OPEN, FOLD_CLOSE);
emit("\nstatic Overloaded_Func Dispatch_Table[] =%s\n{", FOLD_OPEN);
foreach(SC.dispatch_table) {
variable entry = ();
emit(entry);
}
emit("\n { NULL, NULL, 0}\n}; %s\n\n", FOLD_CLOSE);
inject_file("dispatch.c");
}
private define field_get_emitter(fmap, argno_ref)
{
variable retval = fmap.retval, obj = fmap.args[0];
sprintf("(%s) (%s%s)->%s", retval.type, DeReferer[fmap.vectorized],
(@obj.referer)(obj, 0, fmap.pop_args), retval.name);
% S-Lang1 doesn't like @argno_ref += 1
if (argno_ref != NULL) @argno_ref = @argno_ref + 1;
}
private define field_set_emitter(fmap, argno_ref)
{
variable obj = fmap.args[0], field = fmap.args[1];
variable deref = DeReferer[fmap.vectorized];
sprintf("(%s%s)->%s = (%s) %s%s", deref,
(@obj.referer) (obj, 0, fmap.pop_args),
field.name, field.type, deref, field.lname);
if (argno_ref != NULL) @argno_ref = @argno_ref + 2; % S-Lang1
}
% }}}
% Mapping and support code {{{
define cfront_method_referer(meth, argno_ref)
{
sprintf("INVOKE_METHOD(%s, OBJECT(arg0), %s)", meth.class.name, meth.name);
@argno_ref = @argno_ref + 1;
}
private define standard_method_referer(method, argno_ref)
{
if (argno_ref != NULL) @argno_ref = @argno_ref + 1;
return "arg0->" + method.name;
}
private define cfront_class_referer(arg, typed, ignored)
{
if (typed)
sprintf("void* %s", arg.lname);
else {
variable ref = DeReferer[arg.cpp_ref];
sprintf("%s((%s%s)%s)", ref, arg.type, ref, arg.lname);
}
}
private define constructor_referer(method, ignore)
{
return "new " + method.name;
}
static variable method_referer;
static variable class_type_referer;
private define define_class_typemaps(class, parent, destructor)
{
!if (SC.num_reserved_types) define_reserved_opaques();
variable ot = _define_opaque(class.name, parent, destructor);
ot.referer = class_type_referer;
slirp_map_opaque(class.name+"*", class.name);
slirp_map_cpp_ref(class.name);
}
private define finalize_method(meth, class)
{
if (meth== NULL) return;
meth.class = class;
meth.language = CPLUSPLUS;
!if (SC.cfront) meth.pop_args = 1;
if (meth.name == class.name) { % constructor
meth.slname += "_new";
meth.referer = &constructor_referer;
if (SC.genstubs) { % Stubbed constructors should
meth.retval.type = EMPTY; % not reflect a return value,
meth.retval.ltype = VOID; % since that's not legal C++
meth.gname = class.name + "::" + class.name;
}
else
meth.gname = meth.slname;
}
else if (not SC.genstubs) { % regular method, so inject
variable this = @SC.types[class.name]; % "this" as hidden/first arg
this.type = class.name + "*";
this.name = EMPTY;
this.lname = "arg0";
meth.slname = strcat(class.name,"_",meth.slname);
meth.args = [this, meth.args];
meth.nargs++;
meth.referer = method_referer;
meth.gname = meth.slname;
}
else
meth.gname = meth.retval.type + " " + class.name + "::" + meth.slname;
!if (SC.cfront)
annotate(meth);
}
private define queue_public_field_wrapper_generation(class, field)
{
if (field.typeclass != TYPECL_SCALAR) return;
% Fabricate phony get/set function prototypes, and replace the
% default function call emitter (which emit_call_block would
% normally use to invoke the wrapped function) with an
% emitter that simply gets/sets the relevant object fielda
variable f = sprintf("%s %s_get_%s(%s*)", field.type, class.name,
field.name, class.name);
f = parse_func_decl(f, 0, 0);
f.retval.name = field.name;
f.referer = &field_get_emitter;
f.callable = 0;
f = sprintf("void %s_set_%s(%s*, %s %s)", class.name, field.name,
class.name, field.type, field.name);
f = parse_func_decl(f, 0, 0);
f.referer = &field_set_emitter;
f.callable = 0;
}
% }}}
private define parse_destructor(fp, class) % {{{
{
variable decl = get_func_decl(fp, get_token(fp));
if (SC.genstubs) {
variable fmap = parse_func_decl("void ~"+decl, 0, 0);
fmap.retval.type = EMPTY; % not reflect a return value,
fmap.retval.ltype = VOID; % since that's not legal C++
fmap.gname = class.name + "::~" + class.name;
}
} % }}}
private define parse_class(fp, nesting) % {{{
{
variable class = @Class, derived = 0;
class.name = prep_get_token(fp);
class.ancestors = String_Type[0];
class.abstract = 0;
class.constructors = String_Type[0];
class.nesting = nesting;
SC.in_public_interface = 0;
variable token = prep_get_token(fp);
if (token[-1] == ';')
return 1; % a forward declaration
variable global_interface = save_interface(SC.interface, 1);
SC.interface.name = class.name;
while (token != "{") {
switch(token)
{ case ":" or case "," : }
{ case "public" : derived = 1; }
{ case "private" or case "protected" : derived = 0; }
{
% Support inheritance only from known classes
if (andelse {derived} {SC.types[token] != NULL})
class.ancestors = [ class.ancestors, token ];
}
token = prep_get_token(fp);
}
variable parent = NULL, destructor = "_delete";
if (length(class.ancestors)) { % multiple inheritance
parent = class.ancestors[0]; % not yet unsupported
destructor = parent + destructor;
}
else
destructor = class.name + destructor;
define_class_typemaps(class, parent, SC.wrapper_prefix + destructor);
parse_cplusplus_block(fp, class);
class.interface = save_interface(SC.interface, 0);
restore_interface(global_interface, SC.interface);
SC.classes[class.name] = class;
SC.in_public_interface = 1;
} % }}}
private define parse_cplusplus_block(fp, class) % {{{
{
if (SC.debug)
tprintf("%s: ENTERED (class %s)", _function_name, class.name);
SC.tablevel++;
do {
variable fmap, decl, token = prep_get_token(fp);
if (token == ";") continue; % forward declaration
if (SC.debug)
tprintf("%s: token=<%S>", _function_name, token);
switch(token)
{ case "private" or case "protected":
SC.in_public_interface = 0;
() = get_token(fp); % swallow colon
}
{ case "public" :
% Inner classes should never become publicly visible
SC.in_public_interface = (class.nesting == 0);
() = get_token(fp); % swallow colon
}
{ case class.name :
% Treat constructors as if they return type of <class_name>*
decl = strcat(class.name,"* ", class.name, get_token(fp));
decl = get_func_decl(fp, decl);
% Strip initialization lists and inline definitions
if (string_match(decl, "\\(.+)\\)[ \t]*:.+", 1))
decl = get_match(decl, 1);
if (string_match(decl, "\\(.+)\\)[ \t]*{.+}$", 1))
decl = get_match(decl, 1);
% Defer full treatment of constructors until
% we know whether or not class is abstract
class.constructors = [class.constructors, decl];
}
{ case "~": parse_destructor(fp, class); }
{ case "class": parse_class(fp, class.nesting+1); }
{ case "virtual":
token = get_token(fp);
if (token == "~") { parse_destructor(fp, class); continue;}
decl = get_func_decl(fp, token);
variable pure_virtual = string_match(decl,
")[ \t]*=[ \t]*0[ \t]*;[ \t]*$", 1);
if (pure_virtual) {
decl = strcat(decl[[0:pure_virtual-1]],";");
class.abstract = 1;
}
!if (class.nesting) {
fmap = parse_func_decl(decl, 0, 0);
finalize_method(fmap, class);
}
}
{ case "static" : }
{
if (token == EMPTY) abort("incomplete class definition");
!if (macro_or_type_definition(fp, token))
if (SC.in_public_interface) {
if (get_var_decl(fp)) {
decl = strtrim_end(SC.decl, ";");
variable tmp = parse_args(_function_name, decl, 0);
decl = parse_args(_function_name, decl, 0)[0];
queue_public_field_wrapper_generation(class, decl);
}
else {
decl = get_func_decl(fp, SC.decl);
fmap = parse_func_decl(decl, 0, 0);
finalize_method(fmap, class);
SC.decl = EMPTY;
}
}
else { % swallow non-public content
!if (get_var_decl(fp))
() = get_func_decl(fp, SC.decl);
}
}
} while (token != "}");
!if (class.abstract or class.nesting)
foreach(class.constructors) {
decl = ();
fmap = parse_func_decl(decl, 0, 0);
finalize_method(fmap, class);
}
SC.tablevel--;
if (SC.debug)
tprintf("%s: EXITED (class %s)\n",_function_name, class.name);
} % }}}
define cplusplus_token(fp) % {{{
{
variable token = get_token(fp);
switch(token)
{ case "class": activate_cpp_support; parse_class(fp, 0); }
{ case "template":
activate_cpp_support;
swallow_to(fp, '>');
token = get_token(fp);
if (token == "class")
parse_class(fp, 1);
else
swallow_C_block(fp);
}
{ case "inline" : } % inlined func defs: supported
{ case "operator" : swallow_C_block(fp); } % operator defs: ignored
{ case "using": swallow_to(fp, ';'); }
{
if ("operator" == next_token(fp))
swallow_C_block(fp);
else {
unget_token(fp, token);
return 0;
}
}
activate_cpp_support;
return 1;
} % }}}
private define initialize_cpp_support() % {{{
{
if (SC.cfront) {
class_type_referer = &cfront_class_referer;
method_referer = &cfront_method_referer;
}
else {
class_type_referer = &opaque_referer;
method_referer = &standard_method_referer;
}
}
slirp_init_cpp_callback = &initialize_cpp_support;
% }}}
provide("slirpc++");
|