00001
00002
00003 #include "annotation.h"
00004 #include "annotation_i.h"
00005
00006
00007 #line 339 "annotation.cpp"
00008 static const char*
00009 unmangle(const char* name);
00010
00011 #line 48 "annotation.cpp"
00012
00013
00014 Annotation::Annotation(Type type, Symbol* sym)
00015 : type(type), symbol(sym)
00016 { }
00017
00018 #line 53 "annotation.cpp"
00019
00020
00021 Annotation::~Annotation()
00022 { }
00023
00024 #line 112 "annotation.cpp"
00025
00026
00027
00028 Ptree*
00029 make_static_leaf(const char* txt)
00030 {
00031 return new Leaf(const_cast<char*>(txt), std::strlen(txt));
00032 }
00033
00034 #line 120 "annotation.cpp"
00035
00036 Ptree*
00037 make_dup_leaf(const char* txt)
00038 {
00039 char* ntxt = new char[std::strlen(txt)+1];
00040 std::strcpy(ntxt, txt);
00041 Token tmp_tok = { ntxt, std::strlen(txt), ntName };
00042 return new Leaf(tmp_tok);
00043 }
00044
00045 #line 129 "annotation.cpp"
00046
00047 Ptree*
00048 make_dup_leaf(std::string txt)
00049 {
00050 return make_dup_leaf(txt.c_str());
00051 }
00052
00053 #line 135 "annotation.cpp"
00054
00055 Annotated<LeafThis>*
00056 make_this(Type type)
00057 {
00058 Token tmp_tok = { "this", 4, THIS };
00059 LeafThis tmp_leaf(tmp_tok);
00060 return new Annotated<LeafThis>(type.make_pointer_type().with_qualifier(Type::q_Const),
00061 0,
00062 tmp_leaf);
00063 }
00064
00065 #line 145 "annotation.cpp"
00066
00067 Annotated<LeafName>*
00068 make_name(const char* txt, Type type, Symbol* sym)
00069 {
00070 char* ntxt = new char[std::strlen(txt)+1];
00071 std::strcpy(ntxt, txt);
00072 Token tmp_tok = { ntxt, std::strlen(txt), ntName };
00073 LeafName tmp_leaf(tmp_tok);
00074
00075 return new Annotated<LeafName>(type, sym, tmp_leaf);
00076 }
00077
00078 #line 156 "annotation.cpp"
00079
00080 Annotated<LeafName>*
00081 make_name(std::string str, Type type, Symbol* sym)
00082 {
00083 return make_name(str.c_str(), type, sym);
00084 }
00085
00086 #line 162 "annotation.cpp"
00087
00088 Annotated<LeafName>*
00089 make_name(Function_signature* fsig)
00090 {
00091 std::string s = fsig->get_name();
00092 if (!s.length())
00093 s = fsig->get_function()->get_name();
00094
00095 return make_name(s.c_str(),
00096 fsig->get_call_type(),
00097 fsig);
00098 }
00099
00100 #line 174 "annotation.cpp"
00101
00102 Annotated<LeafName>*
00103 make_name(Variable_symbol* vsym)
00104 {
00105 return make_name(vsym->get_name().c_str(),
00106 vsym->get_type(),
00107 vsym);
00108 }
00109
00110 #line 182 "annotation.cpp"
00111
00112 Annotated<PtreeFstyleCastExpr>*
00113 make_cast_expr_unchecked(Ptree* expr, const Type& target_type)
00114 {
00115
00116
00117 return new Annotated<PtreeFstyleCastExpr>(target_type,
00118 0,
00119 (char*) "",
00120 make_name(target_type.get_encoded_type().c_str(),
00121 target_type, 0),
00122 Ptree::List(make_static_leaf("("),
00123 expr,
00124 make_static_leaf(")")));
00125 }
00126
00127 #line 197 "annotation.cpp"
00128
00130 Annotated<PtreeFstyleCastExpr>*
00131 make_cast_expr(Ptree* expr, Type target_type)
00132 {
00133 assert(target_type.is_valid());
00134 assert(!target_type.is_class_type());
00135 return make_cast_expr_unchecked(expr, target_type);
00136 }
00137
00138 #line 206 "annotation.cpp"
00139
00142 Annotated<PtreeFstyleCastExpr>*
00143 make_derived_to_base_cast(Ptree* expr, Type target_type)
00144 {
00145 assert(target_type.is_valid());
00146 assert(target_type.is_class_type());
00147 return make_cast_expr_unchecked(expr, target_type);
00148 }
00149
00150 #line 216 "annotation.cpp"
00151
00155 Annotated<PtreeCommaExpr>*
00156 make_comma_expr(Ptree* lhs, Ptree* rhs)
00157 {
00158 Annotation* lanno = dynamic_cast<Annotation*>(lhs);
00159 Annotation* ranno = dynamic_cast<Annotation*>(rhs);
00160 assert(lanno);
00161 assert(ranno);
00162
00163 if (PtreeCommaExpr* cx = dynamic_cast<PtreeCommaExpr*>(rhs)) {
00164 lhs = make_comma_expr(lhs, rhs->First());
00165 rhs = rhs->Third();
00166 ranno = dynamic_cast<Annotation*>(rhs);
00167 assert(ranno);
00168 }
00169
00170 return new Annotated<PtreeCommaExpr>(ranno->get_type(),
00171 0,
00172 lhs,
00173 Ptree::List(make_static_leaf(","),
00174 rhs));
00175 }
00176
00177 #line 241 "annotation.cpp"
00178
00179 Annotated<PtreeDeclaration>*
00180 make_declaration(Variable_symbol* vsym)
00181 {
00182
00183
00184
00185
00186
00187
00188
00189
00190 Encoding tmp_enc;
00191
00192 PtreeDeclarator* decl =
00193 new PtreeDeclarator(Ptree::List(make_dup_leaf(vsym->get_name())),
00194 tmp_enc);
00195
00196 return new Annotated<PtreeDeclaration>(vsym->get_type(),
00197 vsym,
00198 0 ,
00199 Ptree::List(make_dup_leaf(vsym->get_type().get_encoded_type()),
00200 decl,
00201 make_static_leaf(";")));
00202 }
00203
00204 #line 266 "annotation.cpp"
00205
00208 void
00209 fill_in_overload_annotation(Ptree* tree, Symbol* sym, Type t)
00210 {
00211 Annotation* anno = dynamic_cast<Annotation*>(tree);
00212 assert(anno);
00213 anno->set_type(t);
00214
00215 if (PtreeCommaExpr* cx = dynamic_cast<PtreeCommaExpr*>(tree))
00216 fill_in_overload_annotation(cx->Third(), sym, t);
00217 else
00218 anno->set_symbol(sym);
00219 }
00220
00221 #line 281 "annotation.cpp"
00222
00223
00224
00225
00226 Annotated_funcall_maker::Annotated_funcall_maker(Ptree* function,
00227 Type result_type)
00228 : function(function), result_type(result_type), tree(0)
00229 { }
00230
00231 #line 289 "annotation.cpp"
00232
00233
00234 Annotated_funcall_maker::~Annotated_funcall_maker()
00235 { }
00236
00237 #line 293 "annotation.cpp"
00238
00239 void
00240 Annotated_funcall_maker::add_arg(Ptree* arg)
00241 {
00242 if (!tree) {
00243 tree = new NonLeaf(arg, 0);
00244 } else {
00245 tree = Ptree::Snoc(tree, make_static_leaf(","));
00246 tree = Ptree::Snoc(tree, arg);
00247 }
00248 }
00249
00250 #line 304 "annotation.cpp"
00251
00252 Ptree*
00253 Annotated_funcall_maker::make_funcall()
00254 {
00255
00256
00257
00258 if (PtreeCommaExpr* cx = dynamic_cast<PtreeCommaExpr*>(function)) {
00259 compile_warning("calling a comma expression", cx);
00260 return make_comma_expr(function->First(),
00261 new Annotated<PtreeFuncallExpr>(result_type,
00262 0,
00263 function->Third(),
00264 Ptree::List(make_static_leaf("("),
00265 tree,
00266 make_static_leaf(")"))));
00267 } else {
00268 return new Annotated<PtreeFuncallExpr>(result_type,
00269 0,
00270 function,
00271 Ptree::List(make_static_leaf("("),
00272 tree,
00273 make_static_leaf(")")));
00274 }
00275 }
00276
00277 #line 329 "annotation.cpp"
00278
00279
00280 Ptree*
00281 make_unary_funcall(Ptree* function, Type result, Ptree* arg)
00282 {
00283 Annotated_funcall_maker afm(function, result);
00284 afm.add_arg(arg);
00285 return afm.make_funcall();
00286 }
00287
00288 #line 338 "annotation.cpp"
00289
00290 static const char*
00291 unmangle(const char* name)
00292 {
00293 if (*name == 't') {
00294
00295 int num = 0;
00296 ++name;
00297 while (*name >= '0' && *name <= '9')
00298 num = 10*num + *name++ - '0';
00299 name += num + 2;
00300 }
00301 while (*name >= '0' && *name <= '9')
00302 ++name;
00303 return name;
00304 }
00305 #line 354 "annotation.cpp"
00306
00307
00308 #define COLOR_(c) #c
00309 #line 357 "annotation.cpp"
00310 #define COLOR(c) (colors ? "\033[" COLOR_(c) "m" : "")
00311 #line 358 "annotation.cpp"
00312 #define cBlack 30
00313 #line 359 "annotation.cpp"
00314 #define cRed 31
00315 #line 360 "annotation.cpp"
00316 #define cGreen 32
00317 #line 361 "annotation.cpp"
00318 #define cYellow 33
00319 #line 362 "annotation.cpp"
00320 #define cBlue 34
00321 #line 363 "annotation.cpp"
00322 #define cMagenta 35
00323 #line 364 "annotation.cpp"
00324 #define cCyan 36
00325 #line 365 "annotation.cpp"
00326 #define cWhite 37
00327
00328 #line 366 "annotation.cpp"
00329
00330
00331
00332
00333
00334 void
00335 print_annotated_tree(std::ostream& out, std::string indent, Ptree* tree, bool colors)
00336 {
00337 if (!tree) {
00338 out << indent << "null";
00339 return;
00340 }
00341 Annotation* an = dynamic_cast<Annotation*>(tree);
00342 out << indent;
00343 if (an) {
00344 out << "[";
00345 if (Symbol* sym = an->get_symbol()) {
00346 std::string n = sym->get_name();
00347 if (n.length())
00348 out << COLOR(cRed) << n << COLOR(0);
00349 else
00350 out << "unnamed";
00351 } else {
00352 out << "no symbol";
00353 }
00354 Type t = an->get_type();
00355 if (t.is_valid()) {
00356 out << ", " << COLOR(cGreen) << t.make_pointer_type().get_encoded_type().substr(1)
00357 << COLOR(0);
00358 }
00359 if (an->has_flag(Annotation::af_DirectCall))
00360 out << ", direct";
00361 out << "] ";
00362 }
00363 if (tree->IsLeaf()) {
00364 out << COLOR(cBlue) << tree->ToString() << COLOR(0);
00365 if (Symbol_table::has_dump_flag('a'))
00366 out << " @" << (void*)tree;
00367 } else {
00368 out << unmangle(typeid(*tree).name());
00369 if (Symbol_table::has_dump_flag('a'))
00370 out << " @" << (void*)tree;
00371 while (tree) {
00372 out << "\n";
00373 print_annotated_tree(out, indent + " ", tree->Car(), colors);
00374 tree = tree->Cdr();
00375 }
00376 }
00377 }