ebrowse.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831
  1. /* ebrowse.c --- parsing files for the ebrowse C++ browser
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <stddef.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <assert.h>
  21. #include <getopt.h>
  22. /* The SunOS compiler doesn't have SEEK_END. */
  23. #ifndef SEEK_END
  24. #define SEEK_END 2
  25. #endif
  26. /* Conditionalize function prototypes. */
  27. /* Value is non-zero if strings X and Y compare equal. */
  28. #define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)
  29. #include <min-max.h>
  30. /* Files are read in chunks of this number of bytes. */
  31. #define READ_CHUNK_SIZE (100 * 1024)
  32. #if defined (__MSDOS__)
  33. #define FILENAME_EQ(X,Y) (strcasecmp (X,Y) == 0)
  34. #else
  35. #if defined (WINDOWSNT)
  36. #define FILENAME_EQ(X,Y) (stricmp (X,Y) == 0)
  37. #else
  38. #define FILENAME_EQ(X,Y) (streq (X,Y))
  39. #endif
  40. #endif
  41. /* The default output file name. */
  42. #define DEFAULT_OUTFILE "BROWSE"
  43. /* A version string written to the output file. Change this whenever
  44. the structure of the output file changes. */
  45. #define EBROWSE_FILE_VERSION "ebrowse 5.0"
  46. /* The output file consists of a tree of Lisp objects, with major
  47. nodes built out of Lisp structures. These are the heads of the
  48. Lisp structs with symbols identifying their type. */
  49. #define TREE_HEADER_STRUCT "[ebrowse-hs "
  50. #define TREE_STRUCT "[ebrowse-ts "
  51. #define MEMBER_STRUCT "[ebrowse-ms "
  52. #define CLASS_STRUCT "[ebrowse-cs "
  53. /* The name of the symbol table entry for global functions, variables,
  54. defines etc. This name also appears in the browser display. */
  55. #define GLOBALS_NAME "*Globals*"
  56. /* Token definitions. */
  57. enum token
  58. {
  59. YYEOF = 0, /* end of file */
  60. CSTRING = 256, /* string constant */
  61. CCHAR, /* character constant */
  62. CINT, /* integral constant */
  63. CFLOAT, /* real constant */
  64. ELLIPSIS, /* ... */
  65. LSHIFTASGN, /* <<= */
  66. RSHIFTASGN, /* >>= */
  67. ARROWSTAR, /* ->* */
  68. IDENT, /* identifier */
  69. DIVASGN, /* /= */
  70. INC, /* ++ */
  71. ADDASGN, /* += */
  72. DEC, /* -- */
  73. ARROW, /* -> */
  74. SUBASGN, /* -= */
  75. MULASGN, /* *= */
  76. MODASGN, /* %= */
  77. LOR, /* || */
  78. ORASGN, /* |= */
  79. LAND, /* && */
  80. ANDASGN, /* &= */
  81. XORASGN, /* ^= */
  82. POINTSTAR, /* .* */
  83. DCOLON, /* :: */
  84. EQ, /* == */
  85. NE, /* != */
  86. LE, /* <= */
  87. LSHIFT, /* << */
  88. GE, /* >= */
  89. RSHIFT, /* >> */
  90. /* Keywords. The undef's are there because these
  91. three symbols are very likely to be defined somewhere. */
  92. #undef BOOL
  93. #undef TRUE
  94. #undef FALSE
  95. ASM, /* asm */
  96. AUTO, /* auto */
  97. BREAK, /* break */
  98. CASE, /* case */
  99. CATCH, /* catch */
  100. CHAR, /* char */
  101. CLASS, /* class */
  102. CONST, /* const */
  103. CONTINUE, /* continue */
  104. DEFAULT, /* default */
  105. DELETE, /* delete */
  106. DO, /* do */
  107. DOUBLE, /* double */
  108. ELSE, /* else */
  109. ENUM, /* enum */
  110. EXTERN, /* extern */
  111. FLOAT, /* float */
  112. FOR, /* for */
  113. FRIEND, /* friend */
  114. GOTO, /* goto */
  115. IF, /* if */
  116. T_INLINE, /* inline */
  117. INT, /* int */
  118. LONG, /* long */
  119. NEW, /* new */
  120. OPERATOR, /* operator */
  121. PRIVATE, /* private */
  122. PROTECTED, /* protected */
  123. PUBLIC, /* public */
  124. REGISTER, /* register */
  125. RETURN, /* return */
  126. SHORT, /* short */
  127. SIGNED, /* signed */
  128. SIZEOF, /* sizeof */
  129. STATIC, /* static */
  130. STRUCT, /* struct */
  131. SWITCH, /* switch */
  132. TEMPLATE, /* template */
  133. THIS, /* this */
  134. THROW, /* throw */
  135. TRY, /* try */
  136. TYPEDEF, /* typedef */
  137. UNION, /* union */
  138. UNSIGNED, /* unsigned */
  139. VIRTUAL, /* virtual */
  140. VOID, /* void */
  141. VOLATILE, /* volatile */
  142. WHILE, /* while */
  143. MUTABLE, /* mutable */
  144. BOOL, /* bool */
  145. TRUE, /* true */
  146. FALSE, /* false */
  147. SIGNATURE, /* signature (GNU extension) */
  148. NAMESPACE, /* namespace */
  149. EXPLICIT, /* explicit */
  150. TYPENAME, /* typename */
  151. CONST_CAST, /* const_cast */
  152. DYNAMIC_CAST, /* dynamic_cast */
  153. REINTERPRET_CAST, /* reinterpret_cast */
  154. STATIC_CAST, /* static_cast */
  155. TYPEID, /* typeid */
  156. USING, /* using */
  157. WCHAR /* wchar_t */
  158. };
  159. /* Storage classes, in a wider sense. */
  160. enum sc
  161. {
  162. SC_UNKNOWN,
  163. SC_MEMBER, /* Is an instance member. */
  164. SC_STATIC, /* Is static member. */
  165. SC_FRIEND, /* Is friend function. */
  166. SC_TYPE /* Is a type definition. */
  167. };
  168. /* Member visibility. */
  169. enum visibility
  170. {
  171. V_PUBLIC,
  172. V_PROTECTED,
  173. V_PRIVATE
  174. };
  175. /* Member flags. */
  176. #define F_VIRTUAL 1 /* Is virtual function. */
  177. #define F_INLINE 2 /* Is inline function. */
  178. #define F_CONST 4 /* Is const. */
  179. #define F_PURE 8 /* Is pure virtual function. */
  180. #define F_MUTABLE 16 /* Is mutable. */
  181. #define F_TEMPLATE 32 /* Is a template. */
  182. #define F_EXPLICIT 64 /* Is explicit constructor. */
  183. #define F_THROW 128 /* Has a throw specification. */
  184. #define F_EXTERNC 256 /* Is declared extern "C". */
  185. #define F_DEFINE 512 /* Is a #define. */
  186. /* Two macros to set and test a bit in an int. */
  187. #define SET_FLAG(F, FLAG) ((F) |= (FLAG))
  188. #define HAS_FLAG(F, FLAG) (((F) & (FLAG)) != 0)
  189. /* Structure describing a class member. */
  190. struct member
  191. {
  192. struct member *next; /* Next in list of members. */
  193. struct member *anext; /* Collision chain in member_table. */
  194. struct member **list; /* Pointer to list in class. */
  195. unsigned param_hash; /* Hash value for parameter types. */
  196. int vis; /* Visibility (public, ...). */
  197. int flags; /* See F_* above. */
  198. char *regexp; /* Matching regular expression. */
  199. const char *filename; /* Don't free this shared string. */
  200. int pos; /* Buffer position of occurrence. */
  201. char *def_regexp; /* Regular expression matching definition. */
  202. const char *def_filename; /* File name of definition. */
  203. int def_pos; /* Buffer position of definition. */
  204. char name[FLEXIBLE_ARRAY_MEMBER]; /* Member name. */
  205. };
  206. /* Structures of this type are used to connect class structures with
  207. their super and subclasses. */
  208. struct link
  209. {
  210. struct sym *sym; /* The super or subclass. */
  211. struct link *next; /* Next in list or NULL. */
  212. };
  213. /* Structure used to record namespace aliases. */
  214. struct alias
  215. {
  216. struct alias *next; /* Next in list. */
  217. struct sym *namesp; /* Namespace in which defined. */
  218. struct link *aliasee; /* List of aliased namespaces (A::B::C...). */
  219. char name[FLEXIBLE_ARRAY_MEMBER]; /* Alias name. */
  220. };
  221. /* The structure used to describe a class in the symbol table,
  222. or a namespace in all_namespaces. */
  223. struct sym
  224. {
  225. int flags; /* Is class a template class?. */
  226. unsigned char visited; /* Used to find circles. */
  227. struct sym *next; /* Hash collision list. */
  228. struct link *subs; /* List of subclasses. */
  229. struct link *supers; /* List of superclasses. */
  230. struct member *vars; /* List of instance variables. */
  231. struct member *fns; /* List of instance functions. */
  232. struct member *static_vars; /* List of static variables. */
  233. struct member *static_fns; /* List of static functions. */
  234. struct member *friends; /* List of friend functions. */
  235. struct member *types; /* List of local types. */
  236. char *regexp; /* Matching regular expression. */
  237. int pos; /* Buffer position. */
  238. const char *filename; /* File in which it can be found. */
  239. const char *sfilename; /* File in which members can be found. */
  240. struct sym *namesp; /* Namespace in which defined. . */
  241. char name[FLEXIBLE_ARRAY_MEMBER]; /* Name of the class. */
  242. };
  243. /* Experimental: Print info for `--position-info'. We print
  244. '(CLASS-NAME SCOPE MEMBER-NAME). */
  245. #define P_DEFN 1
  246. #define P_DECL 2
  247. int info_where;
  248. struct sym *info_cls = NULL;
  249. struct member *info_member = NULL;
  250. /* Experimental. For option `--position-info', the buffer position we
  251. are interested in. When this position is reached, print out
  252. information about what we know about that point. */
  253. int info_position = -1;
  254. /* Command line options structure for getopt_long. */
  255. struct option options[] =
  256. {
  257. {"append", no_argument, NULL, 'a'},
  258. {"files", required_argument, NULL, 'f'},
  259. {"help", no_argument, NULL, -2},
  260. {"min-regexp-length", required_argument, NULL, 'm'},
  261. {"max-regexp-length", required_argument, NULL, 'M'},
  262. {"no-nested-classes", no_argument, NULL, 'n'},
  263. {"no-regexps", no_argument, NULL, 'x'},
  264. {"no-structs-or-unions", no_argument, NULL, 's'},
  265. {"output-file", required_argument, NULL, 'o'},
  266. {"position-info", required_argument, NULL, 'p'},
  267. {"search-path", required_argument, NULL, 'I'},
  268. {"verbose", no_argument, NULL, 'v'},
  269. {"version", no_argument, NULL, -3},
  270. {"very-verbose", no_argument, NULL, 'V'},
  271. {NULL, 0, NULL, 0}
  272. };
  273. /* Semantic values of tokens. Set by yylex.. */
  274. unsigned yyival; /* Set for token CINT. */
  275. char *yytext; /* Set for token IDENT. */
  276. char *yytext_end;
  277. /* Output file. */
  278. FILE *yyout;
  279. /* Current line number. */
  280. int yyline;
  281. /* The name of the current input file. */
  282. const char *filename;
  283. /* Three character class vectors, and macros to test membership
  284. of characters. */
  285. char is_ident[255];
  286. char is_digit[255];
  287. char is_white[255];
  288. #define IDENTP(C) is_ident[(unsigned char) (C)]
  289. #define DIGITP(C) is_digit[(unsigned char) (C)]
  290. #define WHITEP(C) is_white[(unsigned char) (C)]
  291. /* Command line flags. */
  292. int f_append;
  293. int f_verbose;
  294. int f_very_verbose;
  295. int f_structs = 1;
  296. int f_regexps = 1;
  297. int f_nested_classes = 1;
  298. /* Maximum and minimum lengths of regular expressions matching a
  299. member, class etc., for writing them to the output file. These are
  300. overridable from the command line. */
  301. int min_regexp = 5;
  302. int max_regexp = 50;
  303. /* Input buffer. */
  304. char *inbuffer;
  305. char *in;
  306. size_t inbuffer_size;
  307. /* Return the current buffer position in the input file. */
  308. #define BUFFER_POS() (in - inbuffer)
  309. /* If current lookahead is CSTRING, the following points to the
  310. first character in the string constant. Used for recognizing
  311. extern "C". */
  312. char *string_start;
  313. /* The size of the hash tables for classes.and members. Should be
  314. prime. */
  315. #define TABLE_SIZE 1001
  316. /* The hash table for class symbols. */
  317. struct sym *class_table[TABLE_SIZE];
  318. /* Hash table containing all member structures. This is generally
  319. faster for member lookup than traversing the member lists of a
  320. `struct sym'. */
  321. struct member *member_table[TABLE_SIZE];
  322. /* Hash table for namespace aliases */
  323. struct alias *namespace_alias_table[TABLE_SIZE];
  324. /* The special class symbol used to hold global functions,
  325. variables etc. */
  326. struct sym *global_symbols;
  327. /* The current namespace. */
  328. struct sym *current_namespace;
  329. /* The list of all known namespaces. */
  330. struct sym *all_namespaces;
  331. /* Stack of namespaces we're currently nested in, during the parse. */
  332. struct sym **namespace_stack;
  333. int namespace_stack_size;
  334. int namespace_sp;
  335. /* The current lookahead token. */
  336. int tk = -1;
  337. /* Structure describing a keyword. */
  338. struct kw
  339. {
  340. const char *name; /* Spelling. */
  341. int tk; /* Token value. */
  342. struct kw *next; /* Next in collision chain. */
  343. };
  344. /* Keywords are lookup up in a hash table of their own. */
  345. #define KEYWORD_TABLE_SIZE 1001
  346. struct kw *keyword_table[KEYWORD_TABLE_SIZE];
  347. /* Search path. */
  348. struct search_path
  349. {
  350. char *path;
  351. struct search_path *next;
  352. };
  353. struct search_path *search_path;
  354. struct search_path *search_path_tail;
  355. /* Function prototypes. */
  356. static char *matching_regexp (void);
  357. static struct sym *add_sym (const char *, struct sym *);
  358. static void add_global_defn (char *, char *, int, unsigned, int, int, int);
  359. static void add_global_decl (char *, char *, int, unsigned, int, int, int);
  360. static struct member *add_member (struct sym *, char *, int, int, unsigned);
  361. static void class_definition (struct sym *, int, int, int);
  362. static char *operator_name (int *);
  363. static void parse_qualified_param_ident_or_type (char **);
  364. /***********************************************************************
  365. Utilities
  366. ***********************************************************************/
  367. /* Print an error in a printf-like style with the current input file
  368. name and line number. */
  369. static void
  370. yyerror (const char *format, const char *s)
  371. {
  372. fprintf (stderr, "%s:%d: ", filename, yyline);
  373. fprintf (stderr, format, s);
  374. putc ('\n', stderr);
  375. }
  376. /* Like malloc but print an error and exit if not enough memory is
  377. available. */
  378. static void *
  379. xmalloc (size_t nbytes)
  380. {
  381. void *p = malloc (nbytes);
  382. if (p == NULL)
  383. {
  384. yyerror ("out of memory", NULL);
  385. exit (EXIT_FAILURE);
  386. }
  387. return p;
  388. }
  389. /* Like realloc but print an error and exit if out of memory. */
  390. static void *
  391. xrealloc (void *p, size_t sz)
  392. {
  393. p = realloc (p, sz);
  394. if (p == NULL)
  395. {
  396. yyerror ("out of memory", NULL);
  397. exit (EXIT_FAILURE);
  398. }
  399. return p;
  400. }
  401. /* Like strdup, but print an error and exit if not enough memory is
  402. available.. If S is null, return null. */
  403. static char *
  404. xstrdup (char *s)
  405. {
  406. if (s)
  407. return strcpy (xmalloc (strlen (s) + 1), s);
  408. return s;
  409. }
  410. /***********************************************************************
  411. Symbols
  412. ***********************************************************************/
  413. /* Initialize the symbol table. This currently only sets up the
  414. special symbol for globals (`*Globals*'). */
  415. static void
  416. init_sym (void)
  417. {
  418. global_symbols = add_sym (GLOBALS_NAME, NULL);
  419. }
  420. /* Add a symbol for class NAME to the symbol table. NESTED_IN_CLASS
  421. is the class in which class NAME was found. If it is null,
  422. this means the scope of NAME is the current namespace.
  423. If a symbol for NAME already exists, return that. Otherwise
  424. create a new symbol and set it to default values. */
  425. static struct sym *
  426. add_sym (const char *name, struct sym *nested_in_class)
  427. {
  428. struct sym *sym;
  429. unsigned h;
  430. const char *s;
  431. struct sym *scope = nested_in_class ? nested_in_class : current_namespace;
  432. for (s = name, h = 0; *s; ++s)
  433. h = (h << 1) ^ *s;
  434. h %= TABLE_SIZE;
  435. for (sym = class_table[h]; sym; sym = sym->next)
  436. if (streq (name, sym->name)
  437. && ((!sym->namesp && !scope)
  438. || (sym->namesp && scope
  439. && streq (sym->namesp->name, scope->name))))
  440. break;
  441. if (sym == NULL)
  442. {
  443. if (f_very_verbose)
  444. {
  445. putchar ('\t');
  446. puts (name);
  447. }
  448. sym = xmalloc (offsetof (struct sym, name) + strlen (name) + 1);
  449. memset (sym, 0, offsetof (struct sym, name));
  450. strcpy (sym->name, name);
  451. sym->namesp = scope;
  452. sym->next = class_table[h];
  453. class_table[h] = sym;
  454. }
  455. return sym;
  456. }
  457. /* Add links between superclass SUPER and subclass SUB. */
  458. static void
  459. add_link (struct sym *super, struct sym *sub)
  460. {
  461. struct link *lnk, *lnk2, *p, *prev;
  462. /* See if a link already exists. */
  463. for (p = super->subs, prev = NULL;
  464. p && strcmp (sub->name, p->sym->name) > 0;
  465. prev = p, p = p->next)
  466. ;
  467. /* Avoid duplicates. */
  468. if (p == NULL || p->sym != sub)
  469. {
  470. lnk = (struct link *) xmalloc (sizeof *lnk);
  471. lnk2 = (struct link *) xmalloc (sizeof *lnk2);
  472. lnk->sym = sub;
  473. lnk->next = p;
  474. if (prev)
  475. prev->next = lnk;
  476. else
  477. super->subs = lnk;
  478. lnk2->sym = super;
  479. lnk2->next = sub->supers;
  480. sub->supers = lnk2;
  481. }
  482. }
  483. /* Find in class CLS member NAME.
  484. VAR non-zero means look for a member variable; otherwise a function
  485. is searched. SC specifies what kind of member is searched---a
  486. static, or per-instance member etc. HASH is a hash code for the
  487. parameter types of functions. Value is a pointer to the member
  488. found or null if not found. */
  489. static struct member *
  490. find_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
  491. {
  492. struct member **list;
  493. struct member *p;
  494. unsigned name_hash = 0;
  495. char *s;
  496. int i;
  497. switch (sc)
  498. {
  499. case SC_FRIEND:
  500. list = &cls->friends;
  501. break;
  502. case SC_TYPE:
  503. list = &cls->types;
  504. break;
  505. case SC_STATIC:
  506. list = var ? &cls->static_vars : &cls->static_fns;
  507. break;
  508. default:
  509. list = var ? &cls->vars : &cls->fns;
  510. break;
  511. }
  512. for (s = name; *s; ++s)
  513. name_hash = (name_hash << 1) ^ *s;
  514. i = name_hash % TABLE_SIZE;
  515. for (p = member_table[i]; p; p = p->anext)
  516. if (p->list == list && p->param_hash == hash && streq (name, p->name))
  517. break;
  518. return p;
  519. }
  520. /* Add to class CLS information for the declaration of member NAME.
  521. REGEXP is a regexp matching the declaration, if non-null. POS is
  522. the position in the source where the declaration is found. HASH is
  523. a hash code for the parameter list of the member, if it's a
  524. function. VAR non-zero means member is a variable or type. SC
  525. specifies the type of member (instance member, static, ...). VIS
  526. is the member's visibility (public, protected, private). FLAGS is
  527. a bit set giving additional information about the member (see the
  528. F_* defines). */
  529. static void
  530. add_member_decl (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int vis, int flags)
  531. {
  532. struct member *m;
  533. m = find_member (cls, name, var, sc, hash);
  534. if (m == NULL)
  535. m = add_member (cls, name, var, sc, hash);
  536. /* Have we seen a new filename? If so record that. */
  537. if (!cls->filename || !FILENAME_EQ (cls->filename, filename))
  538. m->filename = filename;
  539. m->regexp = regexp;
  540. m->pos = pos;
  541. m->flags = flags;
  542. switch (vis)
  543. {
  544. case PRIVATE:
  545. m->vis = V_PRIVATE;
  546. break;
  547. case PROTECTED:
  548. m->vis = V_PROTECTED;
  549. break;
  550. case PUBLIC:
  551. m->vis = V_PUBLIC;
  552. break;
  553. }
  554. info_where = P_DECL;
  555. info_cls = cls;
  556. info_member = m;
  557. }
  558. /* Add to class CLS information for the definition of member NAME.
  559. REGEXP is a regexp matching the declaration, if non-null. POS is
  560. the position in the source where the declaration is found. HASH is
  561. a hash code for the parameter list of the member, if it's a
  562. function. VAR non-zero means member is a variable or type. SC
  563. specifies the type of member (instance member, static, ...). VIS
  564. is the member's visibility (public, protected, private). FLAGS is
  565. a bit set giving additional information about the member (see the
  566. F_* defines). */
  567. static void
  568. add_member_defn (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
  569. {
  570. struct member *m;
  571. if (sc == SC_UNKNOWN)
  572. {
  573. m = find_member (cls, name, var, SC_MEMBER, hash);
  574. if (m == NULL)
  575. {
  576. m = find_member (cls, name, var, SC_STATIC, hash);
  577. if (m == NULL)
  578. m = add_member (cls, name, var, sc, hash);
  579. }
  580. }
  581. else
  582. {
  583. m = find_member (cls, name, var, sc, hash);
  584. if (m == NULL)
  585. m = add_member (cls, name, var, sc, hash);
  586. }
  587. if (!cls->sfilename)
  588. cls->sfilename = filename;
  589. if (!FILENAME_EQ (cls->sfilename, filename))
  590. m->def_filename = filename;
  591. m->def_regexp = regexp;
  592. m->def_pos = pos;
  593. m->flags |= flags;
  594. info_where = P_DEFN;
  595. info_cls = cls;
  596. info_member = m;
  597. }
  598. /* Add a symbol for a define named NAME to the symbol table.
  599. REGEXP is a regular expression matching the define in the source,
  600. if it is non-null. POS is the position in the file. */
  601. static void
  602. add_define (char *name, char *regexp, int pos)
  603. {
  604. add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
  605. add_global_decl (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
  606. }
  607. /* Add information for the global definition of NAME.
  608. REGEXP is a regexp matching the declaration, if non-null. POS is
  609. the position in the source where the declaration is found. HASH is
  610. a hash code for the parameter list of the member, if it's a
  611. function. VAR non-zero means member is a variable or type. SC
  612. specifies the type of member (instance member, static, ...). VIS
  613. is the member's visibility (public, protected, private). FLAGS is
  614. a bit set giving additional information about the member (see the
  615. F_* defines). */
  616. static void
  617. add_global_defn (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
  618. {
  619. int i;
  620. struct sym *sym;
  621. /* Try to find out for which classes a function is a friend, and add
  622. what we know about it to them. */
  623. if (!var)
  624. for (i = 0; i < TABLE_SIZE; ++i)
  625. for (sym = class_table[i]; sym; sym = sym->next)
  626. if (sym != global_symbols && sym->friends)
  627. if (find_member (sym, name, 0, SC_FRIEND, hash))
  628. add_member_defn (sym, name, regexp, pos, hash, 0,
  629. SC_FRIEND, flags);
  630. /* Add to global symbols. */
  631. add_member_defn (global_symbols, name, regexp, pos, hash, var, sc, flags);
  632. }
  633. /* Add information for the global declaration of NAME.
  634. REGEXP is a regexp matching the declaration, if non-null. POS is
  635. the position in the source where the declaration is found. HASH is
  636. a hash code for the parameter list of the member, if it's a
  637. function. VAR non-zero means member is a variable or type. SC
  638. specifies the type of member (instance member, static, ...). VIS
  639. is the member's visibility (public, protected, private). FLAGS is
  640. a bit set giving additional information about the member (see the
  641. F_* defines). */
  642. static void
  643. add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
  644. {
  645. /* Add declaration only if not already declared. Header files must
  646. be processed before source files for this to have the right effect.
  647. I do not want to handle implicit declarations at the moment. */
  648. struct member *m;
  649. struct member *found;
  650. m = found = find_member (global_symbols, name, var, sc, hash);
  651. if (m == NULL)
  652. m = add_member (global_symbols, name, var, sc, hash);
  653. /* Definition already seen => probably last declaration implicit.
  654. Override. This means that declarations must always be added to
  655. the symbol table before definitions. */
  656. if (!found)
  657. {
  658. if (!global_symbols->filename
  659. || !FILENAME_EQ (global_symbols->filename, filename))
  660. m->filename = filename;
  661. m->regexp = regexp;
  662. m->pos = pos;
  663. m->vis = V_PUBLIC;
  664. m->flags = flags;
  665. info_where = P_DECL;
  666. info_cls = global_symbols;
  667. info_member = m;
  668. }
  669. }
  670. /* Add a symbol for member NAME to class CLS.
  671. VAR non-zero means it's a variable. SC specifies the kind of
  672. member. HASH is a hash code for the parameter types of a function.
  673. Value is a pointer to the member's structure. */
  674. static struct member *
  675. add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
  676. {
  677. struct member *m = xmalloc (offsetof (struct member, name)
  678. + strlen (name) + 1);
  679. struct member **list;
  680. struct member *p;
  681. struct member *prev;
  682. unsigned name_hash = 0;
  683. int i;
  684. char *s;
  685. strcpy (m->name, name);
  686. m->param_hash = hash;
  687. m->vis = 0;
  688. m->flags = 0;
  689. m->regexp = NULL;
  690. m->filename = NULL;
  691. m->pos = 0;
  692. m->def_regexp = NULL;
  693. m->def_filename = NULL;
  694. m->def_pos = 0;
  695. assert (cls != NULL);
  696. switch (sc)
  697. {
  698. case SC_FRIEND:
  699. list = &cls->friends;
  700. break;
  701. case SC_TYPE:
  702. list = &cls->types;
  703. break;
  704. case SC_STATIC:
  705. list = var ? &cls->static_vars : &cls->static_fns;
  706. break;
  707. default:
  708. list = var ? &cls->vars : &cls->fns;
  709. break;
  710. }
  711. for (s = name; *s; ++s)
  712. name_hash = (name_hash << 1) ^ *s;
  713. i = name_hash % TABLE_SIZE;
  714. m->anext = member_table[i];
  715. member_table[i] = m;
  716. m->list = list;
  717. /* Keep the member list sorted. It's cheaper to do it here than to
  718. sort them in Lisp. */
  719. for (prev = NULL, p = *list;
  720. p && strcmp (name, p->name) > 0;
  721. prev = p, p = p->next)
  722. ;
  723. m->next = p;
  724. if (prev)
  725. prev->next = m;
  726. else
  727. *list = m;
  728. return m;
  729. }
  730. /* Given the root R of a class tree, step through all subclasses
  731. recursively, marking functions as virtual that are declared virtual
  732. in base classes. */
  733. static void
  734. mark_virtual (struct sym *r)
  735. {
  736. struct link *p;
  737. struct member *m, *m2;
  738. for (p = r->subs; p; p = p->next)
  739. {
  740. for (m = r->fns; m; m = m->next)
  741. if (HAS_FLAG (m->flags, F_VIRTUAL))
  742. {
  743. for (m2 = p->sym->fns; m2; m2 = m2->next)
  744. if (m->param_hash == m2->param_hash && streq (m->name, m2->name))
  745. SET_FLAG (m2->flags, F_VIRTUAL);
  746. }
  747. mark_virtual (p->sym);
  748. }
  749. }
  750. /* For all roots of the class tree, mark functions as virtual that
  751. are virtual because of a virtual declaration in a base class. */
  752. static void
  753. mark_inherited_virtual (void)
  754. {
  755. struct sym *r;
  756. int i;
  757. for (i = 0; i < TABLE_SIZE; ++i)
  758. for (r = class_table[i]; r; r = r->next)
  759. if (r->supers == NULL)
  760. mark_virtual (r);
  761. }
  762. /* Create and return a symbol for a namespace with name NAME. */
  763. static struct sym *
  764. make_namespace (char *name, struct sym *context)
  765. {
  766. struct sym *s = xmalloc (offsetof (struct sym, name) + strlen (name) + 1);
  767. memset (s, 0, offsetof (struct sym, name));
  768. strcpy (s->name, name);
  769. s->next = all_namespaces;
  770. s->namesp = context;
  771. all_namespaces = s;
  772. return s;
  773. }
  774. /* Find the symbol for namespace NAME. If not found, return NULL */
  775. static struct sym *
  776. check_namespace (char *name, struct sym *context)
  777. {
  778. struct sym *p = NULL;
  779. for (p = all_namespaces; p; p = p->next)
  780. {
  781. if (streq (p->name, name) && (p->namesp == context))
  782. break;
  783. }
  784. return p;
  785. }
  786. /* Find the symbol for namespace NAME. If not found, add a new symbol
  787. for NAME to all_namespaces. */
  788. static struct sym *
  789. find_namespace (char *name, struct sym *context)
  790. {
  791. struct sym *p = check_namespace (name, context);
  792. if (p == NULL)
  793. p = make_namespace (name, context);
  794. return p;
  795. }
  796. /* Find namespace alias with name NAME. If not found return NULL. */
  797. static struct link *
  798. check_namespace_alias (char *name)
  799. {
  800. struct link *p = NULL;
  801. struct alias *al;
  802. unsigned h;
  803. char *s;
  804. for (s = name, h = 0; *s; ++s)
  805. h = (h << 1) ^ *s;
  806. h %= TABLE_SIZE;
  807. for (al = namespace_alias_table[h]; al; al = al->next)
  808. if (streq (name, al->name) && (al->namesp == current_namespace))
  809. {
  810. p = al->aliasee;
  811. break;
  812. }
  813. return p;
  814. }
  815. /* Register the name NEW_NAME as an alias for namespace list OLD_NAME. */
  816. static void
  817. register_namespace_alias (char *new_name, struct link *old_name)
  818. {
  819. unsigned h;
  820. char *s;
  821. struct alias *al;
  822. for (s = new_name, h = 0; *s; ++s)
  823. h = (h << 1) ^ *s;
  824. h %= TABLE_SIZE;
  825. /* Is it already in the table of aliases? */
  826. for (al = namespace_alias_table[h]; al; al = al->next)
  827. if (streq (new_name, al->name) && (al->namesp == current_namespace))
  828. return;
  829. al = xmalloc (offsetof (struct alias, name) + strlen (new_name) + 1);
  830. strcpy (al->name, new_name);
  831. al->next = namespace_alias_table[h];
  832. al->namesp = current_namespace;
  833. al->aliasee = old_name;
  834. namespace_alias_table[h] = al;
  835. }
  836. /* Enter namespace with name NAME. */
  837. static void
  838. enter_namespace (char *name)
  839. {
  840. struct sym *p = find_namespace (name, current_namespace);
  841. if (namespace_sp == namespace_stack_size)
  842. {
  843. int size = max (10, 2 * namespace_stack_size);
  844. namespace_stack
  845. = (struct sym **) xrealloc ((void *)namespace_stack,
  846. size * sizeof *namespace_stack);
  847. namespace_stack_size = size;
  848. }
  849. namespace_stack[namespace_sp++] = current_namespace;
  850. current_namespace = p;
  851. }
  852. /* Leave the current namespace. */
  853. static void
  854. leave_namespace (void)
  855. {
  856. assert (namespace_sp > 0);
  857. current_namespace = namespace_stack[--namespace_sp];
  858. }
  859. /***********************************************************************
  860. Writing the Output File
  861. ***********************************************************************/
  862. /* Write string S to the output file FP in a Lisp-readable form.
  863. If S is null, write out `()'. */
  864. static void
  865. putstr (const char *s, FILE *fp)
  866. {
  867. if (!s)
  868. {
  869. putc ('(', fp);
  870. putc (')', fp);
  871. putc (' ', fp);
  872. }
  873. else
  874. {
  875. putc ('"', fp);
  876. fputs (s, fp);
  877. putc ('"', fp);
  878. putc (' ', fp);
  879. }
  880. }
  881. /* A dynamically allocated buffer for constructing a scope name. */
  882. char *scope_buffer;
  883. int scope_buffer_size;
  884. int scope_buffer_len;
  885. /* Make sure scope_buffer has enough room to add LEN chars to it. */
  886. static void
  887. ensure_scope_buffer_room (int len)
  888. {
  889. if (scope_buffer_len + len >= scope_buffer_size)
  890. {
  891. int new_size = max (2 * scope_buffer_size, scope_buffer_len + len);
  892. scope_buffer = (char *) xrealloc (scope_buffer, new_size);
  893. scope_buffer_size = new_size;
  894. }
  895. }
  896. /* Recursively add the scope names of symbol P and the scopes of its
  897. namespaces to scope_buffer. Value is a pointer to the complete
  898. scope name constructed. */
  899. static char *
  900. sym_scope_1 (struct sym *p)
  901. {
  902. int len;
  903. if (p->namesp)
  904. sym_scope_1 (p->namesp);
  905. if (*scope_buffer)
  906. {
  907. ensure_scope_buffer_room (3);
  908. strcpy (scope_buffer + scope_buffer_len, "::");
  909. scope_buffer_len += 2;
  910. }
  911. len = strlen (p->name);
  912. ensure_scope_buffer_room (len + 1);
  913. strcpy (scope_buffer + scope_buffer_len, p->name);
  914. scope_buffer_len += len;
  915. if (HAS_FLAG (p->flags, F_TEMPLATE))
  916. {
  917. ensure_scope_buffer_room (3);
  918. strcpy (scope_buffer + scope_buffer_len, "<>");
  919. scope_buffer_len += 2;
  920. }
  921. return scope_buffer;
  922. }
  923. /* Return the scope of symbol P in printed representation, i.e.
  924. as it would appear in a C*+ source file. */
  925. static char *
  926. sym_scope (struct sym *p)
  927. {
  928. if (!scope_buffer)
  929. {
  930. scope_buffer_size = 1024;
  931. scope_buffer = (char *) xmalloc (scope_buffer_size);
  932. }
  933. *scope_buffer = '\0';
  934. scope_buffer_len = 0;
  935. if (p->namesp)
  936. sym_scope_1 (p->namesp);
  937. return scope_buffer;
  938. }
  939. /* Dump the list of members M to file FP. Value is the length of the
  940. list. */
  941. static int
  942. dump_members (FILE *fp, struct member *m)
  943. {
  944. int n;
  945. putc ('(', fp);
  946. for (n = 0; m; m = m->next, ++n)
  947. {
  948. fputs (MEMBER_STRUCT, fp);
  949. putstr (m->name, fp);
  950. putstr (NULL, fp); /* FIXME? scope for globals */
  951. fprintf (fp, "%u ", (unsigned) m->flags);
  952. putstr (m->filename, fp);
  953. putstr (m->regexp, fp);
  954. fprintf (fp, "%u ", (unsigned) m->pos);
  955. fprintf (fp, "%u ", (unsigned) m->vis);
  956. putc (' ', fp);
  957. putstr (m->def_filename, fp);
  958. putstr (m->def_regexp, fp);
  959. fprintf (fp, "%u", (unsigned) m->def_pos);
  960. putc (']', fp);
  961. putc ('\n', fp);
  962. }
  963. putc (')', fp);
  964. putc ('\n', fp);
  965. return n;
  966. }
  967. /* Dump class ROOT to stream FP. */
  968. static void
  969. dump_sym (FILE *fp, struct sym *root)
  970. {
  971. fputs (CLASS_STRUCT, fp);
  972. putstr (root->name, fp);
  973. /* Print scope, if any. */
  974. if (root->namesp)
  975. putstr (sym_scope (root), fp);
  976. else
  977. putstr (NULL, fp);
  978. /* Print flags. */
  979. fprintf (fp, "%u", root->flags);
  980. putstr (root->filename, fp);
  981. putstr (root->regexp, fp);
  982. fprintf (fp, "%u", (unsigned) root->pos);
  983. putstr (root->sfilename, fp);
  984. putc (']', fp);
  985. putc ('\n', fp);
  986. }
  987. /* Dump class ROOT and its subclasses to file FP. Value is the
  988. number of classes written. */
  989. static int
  990. dump_tree (FILE *fp, struct sym *root)
  991. {
  992. struct link *lk;
  993. unsigned n = 0;
  994. dump_sym (fp, root);
  995. if (f_verbose)
  996. {
  997. putchar ('+');
  998. fflush (stdout);
  999. }
  1000. putc ('(', fp);
  1001. for (lk = root->subs; lk; lk = lk->next)
  1002. {
  1003. fputs (TREE_STRUCT, fp);
  1004. n += dump_tree (fp, lk->sym);
  1005. putc (']', fp);
  1006. }
  1007. putc (')', fp);
  1008. dump_members (fp, root->vars);
  1009. n += dump_members (fp, root->fns);
  1010. dump_members (fp, root->static_vars);
  1011. n += dump_members (fp, root->static_fns);
  1012. n += dump_members (fp, root->friends);
  1013. dump_members (fp, root->types);
  1014. /* Superclasses. */
  1015. putc ('(', fp);
  1016. putc (')', fp);
  1017. /* Mark slot. */
  1018. putc ('(', fp);
  1019. putc (')', fp);
  1020. putc ('\n', fp);
  1021. return n;
  1022. }
  1023. /* Dump the entire class tree to file FP. */
  1024. static void
  1025. dump_roots (FILE *fp)
  1026. {
  1027. int i, n = 0;
  1028. struct sym *r;
  1029. /* Output file header containing version string, command line
  1030. options etc. */
  1031. if (!f_append)
  1032. {
  1033. fputs (TREE_HEADER_STRUCT, fp);
  1034. putstr (EBROWSE_FILE_VERSION, fp);
  1035. putc ('\"', fp);
  1036. if (!f_structs)
  1037. fputs (" -s", fp);
  1038. if (f_regexps)
  1039. fputs (" -x", fp);
  1040. putc ('\"', fp);
  1041. fputs (" ()", fp);
  1042. fputs (" ()", fp);
  1043. putc (']', fp);
  1044. }
  1045. /* Mark functions as virtual that are so because of functions
  1046. declared virtual in base classes. */
  1047. mark_inherited_virtual ();
  1048. /* Dump the roots of the graph. */
  1049. for (i = 0; i < TABLE_SIZE; ++i)
  1050. for (r = class_table[i]; r; r = r->next)
  1051. if (!r->supers)
  1052. {
  1053. fputs (TREE_STRUCT, fp);
  1054. n += dump_tree (fp, r);
  1055. putc (']', fp);
  1056. }
  1057. if (f_verbose)
  1058. putchar ('\n');
  1059. }
  1060. /***********************************************************************
  1061. Scanner
  1062. ***********************************************************************/
  1063. #ifdef DEBUG
  1064. #define INCREMENT_LINENO \
  1065. do { \
  1066. if (f_very_verbose) \
  1067. { \
  1068. ++yyline; \
  1069. printf ("%d:\n", yyline); \
  1070. } \
  1071. else \
  1072. ++yyline; \
  1073. } while (0)
  1074. #else
  1075. #define INCREMENT_LINENO ++yyline
  1076. #endif
  1077. /* Define two macros for accessing the input buffer (current input
  1078. file). GET(C) sets C to the next input character and advances the
  1079. input pointer. UNGET retracts the input pointer. */
  1080. #define GET(C) ((C) = *in++)
  1081. #define UNGET() (--in)
  1082. /* Process a preprocessor line. Value is the next character from the
  1083. input buffer not consumed. */
  1084. static int
  1085. process_pp_line (void)
  1086. {
  1087. int in_comment = 0, in_string = 0;
  1088. int c;
  1089. char *p = yytext;
  1090. /* Skip over white space. The `#' has been consumed already. */
  1091. while (WHITEP (GET (c)))
  1092. ;
  1093. /* Read the preprocessor command (if any). */
  1094. while (IDENTP (c))
  1095. {
  1096. *p++ = c;
  1097. GET (c);
  1098. }
  1099. /* Is it a `define'? */
  1100. *p = '\0';
  1101. if (*yytext && streq (yytext, "define"))
  1102. {
  1103. p = yytext;
  1104. while (WHITEP (c))
  1105. GET (c);
  1106. while (IDENTP (c))
  1107. {
  1108. *p++ = c;
  1109. GET (c);
  1110. }
  1111. *p = '\0';
  1112. if (*yytext)
  1113. {
  1114. char *regexp = matching_regexp ();
  1115. int pos = BUFFER_POS ();
  1116. add_define (yytext, regexp, pos);
  1117. }
  1118. }
  1119. while (c && (c != '\n' || in_comment || in_string))
  1120. {
  1121. if (c == '\\')
  1122. GET (c);
  1123. else if (c == '/' && !in_comment)
  1124. {
  1125. if (GET (c) == '*')
  1126. in_comment = 1;
  1127. }
  1128. else if (c == '*' && in_comment)
  1129. {
  1130. if (GET (c) == '/')
  1131. in_comment = 0;
  1132. }
  1133. else if (c == '"')
  1134. in_string = !in_string;
  1135. if (c == '\n')
  1136. INCREMENT_LINENO;
  1137. GET (c);
  1138. }
  1139. return c;
  1140. }
  1141. /* Value is the next token from the input buffer. */
  1142. static int
  1143. yylex (void)
  1144. {
  1145. int c;
  1146. char end_char;
  1147. char *p;
  1148. for (;;)
  1149. {
  1150. while (WHITEP (GET (c)))
  1151. ;
  1152. switch (c)
  1153. {
  1154. case '\n':
  1155. INCREMENT_LINENO;
  1156. break;
  1157. case '\r':
  1158. break;
  1159. case 0:
  1160. /* End of file. */
  1161. return YYEOF;
  1162. case '\\':
  1163. GET (c);
  1164. break;
  1165. case '"':
  1166. case '\'':
  1167. /* String and character constants. */
  1168. end_char = c;
  1169. string_start = in;
  1170. while (GET (c) && c != end_char)
  1171. {
  1172. switch (c)
  1173. {
  1174. case '\\':
  1175. /* Escape sequences. */
  1176. if (!GET (c))
  1177. {
  1178. if (end_char == '\'')
  1179. yyerror ("EOF in character constant", NULL);
  1180. else
  1181. yyerror ("EOF in string constant", NULL);
  1182. goto end_string;
  1183. }
  1184. else switch (c)
  1185. {
  1186. case '\n':
  1187. INCREMENT_LINENO;
  1188. case 'a':
  1189. case 'b':
  1190. case 'f':
  1191. case 'n':
  1192. case 'r':
  1193. case 't':
  1194. case 'v':
  1195. break;
  1196. case 'x':
  1197. {
  1198. /* Hexadecimal escape sequence. */
  1199. int i;
  1200. for (i = 0; i < 2; ++i)
  1201. {
  1202. GET (c);
  1203. if (c >= '0' && c <= '7')
  1204. ;
  1205. else if (c >= 'a' && c <= 'f')
  1206. ;
  1207. else if (c >= 'A' && c <= 'F')
  1208. ;
  1209. else
  1210. {
  1211. UNGET ();
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. break;
  1217. case '0':
  1218. {
  1219. /* Octal escape sequence. */
  1220. int i;
  1221. for (i = 0; i < 3; ++i)
  1222. {
  1223. GET (c);
  1224. if (c >= '0' && c <= '7')
  1225. ;
  1226. else
  1227. {
  1228. UNGET ();
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. break;
  1234. default:
  1235. break;
  1236. }
  1237. break;
  1238. case '\n':
  1239. if (end_char == '\'')
  1240. yyerror ("newline in character constant", NULL);
  1241. else
  1242. yyerror ("newline in string constant", NULL);
  1243. INCREMENT_LINENO;
  1244. break;
  1245. default:
  1246. break;
  1247. }
  1248. }
  1249. end_string:
  1250. return end_char == '\'' ? CCHAR : CSTRING;
  1251. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
  1252. case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
  1253. case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
  1254. case 'v': case 'w': case 'x': case 'y': case 'z':
  1255. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
  1256. case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
  1257. case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
  1258. case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_':
  1259. {
  1260. /* Identifier and keywords. */
  1261. unsigned hash;
  1262. struct kw *k;
  1263. p = yytext;
  1264. *p++ = hash = c;
  1265. while (IDENTP (GET (*p)))
  1266. {
  1267. hash = (hash << 1) ^ *p++;
  1268. if (p == yytext_end - 1)
  1269. {
  1270. int size = yytext_end - yytext;
  1271. yytext = (char *) xrealloc (yytext, 2 * size);
  1272. yytext_end = yytext + 2 * size;
  1273. p = yytext + size - 1;
  1274. }
  1275. }
  1276. UNGET ();
  1277. *p = 0;
  1278. for (k = keyword_table[hash % KEYWORD_TABLE_SIZE]; k; k = k->next)
  1279. if (streq (k->name, yytext))
  1280. return k->tk;
  1281. return IDENT;
  1282. }
  1283. case '/':
  1284. /* C and C++ comments, '/' and '/='. */
  1285. switch (GET (c))
  1286. {
  1287. case '*':
  1288. while (GET (c))
  1289. {
  1290. switch (c)
  1291. {
  1292. case '*':
  1293. if (GET (c) == '/')
  1294. goto comment_end;
  1295. UNGET ();
  1296. break;
  1297. case '\\':
  1298. GET (c);
  1299. break;
  1300. case '\n':
  1301. INCREMENT_LINENO;
  1302. break;
  1303. }
  1304. }
  1305. comment_end:;
  1306. break;
  1307. case '=':
  1308. return DIVASGN;
  1309. case '/':
  1310. while (GET (c) && c != '\n')
  1311. ;
  1312. /* Don't try to read past the end of the input buffer if
  1313. the file ends in a C++ comment without a newline. */
  1314. if (c == 0)
  1315. return YYEOF;
  1316. INCREMENT_LINENO;
  1317. break;
  1318. default:
  1319. UNGET ();
  1320. return '/';
  1321. }
  1322. break;
  1323. case '+':
  1324. if (GET (c) == '+')
  1325. return INC;
  1326. else if (c == '=')
  1327. return ADDASGN;
  1328. UNGET ();
  1329. return '+';
  1330. case '-':
  1331. switch (GET (c))
  1332. {
  1333. case '-':
  1334. return DEC;
  1335. case '>':
  1336. if (GET (c) == '*')
  1337. return ARROWSTAR;
  1338. UNGET ();
  1339. return ARROW;
  1340. case '=':
  1341. return SUBASGN;
  1342. }
  1343. UNGET ();
  1344. return '-';
  1345. case '*':
  1346. if (GET (c) == '=')
  1347. return MULASGN;
  1348. UNGET ();
  1349. return '*';
  1350. case '%':
  1351. if (GET (c) == '=')
  1352. return MODASGN;
  1353. UNGET ();
  1354. return '%';
  1355. case '|':
  1356. if (GET (c) == '|')
  1357. return LOR;
  1358. else if (c == '=')
  1359. return ORASGN;
  1360. UNGET ();
  1361. return '|';
  1362. case '&':
  1363. if (GET (c) == '&')
  1364. return LAND;
  1365. else if (c == '=')
  1366. return ANDASGN;
  1367. UNGET ();
  1368. return '&';
  1369. case '^':
  1370. if (GET (c) == '=')
  1371. return XORASGN;
  1372. UNGET ();
  1373. return '^';
  1374. case '.':
  1375. if (GET (c) == '*')
  1376. return POINTSTAR;
  1377. else if (c == '.')
  1378. {
  1379. if (GET (c) != '.')
  1380. yyerror ("invalid token '..' ('...' assumed)", NULL);
  1381. UNGET ();
  1382. return ELLIPSIS;
  1383. }
  1384. else if (!DIGITP (c))
  1385. {
  1386. UNGET ();
  1387. return '.';
  1388. }
  1389. goto mantissa;
  1390. case ':':
  1391. if (GET (c) == ':')
  1392. return DCOLON;
  1393. UNGET ();
  1394. return ':';
  1395. case '=':
  1396. if (GET (c) == '=')
  1397. return EQ;
  1398. UNGET ();
  1399. return '=';
  1400. case '!':
  1401. if (GET (c) == '=')
  1402. return NE;
  1403. UNGET ();
  1404. return '!';
  1405. case '<':
  1406. switch (GET (c))
  1407. {
  1408. case '=':
  1409. return LE;
  1410. case '<':
  1411. if (GET (c) == '=')
  1412. return LSHIFTASGN;
  1413. UNGET ();
  1414. return LSHIFT;
  1415. }
  1416. UNGET ();
  1417. return '<';
  1418. case '>':
  1419. switch (GET (c))
  1420. {
  1421. case '=':
  1422. return GE;
  1423. case '>':
  1424. if (GET (c) == '=')
  1425. return RSHIFTASGN;
  1426. UNGET ();
  1427. return RSHIFT;
  1428. }
  1429. UNGET ();
  1430. return '>';
  1431. case '#':
  1432. c = process_pp_line ();
  1433. if (c == 0)
  1434. return YYEOF;
  1435. break;
  1436. case '(': case ')': case '[': case ']': case '{': case '}':
  1437. case ';': case ',': case '?': case '~':
  1438. return c;
  1439. case '0':
  1440. yyival = 0;
  1441. if (GET (c) == 'x' || c == 'X')
  1442. {
  1443. while (GET (c))
  1444. {
  1445. if (DIGITP (c))
  1446. yyival = yyival * 16 + c - '0';
  1447. else if (c >= 'a' && c <= 'f')
  1448. yyival = yyival * 16 + c - 'a' + 10;
  1449. else if (c >= 'A' && c <= 'F')
  1450. yyival = yyival * 16 + c - 'A' + 10;
  1451. else
  1452. break;
  1453. }
  1454. goto int_suffixes;
  1455. }
  1456. else if (c == '.')
  1457. goto mantissa;
  1458. while (c >= '0' && c <= '7')
  1459. {
  1460. yyival = (yyival << 3) + c - '0';
  1461. GET (c);
  1462. }
  1463. int_suffixes:
  1464. /* Integer suffixes. */
  1465. while (isalpha (c))
  1466. GET (c);
  1467. UNGET ();
  1468. return CINT;
  1469. case '1': case '2': case '3': case '4': case '5': case '6':
  1470. case '7': case '8': case '9':
  1471. /* Integer or floating constant, part before '.'. */
  1472. yyival = c - '0';
  1473. while (GET (c) && DIGITP (c))
  1474. yyival = 10 * yyival + c - '0';
  1475. if (c != '.')
  1476. goto int_suffixes;
  1477. mantissa:
  1478. /* Digits following '.'. */
  1479. while (DIGITP (c))
  1480. GET (c);
  1481. /* Optional exponent. */
  1482. if (c == 'E' || c == 'e')
  1483. {
  1484. if (GET (c) == '-' || c == '+')
  1485. GET (c);
  1486. while (DIGITP (c))
  1487. GET (c);
  1488. }
  1489. /* Optional type suffixes. */
  1490. while (isalpha (c))
  1491. GET (c);
  1492. UNGET ();
  1493. return CFLOAT;
  1494. default:
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. /* Actually local to matching_regexp. These variables must be in
  1500. global scope for the case that `static' get's defined away. */
  1501. static char *matching_regexp_buffer, *matching_regexp_end_buf;
  1502. /* Value is the string from the start of the line to the current
  1503. position in the input buffer, or maybe a bit more if that string is
  1504. shorter than min_regexp. */
  1505. static char *
  1506. matching_regexp (void)
  1507. {
  1508. char *p;
  1509. char *s;
  1510. char *t;
  1511. if (!f_regexps)
  1512. return NULL;
  1513. if (matching_regexp_buffer == NULL)
  1514. {
  1515. matching_regexp_buffer = (char *) xmalloc (max_regexp);
  1516. matching_regexp_end_buf = &matching_regexp_buffer[max_regexp] - 1;
  1517. }
  1518. /* Scan back to previous newline of buffer start. */
  1519. for (p = in - 1; p > inbuffer && *p != '\n'; --p)
  1520. ;
  1521. if (*p == '\n')
  1522. {
  1523. while (in - p < min_regexp && p > inbuffer)
  1524. {
  1525. /* Line probably not significant enough */
  1526. for (--p; p > inbuffer && *p != '\n'; --p)
  1527. ;
  1528. }
  1529. if (*p == '\n')
  1530. ++p;
  1531. }
  1532. /* Copy from end to make sure significant portions are included.
  1533. This implies that in the browser a regular expressing of the form
  1534. `^.*{regexp}' has to be used. */
  1535. for (s = matching_regexp_end_buf - 1, t = in;
  1536. s > matching_regexp_buffer && t > p;)
  1537. {
  1538. *--s = *--t;
  1539. if (*s == '"' || *s == '\\')
  1540. *--s = '\\';
  1541. }
  1542. *(matching_regexp_end_buf - 1) = '\0';
  1543. return xstrdup (s);
  1544. }
  1545. /* Return a printable representation of token T. */
  1546. static const char *
  1547. token_string (int t)
  1548. {
  1549. static char b[3];
  1550. switch (t)
  1551. {
  1552. case CSTRING: return "string constant";
  1553. case CCHAR: return "char constant";
  1554. case CINT: return "int constant";
  1555. case CFLOAT: return "floating constant";
  1556. case ELLIPSIS: return "...";
  1557. case LSHIFTASGN: return "<<=";
  1558. case RSHIFTASGN: return ">>=";
  1559. case ARROWSTAR: return "->*";
  1560. case IDENT: return "identifier";
  1561. case DIVASGN: return "/=";
  1562. case INC: return "++";
  1563. case ADDASGN: return "+=";
  1564. case DEC: return "--";
  1565. case ARROW: return "->";
  1566. case SUBASGN: return "-=";
  1567. case MULASGN: return "*=";
  1568. case MODASGN: return "%=";
  1569. case LOR: return "||";
  1570. case ORASGN: return "|=";
  1571. case LAND: return "&&";
  1572. case ANDASGN: return "&=";
  1573. case XORASGN: return "^=";
  1574. case POINTSTAR: return ".*";
  1575. case DCOLON: return "::";
  1576. case EQ: return "==";
  1577. case NE: return "!=";
  1578. case LE: return "<=";
  1579. case LSHIFT: return "<<";
  1580. case GE: return ">=";
  1581. case RSHIFT: return ">>";
  1582. case ASM: return "asm";
  1583. case AUTO: return "auto";
  1584. case BREAK: return "break";
  1585. case CASE: return "case";
  1586. case CATCH: return "catch";
  1587. case CHAR: return "char";
  1588. case CLASS: return "class";
  1589. case CONST: return "const";
  1590. case CONTINUE: return "continue";
  1591. case DEFAULT: return "default";
  1592. case DELETE: return "delete";
  1593. case DO: return "do";
  1594. case DOUBLE: return "double";
  1595. case ELSE: return "else";
  1596. case ENUM: return "enum";
  1597. case EXTERN: return "extern";
  1598. case FLOAT: return "float";
  1599. case FOR: return "for";
  1600. case FRIEND: return "friend";
  1601. case GOTO: return "goto";
  1602. case IF: return "if";
  1603. case T_INLINE: return "inline";
  1604. case INT: return "int";
  1605. case LONG: return "long";
  1606. case NEW: return "new";
  1607. case OPERATOR: return "operator";
  1608. case PRIVATE: return "private";
  1609. case PROTECTED: return "protected";
  1610. case PUBLIC: return "public";
  1611. case REGISTER: return "register";
  1612. case RETURN: return "return";
  1613. case SHORT: return "short";
  1614. case SIGNED: return "signed";
  1615. case SIZEOF: return "sizeof";
  1616. case STATIC: return "static";
  1617. case STRUCT: return "struct";
  1618. case SWITCH: return "switch";
  1619. case TEMPLATE: return "template";
  1620. case THIS: return "this";
  1621. case THROW: return "throw";
  1622. case TRY: return "try";
  1623. case TYPEDEF: return "typedef";
  1624. case UNION: return "union";
  1625. case UNSIGNED: return "unsigned";
  1626. case VIRTUAL: return "virtual";
  1627. case VOID: return "void";
  1628. case VOLATILE: return "volatile";
  1629. case WHILE: return "while";
  1630. case MUTABLE: return "mutable";
  1631. case BOOL: return "bool";
  1632. case TRUE: return "true";
  1633. case FALSE: return "false";
  1634. case SIGNATURE: return "signature";
  1635. case NAMESPACE: return "namespace";
  1636. case EXPLICIT: return "explicit";
  1637. case TYPENAME: return "typename";
  1638. case CONST_CAST: return "const_cast";
  1639. case DYNAMIC_CAST: return "dynamic_cast";
  1640. case REINTERPRET_CAST: return "reinterpret_cast";
  1641. case STATIC_CAST: return "static_cast";
  1642. case TYPEID: return "typeid";
  1643. case USING: return "using";
  1644. case WCHAR: return "wchar_t";
  1645. case YYEOF: return "EOF";
  1646. default:
  1647. if (t < 255)
  1648. {
  1649. b[0] = t;
  1650. b[1] = '\0';
  1651. return b;
  1652. }
  1653. else
  1654. return "???";
  1655. }
  1656. }
  1657. /* Reinitialize the scanner for a new input file. */
  1658. static void
  1659. re_init_scanner (void)
  1660. {
  1661. in = inbuffer;
  1662. yyline = 1;
  1663. if (yytext == NULL)
  1664. {
  1665. int size = 256;
  1666. yytext = (char *) xmalloc (size * sizeof *yytext);
  1667. yytext_end = yytext + size;
  1668. }
  1669. }
  1670. /* Insert a keyword NAME with token value TKV into the keyword hash
  1671. table. */
  1672. static void
  1673. insert_keyword (const char *name, int tkv)
  1674. {
  1675. const char *s;
  1676. unsigned h = 0;
  1677. struct kw *k = (struct kw *) xmalloc (sizeof *k);
  1678. for (s = name; *s; ++s)
  1679. h = (h << 1) ^ *s;
  1680. h %= KEYWORD_TABLE_SIZE;
  1681. k->name = name;
  1682. k->tk = tkv;
  1683. k->next = keyword_table[h];
  1684. keyword_table[h] = k;
  1685. }
  1686. /* Initialize the scanner for the first file. This sets up the
  1687. character class vectors and fills the keyword hash table. */
  1688. static void
  1689. init_scanner (void)
  1690. {
  1691. int i;
  1692. /* Allocate the input buffer */
  1693. inbuffer_size = READ_CHUNK_SIZE + 1;
  1694. inbuffer = in = (char *) xmalloc (inbuffer_size);
  1695. yyline = 1;
  1696. /* Set up character class vectors. */
  1697. for (i = 0; i < sizeof is_ident; ++i)
  1698. {
  1699. if (i == '_' || isalnum (i))
  1700. is_ident[i] = 1;
  1701. if (i >= '0' && i <= '9')
  1702. is_digit[i] = 1;
  1703. if (i == ' ' || i == '\t' || i == '\f' || i == '\v')
  1704. is_white[i] = 1;
  1705. }
  1706. /* Fill keyword hash table. */
  1707. insert_keyword ("and", LAND);
  1708. insert_keyword ("and_eq", ANDASGN);
  1709. insert_keyword ("asm", ASM);
  1710. insert_keyword ("auto", AUTO);
  1711. insert_keyword ("bitand", '&');
  1712. insert_keyword ("bitor", '|');
  1713. insert_keyword ("bool", BOOL);
  1714. insert_keyword ("break", BREAK);
  1715. insert_keyword ("case", CASE);
  1716. insert_keyword ("catch", CATCH);
  1717. insert_keyword ("char", CHAR);
  1718. insert_keyword ("class", CLASS);
  1719. insert_keyword ("compl", '~');
  1720. insert_keyword ("const", CONST);
  1721. insert_keyword ("const_cast", CONST_CAST);
  1722. insert_keyword ("continue", CONTINUE);
  1723. insert_keyword ("default", DEFAULT);
  1724. insert_keyword ("delete", DELETE);
  1725. insert_keyword ("do", DO);
  1726. insert_keyword ("double", DOUBLE);
  1727. insert_keyword ("dynamic_cast", DYNAMIC_CAST);
  1728. insert_keyword ("else", ELSE);
  1729. insert_keyword ("enum", ENUM);
  1730. insert_keyword ("explicit", EXPLICIT);
  1731. insert_keyword ("extern", EXTERN);
  1732. insert_keyword ("false", FALSE);
  1733. insert_keyword ("float", FLOAT);
  1734. insert_keyword ("for", FOR);
  1735. insert_keyword ("friend", FRIEND);
  1736. insert_keyword ("goto", GOTO);
  1737. insert_keyword ("if", IF);
  1738. insert_keyword ("inline", T_INLINE);
  1739. insert_keyword ("int", INT);
  1740. insert_keyword ("long", LONG);
  1741. insert_keyword ("mutable", MUTABLE);
  1742. insert_keyword ("namespace", NAMESPACE);
  1743. insert_keyword ("new", NEW);
  1744. insert_keyword ("not", '!');
  1745. insert_keyword ("not_eq", NE);
  1746. insert_keyword ("operator", OPERATOR);
  1747. insert_keyword ("or", LOR);
  1748. insert_keyword ("or_eq", ORASGN);
  1749. insert_keyword ("private", PRIVATE);
  1750. insert_keyword ("protected", PROTECTED);
  1751. insert_keyword ("public", PUBLIC);
  1752. insert_keyword ("register", REGISTER);
  1753. insert_keyword ("reinterpret_cast", REINTERPRET_CAST);
  1754. insert_keyword ("return", RETURN);
  1755. insert_keyword ("short", SHORT);
  1756. insert_keyword ("signed", SIGNED);
  1757. insert_keyword ("sizeof", SIZEOF);
  1758. insert_keyword ("static", STATIC);
  1759. insert_keyword ("static_cast", STATIC_CAST);
  1760. insert_keyword ("struct", STRUCT);
  1761. insert_keyword ("switch", SWITCH);
  1762. insert_keyword ("template", TEMPLATE);
  1763. insert_keyword ("this", THIS);
  1764. insert_keyword ("throw", THROW);
  1765. insert_keyword ("true", TRUE);
  1766. insert_keyword ("try", TRY);
  1767. insert_keyword ("typedef", TYPEDEF);
  1768. insert_keyword ("typeid", TYPEID);
  1769. insert_keyword ("typename", TYPENAME);
  1770. insert_keyword ("union", UNION);
  1771. insert_keyword ("unsigned", UNSIGNED);
  1772. insert_keyword ("using", USING);
  1773. insert_keyword ("virtual", VIRTUAL);
  1774. insert_keyword ("void", VOID);
  1775. insert_keyword ("volatile", VOLATILE);
  1776. insert_keyword ("wchar_t", WCHAR);
  1777. insert_keyword ("while", WHILE);
  1778. insert_keyword ("xor", '^');
  1779. insert_keyword ("xor_eq", XORASGN);
  1780. }
  1781. /***********************************************************************
  1782. Parser
  1783. ***********************************************************************/
  1784. /* Match the current lookahead token and set it to the next token. */
  1785. #define MATCH() (tk = yylex ())
  1786. /* Return the lookahead token. If current lookahead token is cleared,
  1787. read a new token. */
  1788. #define LA1 (tk == -1 ? (tk = yylex ()) : tk)
  1789. /* Is the current lookahead equal to the token T? */
  1790. #define LOOKING_AT(T) (tk == (T))
  1791. /* Is the current lookahead one of T1 or T2? */
  1792. #define LOOKING_AT2(T1, T2) (tk == (T1) || tk == (T2))
  1793. /* Is the current lookahead one of T1, T2 or T3? */
  1794. #define LOOKING_AT3(T1, T2, T3) (tk == (T1) || tk == (T2) || tk == (T3))
  1795. /* Is the current lookahead one of T1...T4? */
  1796. #define LOOKING_AT4(T1, T2, T3, T4) \
  1797. (tk == (T1) || tk == (T2) || tk == (T3) || tk == (T4))
  1798. /* Match token T if current lookahead is T. */
  1799. #define MATCH_IF(T) if (LOOKING_AT (T)) MATCH (); else ((void) 0)
  1800. /* Skip to matching token if current token is T. */
  1801. #define SKIP_MATCHING_IF(T) \
  1802. if (LOOKING_AT (T)) skip_matching (); else ((void) 0)
  1803. /* Skip forward until a given token TOKEN or YYEOF is seen and return
  1804. the current lookahead token after skipping. */
  1805. static int
  1806. skip_to (int token)
  1807. {
  1808. while (!LOOKING_AT2 (YYEOF, token))
  1809. MATCH ();
  1810. return tk;
  1811. }
  1812. /* Skip over pairs of tokens (parentheses, square brackets,
  1813. angle brackets, curly brackets) matching the current lookahead. */
  1814. static void
  1815. skip_matching (void)
  1816. {
  1817. int open, close, n;
  1818. switch (open = LA1)
  1819. {
  1820. case '{':
  1821. close = '}';
  1822. break;
  1823. case '(':
  1824. close = ')';
  1825. break;
  1826. case '<':
  1827. close = '>';
  1828. break;
  1829. case '[':
  1830. close = ']';
  1831. break;
  1832. default:
  1833. abort ();
  1834. }
  1835. for (n = 0;;)
  1836. {
  1837. if (LOOKING_AT (open))
  1838. ++n;
  1839. else if (LOOKING_AT (close))
  1840. --n;
  1841. else if (LOOKING_AT (YYEOF))
  1842. break;
  1843. MATCH ();
  1844. if (n == 0)
  1845. break;
  1846. }
  1847. }
  1848. static void
  1849. skip_initializer (void)
  1850. {
  1851. for (;;)
  1852. {
  1853. switch (LA1)
  1854. {
  1855. case ';':
  1856. case ',':
  1857. case YYEOF:
  1858. return;
  1859. case '{':
  1860. case '[':
  1861. case '(':
  1862. skip_matching ();
  1863. break;
  1864. default:
  1865. MATCH ();
  1866. break;
  1867. }
  1868. }
  1869. }
  1870. /* Build qualified namespace alias (A::B::c) and return it. */
  1871. static struct link *
  1872. match_qualified_namespace_alias (void)
  1873. {
  1874. struct link *head = NULL;
  1875. struct link *cur = NULL;
  1876. struct link *tmp = NULL;
  1877. for (;;)
  1878. {
  1879. MATCH ();
  1880. switch (LA1)
  1881. {
  1882. case IDENT:
  1883. tmp = (struct link *) xmalloc (sizeof *cur);
  1884. tmp->sym = find_namespace (yytext, cur ? cur->sym : NULL);
  1885. tmp->next = NULL;
  1886. if (head)
  1887. {
  1888. cur = cur->next = tmp;
  1889. }
  1890. else
  1891. {
  1892. head = cur = tmp;
  1893. }
  1894. break;
  1895. case DCOLON:
  1896. /* Just skip */
  1897. break;
  1898. default:
  1899. return head;
  1900. break;
  1901. }
  1902. }
  1903. }
  1904. /* Re-initialize the parser by resetting the lookahead token. */
  1905. static void
  1906. re_init_parser (void)
  1907. {
  1908. tk = -1;
  1909. }
  1910. /* Parse a parameter list, including the const-specifier,
  1911. pure-specifier, and throw-list that may follow a parameter list.
  1912. Return in FLAGS what was seen following the parameter list.
  1913. Returns a hash code for the parameter types. This value is used to
  1914. distinguish between overloaded functions. */
  1915. static unsigned
  1916. parm_list (int *flags)
  1917. {
  1918. unsigned hash = 0;
  1919. int type_seen = 0;
  1920. while (!LOOKING_AT2 (YYEOF, ')'))
  1921. {
  1922. switch (LA1)
  1923. {
  1924. /* Skip over grouping parens or parameter lists in parameter
  1925. declarations. */
  1926. case '(':
  1927. skip_matching ();
  1928. break;
  1929. /* Next parameter. */
  1930. case ',':
  1931. MATCH ();
  1932. type_seen = 0;
  1933. break;
  1934. /* Ignore the scope part of types, if any. This is because
  1935. some types need scopes when defined outside of a class body,
  1936. and don't need them inside the class body. This means that
  1937. we have to look for the last IDENT in a sequence of
  1938. IDENT::IDENT::... */
  1939. case IDENT:
  1940. if (!type_seen)
  1941. {
  1942. char *last_id;
  1943. unsigned ident_type_hash = 0;
  1944. parse_qualified_param_ident_or_type (&last_id);
  1945. if (last_id)
  1946. {
  1947. /* LAST_ID null means something like `X::*'. */
  1948. for (; *last_id; ++last_id)
  1949. ident_type_hash = (ident_type_hash << 1) ^ *last_id;
  1950. hash = (hash << 1) ^ ident_type_hash;
  1951. type_seen = 1;
  1952. }
  1953. }
  1954. else
  1955. MATCH ();
  1956. break;
  1957. case VOID:
  1958. /* This distinction is made to make `func (void)' equivalent
  1959. to `func ()'. */
  1960. type_seen = 1;
  1961. MATCH ();
  1962. if (!LOOKING_AT (')'))
  1963. hash = (hash << 1) ^ VOID;
  1964. break;
  1965. case BOOL: case CHAR: case CLASS: case CONST:
  1966. case DOUBLE: case ENUM: case FLOAT: case INT:
  1967. case LONG: case SHORT: case SIGNED: case STRUCT:
  1968. case UNION: case UNSIGNED: case VOLATILE: case WCHAR:
  1969. case ELLIPSIS:
  1970. type_seen = 1;
  1971. hash = (hash << 1) ^ LA1;
  1972. MATCH ();
  1973. break;
  1974. case '*': case '&': case '[': case ']':
  1975. hash = (hash << 1) ^ LA1;
  1976. MATCH ();
  1977. break;
  1978. default:
  1979. MATCH ();
  1980. break;
  1981. }
  1982. }
  1983. if (LOOKING_AT (')'))
  1984. {
  1985. MATCH ();
  1986. if (LOOKING_AT (CONST))
  1987. {
  1988. /* We can overload the same function on `const' */
  1989. hash = (hash << 1) ^ CONST;
  1990. SET_FLAG (*flags, F_CONST);
  1991. MATCH ();
  1992. }
  1993. if (LOOKING_AT (THROW))
  1994. {
  1995. MATCH ();
  1996. SKIP_MATCHING_IF ('(');
  1997. SET_FLAG (*flags, F_THROW);
  1998. }
  1999. if (LOOKING_AT ('='))
  2000. {
  2001. MATCH ();
  2002. if (LOOKING_AT (CINT) && yyival == 0)
  2003. {
  2004. MATCH ();
  2005. SET_FLAG (*flags, F_PURE);
  2006. }
  2007. }
  2008. }
  2009. return hash;
  2010. }
  2011. /* Print position info to stdout. */
  2012. static void
  2013. print_info (void)
  2014. {
  2015. if (info_position >= 0 && BUFFER_POS () <= info_position)
  2016. if (info_cls)
  2017. printf ("(\"%s\" \"%s\" \"%s\" %d)\n",
  2018. info_cls->name, sym_scope (info_cls),
  2019. info_member->name, info_where);
  2020. }
  2021. /* Parse a member declaration within the class body of CLS. VIS is
  2022. the access specifier for the member (private, protected,
  2023. public). */
  2024. static void
  2025. member (struct sym *cls, int vis)
  2026. {
  2027. char *id = NULL;
  2028. int sc = SC_MEMBER;
  2029. char *regexp = NULL;
  2030. int pos;
  2031. int is_constructor;
  2032. int anonymous = 0;
  2033. int flags = 0;
  2034. int class_tag;
  2035. int type_seen = 0;
  2036. int paren_seen = 0;
  2037. unsigned hash = 0;
  2038. int tilde = 0;
  2039. while (!LOOKING_AT4 (';', '{', '}', YYEOF))
  2040. {
  2041. switch (LA1)
  2042. {
  2043. default:
  2044. MATCH ();
  2045. break;
  2046. /* A function or class may follow. */
  2047. case TEMPLATE:
  2048. MATCH ();
  2049. SET_FLAG (flags, F_TEMPLATE);
  2050. /* Skip over template argument list */
  2051. SKIP_MATCHING_IF ('<');
  2052. break;
  2053. case EXPLICIT:
  2054. SET_FLAG (flags, F_EXPLICIT);
  2055. goto typeseen;
  2056. case MUTABLE:
  2057. SET_FLAG (flags, F_MUTABLE);
  2058. goto typeseen;
  2059. case T_INLINE:
  2060. SET_FLAG (flags, F_INLINE);
  2061. goto typeseen;
  2062. case VIRTUAL:
  2063. SET_FLAG (flags, F_VIRTUAL);
  2064. goto typeseen;
  2065. case '[':
  2066. skip_matching ();
  2067. break;
  2068. case ENUM:
  2069. sc = SC_TYPE;
  2070. goto typeseen;
  2071. case TYPEDEF:
  2072. sc = SC_TYPE;
  2073. goto typeseen;
  2074. case FRIEND:
  2075. sc = SC_FRIEND;
  2076. goto typeseen;
  2077. case STATIC:
  2078. sc = SC_STATIC;
  2079. goto typeseen;
  2080. case '~':
  2081. tilde = 1;
  2082. MATCH ();
  2083. break;
  2084. case IDENT:
  2085. /* Remember IDENTS seen so far. Among these will be the member
  2086. name. */
  2087. id = (char *) xrealloc (id, strlen (yytext) + 2);
  2088. if (tilde)
  2089. {
  2090. *id = '~';
  2091. strcpy (id + 1, yytext);
  2092. }
  2093. else
  2094. strcpy (id, yytext);
  2095. MATCH ();
  2096. break;
  2097. case OPERATOR:
  2098. {
  2099. char *s = operator_name (&sc);
  2100. id = (char *) xrealloc (id, strlen (s) + 1);
  2101. strcpy (id, s);
  2102. }
  2103. break;
  2104. case '(':
  2105. /* Most probably the beginning of a parameter list. */
  2106. MATCH ();
  2107. paren_seen = 1;
  2108. if (id && cls)
  2109. {
  2110. if (!(is_constructor = streq (id, cls->name)))
  2111. regexp = matching_regexp ();
  2112. }
  2113. else
  2114. is_constructor = 0;
  2115. pos = BUFFER_POS ();
  2116. hash = parm_list (&flags);
  2117. if (is_constructor)
  2118. regexp = matching_regexp ();
  2119. if (id && cls != NULL)
  2120. add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, flags);
  2121. while (!LOOKING_AT3 (';', '{', YYEOF))
  2122. MATCH ();
  2123. if (LOOKING_AT ('{') && id && cls)
  2124. add_member_defn (cls, id, regexp, pos, hash, 0, sc, flags);
  2125. free (id);
  2126. id = NULL;
  2127. sc = SC_MEMBER;
  2128. break;
  2129. case STRUCT: case UNION: case CLASS:
  2130. /* Nested class */
  2131. class_tag = LA1;
  2132. type_seen = 1;
  2133. MATCH ();
  2134. anonymous = 1;
  2135. /* More than one ident here to allow for MS-DOS specialties
  2136. like `_export class' etc. The last IDENT seen counts
  2137. as the class name. */
  2138. while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
  2139. {
  2140. if (LOOKING_AT (IDENT))
  2141. anonymous = 0;
  2142. MATCH ();
  2143. }
  2144. if (LOOKING_AT2 (':', '{'))
  2145. class_definition (anonymous ? NULL : cls, class_tag, flags, 1);
  2146. else
  2147. skip_to (';');
  2148. break;
  2149. case INT: case CHAR: case LONG: case UNSIGNED:
  2150. case SIGNED: case CONST: case DOUBLE: case VOID:
  2151. case SHORT: case VOLATILE: case BOOL: case WCHAR:
  2152. case TYPENAME:
  2153. typeseen:
  2154. type_seen = 1;
  2155. MATCH ();
  2156. break;
  2157. }
  2158. }
  2159. if (LOOKING_AT (';'))
  2160. {
  2161. /* The end of a member variable, a friend declaration or an access
  2162. declaration. We don't want to add friend classes as members. */
  2163. if (id && sc != SC_FRIEND && cls)
  2164. {
  2165. regexp = matching_regexp ();
  2166. pos = BUFFER_POS ();
  2167. if (cls != NULL)
  2168. {
  2169. if (type_seen || !paren_seen)
  2170. add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
  2171. else
  2172. add_member_decl (cls, id, regexp, pos, hash, 0, sc, vis, 0);
  2173. }
  2174. }
  2175. MATCH ();
  2176. print_info ();
  2177. }
  2178. else if (LOOKING_AT ('{'))
  2179. {
  2180. /* A named enum. */
  2181. if (sc == SC_TYPE && id && cls)
  2182. {
  2183. regexp = matching_regexp ();
  2184. pos = BUFFER_POS ();
  2185. if (cls != NULL)
  2186. {
  2187. add_member_decl (cls, id, regexp, pos, 0, 1, sc, vis, 0);
  2188. add_member_defn (cls, id, regexp, pos, 0, 1, sc, 0);
  2189. }
  2190. }
  2191. skip_matching ();
  2192. print_info ();
  2193. }
  2194. free (id);
  2195. }
  2196. /* Parse the body of class CLS. TAG is the tag of the class (struct,
  2197. union, class). */
  2198. static void
  2199. class_body (struct sym *cls, int tag)
  2200. {
  2201. int vis = tag == CLASS ? PRIVATE : PUBLIC;
  2202. int temp;
  2203. while (!LOOKING_AT2 (YYEOF, '}'))
  2204. {
  2205. switch (LA1)
  2206. {
  2207. case PRIVATE: case PROTECTED: case PUBLIC:
  2208. temp = LA1;
  2209. MATCH ();
  2210. if (LOOKING_AT (':'))
  2211. {
  2212. vis = temp;
  2213. MATCH ();
  2214. }
  2215. else
  2216. {
  2217. /* Probably conditional compilation for inheritance list.
  2218. We don't known whether there comes more of this.
  2219. This is only a crude fix that works most of the time. */
  2220. do
  2221. {
  2222. MATCH ();
  2223. }
  2224. while (LOOKING_AT2 (IDENT, ',')
  2225. || LOOKING_AT3 (PUBLIC, PROTECTED, PRIVATE));
  2226. }
  2227. break;
  2228. case TYPENAME:
  2229. case USING:
  2230. skip_to (';');
  2231. break;
  2232. /* Try to synchronize */
  2233. case CHAR: case CLASS: case CONST:
  2234. case DOUBLE: case ENUM: case FLOAT: case INT:
  2235. case LONG: case SHORT: case SIGNED: case STRUCT:
  2236. case UNION: case UNSIGNED: case VOID: case VOLATILE:
  2237. case TYPEDEF: case STATIC: case T_INLINE: case FRIEND:
  2238. case VIRTUAL: case TEMPLATE: case IDENT: case '~':
  2239. case BOOL: case WCHAR: case EXPLICIT: case MUTABLE:
  2240. member (cls, vis);
  2241. break;
  2242. default:
  2243. MATCH ();
  2244. break;
  2245. }
  2246. }
  2247. }
  2248. /* Parse a qualified identifier. Current lookahead is IDENT. A
  2249. qualified ident has the form `X<..>::Y<...>::T<...>. Returns a
  2250. symbol for that class. */
  2251. static struct sym *
  2252. parse_classname (void)
  2253. {
  2254. struct sym *last_class = NULL;
  2255. while (LOOKING_AT (IDENT))
  2256. {
  2257. last_class = add_sym (yytext, last_class);
  2258. MATCH ();
  2259. if (LOOKING_AT ('<'))
  2260. {
  2261. skip_matching ();
  2262. SET_FLAG (last_class->flags, F_TEMPLATE);
  2263. }
  2264. if (!LOOKING_AT (DCOLON))
  2265. break;
  2266. MATCH ();
  2267. }
  2268. return last_class;
  2269. }
  2270. /* Parse an operator name. Add the `static' flag to *SC if an
  2271. implicitly static operator has been parsed. Value is a pointer to
  2272. a static buffer holding the constructed operator name string. */
  2273. static char *
  2274. operator_name (int *sc)
  2275. {
  2276. static size_t id_size = 0;
  2277. static char *id = NULL;
  2278. const char *s;
  2279. size_t len;
  2280. MATCH ();
  2281. if (LOOKING_AT2 (NEW, DELETE))
  2282. {
  2283. /* `new' and `delete' are implicitly static. */
  2284. if (*sc != SC_FRIEND)
  2285. *sc = SC_STATIC;
  2286. s = token_string (LA1);
  2287. MATCH ();
  2288. ptrdiff_t slen = strlen (s);
  2289. len = slen + 10;
  2290. if (len > id_size)
  2291. {
  2292. size_t new_size = max (len, 2 * id_size);
  2293. id = (char *) xrealloc (id, new_size);
  2294. id_size = new_size;
  2295. }
  2296. char *z = stpcpy (id, s);
  2297. /* Vector new or delete? */
  2298. if (LOOKING_AT ('['))
  2299. {
  2300. z = stpcpy (z, "[");
  2301. MATCH ();
  2302. if (LOOKING_AT (']'))
  2303. {
  2304. strcpy (z, "]");
  2305. MATCH ();
  2306. }
  2307. }
  2308. }
  2309. else
  2310. {
  2311. size_t tokens_matched = 0;
  2312. len = 20;
  2313. if (len > id_size)
  2314. {
  2315. int new_size = max (len, 2 * id_size);
  2316. id = (char *) xrealloc (id, new_size);
  2317. id_size = new_size;
  2318. }
  2319. char *z = stpcpy (id, "operator");
  2320. /* Beware access declarations of the form "X::f;" Beware of
  2321. `operator () ()'. Yet another difficulty is found in
  2322. GCC 2.95's STL: `operator == __STL_NULL_TMPL_ARGS (...'. */
  2323. while (!(LOOKING_AT ('(') && tokens_matched)
  2324. && !LOOKING_AT2 (';', YYEOF))
  2325. {
  2326. s = token_string (LA1);
  2327. len += strlen (s) + 2;
  2328. if (len > id_size)
  2329. {
  2330. ptrdiff_t idlen = z - id;
  2331. size_t new_size = max (len, 2 * id_size);
  2332. id = (char *) xrealloc (id, new_size);
  2333. id_size = new_size;
  2334. z = id + idlen;
  2335. }
  2336. if (*s != ')' && *s != ']')
  2337. *z++ = ' ';
  2338. z = stpcpy (z, s);
  2339. MATCH ();
  2340. /* If this is a simple operator like `+', stop now. */
  2341. if (!isalpha ((unsigned char) *s) && *s != '(' && *s != '[')
  2342. break;
  2343. ++tokens_matched;
  2344. }
  2345. }
  2346. return id;
  2347. }
  2348. /* This one consumes the last IDENT of a qualified member name like
  2349. `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
  2350. symbol structure for the ident. */
  2351. static struct sym *
  2352. parse_qualified_ident_or_type (char **last_id)
  2353. {
  2354. struct sym *cls = NULL;
  2355. char *id = NULL;
  2356. size_t id_size = 0;
  2357. int enter = 0;
  2358. while (LOOKING_AT (IDENT))
  2359. {
  2360. int len = strlen (yytext) + 1;
  2361. if (len > id_size)
  2362. {
  2363. id = (char *) xrealloc (id, len);
  2364. id_size = len;
  2365. }
  2366. strcpy (id, yytext);
  2367. *last_id = id;
  2368. MATCH ();
  2369. SKIP_MATCHING_IF ('<');
  2370. if (LOOKING_AT (DCOLON))
  2371. {
  2372. struct sym *pcn = NULL;
  2373. struct link *pna = check_namespace_alias (id);
  2374. if (pna)
  2375. {
  2376. do
  2377. {
  2378. enter_namespace (pna->sym->name);
  2379. enter++;
  2380. pna = pna->next;
  2381. }
  2382. while (pna);
  2383. }
  2384. else if ((pcn = check_namespace (id, current_namespace)))
  2385. {
  2386. enter_namespace (pcn->name);
  2387. enter++;
  2388. }
  2389. else
  2390. cls = add_sym (id, cls);
  2391. *last_id = NULL;
  2392. free (id);
  2393. id = NULL;
  2394. id_size = 0;
  2395. MATCH ();
  2396. }
  2397. else
  2398. break;
  2399. }
  2400. while (enter--)
  2401. leave_namespace ();
  2402. return cls;
  2403. }
  2404. /* This one consumes the last IDENT of a qualified member name like
  2405. `X::Y::z'. This IDENT is returned in LAST_ID. Value is the
  2406. symbol structure for the ident. */
  2407. static void
  2408. parse_qualified_param_ident_or_type (char **last_id)
  2409. {
  2410. struct sym *cls = NULL;
  2411. static char *id = NULL;
  2412. static int id_size = 0;
  2413. assert (LOOKING_AT (IDENT));
  2414. do
  2415. {
  2416. int len = strlen (yytext) + 1;
  2417. if (len > id_size)
  2418. {
  2419. id = (char *) xrealloc (id, len);
  2420. id_size = len;
  2421. }
  2422. strcpy (id, yytext);
  2423. *last_id = id;
  2424. MATCH ();
  2425. SKIP_MATCHING_IF ('<');
  2426. if (LOOKING_AT (DCOLON))
  2427. {
  2428. cls = add_sym (id, cls);
  2429. *last_id = NULL;
  2430. MATCH ();
  2431. }
  2432. else
  2433. break;
  2434. }
  2435. while (LOOKING_AT (IDENT));
  2436. }
  2437. /* Parse a class definition.
  2438. CONTAINING is the class containing the class being parsed or null.
  2439. This may also be null if NESTED != 0 if the containing class is
  2440. anonymous. TAG is the tag of the class (struct, union, class).
  2441. NESTED is non-zero if we are parsing a nested class.
  2442. Current lookahead is the class name. */
  2443. static void
  2444. class_definition (struct sym *containing, int tag, int flags, int nested)
  2445. {
  2446. struct sym *current;
  2447. struct sym *base_class;
  2448. /* Set CURRENT to null if no entry has to be made for the class
  2449. parsed. This is the case for certain command line flag
  2450. settings. */
  2451. if ((tag != CLASS && !f_structs) || (nested && !f_nested_classes))
  2452. current = NULL;
  2453. else
  2454. {
  2455. current = add_sym (yytext, containing);
  2456. current->pos = BUFFER_POS ();
  2457. current->regexp = matching_regexp ();
  2458. current->filename = filename;
  2459. current->flags = flags;
  2460. }
  2461. /* If at ':', base class list follows. */
  2462. if (LOOKING_AT (':'))
  2463. {
  2464. int done = 0;
  2465. MATCH ();
  2466. while (!done)
  2467. {
  2468. switch (LA1)
  2469. {
  2470. case VIRTUAL: case PUBLIC: case PROTECTED: case PRIVATE:
  2471. MATCH ();
  2472. break;
  2473. case IDENT:
  2474. base_class = parse_classname ();
  2475. if (base_class && current && base_class != current)
  2476. add_link (base_class, current);
  2477. break;
  2478. /* The `,' between base classes or the end of the base
  2479. class list. Add the previously found base class.
  2480. It's done this way to skip over sequences of
  2481. `A::B::C' until we reach the end.
  2482. FIXME: it is now possible to handle `class X : public B::X'
  2483. because we have enough information. */
  2484. case ',':
  2485. MATCH ();
  2486. break;
  2487. default:
  2488. /* A syntax error, possibly due to preprocessor constructs
  2489. like
  2490. #ifdef SOMETHING
  2491. class A : public B
  2492. #else
  2493. class A : private B.
  2494. MATCH until we see something like `;' or `{'. */
  2495. while (!LOOKING_AT3 (';', YYEOF, '{'))
  2496. MATCH ();
  2497. done = 1;
  2498. case '{':
  2499. done = 1;
  2500. break;
  2501. }
  2502. }
  2503. }
  2504. /* Parse the class body if there is one. */
  2505. if (LOOKING_AT ('{'))
  2506. {
  2507. if (tag != CLASS && !f_structs)
  2508. skip_matching ();
  2509. else
  2510. {
  2511. MATCH ();
  2512. class_body (current, tag);
  2513. if (LOOKING_AT ('}'))
  2514. {
  2515. MATCH ();
  2516. if (LOOKING_AT (';') && !nested)
  2517. MATCH ();
  2518. }
  2519. }
  2520. }
  2521. }
  2522. /* Add to class *CLS information for the declaration of variable or
  2523. type *ID. If *CLS is null, this means a global declaration. SC is
  2524. the storage class of *ID. FLAGS is a bit set giving additional
  2525. information about the member (see the F_* defines). */
  2526. static void
  2527. add_declarator (struct sym **cls, char **id, int flags, int sc)
  2528. {
  2529. if (LOOKING_AT2 (';', ','))
  2530. {
  2531. /* The end of a member variable or of an access declaration
  2532. `X::f'. To distinguish between them we have to know whether
  2533. type information has been seen. */
  2534. if (*id)
  2535. {
  2536. char *regexp = matching_regexp ();
  2537. int pos = BUFFER_POS ();
  2538. if (*cls)
  2539. add_member_defn (*cls, *id, regexp, pos, 0, 1, SC_UNKNOWN, flags);
  2540. else
  2541. add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
  2542. }
  2543. MATCH ();
  2544. print_info ();
  2545. }
  2546. else if (LOOKING_AT ('{'))
  2547. {
  2548. if (sc == SC_TYPE && *id)
  2549. {
  2550. /* A named enumeration. */
  2551. char *regexp = matching_regexp ();
  2552. int pos = BUFFER_POS ();
  2553. add_global_defn (*id, regexp, pos, 0, 1, sc, flags);
  2554. }
  2555. skip_matching ();
  2556. print_info ();
  2557. }
  2558. free (*id);
  2559. *id = NULL;
  2560. *cls = NULL;
  2561. }
  2562. /* Parse a declaration. */
  2563. static void
  2564. declaration (int flags)
  2565. {
  2566. char *id = NULL;
  2567. struct sym *cls = NULL;
  2568. char *regexp = NULL;
  2569. int pos = 0;
  2570. unsigned hash = 0;
  2571. int is_constructor;
  2572. int sc = 0;
  2573. while (!LOOKING_AT3 (';', '{', YYEOF))
  2574. {
  2575. switch (LA1)
  2576. {
  2577. default:
  2578. MATCH ();
  2579. break;
  2580. case '[':
  2581. skip_matching ();
  2582. break;
  2583. case ENUM:
  2584. case TYPEDEF:
  2585. sc = SC_TYPE;
  2586. MATCH ();
  2587. break;
  2588. case STATIC:
  2589. sc = SC_STATIC;
  2590. MATCH ();
  2591. break;
  2592. case INT: case CHAR: case LONG: case UNSIGNED:
  2593. case SIGNED: case CONST: case DOUBLE: case VOID:
  2594. case SHORT: case VOLATILE: case BOOL: case WCHAR:
  2595. MATCH ();
  2596. break;
  2597. case CLASS: case STRUCT: case UNION:
  2598. /* This is for the case `STARTWRAP class X : ...' or
  2599. `declare (X, Y)\n class A : ...'. */
  2600. if (id)
  2601. {
  2602. free (id);
  2603. return;
  2604. }
  2605. case '=':
  2606. /* Assumed to be the start of an initialization in this
  2607. context. */
  2608. skip_initializer ();
  2609. break;
  2610. case ',':
  2611. add_declarator (&cls, &id, flags, sc);
  2612. break;
  2613. case OPERATOR:
  2614. {
  2615. char *s = operator_name (&sc);
  2616. id = (char *) xrealloc (id, strlen (s) + 1);
  2617. strcpy (id, s);
  2618. }
  2619. break;
  2620. case T_INLINE:
  2621. SET_FLAG (flags, F_INLINE);
  2622. MATCH ();
  2623. break;
  2624. case '~':
  2625. MATCH ();
  2626. if (LOOKING_AT (IDENT))
  2627. {
  2628. id = (char *) xrealloc (id, strlen (yytext) + 2);
  2629. *id = '~';
  2630. strcpy (id + 1, yytext);
  2631. MATCH ();
  2632. }
  2633. break;
  2634. case IDENT:
  2635. cls = parse_qualified_ident_or_type (&id);
  2636. break;
  2637. case '(':
  2638. /* Most probably the beginning of a parameter list. */
  2639. if (cls)
  2640. {
  2641. MATCH ();
  2642. if (id && cls)
  2643. {
  2644. if (!(is_constructor = streq (id, cls->name)))
  2645. regexp = matching_regexp ();
  2646. }
  2647. else
  2648. is_constructor = 0;
  2649. pos = BUFFER_POS ();
  2650. hash = parm_list (&flags);
  2651. if (is_constructor)
  2652. regexp = matching_regexp ();
  2653. if (id && cls)
  2654. add_member_defn (cls, id, regexp, pos, hash, 0,
  2655. SC_UNKNOWN, flags);
  2656. }
  2657. else
  2658. {
  2659. /* This may be a C functions, but also a macro
  2660. call of the form `declare (A, B)' --- such macros
  2661. can be found in some class libraries. */
  2662. MATCH ();
  2663. if (id)
  2664. {
  2665. regexp = matching_regexp ();
  2666. pos = BUFFER_POS ();
  2667. hash = parm_list (&flags);
  2668. add_global_decl (id, regexp, pos, hash, 0, sc, flags);
  2669. }
  2670. /* This is for the case that the function really is
  2671. a macro with no `;' following it. If a CLASS directly
  2672. follows, we would miss it otherwise. */
  2673. if (LOOKING_AT3 (CLASS, STRUCT, UNION))
  2674. return;
  2675. }
  2676. while (!LOOKING_AT3 (';', '{', YYEOF))
  2677. MATCH ();
  2678. if (!cls && id && LOOKING_AT ('{'))
  2679. add_global_defn (id, regexp, pos, hash, 0, sc, flags);
  2680. free (id);
  2681. id = NULL;
  2682. break;
  2683. }
  2684. }
  2685. add_declarator (&cls, &id, flags, sc);
  2686. }
  2687. /* Parse a list of top-level declarations/definitions. START_FLAGS
  2688. says in which context we are parsing. If it is F_EXTERNC, we are
  2689. parsing in an `extern "C"' block. Value is 1 if EOF is reached, 0
  2690. otherwise. */
  2691. static int
  2692. globals (int start_flags)
  2693. {
  2694. int anonymous;
  2695. int class_tk;
  2696. int flags = start_flags;
  2697. for (;;)
  2698. {
  2699. char *prev_in = in;
  2700. switch (LA1)
  2701. {
  2702. case NAMESPACE:
  2703. {
  2704. MATCH ();
  2705. if (LOOKING_AT (IDENT))
  2706. {
  2707. char *namespace_name = xstrdup (yytext);
  2708. MATCH ();
  2709. if (LOOKING_AT ('='))
  2710. {
  2711. struct link *qna = match_qualified_namespace_alias ();
  2712. if (qna)
  2713. register_namespace_alias (namespace_name, qna);
  2714. if (skip_to (';') == ';')
  2715. MATCH ();
  2716. }
  2717. else if (LOOKING_AT ('{'))
  2718. {
  2719. MATCH ();
  2720. enter_namespace (namespace_name);
  2721. globals (0);
  2722. leave_namespace ();
  2723. MATCH_IF ('}');
  2724. }
  2725. free (namespace_name);
  2726. }
  2727. }
  2728. break;
  2729. case EXTERN:
  2730. MATCH ();
  2731. if (LOOKING_AT (CSTRING) && *string_start == 'C'
  2732. && *(string_start + 1) == '"')
  2733. {
  2734. /* This is `extern "C"'. */
  2735. MATCH ();
  2736. if (LOOKING_AT ('{'))
  2737. {
  2738. MATCH ();
  2739. globals (F_EXTERNC);
  2740. MATCH_IF ('}');
  2741. }
  2742. else
  2743. SET_FLAG (flags, F_EXTERNC);
  2744. }
  2745. break;
  2746. case TEMPLATE:
  2747. MATCH ();
  2748. SKIP_MATCHING_IF ('<');
  2749. SET_FLAG (flags, F_TEMPLATE);
  2750. break;
  2751. case CLASS: case STRUCT: case UNION:
  2752. class_tk = LA1;
  2753. MATCH ();
  2754. anonymous = 1;
  2755. /* More than one ident here to allow for MS-DOS and OS/2
  2756. specialties like `far', `_Export' etc. Some C++ libs
  2757. have constructs like `_OS_DLLIMPORT(_OS_CLIENT)' in front
  2758. of the class name. */
  2759. while (!LOOKING_AT4 (YYEOF, ';', ':', '{'))
  2760. {
  2761. if (LOOKING_AT (IDENT))
  2762. anonymous = 0;
  2763. MATCH ();
  2764. }
  2765. /* Don't add anonymous unions. */
  2766. if (LOOKING_AT2 (':', '{') && !anonymous)
  2767. class_definition (NULL, class_tk, flags, 0);
  2768. else
  2769. {
  2770. if (skip_to (';') == ';')
  2771. MATCH ();
  2772. }
  2773. flags = start_flags;
  2774. break;
  2775. case YYEOF:
  2776. return 1;
  2777. case '}':
  2778. return 0;
  2779. default:
  2780. declaration (flags);
  2781. flags = start_flags;
  2782. break;
  2783. }
  2784. if (prev_in == in)
  2785. yyerror ("parse error", NULL);
  2786. }
  2787. }
  2788. /* Parse the current input file. */
  2789. static void
  2790. yyparse (void)
  2791. {
  2792. while (globals (0) == 0)
  2793. MATCH_IF ('}');
  2794. }
  2795. /***********************************************************************
  2796. Main Program
  2797. ***********************************************************************/
  2798. /* Add the list of paths PATH_LIST to the current search path for
  2799. input files. */
  2800. static void
  2801. add_search_path (char *path_list)
  2802. {
  2803. while (*path_list)
  2804. {
  2805. char *start = path_list;
  2806. struct search_path *p;
  2807. while (*path_list && *path_list != SEPCHAR)
  2808. ++path_list;
  2809. p = (struct search_path *) xmalloc (sizeof *p);
  2810. p->path = (char *) xmalloc (path_list - start + 1);
  2811. memcpy (p->path, start, path_list - start);
  2812. p->path[path_list - start] = '\0';
  2813. p->next = NULL;
  2814. if (search_path_tail)
  2815. {
  2816. search_path_tail->next = p;
  2817. search_path_tail = p;
  2818. }
  2819. else
  2820. search_path = search_path_tail = p;
  2821. while (*path_list == SEPCHAR)
  2822. ++path_list;
  2823. }
  2824. }
  2825. /* Open FILE and return a file handle for it, or -1 if FILE cannot be
  2826. opened. Try to find FILE in search_path first, then try the
  2827. unchanged file name. */
  2828. static FILE *
  2829. open_file (char *file)
  2830. {
  2831. FILE *fp = NULL;
  2832. static char *buffer;
  2833. static int buffer_size;
  2834. struct search_path *path;
  2835. int flen = strlen (file) + 1; /* +1 for the slash */
  2836. filename = xstrdup (file);
  2837. for (path = search_path; path && fp == NULL; path = path->next)
  2838. {
  2839. int len = strlen (path->path) + flen;
  2840. if (len + 1 >= buffer_size)
  2841. {
  2842. buffer_size = max (len + 1, 2 * buffer_size);
  2843. buffer = (char *) xrealloc (buffer, buffer_size);
  2844. }
  2845. char *z = stpcpy (buffer, path->path);
  2846. *z++ = '/';
  2847. strcpy (z, file);
  2848. fp = fopen (buffer, "r");
  2849. }
  2850. /* Try the original file name. */
  2851. if (fp == NULL)
  2852. fp = fopen (file, "r");
  2853. if (fp == NULL)
  2854. yyerror ("cannot open", NULL);
  2855. return fp;
  2856. }
  2857. /* Display usage information and exit program. */
  2858. static char const *const usage_message[] =
  2859. {
  2860. "\
  2861. Usage: ebrowse [options] {files}\n\
  2862. \n\
  2863. -a, --append append output to existing file\n\
  2864. -f, --files=FILES read input file names from FILE\n\
  2865. -I, --search-path=LIST set search path for input files\n\
  2866. -m, --min-regexp-length=N set minimum regexp length to N\n\
  2867. -M, --max-regexp-length=N set maximum regexp length to N\n\
  2868. ",
  2869. "\
  2870. -n, --no-nested-classes exclude nested classes\n\
  2871. -o, --output-file=FILE set output file name to FILE\n\
  2872. -p, --position-info print info about position in file\n\
  2873. -s, --no-structs-or-unions don't record structs or unions\n\
  2874. -v, --verbose be verbose\n\
  2875. -V, --very-verbose be very verbose\n\
  2876. -x, --no-regexps don't record regular expressions\n\
  2877. --help display this help\n\
  2878. --version display version info\n\
  2879. \n\
  2880. "
  2881. };
  2882. static _Noreturn void
  2883. usage (int error)
  2884. {
  2885. int i;
  2886. for (i = 0; i < sizeof usage_message / sizeof *usage_message; i++)
  2887. fputs (usage_message[i], stdout);
  2888. exit (error ? EXIT_FAILURE : EXIT_SUCCESS);
  2889. }
  2890. /* Display version and copyright info. The VERSION macro is set
  2891. from config.h and contains the Emacs version. */
  2892. #ifndef VERSION
  2893. # define VERSION "21"
  2894. #endif
  2895. static _Noreturn void
  2896. version (void)
  2897. {
  2898. char emacs_copyright[] = COPYRIGHT;
  2899. printf ("ebrowse %s\n", VERSION);
  2900. puts (emacs_copyright);
  2901. puts ("This program is distributed under the same terms as Emacs.");
  2902. exit (EXIT_SUCCESS);
  2903. }
  2904. /* Parse one input file FILE, adding classes and members to the symbol
  2905. table. */
  2906. static void
  2907. process_file (char *file)
  2908. {
  2909. FILE *fp;
  2910. fp = open_file (file);
  2911. if (fp)
  2912. {
  2913. size_t nread, nbytes;
  2914. /* Give a progress indication if needed. */
  2915. if (f_very_verbose)
  2916. {
  2917. puts (filename);
  2918. fflush (stdout);
  2919. }
  2920. else if (f_verbose)
  2921. {
  2922. putchar ('.');
  2923. fflush (stdout);
  2924. }
  2925. /* Read file to inbuffer. */
  2926. for (nread = 0;;)
  2927. {
  2928. if (nread + READ_CHUNK_SIZE >= inbuffer_size)
  2929. {
  2930. inbuffer_size = nread + READ_CHUNK_SIZE + 1;
  2931. inbuffer = (char *) xrealloc (inbuffer, inbuffer_size);
  2932. }
  2933. nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp);
  2934. if (nbytes == 0)
  2935. break;
  2936. nread += nbytes;
  2937. }
  2938. inbuffer[nread] = '\0';
  2939. /* Reinitialize scanner and parser for the new input file. */
  2940. re_init_scanner ();
  2941. re_init_parser ();
  2942. /* Parse it and close the file. */
  2943. yyparse ();
  2944. fclose (fp);
  2945. }
  2946. }
  2947. /* Read a line from stream FP and return a pointer to a static buffer
  2948. containing its contents without the terminating newline. Value
  2949. is null when EOF is reached. */
  2950. static char *
  2951. read_line (FILE *fp)
  2952. {
  2953. static char *buffer;
  2954. static int buffer_size;
  2955. int i = 0, c;
  2956. while ((c = getc (fp)) != EOF && c != '\n')
  2957. {
  2958. if (i >= buffer_size)
  2959. {
  2960. buffer_size = max (100, buffer_size * 2);
  2961. buffer = (char *) xrealloc (buffer, buffer_size);
  2962. }
  2963. buffer[i++] = c;
  2964. }
  2965. if (c == EOF && i == 0)
  2966. return NULL;
  2967. if (i == buffer_size)
  2968. {
  2969. buffer_size = max (100, buffer_size * 2);
  2970. buffer = (char *) xrealloc (buffer, buffer_size);
  2971. }
  2972. buffer[i] = '\0';
  2973. if (i > 0 && buffer[i - 1] == '\r')
  2974. buffer[i - 1] = '\0';
  2975. return buffer;
  2976. }
  2977. /* Main entry point. */
  2978. int
  2979. main (int argc, char **argv)
  2980. {
  2981. int i;
  2982. int any_inputfiles = 0;
  2983. static const char *out_filename = DEFAULT_OUTFILE;
  2984. static char **input_filenames = NULL;
  2985. static int input_filenames_size = 0;
  2986. static int n_input_files;
  2987. filename = "command line";
  2988. yyout = stdout;
  2989. while ((i = getopt_long (argc, argv, "af:I:m:M:no:p:svVx",
  2990. options, NULL)) != EOF)
  2991. {
  2992. switch (i)
  2993. {
  2994. /* Experimental. */
  2995. case 'p':
  2996. info_position = atoi (optarg);
  2997. break;
  2998. case 'n':
  2999. f_nested_classes = 0;
  3000. break;
  3001. case 'x':
  3002. f_regexps = 0;
  3003. break;
  3004. /* Add the name of a file containing more input files. */
  3005. case 'f':
  3006. if (n_input_files == input_filenames_size)
  3007. {
  3008. input_filenames_size = max (10, 2 * input_filenames_size);
  3009. input_filenames = (char **) xrealloc ((void *)input_filenames,
  3010. input_filenames_size);
  3011. }
  3012. input_filenames[n_input_files++] = xstrdup (optarg);
  3013. break;
  3014. /* Append new output to output file instead of truncating it. */
  3015. case 'a':
  3016. f_append = 1;
  3017. break;
  3018. /* Include structs in the output */
  3019. case 's':
  3020. f_structs = 0;
  3021. break;
  3022. /* Be verbose (give a progress indication). */
  3023. case 'v':
  3024. f_verbose = 1;
  3025. break;
  3026. /* Be very verbose (print file names as they are processed). */
  3027. case 'V':
  3028. f_verbose = 1;
  3029. f_very_verbose = 1;
  3030. break;
  3031. /* Change the name of the output file. */
  3032. case 'o':
  3033. out_filename = optarg;
  3034. break;
  3035. /* Set minimum length for regular expression strings
  3036. when recorded in the output file. */
  3037. case 'm':
  3038. min_regexp = atoi (optarg);
  3039. break;
  3040. /* Set maximum length for regular expression strings
  3041. when recorded in the output file. */
  3042. case 'M':
  3043. max_regexp = atoi (optarg);
  3044. break;
  3045. /* Add to search path. */
  3046. case 'I':
  3047. add_search_path (optarg);
  3048. break;
  3049. /* Display help */
  3050. case -2:
  3051. usage (0);
  3052. break;
  3053. case -3:
  3054. version ();
  3055. break;
  3056. }
  3057. }
  3058. /* Call init_scanner after command line flags have been processed to be
  3059. able to add keywords depending on command line (not yet
  3060. implemented). */
  3061. init_scanner ();
  3062. init_sym ();
  3063. /* Open output file */
  3064. if (*out_filename)
  3065. {
  3066. if (f_append)
  3067. {
  3068. /* Check that the file to append to exists, and is not
  3069. empty. More specifically, it should be a valid file
  3070. produced by a previous run of ebrowse, but that's too
  3071. difficult to check. */
  3072. FILE *fp;
  3073. int rc;
  3074. fp = fopen (out_filename, "r");
  3075. if (fp == NULL)
  3076. {
  3077. yyerror ("file `%s' must exist for --append", out_filename);
  3078. exit (EXIT_FAILURE);
  3079. }
  3080. rc = fseek (fp, 0, SEEK_END);
  3081. if (rc == -1)
  3082. {
  3083. yyerror ("error seeking in file `%s'", out_filename);
  3084. exit (EXIT_FAILURE);
  3085. }
  3086. rc = ftell (fp);
  3087. if (rc == -1)
  3088. {
  3089. yyerror ("error getting size of file `%s'", out_filename);
  3090. exit (EXIT_FAILURE);
  3091. }
  3092. else if (rc == 0)
  3093. {
  3094. yyerror ("file `%s' is empty", out_filename);
  3095. /* It may be ok to use an empty file for appending.
  3096. exit (EXIT_FAILURE); */
  3097. }
  3098. fclose (fp);
  3099. }
  3100. yyout = fopen (out_filename, f_append ? "a" : "w");
  3101. if (yyout == NULL)
  3102. {
  3103. yyerror ("cannot open output file `%s'", out_filename);
  3104. exit (EXIT_FAILURE);
  3105. }
  3106. }
  3107. /* Process input files specified on the command line. */
  3108. while (optind < argc)
  3109. {
  3110. process_file (argv[optind++]);
  3111. any_inputfiles = 1;
  3112. }
  3113. /* Process files given on stdin if no files specified. */
  3114. if (!any_inputfiles && n_input_files == 0)
  3115. {
  3116. char *file;
  3117. while ((file = read_line (stdin)) != NULL)
  3118. process_file (file);
  3119. }
  3120. else
  3121. {
  3122. /* Process files from `--files=FILE'. Every line in FILE names
  3123. one input file to process. */
  3124. for (i = 0; i < n_input_files; ++i)
  3125. {
  3126. FILE *fp = fopen (input_filenames[i], "r");
  3127. if (fp == NULL)
  3128. yyerror ("cannot open input file `%s'", input_filenames[i]);
  3129. else
  3130. {
  3131. char *file;
  3132. while ((file = read_line (fp)) != NULL)
  3133. process_file (file);
  3134. fclose (fp);
  3135. }
  3136. }
  3137. }
  3138. /* Write output file. */
  3139. dump_roots (yyout);
  3140. /* Close output file. */
  3141. if (yyout != stdout)
  3142. fclose (yyout);
  3143. return EXIT_SUCCESS;
  3144. }
  3145. /* ebrowse.c ends here */