-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassdesc.cc
1717 lines (1579 loc) · 59.5 KB
/
classdesc.cc
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
/* Analyse all class types within a preprocessed file, and output an
action statement for all parent classes and members */
#include <assert.h>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <ctype.h>
using namespace std;
#include "tokeninput.h"
#include "classdescVersion.h"
// for parsing Objective flag
int objc = 0;
bool respect_private=false; /* default is to call descriptor on private members */
bool typeName=false; // generate type and enum symbol tables
bool onBase=false;
bool use_mbr_pointers=false; // use member pointers for referencing member objects
bool qt=false; // true for processing Qt MOC header files
bool overloadingAllowed=false;
// name of file being processed
string inputFile="stdin";
int errorcode=0;
// start of a legal C++ identifier
inline bool isIdentifierStart(char x)
{return x=='_'||isalpha(x);}
// valid interior character of legal identifier
inline bool isIdentifierChar(char x)
{return x=='_'||isalnum(x);}
template <class T> std::string str(T x) {
std::ostringstream s;
s<<x;
return s.str();
}
// removes white space from beginning and end
inline std::string trimWS(const std::string& s)
{
int start=0, end=s.length()-1;
while (start<int(s.length()) && isspace(s[start])) ++start;
while (end>=0 && isspace(s[end])) --end;
if (end>=start)
return s.substr(start,end-start+1);
else
return "";
}
/* store the action strings (arguments passed) for base classes and
members, for each class parsed */
struct act_pair
{
string name, action, member;
bool is_static; // is a static member
bool base; // is a base (not a member)
act_pair(string n, string a, string m="", bool is_static=false, bool b=false):
name(n), action(a), member(m), is_static(is_static), base(b) {}
};
typedef vector<act_pair> actionlist_t;
struct action_t
{
string type, templ, tnTempl, namespace_name;
actionlist_t actionlist;
action_t(string t, actionlist_t a, string ns, string te="")
{
type=t; actionlist=a; templ=te; namespace_name=ns;
string::size_type p=namespace_name.rfind("::");
namespace_name.erase( p==string::npos? 0:p, string::npos);
// TODO: handle template template parameters properly. For now,
// ignore the problem in a syntactically valid way
if (templ.find("template")!=string::npos)
tnTempl=templ;
else
// extract template typename parameters from templ, and replace with typename calls
for (size_t i=0; i<templ.size(); )
{
// pick first of class or template
size_t j=templ.find("class",i);
if (j==string::npos) j=templ.size();
size_t jj=templ.find("typename",i);
if (jj!=string::npos && jj<j)
j=jj;
tnTempl+=te.substr(i,j-i);
j=templ.find_first_of(" \t.",j); // . for handling variadic arguments
if (j==string::npos) break; // we're done
j=templ.find_first_not_of(" \t",j);
bool variadic=templ.substr(j,3)=="...";
j=templ.find_first_not_of(" \t.",j); // . for handling variadic arguments
if (j==string::npos) break; // we're done
int braces=0;
// grab out full type name
for (i=j; i<templ.size(); ++i)
{
switch(templ[i])
{
case '<':
++braces; continue;
case '>':
if (--braces==0) break;
continue;
default:
if (isalnum(templ[i]) || templ[i]=='_' || braces>0)
continue;
else
break;
}
break;
}
if (variadic)
tnTempl+="\"+varTn<"+templ.substr(j,i-j)+"...>()+\"";
else
tnTempl+="\"+typeName<"+templ.substr(j,i-j)+">()+\"";
}
}
};
// predicate indicating invalid preprocessor token character
struct NotAlNumUnderScore
{
bool operator()(char x) const {
return !isIdentifierChar(x);
}
};
#if 0 // used for debugging purposes
class actions_t: public vector<action_t>
{
public:
void push_back(action_t x)
{
cerr << "registering:" << x.type << endl;
vector<action_t>::push_back(x);
}
} actions;
#else
vector<action_t> actions;
#endif
// store nested types here
map<string,vector<string> > nested;
/* store a list of virtual functions here */
typedef hash_set<string> virt_list_t;
hash_map<string,virt_list_t> virtualsdb;
string namespace_name="::";
/* type_defined[class]=1 if defined, else 0 if only declared */
struct type_defined_t: hash_map<string,int>
{
void defined(string name) {(*this)[name]=1; }
void declared(string name)
{
/* strip class qualifiers */
if (name.rfind("::")!=name.npos)
name=name.substr(name.rfind("::")+2);
/* don't register template declarations */
if (name.find("<")!=name.npos) return;
if (count(name)==0 || !(*this)[name])
{ (*this)[name]=0; }
}
} type_defined;
std::map<std::string, std::vector<std::string> > enum_keys;
// insert a typename keyword on types with :: qualification
string insertTypename(string type)
{
// replace whitespace characters with space
for (size_t i=0; i<type.size(); ++i)
if (isspace(type[i]))
type[i]=' ';
if (type.find("::")==string::npos || type.find("typename ")!=string::npos)
return type; // nothing needs to be done
type=trimWS(type);
static const int constSize=strlen("const ");
if (type.find("const ")==0)
// insert typename between const and type
return "const typename "+type.substr(constSize);
else
return "typename "+type;
}
// overloaded member database
struct MemberSig
{
string declName, returnType, argList, prefix, name;
enum Type {none, is_const, is_static, is_constructor};
Type type;
MemberSig(string d, string r, string a, string p, string n, Type t):
declName(d), returnType(r), argList(a), prefix(p), name(n), type(t) {}
string declare() {
switch (type)
{
case none:
return insertTypename(returnType)+"("+prefix+"*"+declName+")("+argList+")=&"+prefix+name+";";
case is_const:
return insertTypename(returnType)+"("+prefix+"*"+declName+")("+argList+") const=&"+prefix+name+";";
case is_static:
return insertTypename(returnType)+"(*"+declName+")("+argList+")=&"+prefix+name+";";
case is_constructor:
return "void (*"+declName+")("+argList+")=0;";
default: assert(false); return ""; // should never be executed, - statement to silence warning
}
}
};
map<string, vector<MemberSig> > overloadTempVarDecls;
// quick and dirty tokenizer
set<string> token_set(const string& s)
{
set<string> r;
string::size_type start, end;
for (start=0; start<s.size(); start=end)
{
// advance to next alpha
for (; start<s.size() && !isIdentifierStart(s[start]); ++start);
//find end of token
for (end=start+1; end<s.size() && isIdentifierChar(s[end]); ++end);
if (start<s.size() && start<end)
r.insert(s.substr(start,end-start));
}
return r;
}
void assign_enum_action(tokeninput& input, string prefix)
{
string enumname="enum ";
if (input.token=="class")
{
input.nexttok();
enumname+="class ";
}
enumname+=prefix + input.token;
string bareEnumName=input.token;
input.nexttok();
if (input.token==":") // type specification supplied, so skip it
while (!strchr(";{",input.token[0])) input.nexttok();
if (input.token=="{") /* only assign action if definition, not declaration */
{
nested[prefix].push_back(bareEnumName);
// load up the list of enum symbols
for ( ; input.token != ";"; input.nexttok())
{
if (input.token == "=" || input.token == "," || input.token=="}")
if (input.lasttoken!=",") //not sure if this is correct C++, but
//compilers accept blank enumerator-definitions
enum_keys[enumname].push_back(input.lasttoken);
if (input.token == "=") {
for (input.nexttok(); input.token!="," && input.token!="}";
input.nexttok()); //skip value
}
}
actionlist_t actionlist;
if (typeName)
actionlist.push_back(act_pair("","classdesc::enum_handle(arg)"));
else
actionlist.push_back(act_pair("","(int&)arg"));
actions.push_back(action_t(enumname,actionlist,namespace_name));
}
}
void parse_typedef(tokeninput& input, string prefix="");
void assign_class_action(tokeninput& input, string prefix,
string template_args, string classargs,
bool is_private);
// search and replace any shorn class types with fully qualified versions
string replaceShornTypes(const string& input, const string& shornType, const string& fullType)
{
string r;
for (size_t i=0, j=input.find(shornType); i!=string::npos; i=j, j=input.find(shornType,i))
{
r+=input.substr(i,j);
if (j!=string::npos)
{
j+=shornType.size();
size_t k=j;
// check if '<' is the next non white space character
while (k<input.size() && isspace(input[k])) ++k;
if (k>=input.size() || input[k]!='<')
r+=fullType;
else
r+=shornType;
}
}
return r;
}
class register_classname
{
actionlist_t& actionlist;
public:
bool is_static;
bool is_const;
bool is_private;
register_classname(actionlist_t& a, bool p):
actionlist(a), is_static(false), is_const(false), is_private(p) {}
void register_class(string name, string memberName, string action)
{
if (!is_private) /* don't register private, if privates respected */
{
// We need to treat static const members differently, as simple
// ones do not have addresses
if (is_static)
{
if (is_const)
actionlist.push_back(act_pair("."+name,"classdesc::is_const_static(),"+action));
else
actionlist.push_back(act_pair("."+name,action,memberName,is_static));
}
else
actionlist.push_back(act_pair("."+name,action,memberName,is_static));
}
is_static=false;
}
};
actionlist_t parse_class(tokeninput& input, bool is_class, string prefix="::", string targs="")
{
actionlist_t actionlist;
string varname="arg", targs1;
int is_virtual=0;
bool is_template=false, is_private=is_class; // track private, protected decls
virt_list_t virt_list;
register_classname reg(actionlist, respect_private && is_class);
hash_map<string,int> num_instances;
string baseclass;
string argList;
string rType;
string returnType;
set<string> usingNames;
/* handle inheritance */
for (;; input.nexttok() )
{
if (input.token=="virtual") continue;
if (input.token==":" ||
input.token=="private" || input.token=="protected" ||
input.token=="public")
{
is_private= (is_class && input.token!="public") ||
(!is_class && (input.token=="private" || input.token=="protected"));
continue;
}
if (input.token=="<")
{
for (int ang_count=0; ang_count ||
(input.lasttoken!=">" && input.lasttoken!=">>");
input.nexttok())
{
baseclass += input.token+" ";
if (input.token=="<") ang_count++;
if (input.token==">")
{
ang_count--;
if (ang_count) baseclass+=" ";/* handle nested templates */
}
else if (input.token==">>")
{
if (ang_count>1)
ang_count-=2;
else
ang_count--;
if (ang_count) baseclass+=" ";/* handle nested templates */
}
}
}
if (strchr(",{", input.token[0]) && baseclass.length())
{
// work out whether a typename qualifier is needed. Check
// whether any component of baseclass is contained in the
// parameter list of the template arguments
set<string> arg_toks=token_set(targs),
base_toks=token_set(baseclass), args_in_base;
// set intersection
for (set<string>::iterator i=base_toks.begin();
i!=base_toks.end(); ++i)
if (arg_toks.count(*i))
args_in_base.insert(*i);
// find last :: that is not in a template argument
int dbl_colon=int(baseclass.size())-2;
int numAngle=baseclass[dbl_colon+1]=='>';
for (; dbl_colon>=0; dbl_colon--)
{
switch (baseclass[dbl_colon])
{
case '>':
numAngle++;
break;
case '<':
numAngle--;
break;
case ':':
if (numAngle==0 && baseclass[dbl_colon+1]==':')
goto dbl_colon_found;
break;
}
}
dbl_colon_found:
bool typename_needed=false;
if (dbl_colon>=0)
for (set<string>::iterator i=args_in_base.begin();
i!=args_in_base.end(); ++i)
if (int(baseclass.find(*i))<dbl_colon)
{
typename_needed=true;
break;
}
if (!respect_private || !is_private)
actionlist.push_back
(act_pair("",
"classdesc::base_cast<"+
// look for member qualification in a template context
string(typename_needed? "typename ": "")+
baseclass+" >::cast("+varname+")",
onBase? baseclass:"",false,/*!static*/
onBase
)
);
virt_list_t t=virtualsdb[baseclass+"::"];
virt_list.insert(t.begin(),t.end());
baseclass.erase();
// reset is_private flag
is_private=is_class;
}
else
baseclass += input.token;
if (input.token=="{") break;
}
/* now handle members */
for (input.nexttok(); input.token!="}"; input.nexttok() )
{
// handle qt macros
if (qt)
{
if (input.token=="Q_OBJECT") continue;
if (input.token=="Q_PROPERTY" || input.token=="Q_ENUMS" || input.token=="Q_CLASSINFO")
{
input.nexttok();
gobble_delimited(input,"(",")");
continue;
}
if (input.token=="signals" || input.token=="slots") // these appear to be reserved words in MOC C++
{
reg.is_private = is_private = input.token=="signals"; //In Qt4, signals are protected
gobble_delimited(input,"",":");
continue;
}
}
rType += input.token + " ";
/* skip public/private etc labels */
if (input.token=="private" || input.token=="protected" ||
input.token=="public")
{
is_private = (input.token=="private" || input.token=="protected");
reg.is_private = respect_private && is_private;
gobble_delimited(input,"",":");
rType.erase();
continue; /* skip ':' */
}
if (input.token=="typedef")
{
/* grab typedef name to handle nested type definitions */
tokeninput::mark_t mark=input;
while (input.token!=";")
{
if (input.token=="{") gobble_delimited(input,"{","}");
input.nexttok();
}
string key=prefix;
if (key.empty()||key[0]!=':') key="::"+key;
if (isIdentifierStart(input.lasttoken[0]) && !is_private) //ignore any strange function type typedefs
nested[prefix].push_back(input.lasttoken);
input=mark;
parse_typedef(input,prefix);
while (input.token!=";") input.nexttok();
continue;
}
if (input.token=="template")
{
is_template=true;
input.nexttok();
targs1=get_template_args(input);
if ((input.token!="class" && input.token!="struct" )
|| targs1=="< > ")
{
targs1.erase(); /* not a template class */
}
}
if (input.token=="using") //ignore using declarations
{
string lastToken;
while (input.token!=";")
{
lastToken=input.token;
input.nexttok();
}
usingNames.insert('.'+lastToken);
continue;
}
if (input.token=="class" || input.token=="struct")
{
/* if boths targs and targs1 are defined, then we must
merge template arguments */
string targs2;
if (targs.length() && targs1.length())
/* strip angle brackets and insert into targs */
targs2=targs.substr(0,targs.find('>')) + "," +
targs1.substr(1,targs1.find('>'));
else
targs2=targs+targs1;
input.nexttok();
if (!is_private && !is_template) nested[prefix].push_back(input.token);
/* handle new templated typename rules */
if (targs.length()) input.lasttoken="typename";
if (isIdentifierStart(input.token[0])) /* named class */
assign_class_action(input,prefix,targs2,targs1,is_private);
else
{
cerr <<inputFile<<":"<<input.line()<<"::error";
cerr << "anonymous structs not allowed within class defs\n";
errorcode=1;
#if 0
vector<string> sublist=parse_class(input,"#");
/* find var name - checking it is not an array aaargh!*/
for (vector<string>::iterator i=sublist.begin();
i!=sublist.end(); i++)
i->replace(i->find("#"),1,varname);
#endif
}
targs1.erase();
continue;
}
if (input.token=="enum")
{
input.nexttok();
if (!is_private &&
isIdentifierStart(input.token[0])) /* named enum */
{
assign_enum_action(input,prefix);
}
else
gobble_delimited(input,"","}");
}
/* handle unnamed unions as a special case */
if (input.lasttoken=="union" && input.token=="{")
{
gobble_delimited(input,"{","}");
while (input.token!=";") input.nexttok();
if (isIdentifierStart(input.lasttoken[0]))
reg.register_class
(input.lasttoken,input.lasttoken,
"classdesc::is_array(),(char&)("+varname+"."+input.lasttoken+
"),1,sizeof("+varname+"."+input.lasttoken+")");
continue;
}
/* scalar member type */
if (strchr("{;,:=",input.token[0]) && input.token!="::")
{ rType.erase();
if (isIdentifierStart(input.lasttoken[0]))
{
// One cannot create a member pointer of a reference
// member, so we need to use old style methods to access
// it
if (use_mbr_pointers && input.tokenBeforeLast!="&")
reg.register_class(input.lasttoken,
varname,"&"+prefix+input.lasttoken);
else
reg.register_class(input.lasttoken,"",
varname+"." + input.lasttoken );
}
if (input.token[0]=='{')
gobble_delimited(input,"{","}");
/* skip over field specifier or initializer */
while (!strchr(";,",input.token[0])) input.nexttok();
}
/* operator members - discard these */
if (input.token=="operator")
{
input.nexttok();
if (input.token=="()") input.nexttok(); /* skip () if operator() */
/* find and skip arguments */
while (input.token[0]!='(') input.nexttok();
if (input.token=="()") input.nexttok();
else
gobble_delimited(input,"(",")");
/* skip to end of statement */
while (!strchr("{;",input.token[0])) input.nexttok();
reg.is_static=false;
}
// alignas declarations need to be discarded
if (input.token=="alignas")
{input.nexttok(); gobble_delimited(input,"(",")");}
if (input.token=="virtual") is_virtual=true;
if (input.token=="static") reg.is_static=true;
if (input.token=="const") reg.is_const=true;
if (input.token=="friend") /* skip friend statement */
while (input.token!=";")
input.nexttok();
if (input.token[0]=='(') /* member functions, or function pointers */
{
string memname=input.lasttoken; // function name
bool is_destructor=input.tokenBeforeLast=="~";
argList.erase();
/* check ahead for function pointers */
if (input.token!="()")
{
input.nexttok();
if (isIdentifierStart(input.token[0]) &&
(input.nexttok(),input.token=="::")) /* possible member pointer */
{
argList=input.lasttoken;
input.nexttok();
}
if (input.token=="*" && /* member/function pointer */
(input.lasttoken=="(" || input.lasttoken=="::"))
{
input.nexttok();
memname=input.token;
gobble_delimited(input,"(",")");
while (input.token[0]!='(') input.nexttok();
gobble_delimited(input,"(",")");
}
else if (input.token!=")") /* skip fn args */
argList += gobble_delimited(input,"(",")");
else
argList += input.lasttoken;
}
rType = rType.substr(0, rType.find(memname));
// look ahead for const or deleted specification
bool is_const=false, deleted=false;
while (!strchr(";{",input.token[0]))
{
input.nexttok();
if (input.token=="const") is_const=true;
if (input.token=="delete") deleted=true;
}
/* member functions require object passed as well*/
if (isIdentifierStart(memname[0]))
{
num_instances["."+memname]++;
if (is_virtual) virt_list.insert(memname);
/* strip out qualifier components, and template
arguments, for comparison with the member function
name */
string sprefix=prefix.substr(0,prefix.length()-2);
sprefix=sprefix.substr(0,sprefix.find('<'));
string::size_type start=sprefix.rfind("::");
if (start!=string::npos) sprefix=sprefix.substr(start+2);
MemberSig::Type type=MemberSig::none;
if (memname==sprefix && !is_destructor)
type=MemberSig::is_constructor;
else if (reg.is_static)
type=MemberSig::is_static;
else if (is_const)
type=MemberSig::is_const;
/* do not register constructors/destructors also do not
register virtual members or inline members - g++ has
trouble taking their addresses */
if ((type!=MemberSig::is_constructor || overloadingAllowed) &&
!is_destructor && !is_template && !deleted &&
memname!="CLASSDESC_ACCESS" &&
memname!="CLASSDESC_ACCESS_TEMPLATE")
{
string action;
string tempFnPtrDecl="TmpMemPtr_"+memname+str(num_instances["."+memname]);
if (type==MemberSig::is_constructor)
action="classdesc::is_constructor(),";
if (overloadingAllowed)
action+=tempFnPtrDecl;
else
action+="&"+ /*namespace_name +*/ prefix + memname;
// strip any default arguments from argList
string al;
int braceCnt=0;
bool include=true;
for (string::iterator i=argList.begin(); i!=argList.end(); ++i)
switch (*i)
{
case '=': include=false; break;
case '{': braceCnt++; break;
case '}': braceCnt--; break;
case ',': if (braceCnt==0) include=true;
/*fallthrough*/
default:
if (include) al+=*i;
}
if (prefix.find("<")!=string::npos)
{
string fullType=prefix.substr(0,prefix.length()-2);
// mentions this class type within templates are usually
// shorn of template arguments. Reinstate them
returnType=replaceShornTypes(returnType,sprefix,fullType);
al=replaceShornTypes(al,sprefix,fullType);
}
if (objc) { action += ", \"" + rType + "\", " + "\"" + argList + "\""; }
rType.erase();
if (overloadingAllowed && !reg.is_private)
overloadTempVarDecls[/*namespace_name+*/prefix].push_back
(MemberSig(tempFnPtrDecl,returnType,al,prefix,memname,type));
if (reg.is_static)
// static member functions cannot be attched to an
// object, however static object member pointers
// cannot be distinguished from a regular pointer,
// except by attaching it to an object. Go figure!
reg.register_class(memname, "", action);
else
reg.register_class(memname, varname, action);
}
}
reg.is_static=reg.is_const=false;
is_virtual=0;
is_template=false;
}
/* array member type */
if (input.token=="[")
{
int dim = 0;
string name=input.lasttoken;
string action = "classdesc::is_array(),"+varname+"." + name;
string action2 = ",";
if (objc) action2+="\"";
for (; input.token=="["; input.nexttok())
{
dim++;
input.nexttok();
for (;input.token!="]"; input.nexttok())
{
if (!objc)
action2 += " " + input.token;
else // objc
{
action2 += "[" + input.token + "]";
}
}
if (!objc) action2 += ",";
}
for (int i=0; i<dim; i++)
{ action += "[0]"; }
if (!objc) {char s[20]; sprintf(s,",%d",dim); action+=s;}
action += action2;
if (!objc) action.erase(action.size()-1,1); /* remove trailing "," */
else action+="\"";
reg.register_class(name,name,action);
}
if (input.token=="{") /* skip function/enum/whatever definitions */
gobble_delimited(input,"{","}");
// capture member function return type for overload processing
/* handle templated types */
if (input.token=="<")
returnType += " "+gobble_delimited(input,"<",">");
else if (strchr(":;{}()",input.lasttoken[0]) && input.lasttoken!="::")
returnType.clear();
else if (input.lasttoken!="virtual" && input.lasttoken!="static"
&& input.lasttoken!="inline")
returnType+=" "+input.lasttoken;
}
virtualsdb[prefix]=virt_list;
if (!overloadingAllowed)
{
/* remove overloaded member functions */
actionlist_t copy_of_alist;
string name;
for (size_t i=0; i<actionlist.size(); i++)
{
name=actionlist[i].name;
if (!usingNames.count(name) &&
(!num_instances.count(name) || num_instances[name]==1))
copy_of_alist.push_back(actionlist[i]);
}
return copy_of_alist;
}
return actionlist;
}
/* parse a typedef statement, handling anonymous structs, unions and
arrays - all other types are ignored */
class is_ptr {};
string get_typedef_name(tokeninput& input)
{
bool ptr=false;
for (; input.token!=";"; input.nexttok())
{
if (input.token=="{") gobble_delimited(input,"{","}");
ptr=input.lasttoken=="*";
}
if (ptr||input.lasttoken=="]")
throw is_ptr(); /* the buggers have probably declared a pointer to */
/* an anonymous struct data type */
return input.lasttoken;
}
void parse_typedef(tokeninput& input, string prefix)
{
actionlist_t actionlist;
input.nexttok();
if (input.token=="struct" || input.token=="class")
{
bool is_class=input.token=="class";
tokeninput::mark_t mark=input;
string name, structname;
input.nexttok();
if (isIdentifierStart(input.token[0])) structname=input.token;
while (!strchr(";:{",input.token[0])) input.nexttok();
if (input.token==";")
{type_defined.declared(structname); return;} /* nothing to be done */
try {name=prefix+get_typedef_name(input);}
catch (is_ptr) /* the buggers have declared a pointer to */
{ /* an anonymous struct data type */
/* check first to see if name already assigned to struct */
input=mark;
input.nexttok();
if (isIdentifierStart(input.token[0]))
name=input.token;
else
{
static int cnt=0; /* declare a temporary data type */
char tname[20]; /* 10^10 temp identifiers should be ample here!*/
sprintf(tname,"___ecotmp%-10d",cnt++);
name=tname;
input=mark;
cout << "#ifndef CLASSDESC_TYPENAME_"<<name<<endl;
cout << "#define CLASSDESC_TYPENAME_"<<name<<endl;
cout << input.token << " " << name << " ";
for (int nlvl=0; input.token!="}" || nlvl>0; )
{
input.nexttok();
if (input.token=="{") nlvl++;
if (input.token=="}") nlvl--;
cout << input.token << " ";
}
cout << ";" << endl;
cout << "#endif"<<endl;
}
}
input=mark;
while (!strchr(";:{",input.token[0])) input.nexttok();
// if a struct or class name has been provided, use that in
// preference to the typedef name - see ticket #11
if (!structname.empty()) name=prefix+structname;
actionlist=parse_class(input,is_class,name+"::");
type_defined.defined(structname);
actions.push_back(action_t(name,actionlist,namespace_name));
}
else if (input.token=="union")
{
actionlist.push_back(
act_pair("","classdesc::is_array(),(char&)arg,1,sizeof(arg)"));
try {
actions.push_back(action_t(prefix+get_typedef_name(input),actionlist,namespace_name));
}
catch (is_ptr)
{
actionlist_t actionlist;
actionlist.push_back(
act_pair("","arg"));
actions.push_back(action_t(prefix+input.lasttoken,actionlist,namespace_name));
}
}
else
while (input.token!=";") input.nexttok();
#if 0 /* what was I thinking here !!! */
else
{
/* otherwise search for an array definition */
while (input.token!="[" && input.token!=";") input.nexttok();
if (input.token=="[")
{
string size;
for (input.nexttok(); input.token!="]"; input.nexttok())
size+=input.token;
actions.push_back(action_t(prefix+get_typedef_name(input),actionlist,namespace_name));
}
}
#endif
}
/* create list of arguments used in a template */
string extract_targs(string targs)
{
if (targs.empty()) return targs; /* nothing to do */
string r="<", tok, elipsis;
const char *targsptr=targs.c_str()+1;
for (; *targsptr; targsptr++)
{
if (isIdentifierChar(*targsptr)) tok+=*targsptr;
else
{
while (isspace(*targsptr)) targsptr++;/* skip any spaces */
if (!*targsptr) break; /* end of string already */
else if (*targsptr=='<') /* skip over stuff between <..> */
{
int angle_count=1;
while (angle_count)
{
targsptr++;
if (*targsptr=='<') angle_count++;
else if (*targsptr=='>') angle_count--;
}
}
else if (*targsptr=='=')
{
for (int angle_count=0;
!strchr(">,",*(targsptr)) || angle_count; targsptr++)
if (*targsptr=='<') angle_count++;
else if (*targsptr=='>') angle_count--;
// not sure, but inserting elipsis here might be a C++
// grammatical error
r+=tok+elipsis+*targsptr;
elipsis.clear();
}
else if (strchr(">,",*targsptr))
{
r+=tok+elipsis+*targsptr;
elipsis.clear();
}
else if (isIdentifierChar(*targsptr))
targsptr--; /* rewind ptr so next loop starts token */
else if (*targsptr=='.')
elipsis+=*targsptr;
tok.erase();
}
}
return r;
}
void assign_class_action(tokeninput& input, string prefix, string targs, string classargs, bool is_private)
{
/* refer to class/struct type with explicit class/struct leadin
identifier, so as to avoid typename being hidden by
object/function declarations */