00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "c_breeze.h"
00039 #include "ref_clone_changer.h"
00040
00041
00042
00043
00044
00045 funcNode::funcNode(Type_qualifiers tq, decl_list * args, typeNode * returns,
00046 const Coord coord)
00047 : typeNode(Func, tq, returns, coord),
00048 _args(),
00049 _is_knr(false)
00050 {
00051 if (args) {
00052 _args.swap(*args);
00053
00054 }
00055 }
00056
00057
00058
00059
00060
00061 bool funcNode::qualified_equal_to(typeNode * node2,
00062 bool strict_toplevel, bool strict_recursive)
00063 {
00064 funcNode * n2 = (funcNode *) node2;
00065
00066
00067
00068 if (! equal_to(returns(), n2->returns(),
00069 strict_recursive, strict_recursive))
00070 return false;
00071
00072
00073
00074 decl_list & args1 = args();
00075 decl_list & args2 = n2->args();
00076
00077 if (args1.empty() || args2.empty())
00078 return true;
00079
00080
00081
00082 if (args1.size() != args2.size())
00083 return false;
00084
00085
00086 decl_list_p p1;
00087 decl_list_p p2;
00088
00089 for (p1 = args1.begin(), p2 = args2.begin();
00090 (p1 != args1.end()) && (p1 != args2.end()) ;
00091 ++p1, ++p2) {
00092 typeNode * type1;
00093 typeNode * type2;
00094
00095
00096
00097 if ((*p1)->typ() == Decl)
00098 type1 = ((declNode *)(*p1))->type();
00099 else
00100 type1 = (typeNode *)(*p1);
00101
00102 if ((*p2)->typ() == Decl)
00103 type2 = ((declNode *)(*p2))->type();
00104 else
00105 type2 = (typeNode *)(*p2);
00106
00107 if (! equal_to(type1, type2,
00108 strict_recursive, strict_recursive))
00109 return false;
00110 }
00111
00112 return true;
00113 }
00114
00115
00116
00117
00118
00119 bool funcNode::is_void_args()
00120 {
00121 decl_list & the_args = args();
00122
00123 if (the_args.size() == 1) {
00124 Node * f = the_args.front();
00125 return f->datatype()->is_void();
00126 }
00127 return false;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 bool funcNode::check_conversions()
00166 {
00167 decl_list & the_args = args();
00168 decl_list_p p;
00169
00170
00171
00172 for (p = the_args.begin(); p != the_args.end(); ++p) {
00173
00174
00175
00176 typeNode * the_type = (*p)->datatype();
00177
00178
00179
00180 typeNode * conv = the_type->usual_unary_conversion_type();
00181 bool compatible = (* the_type == * conv );
00182
00183
00184 if ( ! compatible || the_type->is_ellipsis())
00185 return false;
00186 }
00187
00188 return true;
00189 }
00190
00191
00192
00193 bool funcNode::is_compatible_with(funcNode * nfunc)
00194 {
00195 funcNode * ofunc = this;
00196
00197
00198
00199 if (! (* ofunc->returns() == * nfunc->returns()) )
00200 return false;
00201
00202
00203
00204 decl_list & oargs = ofunc->args();
00205 decl_list & nargs = nfunc->args();
00206
00207
00208
00209 if (! oargs.empty() && ! nargs.empty()) {
00210 decl_list_p optr;
00211 decl_list_p nptr;
00212
00213
00214
00215 if (oargs.size() != nargs.size())
00216 return false;
00217
00218
00219
00220 for (optr = oargs.begin(), nptr = nargs.begin();
00221 (optr != oargs.end()) && (nptr != nargs.end());
00222 ++optr, ++nptr) {
00223 typeNode * otype = (*optr)->datatype_superior();
00224 typeNode * ntype = (*nptr)->datatype_superior();
00225
00226 if ( * otype != * ntype ) {
00227
00228 return false;
00229 }
00230 }
00231
00232 return true;
00233 }
00234 else
00235 if (oargs.empty() && nargs.empty())
00236
00237 return true;
00238
00239 else {
00240
00241
00242
00243
00244
00245 if (oargs.empty()) {
00246
00247 decl_list_p fptr;
00248
00249
00250
00251 for (fptr = oargs.begin();
00252 fptr != oargs.end();
00253 ++fptr)
00254 nargs.push_back((declNode *) ref_clone_changer::clone(*fptr, false));
00255 }
00256
00257 return true;
00258 }
00259
00260
00261
00262
00263
00264 if (ofunc->is_void_args())
00265 return true;
00266
00267
00268
00269 if (nfunc->is_void_args()) {
00270
00271
00272
00273 declNode * void_decl = new declNode((typeNode *) new primNode(basic_type::Void), declNode::NONE);
00274 void_decl->decl_location(declNode::FORMAL);
00275
00276 ofunc->args().push_front(void_decl);
00277
00278 return true;
00279 }
00280
00281
00282
00283
00284 if (! ofunc->check_conversions())
00285 return false;
00286
00287 if (! nfunc->check_conversions())
00288 return false;
00289
00290 return true;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 void funcNode::add_parameter_types(decl_list * types)
00312 {
00313 decl_list & ids = args();
00314
00315 decl_list_p ids_p;
00316 decl_list_p types_p;
00317
00318 bool found;
00319
00320
00321 if (types) {
00322
00323
00324
00325 for (types_p = types->begin(); types_p != types->end(); ++types_p) {
00326
00327 declNode * t = ( * types_p );
00328
00329 const string & the_name = t->name();
00330 found = false;
00331
00332
00333
00334
00335
00336
00337 for (ids_p = ids.begin(); ids_p != ids.end(); ++ids_p) {
00338
00339 declNode * the_param = (*ids_p);
00340
00341 if (the_param->name() == the_name) {
00342
00343 found = true;
00344
00345 if (! the_param->type()) {
00346
00347
00348
00349
00350
00351
00352 (*ids_p) = t;
00353 found = true;
00354
00355 break;
00356 }
00357 else {
00358 CBZ::SyntaxError(t->coord(),
00359 string("multiple declarations for parameter `") +
00360 the_name + string("'"));
00361 break;
00362 }
00363 }
00364 }
00365
00366 if (! found)
00367 CBZ::SyntaxError(t->coord(),
00368 string("declaration for nonexistent parameter `") +
00369 the_name + string("'"));
00370
00371 }
00372
00373
00374
00375 }
00376
00377
00378
00379 for (ids_p = ids.begin(); ids_p != ids.end(); ++ids_p) {
00380
00381 declNode * dec = (*ids_p);
00382
00383 if (! dec->type()) {
00384
00385
00386
00387 CBZ::Warning(2, dec->coord(),
00388 string("parameter `") + dec->name() +
00389 string("' defaults to signed int"));
00390
00391 dec->type(new primNode(dec->coord()));
00392 }
00393 }
00394 }
00395
00396
00397
00398
00399
00400 void funcNode::visit(Visitor * the_visitor)
00401 {
00402 the_visitor->at_func(this);
00403 }
00404
00405 void funcNode::walk(Walker & the_walker)
00406 {
00407 Walker::Order ord = the_walker.order();
00408
00409 if (ord == Walker::Preorder || ord == Walker::Both)
00410 the_walker.at_func(this, Walker::Preorder);
00411
00412 if (the_walker.depth() == Walker::Subtree) {
00413
00414
00415 list_walker(args(), the_walker);
00416
00417 if (returns())
00418 returns()->walk(the_walker);
00419 }
00420
00421 if (ord == Walker::Postorder || ord == Walker::Both)
00422 the_walker.at_func(this, Walker::Postorder);
00423 }
00424
00425
00426
00427
00428
00429 void funcNode::output_type(output_context & ct, Node * parent, Assoc context, Type_qualifiers q)
00430 {
00431 if (context == Left) {
00432 const string & qs = type_qualifiers_name();
00433
00434 if (! qs.empty())
00435 ct.space();
00436 ct << qs;
00437
00438 returns()->output_type(ct, this, Left, q);
00439 }
00440 else {
00441 ct << '(';
00442 if (is_knr()) {
00443
00444
00445
00446
00447
00448
00449
00450
00451 decl_list_p i=args().begin();
00452 for (;;) {
00453 ct << (*i)->name();
00454 i++;
00455 if (i == args().end()) break;
00456 ct << ", ";
00457 }
00458 ct << ")\n";
00459 output_delim_list(args(), ct, this, ';');
00460 ct << ';';
00461 } else {
00462 output_delim_list(args(), ct, this, ',');
00463 ct << ')';
00464 }
00465
00466 returns()->output_type(ct, this, Right, q);
00467 }
00468 }
00469
00470
00471
00472
00473
00474 Node * funcNode::change(Changer & the_changer, bool redispatch)
00475 {
00476 Changer::Order ord = the_changer.order();
00477 funcNode * the_func = this;
00478
00479 if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00480 the_func = (funcNode *) the_changer.at_func(the_func, Changer::Preorder);
00481
00482 if (the_func) {
00483
00484 if (the_func != this)
00485 return the_func->change(the_changer, true);
00486
00487 change_list(the_func->args(), the_changer);
00488
00489 typeNode * old_returns = the_func->returns();
00490 if (old_returns) {
00491 typeNode * new_returns = (typeNode *) old_returns->change(the_changer);
00492 if (old_returns != new_returns) {
00493
00494
00495 the_func->returns(new_returns);
00496 }
00497 }
00498
00499 }
00500
00501 if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00502 the_func = (funcNode *) the_changer.at_func(the_func, Changer::Postorder);
00503
00504 return the_func;
00505 }
00506
00507
00508
00509
00510
00511
00512 funcNode::~funcNode()
00513 {
00514
00515 }