idldump.cc

来自「编译工具」· CC 代码 · 共 878 行 · 第 1/2 页

CC
878
字号
// -*- c++ -*-//                          Package   : omniidl// idldump.cc               Created on: 1999/10/26//			    Author    : Duncan Grisby (dpg1)////    Copyright (C) 1999 AT&T Laboratories Cambridge////  This file is part of omniidl.////  omniidl is free software; you can redistribute it and/or modify it//  under the terms of the GNU General Public License as published by//  the Free Software Foundation; either version 2 of the License, or//  (at your option) any later version.////  This program is distributed in the hope that it will be useful,//  but WITHOUT ANY WARRANTY; without even the implied warranty of//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU//  General Public License for more details.////  You should have received a copy of the GNU General Public License//  along with this program; if not, write to the Free Software//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA//  02111-1307, USA.//// Description://   //   Visitor object to dump the tree// $Id: idldump.cc,v 1.11.2.10 2002/02/25 15:02:18 dpg1 Exp $// $Log: idldump.cc,v $// Revision 1.11.2.10  2002/02/25 15:02:18  dpg1// Dump wstring constants properly.//// Revision 1.11.2.9  2002/02/18 11:59:22  dpg1// Full autoconf support.//// Revision 1.11.2.8  2001/11/14 17:13:43  dpg1// Long double support.//// Revision 1.11.2.7  2001/08/29 11:54:20  dpg1// Clean up const handling in IDL compiler.//// Revision 1.11.2.6  2001/06/12 11:35:25  dpg1// Minor omniidl tweaks for valuetype.//// Revision 1.11.2.5  2001/03/13 10:32:11  dpg1// Fixed point support.//// Revision 1.11.2.4  2000/11/01 15:57:03  dpg1// More updates for 2.4.//// Revision 1.11.2.3  2000/11/01 12:45:56  dpg1// Update to CORBA 2.4 specification.//// Revision 1.11.2.2  2000/10/10 10:18:50  dpg1// Update omniidl front-end from omni3_develop.//// Revision 1.9.2.2  2000/08/29 10:20:26  dpg1// Operations and attributes now have repository ids.//// Revision 1.9.2.1  2000/08/14 14:35:13  dpg1// IDL dumping now properly escapes string and char constants//// Revision 1.9  2000/02/03 14:50:08  dpg1// Native declarations can now be used as types.//// Revision 1.8  1999/11/17 18:07:23  dpg1// Tiny change to enum printing.//// Revision 1.7  1999/11/17 18:05:16  dpg1// Tiny change to enum printing.//// Revision 1.6  1999/11/17 17:50:42  dpg1// Unions with nested declarations now output properly.//// Revision 1.5  1999/11/02 17:07:27  dpg1// Changes to compile on Solaris.//// Revision 1.4  1999/11/01 20:19:56  dpg1// Support for union switch types declared inside the switch statement.//// Revision 1.3  1999/10/29 15:43:15  dpg1// Code to detect recursive structs and unions.//// Revision 1.2  1999/10/29 10:01:12  dpg1// Small fixes.//// Revision 1.1  1999/10/27 14:05:58  dpg1// *** empty log message ***//#include <idldump.h>#include <idlutil.h>#include <idlast.h>#include <idltype.h>#include <stdio.h>#include <ctype.h>DumpVisitor::DumpVisitor()  : indent_(0){}DumpVisitor::~DumpVisitor(){}voidDumpVisitor::printIndent(){  for (int i=0; i<indent_; ++i)    printf("  ");}voidDumpVisitor::printScopedName(const ScopedName* sn){  char* ssn = sn->toString();  printf("%s", ssn);  delete [] ssn;}voidDumpVisitor::printString(const char* str){  const char* c;  for (c=str; *c; c++) {    if (*c == '\\')      printf("\\\\");    else if (isprint(*c))      putchar(*c);    else      printf("\\%03o", (int)(unsigned char)*c);  }}voidDumpVisitor::printChar(const char c){  if (c == '\\')    printf("\\\\");  else if (isprint(c))    putchar(c);  else    printf("\\%03o", (int)(unsigned char)c);}voidDumpVisitor::visitAST(AST* a){  printf("\n");  for (Decl* d = a->declarations(); d; d = d->next()) {    d->accept(*this);    printf(";\n\n");  }}voidDumpVisitor::visitModule(Module* m){  printf("module %s { // RepoId = %s, file = %s, line = %d, %s\n",	 m->identifier(), m->repoId(), m->file(), m->line(),	 m->mainFile() ? "in main file" : "not in main file");  ++indent_;  for (Decl* d = m->definitions(); d; d = d->next()) {    printIndent();    d->accept(*this);    printf(";\n");  }  --indent_;  printIndent();  printf("}");}voidDumpVisitor::visitInterface(Interface* i){  if (i->abstract()) printf("abstract ");  if (i->local())    printf("local ");  printf("interface %s ", i->identifier());  if (i->inherits()) {    printf(": ");    char* ssn;    for (InheritSpec* is = i->inherits(); is; is = is->next()) {      ssn = is->interface()->scopedName()->toString();      printf("%s%s ", ssn, is->next() ? "," : "");      delete [] ssn;    }  }  printf("{ // RepoId = %s\n", i->repoId());  ++indent_;  for (Decl* d = i->contents(); d; d = d->next()) {    printIndent();    d->accept(*this);    printf(";\n");  }  --indent_;  printIndent();  printf("}");}voidDumpVisitor::visitForward(Forward* f){  if (f->abstract()) printf("abstract ");  if (f->local())    printf("local ");  printf("interface %s; // RepoId = %s", f->identifier(), f->repoId());}static voidprintdouble(IDL_Double d){  // .17g guarantees an IEEE 754 double can be exactly reconstructed,  // but it prints integers without a trailing .0, so we must put it  // back on in that case.  char buffer[1024];  sprintf(buffer, "%.17g", d);  char* c = buffer;  if (*c == '-') ++c;  for (; *c; c++) {    if (!isdigit(*c))      break;  }  if (*c == '\0') {    *c++ = '.';    *c++ = '0';    *c++ = '\0';  }  printf("%s", buffer);}#ifdef HAS_LongDoublestatic voidprintlongdouble(IDL_LongDouble d){  // Can't find a reference for how many digits it enough for long  // double. 40 is probably enough.  char buffer[1024];  sprintf(buffer, "%.40Lg", d);  char* c = buffer;  if (*c == '-') ++c;  for (; *c; c++) {    if (!isdigit(*c))      break;  }  if (*c == '\0') {    *c++ = '.';    *c++ = '0';    *c++ = '\0';  }  printf("%s", buffer);}#endifstatic voidprintwchar(IDL_WChar wc){  if (wc == '\\')    printf("L'\\\\'");  else if (wc < 0xff && isprint(wc))    printf("L'%c'", (char)wc);  else    printf("L'\\u%04x", (int)wc);}static voidprintwstring(const IDL_WChar* ws){  printf("L\"");  int i, wc;  for (i=0; ws[i]; ++i) {    wc = ws[i];    if (wc == '\\')      printf("\\\\");    else if (wc < 0xff && isprint(wc))      putchar(wc);    else      printf("\\u%04x", (int)wc);  }  putchar('"');}voidDumpVisitor::visitConst(Const* c){  printf("const ");  c->constType()->accept(*this);  printf(" %s = ", c->identifier());  switch(c->constKind()) {  case IdlType::tk_short:   printf("%hd", c->constAsShort());           break;  case IdlType::tk_long:    printf("%ld", (long)c->constAsLong());      break;  case IdlType::tk_ushort:  printf("%hu", c->constAsUShort());          break;  case IdlType::tk_ulong:   printf("%lu", (unsigned long)c->constAsULong());                                                                        break;  case IdlType::tk_float:   printdouble(c->constAsFloat());             break;  case IdlType::tk_double:  printdouble(c->constAsDouble());            break;  case IdlType::tk_boolean:    printf("%s", c->constAsBoolean() ? "TRUE" : "FALSE");    break;  case IdlType::tk_char:    printf("'");    printChar(c->constAsChar());    printf("'");    break;  case IdlType::tk_octet:   printf("%d",     (int)c->constAsOctet());  break;  case IdlType::tk_string:    printf("\"");    printString(c->constAsString());    printf("\"");    break;#ifdef HAS_LongLong  case IdlType::tk_longlong:  printf("%Ld", c->constAsLongLong());     break;  case IdlType::tk_ulonglong: printf("%Lu", c->constAsULongLong());    break;#endif#ifdef HAS_LongDouble  case IdlType::tk_longdouble:printlongdouble(c->constAsLongDouble()); break;#endif  case IdlType::tk_wchar:     printwchar(c->constAsWChar());           break;  case IdlType::tk_wstring:   printwstring(c->constAsWString());       break;  case IdlType::tk_fixed:    {      char* fs = c->constAsFixed()->asString();      printf("%sd", fs);      delete [] fs;    }    break;  case IdlType::tk_enum:      c->constAsEnumerator()->accept(*this);   break;  default:    assert(0);  }}voidDumpVisitor::visitDeclarator(Declarator* d){  printf("%s", d->identifier());  for (ArraySize* s = d->sizes(); s; s = s->next())    printf("[%d]", s->size());}voidDumpVisitor::visitTypedef(Typedef* t){  printf("typedef ");  if (t->constrType()) {     assert(t->aliasType()->kind() == IdlType::tk_struct ||	   t->aliasType()->kind() == IdlType::tk_union  ||	   t->aliasType()->kind() == IdlType::tk_enum);    ((DeclaredType*)t->aliasType())->decl()->accept(*this);  }  else    t->aliasType()->accept(*this);  printf(" ");  for (Declarator* d = t->declarators(); d; d = (Declarator*)d->next()) {    d->accept(*this);    if (d->next()) printf(", ");  }}voidDumpVisitor::visitMember(Member* m){  if (m->constrType()) {    assert(m->memberType()->kind() == IdlType::tk_struct ||	   m->memberType()->kind() == IdlType::tk_union  ||	   m->memberType()->kind() == IdlType::tk_enum);    ((DeclaredType*)m->memberType())->decl()->accept(*this);  }  else    m->memberType()->accept(*this);  printf(" ");  for (Declarator* d = m->declarators(); d; d = (Declarator*)d->next()) {    d->accept(*this);    if (d->next())      printf(", ");  }}voidDumpVisitor::visitStruct(Struct* s){  printf("struct %s { // RepoId = %s%s\n", s->identifier(), s->repoId(),	 s->recursive() ? " recursive" : "");  ++indent_;  for (Member* m = s->members(); m; m = (Member*)m->next()) {    printIndent();    m->accept(*this);    printf(";\n");  }  --indent_;  printIndent();  printf("}");}voidDumpVisitor::visitStructForward(StructForward* s){  printf("struct %s", s->identifier());}voidDumpVisitor::visitException(Exception* e){  printf("exception %s {\n", e->identifier());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?