/* $Id: Cname.cc,v 1.4 1997/07/30 17:46:13 isis Exp $ */ // $Log: Cname.cc,v $ // Revision 1.4 1997/07/30 17:46:13 isis // Some minor new features and bug fixes // This seems to work fine // // Revision 1.3 1997/06/22 14:48:43 isis // This is the initial version for // the generating functions module // // Revision 1.2 1997/06/01 13:23:10 isis // Parsing of const: // e.g const char const * const a; // Changed the internal represantation of '*' to support const qualifiers // // Revision 1.1.1.1 1997/05/18 12:15:40 isis // This is the initial distribution // of 'c-headers' using CVS // #include "Cname.h" #include "gc_stl.h" ostream& operator<<(ostream& out,Cname &a) { return a.Print(out); } void Cname::RemoveArgs(bool all=false) { Cname *cur; cur=this; while(cur!=NULL) { if(cur->function) { cur->function=false; if(cur->parameters) { delete cur->parameters; cur->parameters=NULL; if(!all) break; } } cur=cur->next; } } void Cname::RemoveArgNames(void) { Cname *cur; cur=this; while(cur!=NULL) { if(cur->function) { if(cur->parameters) { gc_type_id_list::iterator i; for(i=cur->parameters->begin();i!=cur->parameters->end();i++) { (*i)->second->ReplaceName(""); (*i)->second->RemoveArgNames(); } } } cur=cur->next; } } bool Cname::ReplaceName(char *na) { Cname *cur; bool done=false; cur=this; while(cur!=NULL) { if( (cur->name[0])!='\0') { done=true; strcpy(cur->name,na); break; } cur=cur->next; } return done; } Cname::Cname(gc_Cstar_list *st,char *na,int wi,gc_int_list *ar,Cname *ne,bool cons) { if(st!=NULL) star_list=new gc_Cstar_list(*st); else star_list=NULL; if(na!=NULL) strncpy(name,na,80); else name[0]='\0'; width=wi; if(ar!=NULL) array_sizes=new gc_int_list(*ar); else array_sizes=NULL; next=ne; function=false; parameters=NULL; constant=cons; } Cname::Cname(Cname &op) { if(op.star_list!=NULL) { star_list=new gc_Cstar_list; gc_Cstar_list::iterator i; for(i=op.star_list->begin();i!=op.star_list->end();i++) star_list->push_back(new Cstar( *(*i) )); } else star_list=NULL; strncpy(name,op.name,80); width=op.width; if(op.array_sizes!=NULL) array_sizes=new gc_int_list(*op.array_sizes); else array_sizes=NULL; if(op.parameters!=NULL) { parameters=new gc_type_id_list; gc_type_id_list::iterator i; for(i=op.parameters->begin();i!=op.parameters->end();i++) { Cname *n_cn=new Cname( *((*i)->second) ); Ctype *n_ct=new Ctype( *((*i)->first) ); gc_type_id *np=new gc_type_id; np->first=n_ct; np->second=n_cn; parameters->push_back(np); } } else parameters=NULL; function=op.function; constant=op.constant; if(op.next) next=new Cname(*op.next); else next=NULL; } char* Ctype::Duration2Str(DurationStyle s) { switch(s) { case CONSTLIFE: return "const "; case VOLATILELIFE: return "volatile "; case DEFLIFE: return ""; default: assert(0==1); } return NULL; } char* Ctype::Modifier2Str(Modifier m) { switch(m) { case SHORT: return "short "; case LONG: return "long "; case LONGLONG: return "long long "; case DEFMOD: return ""; default: assert(0==1); } return NULL; } char* Ctype::Sign2Str(Sign s) { switch(s) { case SIGNED: return "signed "; case UNSIGNED: return "unsigned "; case DEFSIGN: return ""; default: assert(0==1); } return NULL; } char* Ctype::Style2Str(DefStyle st) { switch(st) { case DEFTYPED: return "typedef "; case STRUCTDEF: return "struct "; case UNIONDEF: return "union "; case ENUMDEF: return "enum "; case SIMPLEDEF: return ""; default: assert(0==1); } return NULL; } ostream& operator<<(ostream& out,Ctype &a) { out << Ctype::Sign2Str(a.sign_attr) << Ctype::Modifier2Str(a.mod_attr) << Ctype::Style2Str(a.style_attr); if(a.type_attr!=NULL) out << clean_Cname((a.type_attr)->GetCname()); return out; } Scope* Ctype::GetScope() { assert(style_attr!=SIMPLEDEF); return scope; } bool Ctype::IsSimple() { switch(style_attr) { case SIMPLEDEF: return true; case DEFTYPED: case STRUCTDEF: case UNIONDEF: case ENUMDEF: return false; default: assert(0==1); } return false; } Ctype::Ctype(TypeEntry *tt,Modifier m=DEFMOD,Sign s=DEFSIGN,DefStyle st=SIMPLEDEF,Scope *sc=NULL) { type_attr=tt; mod_attr=m; sign_attr=s; style_attr=st; duration_attr=DEFLIFE; scope=sc; } Ctype::Ctype(Ctype &op) { type_attr=op.type_attr; sign_attr=op.sign_attr; mod_attr=op.mod_attr; style_attr=op.style_attr; duration_attr=op.duration_attr; scope=op.scope; } void Ctype::SetLife(DurationStyle s) { duration_attr=s; } gc_enum_id::gc_enum_id() { name[0]='\0'; has_value=false; value=0; } gc_enum_id::gc_enum_id(char*str) { strncpy(name,str,80); has_value=false; value=0; } gc_enum_id::gc_enum_id(char*str,int val) { strncpy(name,str,80); has_value=true; value=val; } void gc_enum_id::SetValue(int val) { has_value=true; value=val; } ostream& Cname::Print(ostream&out) { out << '('; if(star_list!=NULL) { gc_Cstar_list::iterator i; int total=0; for(i=star_list->begin();i!=star_list->end();i++) total++; out << total; }else out << "no stars"; out << ',' << name << ','; if(array_sizes!=NULL) { gc_int_list::iterator i; for(i=array_sizes->begin();i!=array_sizes->end();i++) { out << '[' << (*i) << ']'; } }else out << "no array"; if(width!=0) out << ",:" << width; else out << ",no width,"; if(function) { out << '('; gc_type_id_list::iterator i; if(parameters!=NULL) { //Maybe void i=parameters->begin(); out << *(*i)->first << ' ' << *(*i)->second; i++; for(;i!=parameters->end();i++) { out << ','; out << *(*i)->first << ' ' << *(*i)->second; } } out << "),"; }else out << "no function,"; if(next!=NULL) { out << '(' << *next << ')'; }else out << "no next"; out << ')'; return out; } void make_cname_function(Cname *cn,gc_type_id_list *li) { cn->parameters=li; cn->function=true; }