symtable.pas 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program 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 2 of the License, or
  7. (at your option) any later version.
  8. This program 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 this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym,
  25. { ppu }
  26. ppu;
  27. {****************************************************************************
  28. Symtable types
  29. ****************************************************************************}
  30. type
  31. tstoredsymtable = class(TSymtable)
  32. private
  33. init_final_check_done : boolean;
  34. procedure _needs_init_final(sym:TObject;arg:pointer);
  35. procedure check_forward(sym:TObject;arg:pointer);
  36. procedure check_block_valid(def: TObject;arg:pointer);
  37. procedure labeldefined(sym:TObject;arg:pointer);
  38. procedure varsymbolused(sym:TObject;arg:pointer);
  39. procedure TestPrivate(sym:TObject;arg:pointer);
  40. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  41. procedure loaddefs(ppufile:tcompilerppufile);
  42. procedure loadsyms(ppufile:tcompilerppufile);
  43. procedure writedefs(ppufile:tcompilerppufile);
  44. procedure writesyms(ppufile:tcompilerppufile);
  45. public
  46. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  47. procedure delete(sym:TSymEntry);override;
  48. { load/write }
  49. procedure ppuload(ppufile:tcompilerppufile);virtual;
  50. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  51. procedure buildderef;virtual;
  52. procedure buildderefimpl;virtual;
  53. procedure deref;virtual;
  54. procedure derefimpl;virtual;
  55. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  56. procedure allsymbolsused;
  57. procedure allprivatesused;
  58. procedure check_forwards;
  59. procedure checklabels;
  60. function needs_init_final : boolean;
  61. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  62. end;
  63. tabstractrecordsymtable = class(tstoredsymtable)
  64. public
  65. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  66. recordalignment, { alignment desired when inserting this record }
  67. fieldalignment, { alignment current alignment used when fields are inserted }
  68. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  69. constructor create(const n:string;usealign:shortint);
  70. procedure ppuload(ppufile:tcompilerppufile);override;
  71. procedure ppuwrite(ppufile:tcompilerppufile);override;
  72. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  73. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  74. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  75. procedure addalignmentpadding;
  76. procedure insertdef(def:TDefEntry);override;
  77. function is_packed: boolean;
  78. function has_single_field(out sym:tfieldvarsym): boolean;
  79. function get_unit_symtable: tsymtable;
  80. protected
  81. { size in bytes including padding }
  82. _datasize : asizeint;
  83. { size in bits of the data in case of bitpacked record. Only important during construction, }
  84. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  85. databitsize : asizeint;
  86. { size in bytes of padding }
  87. _paddingsize : word;
  88. procedure setdatasize(val: asizeint);
  89. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  90. public
  91. function iscurrentunit: boolean; override;
  92. property datasize : asizeint read _datasize write setdatasize;
  93. property paddingsize: word read _paddingsize write _paddingsize;
  94. end;
  95. trecordsymtable = class(tabstractrecordsymtable)
  96. public
  97. constructor create(const n:string;usealign:shortint);
  98. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  99. end;
  100. tObjectSymtable = class(tabstractrecordsymtable)
  101. public
  102. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  103. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  104. end;
  105. { tabstractlocalsymtable }
  106. tabstractlocalsymtable = class(tstoredsymtable)
  107. public
  108. procedure ppuwrite(ppufile:tcompilerppufile);override;
  109. function count_locals:longint;
  110. end;
  111. tlocalsymtable = class(tabstractlocalsymtable)
  112. public
  113. constructor create(adefowner:tdef;level:byte);
  114. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  115. end;
  116. { tparasymtable }
  117. tparasymtable = class(tabstractlocalsymtable)
  118. public
  119. readonly: boolean;
  120. constructor create(adefowner:tdef;level:byte);
  121. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  122. procedure insertdef(def:TDefEntry);override;
  123. end;
  124. tabstractuniTSymtable = class(tstoredsymtable)
  125. public
  126. constructor create(const n : string;id:word);
  127. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  128. function findnamespace(const n:string):TSymEntry;virtual;
  129. function iscurrentunit:boolean;override;
  130. procedure insertunit(sym:TSymEntry);
  131. end;
  132. tglobalsymtable = class(tabstractuniTSymtable)
  133. public
  134. unittypecount : word;
  135. constructor create(const n : string;id:word);
  136. procedure ppuload(ppufile:tcompilerppufile);override;
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. end;
  139. tstaticsymtable = class(tabstractuniTSymtable)
  140. public
  141. constructor create(const n : string;id:word);
  142. procedure ppuload(ppufile:tcompilerppufile);override;
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  145. function findnamespace(const n:string):TSymEntry;override;
  146. end;
  147. tspecializesymtable = class(tglobalsymtable)
  148. public
  149. constructor create(const n : string;id:word);
  150. function iscurrentunit:boolean;override;
  151. end;
  152. twithsymtable = class(TSymtable)
  153. withrefnode : tobject; { tnode }
  154. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  155. destructor destroy;override;
  156. procedure clear;override;
  157. procedure insertdef(def:TDefEntry);override;
  158. end;
  159. tstt_excepTSymtable = class(TSymtable)
  160. public
  161. constructor create;
  162. end;
  163. tmacrosymtable = class(tstoredsymtable)
  164. public
  165. constructor create(exported: boolean);
  166. end;
  167. { tenumsymtable }
  168. tenumsymtable = class(tstoredsymtable)
  169. public
  170. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  171. constructor create(adefowner:tdef);
  172. end;
  173. { tarraysymtable }
  174. tarraysymtable = class(tstoredsymtable)
  175. public
  176. procedure insertdef(def:TDefEntry);override;
  177. constructor create(adefowner:tdef);
  178. end;
  179. var
  180. systemunit : tglobalsymtable; { pointer to the system unit }
  181. type
  182. tsymbol_search_flag = (
  183. ssf_search_option,
  184. ssf_search_helper,
  185. ssf_has_inherited,
  186. ssf_no_addsymref
  187. );
  188. tsymbol_search_flags = set of tsymbol_search_flag;
  189. {****************************************************************************
  190. Functions
  191. ****************************************************************************}
  192. {*** Misc ***}
  193. function FullTypeName(def,otherdef:tdef):string;
  194. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  195. { def is the extended type of a helper }
  196. function generate_objectpascal_helper_key(def:tdef):string;
  197. procedure incompatibletypes(def1,def2:tdef);
  198. procedure hidesym(sym:TSymEntry);
  199. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  200. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  201. function get_jumpbuf_size : longint;
  202. {*** Search ***}
  203. procedure addsymref(sym:tsym);
  204. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  205. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  206. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  207. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  208. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  209. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  210. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  211. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  212. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  213. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  214. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  215. { searches for a symbol with the given name that has the given option in
  216. symoptions set }
  217. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  218. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  219. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  220. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  221. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  222. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  223. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  224. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  225. { searches symbols inside of a helper's implementation }
  226. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  227. function search_system_type(const s: TIDString): ttypesym;
  228. function try_search_system_type(const s: TIDString): ttypesym;
  229. function search_system_proc(const s: TIDString): tprocdef;
  230. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  231. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  232. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  233. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  234. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  235. { searches for the helper definition that's currently active for pd }
  236. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  237. { searches whether the symbol s is available in the currently active }
  238. { helper for pd }
  239. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  240. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  241. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  242. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  243. {and returns it if found. Returns nil otherwise.}
  244. function search_macro(const s : string):tsym;
  245. { Additionally to searching for a macro, also checks whether it's still }
  246. { actually defined (could be disable using "undef") }
  247. function defined_macro(const s : string):boolean;
  248. { Look for a system procedure (no overloads supported) }
  249. {*** Object Helpers ***}
  250. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  251. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  252. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  253. {*** Macro Helpers ***}
  254. {If called initially, the following procedures manipulate macros in }
  255. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  256. {Name can be given in any case (it will be converted to upper case).}
  257. procedure def_system_macro(const name : string);
  258. procedure set_system_macro(const name, value : string);
  259. procedure set_system_compvar(const name, value : string);
  260. procedure undef_system_macro(const name : string);
  261. {*** symtable stack ***}
  262. { $ifdef DEBUG
  263. procedure test_symtablestack;
  264. procedure list_symtablestack;
  265. $endif DEBUG}
  266. {$ifdef UNITALIASES}
  267. type
  268. punit_alias = ^tunit_alias;
  269. tunit_alias = object(TNamedIndexItem)
  270. newname : pshortstring;
  271. constructor init(const n:string);
  272. destructor done;virtual;
  273. end;
  274. var
  275. unitaliases : pdictionary;
  276. procedure addunitalias(const n:string);
  277. function getunitalias(const n:string):string;
  278. {$endif UNITALIASES}
  279. {*** Init / Done ***}
  280. procedure IniTSymtable;
  281. procedure DoneSymtable;
  282. const
  283. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  284. { NOTOKEN } 'error',
  285. { _PLUS } 'plus',
  286. { _MINUS } 'minus',
  287. { _STAR } 'star',
  288. { _SLASH } 'slash',
  289. { _EQ } 'equal',
  290. { _GT } 'greater',
  291. { _LT } 'lower',
  292. { _GTE } 'greater_or_equal',
  293. { _LTE } 'lower_or_equal',
  294. { _NE } 'not_equal',
  295. { _SYMDIF } 'sym_diff',
  296. { _STARSTAR } 'starstar',
  297. { _OP_AS } 'as',
  298. { _OP_IN } 'in',
  299. { _OP_IS } 'is',
  300. { _OP_OR } 'or',
  301. { _OP_AND } 'and',
  302. { _OP_DIV } 'div',
  303. { _OP_MOD } 'mod',
  304. { _OP_NOT } 'not',
  305. { _OP_SHL } 'shl',
  306. { _OP_SHR } 'shr',
  307. { _OP_XOR } 'xor',
  308. { _ASSIGNMENT } 'assign',
  309. { _OP_EXPLICIT } 'explicit',
  310. { _OP_ENUMERATOR } 'enumerator',
  311. { _OP_INC } 'inc',
  312. { _OP_DEC } 'dec');
  313. implementation
  314. uses
  315. { global }
  316. verbose,globals,
  317. { symtable }
  318. symutil,defutil,defcmp,objcdef,
  319. { module }
  320. fmodule,
  321. { codegen }
  322. procinfo
  323. ;
  324. var
  325. dupnr : longint; { unique number for duplicate symbols }
  326. {*****************************************************************************
  327. TStoredSymtable
  328. *****************************************************************************}
  329. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  330. begin
  331. inherited insert(sym,checkdup);
  332. end;
  333. procedure tstoredsymtable.delete(sym:TSymEntry);
  334. begin
  335. inherited delete(sym);
  336. end;
  337. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  338. begin
  339. { load the table's flags }
  340. if ppufile.readentry<>ibsymtableoptions then
  341. Message(unit_f_ppu_read_error);
  342. ppufile.getsmallset(tableoptions);
  343. { load definitions }
  344. loaddefs(ppufile);
  345. { load symbols }
  346. loadsyms(ppufile);
  347. init_final_check_done:=true;
  348. end;
  349. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  350. begin
  351. { ensure that we have the sto_needs_init_final flag set if needed }
  352. if not init_final_check_done then
  353. needs_init_final;
  354. { write the table's flags }
  355. ppufile.putsmallset(tableoptions);
  356. ppufile.writeentry(ibsymtableoptions);
  357. { write definitions }
  358. writedefs(ppufile);
  359. { write symbols }
  360. writesyms(ppufile);
  361. end;
  362. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  363. var
  364. def : tdef;
  365. b : byte;
  366. begin
  367. def:=nil;
  368. { load start of definition section, which holds the amount of defs }
  369. if ppufile.readentry<>ibstartdefs then
  370. Message(unit_f_ppu_read_error);
  371. { read definitions }
  372. repeat
  373. b:=ppufile.readentry;
  374. case b of
  375. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  376. ibarraydef : def:=carraydef.ppuload(ppufile);
  377. iborddef : def:=corddef.ppuload(ppufile);
  378. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  379. ibprocdef : def:=cprocdef.ppuload(ppufile);
  380. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  381. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  382. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  383. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  384. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  385. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  386. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  387. ibenumdef : def:=cenumdef.ppuload(ppufile);
  388. ibsetdef : def:=csetdef.ppuload(ppufile);
  389. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  390. ibfiledef : def:=cfiledef.ppuload(ppufile);
  391. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  392. ibformaldef : def:=cformaldef.ppuload(ppufile);
  393. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  394. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  395. ibenddefs : break;
  396. ibend : Message(unit_f_ppu_read_error);
  397. else
  398. Message1(unit_f_ppu_invalid_entry,tostr(b));
  399. end;
  400. InsertDef(def);
  401. until false;
  402. end;
  403. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  404. var
  405. b : byte;
  406. sym : tsym;
  407. begin
  408. sym:=nil;
  409. { load start of definition section, which holds the amount of defs }
  410. if ppufile.readentry<>ibstartsyms then
  411. Message(unit_f_ppu_read_error);
  412. { now read the symbols }
  413. repeat
  414. b:=ppufile.readentry;
  415. case b of
  416. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  417. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  418. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  419. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  420. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  421. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  422. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  423. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  424. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  425. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  426. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  427. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  428. ibsyssym : sym:=csyssym.ppuload(ppufile);
  429. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  430. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  431. ibendsyms : break;
  432. ibend : Message(unit_f_ppu_read_error);
  433. else
  434. Message1(unit_f_ppu_invalid_entry,tostr(b));
  435. end;
  436. Insert(sym,false);
  437. until false;
  438. end;
  439. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  440. var
  441. i : longint;
  442. def : tstoreddef;
  443. begin
  444. { each definition get a number, write then the amount of defs to the
  445. ibstartdef entry }
  446. ppufile.putlongint(DefList.count);
  447. ppufile.writeentry(ibstartdefs);
  448. { now write the definition }
  449. for i:=0 to DefList.Count-1 do
  450. begin
  451. def:=tstoreddef(DefList[i]);
  452. def.ppuwrite(ppufile);
  453. end;
  454. { write end of definitions }
  455. ppufile.writeentry(ibenddefs);
  456. end;
  457. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  458. var
  459. i : longint;
  460. sym : Tstoredsym;
  461. begin
  462. { each definition get a number, write then the amount of syms and the
  463. datasize to the ibsymdef entry }
  464. ppufile.putlongint(SymList.count);
  465. ppufile.writeentry(ibstartsyms);
  466. { foreach is used to write all symbols }
  467. for i:=0 to SymList.Count-1 do
  468. begin
  469. sym:=tstoredsym(SymList[i]);
  470. sym.ppuwrite(ppufile);
  471. end;
  472. { end of symbols }
  473. ppufile.writeentry(ibendsyms);
  474. end;
  475. procedure tstoredsymtable.buildderef;
  476. var
  477. i : longint;
  478. def : tstoreddef;
  479. sym : tstoredsym;
  480. begin
  481. { interface definitions }
  482. for i:=0 to DefList.Count-1 do
  483. begin
  484. def:=tstoreddef(DefList[i]);
  485. def.buildderef;
  486. end;
  487. { interface symbols }
  488. for i:=0 to SymList.Count-1 do
  489. begin
  490. sym:=tstoredsym(SymList[i]);
  491. sym.buildderef;
  492. end;
  493. end;
  494. procedure tstoredsymtable.buildderefimpl;
  495. var
  496. i : longint;
  497. def : tstoreddef;
  498. begin
  499. { implementation definitions }
  500. for i:=0 to DefList.Count-1 do
  501. begin
  502. def:=tstoreddef(DefList[i]);
  503. def.buildderefimpl;
  504. end;
  505. end;
  506. procedure tstoredsymtable.deref;
  507. var
  508. i : longint;
  509. def : tstoreddef;
  510. sym : tstoredsym;
  511. begin
  512. { first deref the interface ttype symbols. This is needs
  513. to be done before the interface defs are derefed, because
  514. the interface defs can contain references to the type symbols
  515. which then already need to contain a resolved typedef field (PFV) }
  516. for i:=0 to SymList.Count-1 do
  517. begin
  518. sym:=tstoredsym(SymList[i]);
  519. if sym.typ=typesym then
  520. sym.deref;
  521. end;
  522. { interface definitions }
  523. for i:=0 to DefList.Count-1 do
  524. begin
  525. def:=tstoreddef(DefList[i]);
  526. def.deref;
  527. end;
  528. { interface symbols }
  529. for i:=0 to SymList.Count-1 do
  530. begin
  531. sym:=tstoredsym(SymList[i]);
  532. if sym.typ<>typesym then
  533. sym.deref;
  534. end;
  535. end;
  536. procedure tstoredsymtable.derefimpl;
  537. var
  538. i : longint;
  539. def : tstoreddef;
  540. begin
  541. { implementation definitions }
  542. for i:=0 to DefList.Count-1 do
  543. begin
  544. def:=tstoreddef(DefList[i]);
  545. def.derefimpl;
  546. end;
  547. end;
  548. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  549. var
  550. hsym : tsym;
  551. begin
  552. hsym:=tsym(FindWithHash(hashedid));
  553. if assigned(hsym) then
  554. DuplicateSym(hashedid,sym,hsym,false);
  555. result:=assigned(hsym);
  556. end;
  557. {**************************************
  558. Callbacks
  559. **************************************}
  560. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  561. begin
  562. if tsym(sym).typ=procsym then
  563. tprocsym(sym).check_forward
  564. { check also object method table }
  565. { we needn't to test the def list }
  566. { because each object has to have a type sym,
  567. only test objects declarations, not type renamings }
  568. else
  569. if (tsym(sym).typ=typesym) and
  570. assigned(ttypesym(sym).typedef) and
  571. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  572. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  573. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  574. end;
  575. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  576. var
  577. founderrordef: tdef;
  578. begin
  579. { all parameters passed to a block must be handled by the Objective-C
  580. runtime }
  581. if is_block(tdef(def)) and
  582. not objcchecktype(tdef(def),founderrordef) then
  583. if assigned(tdef(def).typesym) then
  584. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  585. else
  586. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  587. end;
  588. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  589. begin
  590. if (tsym(sym).typ=labelsym) and
  591. not(tlabelsym(sym).defined) then
  592. begin
  593. if tlabelsym(sym).used then
  594. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  595. else
  596. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  597. end;
  598. end;
  599. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  600. begin
  601. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  602. ((tsym(sym).owner.symtabletype in
  603. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  604. begin
  605. { unused symbol should be reported only if no }
  606. { error is reported }
  607. { if the symbol is in a register it is used }
  608. { also don't count the value parameters which have local copies }
  609. { also don't claim for high param of open parameters (PM) }
  610. { also don't complain about unused symbols in generic procedures }
  611. { and methods }
  612. if (Errorcount<>0) or
  613. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  614. (sp_internal in tsym(sym).symoptions) or
  615. ((assigned(tsym(sym).owner.defowner) and
  616. (tsym(sym).owner.defowner.typ=procdef) and
  617. (df_generic in tprocdef(tsym(sym).owner.defowner).defoptions))) then
  618. exit;
  619. if (tstoredsym(sym).refs=0) then
  620. begin
  621. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  622. begin
  623. { don't warn about the result of constructors }
  624. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  625. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  626. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  627. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  628. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  629. end
  630. else if (tsym(sym).owner.symtabletype=parasymtable) then
  631. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  632. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  633. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  634. else
  635. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  636. end
  637. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  638. begin
  639. if (tsym(sym).owner.symtabletype=parasymtable) then
  640. begin
  641. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  642. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  643. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  644. end
  645. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  646. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  647. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  648. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  649. end
  650. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  651. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  652. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  653. end
  654. else if ((tsym(sym).owner.symtabletype in
  655. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  656. begin
  657. if (Errorcount<>0) or
  658. (sp_internal in tsym(sym).symoptions) then
  659. exit;
  660. { do not claim for inherited private fields !! }
  661. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  662. case tsym(sym).typ of
  663. typesym:
  664. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  665. constsym:
  666. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  667. propertysym:
  668. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  669. else
  670. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  671. end
  672. { units references are problematic }
  673. else
  674. begin
  675. if (tsym(sym).refs=0) and
  676. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  677. not(is_funcret_sym(tsym(sym))) and
  678. { don't complain about compiler generated syms for specializations, see also #13405 }
  679. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  680. (pos('$',ttypesym(sym).Realname)<>0)) and
  681. (
  682. (tsym(sym).typ<>procsym) or
  683. ((tsym(sym).owner.symtabletype=staticsymtable) and
  684. not current_module.is_unit)
  685. ) and
  686. { don't complain about alias for hidden _cmd parameter to
  687. obj-c methods }
  688. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  689. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  690. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  691. end;
  692. end;
  693. end;
  694. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  695. begin
  696. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  697. varsymbolused(sym,arg);
  698. end;
  699. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  700. begin
  701. {
  702. Don't test simple object aliases PM
  703. }
  704. if (tsym(sym).typ=typesym) and
  705. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  706. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  707. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  708. end;
  709. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  710. begin
  711. if (tsym(sym).typ=propertysym) and
  712. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  713. ppointer(arg)^:=sym;
  714. end;
  715. {***********************************************
  716. Process all entries
  717. ***********************************************}
  718. { checks, if all procsyms and methods are defined }
  719. procedure tstoredsymtable.check_forwards;
  720. begin
  721. SymList.ForEachCall(@check_forward,nil);
  722. { check whether all block definitions contain valid Objective-C types
  723. (now that all forward definitions have been resolved) }
  724. DefList.ForEachCall(@check_block_valid,nil);
  725. end;
  726. procedure tstoredsymtable.checklabels;
  727. begin
  728. SymList.ForEachCall(@labeldefined,nil);
  729. end;
  730. procedure tstoredsymtable.allsymbolsused;
  731. begin
  732. SymList.ForEachCall(@varsymbolused,nil);
  733. end;
  734. procedure tstoredsymtable.allprivatesused;
  735. begin
  736. SymList.ForEachCall(@objectprivatesymbolused,nil);
  737. end;
  738. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  739. begin
  740. if sto_needs_init_final in tableoptions then
  741. exit;
  742. { don't check static symbols - they can be present in structures only and
  743. always have a reference to a symbol defined on unit level }
  744. if sp_static in tsym(sym).symoptions then
  745. exit;
  746. case tsym(sym).typ of
  747. fieldvarsym,
  748. staticvarsym,
  749. localvarsym,
  750. paravarsym :
  751. begin
  752. if assigned(tabstractvarsym(sym).vardef) and
  753. is_managed_type(tabstractvarsym(sym).vardef) then
  754. include(tableoptions,sto_needs_init_final);
  755. end;
  756. end;
  757. end;
  758. { returns true, if p contains data which needs init/final code }
  759. function tstoredsymtable.needs_init_final : boolean;
  760. begin
  761. if not init_final_check_done then
  762. begin
  763. exclude(tableoptions,sto_needs_init_final);
  764. SymList.ForEachCall(@_needs_init_final,nil);
  765. init_final_check_done:=true;
  766. end;
  767. result:=sto_needs_init_final in tableoptions;
  768. end;
  769. {****************************************************************************
  770. TAbstractRecordSymtable
  771. ****************************************************************************}
  772. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  773. begin
  774. inherited create(n);
  775. moduleid:=current_module.moduleid;
  776. _datasize:=0;
  777. databitsize:=0;
  778. recordalignment:=1;
  779. usefieldalignment:=usealign;
  780. padalignment:=1;
  781. { recordalign C_alignment means C record packing, that starts
  782. with an alignment of 1 }
  783. case usealign of
  784. C_alignment,
  785. bit_alignment:
  786. fieldalignment:=1;
  787. mac68k_alignment:
  788. fieldalignment:=2;
  789. else
  790. fieldalignment:=usealign;
  791. end;
  792. end;
  793. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  794. begin
  795. if ppufile.readentry<>ibrecsymtableoptions then
  796. Message(unit_f_ppu_read_error);
  797. recordalignment:=shortint(ppufile.getbyte);
  798. usefieldalignment:=shortint(ppufile.getbyte);
  799. if (usefieldalignment=C_alignment) then
  800. fieldalignment:=shortint(ppufile.getbyte);
  801. inherited ppuload(ppufile);
  802. end;
  803. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  804. var
  805. oldtyp : byte;
  806. begin
  807. oldtyp:=ppufile.entrytyp;
  808. ppufile.entrytyp:=subentryid;
  809. { in case of classes using C alignment, the alignment of the parent
  810. affects the alignment of fields of the childs }
  811. ppufile.putbyte(byte(recordalignment));
  812. ppufile.putbyte(byte(usefieldalignment));
  813. if (usefieldalignment=C_alignment) then
  814. ppufile.putbyte(byte(fieldalignment));
  815. ppufile.writeentry(ibrecsymtableoptions);
  816. inherited ppuwrite(ppufile);
  817. ppufile.entrytyp:=oldtyp;
  818. end;
  819. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  820. begin
  821. { optimal alignment of the record when declaring a variable of this }
  822. { type is independent of the packrecords setting }
  823. if (fieldoffs mod fieldalign) = 0 then
  824. result:=fieldalign
  825. else if (fieldalign >= 16) and
  826. ((fieldoffs mod 16) = 0) and
  827. ((fieldalign mod 16) = 0) then
  828. result:=16
  829. else if (fieldalign >= 8) and
  830. ((fieldoffs mod 8) = 0) and
  831. ((fieldalign mod 8) = 0) then
  832. result:=8
  833. else if (fieldalign >= 4) and
  834. ((fieldoffs mod 4) = 0) and
  835. ((fieldalign mod 4) = 0) then
  836. result:=4
  837. else if (fieldalign >= 2) and
  838. ((fieldoffs mod 2) = 0) and
  839. ((fieldalign mod 2) = 0) then
  840. result:=2
  841. else
  842. result:=1;
  843. end;
  844. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  845. var
  846. varalignrecord: shortint;
  847. begin
  848. case usefieldalignment of
  849. C_alignment:
  850. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  851. mac68k_alignment:
  852. varalignrecord:=2;
  853. else
  854. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  855. end;
  856. recordalignment:=max(recordalignment,varalignrecord);
  857. end;
  858. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  859. var
  860. l : asizeint;
  861. varalign : shortint;
  862. vardef : tdef;
  863. begin
  864. if (sym.owner<>self) then
  865. internalerror(200602031);
  866. if sym.fieldoffset<>-1 then
  867. internalerror(200602032);
  868. { set visibility for the symbol }
  869. sym.visibility:=vis;
  870. { this symbol can't be loaded to a register }
  871. sym.varregable:=vr_none;
  872. { Calculate field offset }
  873. l:=sym.getsize;
  874. vardef:=sym.vardef;
  875. varalign:=vardef.structalignment;
  876. case usefieldalignment of
  877. bit_alignment:
  878. begin
  879. { bitpacking only happens for ordinals, the rest is aligned at }
  880. { 1 byte (compatible with GPC/GCC) }
  881. if is_ordinal(vardef) then
  882. begin
  883. sym.fieldoffset:=databitsize;
  884. l:=sym.getpackedbitsize;
  885. end
  886. else
  887. begin
  888. databitsize:=_datasize*8;
  889. sym.fieldoffset:=databitsize;
  890. if (l>high(asizeint) div 8) then
  891. Message(sym_e_segment_too_large);
  892. l:=l*8;
  893. end;
  894. if varalign=0 then
  895. varalign:=size_2_align(l);
  896. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  897. { bit packed records are limited to high(aint) bits }
  898. { instead of bytes to avoid double precision }
  899. { arithmetic in offset calculations }
  900. if int64(l)>high(asizeint)-sym.fieldoffset then
  901. begin
  902. Message(sym_e_segment_too_large);
  903. _datasize:=high(asizeint);
  904. databitsize:=high(asizeint);
  905. end
  906. else
  907. begin
  908. databitsize:=sym.fieldoffset+l;
  909. _datasize:=(databitsize+7) div 8;
  910. end;
  911. { rest is not applicable }
  912. exit;
  913. end;
  914. else
  915. begin
  916. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  917. if l>high(asizeint)-sym.fieldoffset then
  918. begin
  919. Message(sym_e_segment_too_large);
  920. _datasize:=high(asizeint);
  921. end
  922. else
  923. _datasize:=sym.fieldoffset+l;
  924. { Calc alignment needed for this record }
  925. alignrecord(sym.fieldoffset,varalign);
  926. end;
  927. end;
  928. end;
  929. function field_alignment_compare(item1, item2: pointer): integer;
  930. var
  931. field1: tfieldvarsym absolute item1;
  932. field2: tfieldvarsym absolute item2;
  933. begin
  934. { we don't care about static fields, those become global variables }
  935. if (sp_static in field1.symoptions) or
  936. (sp_static in field2.symoptions) then
  937. exit(0);
  938. { sort from large to small alignment, and in case of the same alignment
  939. in declaration order (items declared close together are possibly
  940. also related and hence possibly used together -> putting them next
  941. to each other can improve cache behaviour) }
  942. result:=field2.vardef.alignment-field1.vardef.alignment;
  943. if result=0 then
  944. result:=field1.symid-field2.symid;
  945. end;
  946. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  947. var
  948. fieldvs, insertfieldvs: tfieldvarsym;
  949. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  950. i, j, bestfieldindex: longint;
  951. globalfieldalignment,
  952. prevglobalfieldalignment,
  953. newfieldalignment: shortint;
  954. changed: boolean;
  955. begin
  956. if maybereorder and
  957. (cs_opt_reorder_fields in current_settings.optimizerswitches) then
  958. begin
  959. { sort the non-class fields to minimise losses due to alignment }
  960. list.sort(@field_alignment_compare);
  961. { now fill up gaps caused by alignment skips with smaller fields
  962. where possible }
  963. repeat
  964. i:=0;
  965. base:=_datasize;
  966. globalfieldalignment:=fieldalignment;
  967. changed:=false;
  968. while i<list.count do
  969. begin
  970. fieldvs:=tfieldvarsym(list[i]);
  971. if sp_static in fieldvs.symoptions then
  972. begin
  973. inc(i);
  974. continue;
  975. end;
  976. prevglobalfieldalignment:=globalfieldalignment;
  977. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  978. newfieldalignment:=globalfieldalignment;
  979. { size of the gap between the end of the previous field and
  980. the start of the current one }
  981. space:=fieldoffset-base;
  982. bestspaceleft:=space;
  983. while space>0 do
  984. begin
  985. bestfieldindex:=-1;
  986. bestinsertfieldoffset:=-1;
  987. for j:=i+1 to list.count-1 do
  988. begin
  989. insertfieldvs:=tfieldvarsym(list[j]);
  990. if sp_static in insertfieldvs.symoptions then
  991. continue;
  992. insertfieldsize:=insertfieldvs.getsize;
  993. { can the new field fit possibly in the gap? }
  994. if insertfieldsize<=space then
  995. begin
  996. { restore globalfieldalignment to situation before
  997. the original field was inserted }
  998. globalfieldalignment:=prevglobalfieldalignment;
  999. { at what offset would it be inserted? (this new
  1000. field has its own alignment requirements, which
  1001. may make it impossible to fit after all) }
  1002. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1003. globalfieldalignment:=prevglobalfieldalignment;
  1004. { taking into account the alignment, does it still
  1005. fit and if so, does it fit better than the
  1006. previously found best fit? }
  1007. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1008. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1009. begin
  1010. { new best fit }
  1011. bestfieldindex:=j;
  1012. bestinsertfieldoffset:=insertfieldoffset;
  1013. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1014. if bestspaceleft=0 then
  1015. break;
  1016. end;
  1017. end;
  1018. end;
  1019. { if we didn't find any field to fit, stop trying for this
  1020. gap }
  1021. if bestfieldindex=-1 then
  1022. break;
  1023. changed:=true;
  1024. { we found a field to insert -> adjust the new base
  1025. address }
  1026. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1027. { update globalfieldalignment for this newly inserted
  1028. field }
  1029. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1030. { move the new field before the current one }
  1031. list.move(bestfieldindex,i);
  1032. { and skip the new field (which is now at position i) }
  1033. inc(i);
  1034. { there may be more space left -> continue }
  1035. space:=bestspaceleft;
  1036. end;
  1037. if base>fieldoffset then
  1038. internalerror(2012071302);
  1039. { check the next field }
  1040. base:=fieldoffset+fieldvs.getsize;
  1041. { since the original field had the same or greater alignment
  1042. than anything we inserted before it, the global field
  1043. alignment is still the same now as it was originally after
  1044. inserting that field }
  1045. globalfieldalignment:=newfieldalignment;
  1046. inc(i);
  1047. end;
  1048. { there may be small gaps left *before* inserted fields }
  1049. until not changed;
  1050. end;
  1051. { finally, set the actual field offsets }
  1052. for i:=0 to list.count-1 do
  1053. begin
  1054. fieldvs:=tfieldvarsym(list[i]);
  1055. { static data fields are already inserted in the globalsymtable }
  1056. if not(sp_static in fieldvs.symoptions) then
  1057. begin
  1058. { read_record_fields already set the visibility of the fields,
  1059. because a single list can contain symbols with different
  1060. visibility }
  1061. addfield(fieldvs,fieldvs.visibility);
  1062. end;
  1063. end;
  1064. end;
  1065. procedure tabstractrecordsymtable.addalignmentpadding;
  1066. var
  1067. padded_datasize: asizeint;
  1068. begin
  1069. { make the record size aligned correctly so it can be
  1070. used as elements in an array. For C records we
  1071. use the fieldalignment, because that is updated with the
  1072. used alignment. }
  1073. if (padalignment = 1) then
  1074. case usefieldalignment of
  1075. C_alignment:
  1076. padalignment:=fieldalignment;
  1077. { bitpacked }
  1078. bit_alignment:
  1079. padalignment:=1;
  1080. { mac68k: always round to multiple of 2 }
  1081. mac68k_alignment:
  1082. padalignment:=2;
  1083. { default/no packrecords specified }
  1084. 0:
  1085. padalignment:=recordalignment
  1086. { specific packrecords setting -> use as upper limit }
  1087. else
  1088. padalignment:=min(recordalignment,usefieldalignment);
  1089. end;
  1090. padded_datasize:=align(_datasize,padalignment);
  1091. _paddingsize:=padded_datasize-_datasize;
  1092. _datasize:=padded_datasize;
  1093. end;
  1094. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1095. begin
  1096. { Enums must also be available outside the record scope,
  1097. insert in the owner of this symtable }
  1098. if def.typ=enumdef then
  1099. defowner.owner.insertdef(def)
  1100. else
  1101. inherited insertdef(def);
  1102. end;
  1103. function tabstractrecordsymtable.is_packed: boolean;
  1104. begin
  1105. result:=usefieldalignment=bit_alignment;
  1106. end;
  1107. function tabstractrecordsymtable.has_single_field(out sym: tfieldvarsym): boolean;
  1108. var
  1109. i: longint;
  1110. begin
  1111. result:=false;
  1112. { If a record contains a union, it does not contain a "single
  1113. non-composite field" in the context of certain ABIs requiring
  1114. special treatment for such records }
  1115. if (defowner.typ=recorddef) and
  1116. trecorddef(defowner).isunion then
  1117. exit;
  1118. { a record/object can contain other things than fields }
  1119. for i:=0 to SymList.Count-1 do
  1120. begin
  1121. if tsym(symlist[i]).typ=fieldvarsym then
  1122. begin
  1123. if result then
  1124. begin
  1125. result:=false;
  1126. exit;
  1127. end;
  1128. result:=true;
  1129. sym:=tfieldvarsym(symlist[i])
  1130. end;
  1131. end;
  1132. end;
  1133. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1134. begin
  1135. result:=defowner.owner;
  1136. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1137. result:=result.defowner.owner;
  1138. end;
  1139. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1140. begin
  1141. _datasize:=val;
  1142. if (usefieldalignment=bit_alignment) then
  1143. { can overflow in non bitpacked records }
  1144. databitsize:=val*8;
  1145. end;
  1146. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1147. var
  1148. l : asizeint;
  1149. varalignfield,
  1150. varalign : shortint;
  1151. vardef : tdef;
  1152. begin
  1153. { Calculate field offset }
  1154. l:=sym.getsize;
  1155. vardef:=sym.vardef;
  1156. varalign:=vardef.structalignment;
  1157. case usefieldalignment of
  1158. bit_alignment:
  1159. { has to be handled separately }
  1160. internalerror(2012071301);
  1161. C_alignment:
  1162. begin
  1163. { Calc the alignment size for C style records }
  1164. if (varalign>4) and
  1165. ((varalign mod 4)<>0) and
  1166. (vardef.typ=arraydef) then
  1167. Message1(sym_w_wrong_C_pack,vardef.typename);
  1168. if varalign=0 then
  1169. varalign:=l;
  1170. if (globalfieldalignment<current_settings.alignment.maxCrecordalign) then
  1171. begin
  1172. if (varalign>16) and (globalfieldalignment<32) then
  1173. globalfieldalignment:=32
  1174. else if (varalign>12) and (globalfieldalignment<16) then
  1175. globalfieldalignment:=16
  1176. { 12 is needed for long double }
  1177. else if (varalign>8) and (globalfieldalignment<12) then
  1178. globalfieldalignment:=12
  1179. else if (varalign>4) and (globalfieldalignment<8) then
  1180. globalfieldalignment:=8
  1181. else if (varalign>2) and (globalfieldalignment<4) then
  1182. globalfieldalignment:=4
  1183. else if (varalign>1) and (globalfieldalignment<2) then
  1184. globalfieldalignment:=2;
  1185. end;
  1186. globalfieldalignment:=min(globalfieldalignment,current_settings.alignment.maxCrecordalign);
  1187. end;
  1188. mac68k_alignment:
  1189. begin
  1190. { mac68k alignment (C description):
  1191. * char is aligned to 1 byte
  1192. * everything else (except vector) is aligned to 2 bytes
  1193. * vector is aligned to 16 bytes
  1194. }
  1195. if l>1 then
  1196. globalfieldalignment:=2
  1197. else
  1198. globalfieldalignment:=1;
  1199. varalign:=2;
  1200. end;
  1201. end;
  1202. if varalign=0 then
  1203. varalign:=size_2_align(l);
  1204. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,globalfieldalignment);
  1205. result:=align(base,varalignfield);
  1206. end;
  1207. function tabstractrecordsymtable.iscurrentunit: boolean;
  1208. begin
  1209. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1210. end;
  1211. {****************************************************************************
  1212. TRecordSymtable
  1213. ****************************************************************************}
  1214. constructor trecordsymtable.create(const n:string;usealign:shortint);
  1215. begin
  1216. inherited create(n,usealign);
  1217. symtabletype:=recordsymtable;
  1218. end;
  1219. { this procedure is reserved for inserting case variant into
  1220. a record symtable }
  1221. { the offset is the location of the start of the variant
  1222. and datasize and dataalignment corresponds to
  1223. the complete size (see code in pdecl unit) PM }
  1224. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1225. var
  1226. sym : tsym;
  1227. def : tdef;
  1228. i : integer;
  1229. varalignrecord,varalign,
  1230. storesize,storealign : aint;
  1231. bitsize: tcgint;
  1232. begin
  1233. storesize:=_datasize;
  1234. storealign:=fieldalignment;
  1235. _datasize:=offset;
  1236. if (usefieldalignment=bit_alignment) then
  1237. databitsize:=offset*8;
  1238. { We move the ownership of the defs and symbols to the new recordsymtable.
  1239. The old unionsymtable keeps the references, but doesn't own the
  1240. objects anymore }
  1241. unionst.DefList.OwnsObjects:=false;
  1242. unionst.SymList.OwnsObjects:=false;
  1243. { copy symbols }
  1244. for i:=0 to unionst.SymList.Count-1 do
  1245. begin
  1246. sym:=TSym(unionst.SymList[i]);
  1247. if sym.typ<>fieldvarsym then
  1248. internalerror(200601272);
  1249. if tfieldvarsym(sym).fieldoffset=0 then
  1250. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1251. { add to this record symtable, checking for duplicate names }
  1252. // unionst.SymList.List.List^[i].Data:=nil;
  1253. insert(sym);
  1254. varalign:=tfieldvarsym(sym).vardef.alignment;
  1255. if varalign=0 then
  1256. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1257. { retrieve size }
  1258. if (usefieldalignment=bit_alignment) then
  1259. begin
  1260. { bit packed records are limited to high(aint) bits }
  1261. { instead of bytes to avoid double precision }
  1262. { arithmetic in offset calculations }
  1263. if is_ordinal(tfieldvarsym(sym).vardef) then
  1264. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1265. else
  1266. begin
  1267. bitsize:=tfieldvarsym(sym).getsize;
  1268. if (bitsize>high(asizeint) div 8) then
  1269. Message(sym_e_segment_too_large);
  1270. bitsize:=bitsize*8;
  1271. end;
  1272. if bitsize>high(asizeint)-databitsize then
  1273. begin
  1274. Message(sym_e_segment_too_large);
  1275. _datasize:=high(asizeint);
  1276. databitsize:=high(asizeint);
  1277. end
  1278. else
  1279. begin
  1280. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1281. _datasize:=(databitsize+7) div 8;
  1282. end;
  1283. tfieldvarsym(sym).fieldoffset:=databitsize;
  1284. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1285. end
  1286. else
  1287. begin
  1288. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1289. begin
  1290. Message(sym_e_segment_too_large);
  1291. _datasize:=high(asizeint);
  1292. end
  1293. else
  1294. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1295. { update address }
  1296. tfieldvarsym(sym).fieldoffset:=_datasize;
  1297. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1298. end;
  1299. { update alignment of this record }
  1300. if (usefieldalignment<>C_alignment) and
  1301. (usefieldalignment<>mac68k_alignment) then
  1302. recordalignment:=max(recordalignment,varalignrecord);
  1303. end;
  1304. { update alignment for C records }
  1305. if (usefieldalignment=C_alignment) and
  1306. (usefieldalignment<>mac68k_alignment) then
  1307. recordalignment:=max(recordalignment,unionst.recordalignment);
  1308. { Register defs in the new record symtable }
  1309. for i:=0 to unionst.DefList.Count-1 do
  1310. begin
  1311. def:=TDef(unionst.DefList[i]);
  1312. def.ChangeOwner(self);
  1313. end;
  1314. _datasize:=storesize;
  1315. fieldalignment:=storealign;
  1316. { If a record contains a union, it does not contain a "single
  1317. non-composite field" in the context of certain ABIs requiring
  1318. special treatment for such records }
  1319. if defowner.typ=recorddef then
  1320. trecorddef(defowner).isunion:=true;
  1321. end;
  1322. {****************************************************************************
  1323. TObjectSymtable
  1324. ****************************************************************************}
  1325. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  1326. begin
  1327. inherited create(n,usealign);
  1328. symtabletype:=ObjectSymtable;
  1329. defowner:=adefowner;
  1330. end;
  1331. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1332. var
  1333. hsym: tsym;
  1334. warn: boolean;
  1335. begin
  1336. result:=false;
  1337. if not assigned(defowner) then
  1338. internalerror(200602061);
  1339. { procsym and propertysym have special code
  1340. to override values in inherited classes. For other
  1341. symbols check for duplicates }
  1342. if not(sym.typ in [procsym,propertysym]) then
  1343. begin
  1344. { but private ids can be reused }
  1345. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1346. if assigned(hsym) and
  1347. (
  1348. (
  1349. not(m_delphi in current_settings.modeswitches) and
  1350. is_visible_for_object(hsym,tobjectdef(defowner))
  1351. ) or
  1352. (
  1353. { In Delphi, you can repeat members of a parent class. You can't }
  1354. { do this for objects however, and you (obviouly) can't }
  1355. { declare two fields with the same name in a single class }
  1356. (m_delphi in current_settings.modeswitches) and
  1357. (
  1358. is_object(tdef(defowner)) or
  1359. (hsym.owner = self)
  1360. )
  1361. )
  1362. ) then
  1363. begin
  1364. { only watn when a parameter/local variable in a method
  1365. conflicts with a category method, because this can easily
  1366. happen due to all possible categories being imported via
  1367. CocoaAll }
  1368. warn:=
  1369. (is_objccategory(tdef(hsym.owner.defowner)) or
  1370. is_classhelper(tdef(hsym.owner.defowner))) and
  1371. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1372. DuplicateSym(hashedid,sym,hsym,warn);
  1373. result:=true;
  1374. end;
  1375. end
  1376. else
  1377. result:=inherited checkduplicate(hashedid,sym);
  1378. end;
  1379. {****************************************************************************
  1380. TAbstractLocalSymtable
  1381. ****************************************************************************}
  1382. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1383. var
  1384. oldtyp : byte;
  1385. begin
  1386. oldtyp:=ppufile.entrytyp;
  1387. ppufile.entrytyp:=subentryid;
  1388. inherited ppuwrite(ppufile);
  1389. ppufile.entrytyp:=oldtyp;
  1390. end;
  1391. function tabstractlocalsymtable.count_locals:longint;
  1392. var
  1393. i : longint;
  1394. sym : tsym;
  1395. begin
  1396. result:=0;
  1397. for i:=0 to SymList.Count-1 do
  1398. begin
  1399. sym:=tsym(SymList[i]);
  1400. { Count only varsyms, but ignore the funcretsym }
  1401. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1402. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1403. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1404. (tstoredsym(sym).refs>0)) then
  1405. inc(result);
  1406. end;
  1407. end;
  1408. {****************************************************************************
  1409. TLocalSymtable
  1410. ****************************************************************************}
  1411. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1412. begin
  1413. inherited create('');
  1414. defowner:=adefowner;
  1415. symtabletype:=localsymtable;
  1416. symtablelevel:=level;
  1417. end;
  1418. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1419. var
  1420. hsym : tsym;
  1421. begin
  1422. if not assigned(defowner) or
  1423. (defowner.typ<>procdef) then
  1424. internalerror(200602042);
  1425. result:=false;
  1426. hsym:=tsym(FindWithHash(hashedid));
  1427. if assigned(hsym) then
  1428. begin
  1429. { a local and the function can have the same
  1430. name in TP and Delphi, but RESULT not }
  1431. if (m_duplicate_names in current_settings.modeswitches) and
  1432. (hsym.typ in [absolutevarsym,localvarsym]) and
  1433. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1434. not((m_result in current_settings.modeswitches) and
  1435. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1436. HideSym(hsym)
  1437. else
  1438. DuplicateSym(hashedid,sym,hsym,false);
  1439. result:=true;
  1440. exit;
  1441. end;
  1442. { check also parasymtable, this needs to be done here because
  1443. of the special situation with the funcret sym that needs to be
  1444. hidden for tp and delphi modes }
  1445. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1446. if assigned(hsym) then
  1447. begin
  1448. { a local and the function can have the same
  1449. name in TP and Delphi, but RESULT not }
  1450. if (m_duplicate_names in current_settings.modeswitches) and
  1451. (sym.typ in [absolutevarsym,localvarsym]) and
  1452. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1453. not((m_result in current_settings.modeswitches) and
  1454. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1455. Hidesym(sym)
  1456. else
  1457. DuplicateSym(hashedid,sym,hsym,false);
  1458. result:=true;
  1459. exit;
  1460. end;
  1461. { check ObjectSymtable, skip this for funcret sym because
  1462. that will always be positive because it has the same name
  1463. as the procsym }
  1464. if not is_funcret_sym(sym) and
  1465. (defowner.typ=procdef) and
  1466. assigned(tprocdef(defowner).struct) and
  1467. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1468. (
  1469. not(m_delphi in current_settings.modeswitches) or
  1470. is_object(tprocdef(defowner).struct)
  1471. ) then
  1472. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1473. end;
  1474. {****************************************************************************
  1475. TParaSymtable
  1476. ****************************************************************************}
  1477. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1478. begin
  1479. inherited create('');
  1480. readonly:=false;
  1481. defowner:=adefowner;
  1482. symtabletype:=parasymtable;
  1483. symtablelevel:=level;
  1484. end;
  1485. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1486. begin
  1487. result:=inherited checkduplicate(hashedid,sym);
  1488. if result then
  1489. exit;
  1490. if not(m_duplicate_names in current_settings.modeswitches) and
  1491. assigned(defowner) and (defowner.typ=procdef) and
  1492. assigned(tprocdef(defowner).struct) and
  1493. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1494. (
  1495. not(m_delphi in current_settings.modeswitches) or
  1496. is_object(tprocdef(defowner).struct)
  1497. ) then
  1498. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1499. end;
  1500. procedure tparasymtable.insertdef(def: TDefEntry);
  1501. begin
  1502. if readonly then
  1503. defowner.owner.insertdef(def)
  1504. else
  1505. inherited insertdef(def);
  1506. end;
  1507. {****************************************************************************
  1508. TAbstractUniTSymtable
  1509. ****************************************************************************}
  1510. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1511. begin
  1512. inherited create(n);
  1513. moduleid:=id;
  1514. end;
  1515. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1516. var
  1517. hsym : tsym;
  1518. begin
  1519. result:=false;
  1520. hsym:=tsym(FindWithHash(hashedid));
  1521. if assigned(hsym) then
  1522. begin
  1523. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  1524. exit;
  1525. if hsym.typ=symconst.namespacesym then
  1526. begin
  1527. case sym.typ of
  1528. symconst.namespacesym:;
  1529. symconst.unitsym:
  1530. begin
  1531. HideSym(sym); { if we add a unit and there is a namespace with the same name then hide the unit name and not the namespace }
  1532. tnamespacesym(hsym).unitsym:=tsym(sym);
  1533. end
  1534. else
  1535. HideSym(hsym);
  1536. end;
  1537. end
  1538. else
  1539. { In delphi (contrary to TP) you can have a symbol with the same name as the
  1540. unit, the unit can then not be accessed anymore using
  1541. <unit>.<id>, so we can hide the symbol.
  1542. Do the same if we add a namespace and there is a unit with the same name }
  1543. if (hsym.typ=symconst.unitsym) and
  1544. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  1545. begin
  1546. HideSym(hsym);
  1547. if sym.typ=symconst.namespacesym then
  1548. tnamespacesym(sym).unitsym:=tsym(hsym);
  1549. end
  1550. else
  1551. DuplicateSym(hashedid,sym,hsym,false);
  1552. result:=true;
  1553. exit;
  1554. end;
  1555. end;
  1556. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  1557. begin
  1558. result:=find(n);
  1559. if assigned(result)and(result.typ<>namespacesym)then
  1560. result:=nil;
  1561. end;
  1562. function tabstractuniTSymtable.iscurrentunit:boolean;
  1563. begin
  1564. result:=assigned(current_module) and
  1565. (
  1566. (current_module.globalsymtable=self) or
  1567. (current_module.localsymtable=self)
  1568. );
  1569. end;
  1570. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  1571. var
  1572. p:integer;
  1573. n,ns:string;
  1574. oldsym:TSymEntry;
  1575. begin
  1576. insert(sym);
  1577. n:=sym.realname;
  1578. p:=pos('.',n);
  1579. ns:='';
  1580. while p>0 do
  1581. begin
  1582. if ns='' then
  1583. ns:=copy(n,1,p-1)
  1584. else
  1585. ns:=ns+'.'+copy(n,1,p-1);
  1586. system.delete(n,1,p);
  1587. oldsym:=findnamespace(upper(ns));
  1588. if not assigned(oldsym) then
  1589. insert(cnamespacesym.create(ns));
  1590. p:=pos('.',n);
  1591. end;
  1592. end;
  1593. {****************************************************************************
  1594. TStaticSymtable
  1595. ****************************************************************************}
  1596. constructor tstaticsymtable.create(const n : string;id:word);
  1597. begin
  1598. inherited create(n,id);
  1599. symtabletype:=staticsymtable;
  1600. symtablelevel:=main_program_level;
  1601. currentvisibility:=vis_private;
  1602. end;
  1603. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1604. begin
  1605. inherited ppuload(ppufile);
  1606. { now we can deref the syms and defs }
  1607. deref;
  1608. end;
  1609. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1610. begin
  1611. inherited ppuwrite(ppufile);
  1612. end;
  1613. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1614. begin
  1615. result:=inherited checkduplicate(hashedid,sym);
  1616. if not result and
  1617. (current_module.localsymtable=self) and
  1618. assigned(current_module.globalsymtable) then
  1619. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1620. end;
  1621. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  1622. begin
  1623. result:=inherited findnamespace(n);
  1624. if not assigned(result) and
  1625. (current_module.localsymtable=self) and
  1626. assigned(current_module.globalsymtable) then
  1627. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  1628. end;
  1629. {****************************************************************************
  1630. TGlobalSymtable
  1631. ****************************************************************************}
  1632. constructor tglobalsymtable.create(const n : string;id:word);
  1633. begin
  1634. inherited create(n,id);
  1635. symtabletype:=globalsymtable;
  1636. symtablelevel:=main_program_level;
  1637. end;
  1638. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1639. begin
  1640. inherited ppuload(ppufile);
  1641. { now we can deref the syms and defs }
  1642. deref;
  1643. end;
  1644. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1645. begin
  1646. { write the symtable entries }
  1647. inherited ppuwrite(ppufile);
  1648. end;
  1649. {*****************************************************************************
  1650. tspecializesymtable
  1651. *****************************************************************************}
  1652. constructor tspecializesymtable.create(const n : string;id:word);
  1653. begin
  1654. inherited create(n,id);
  1655. { the specialize symtable does not own the syms and defs as they are all
  1656. moved to a different symtable before the symtable is destroyed; this
  1657. avoids calls to "extract" }
  1658. symlist.ownsobjects:=false;
  1659. deflist.ownsobjects:=false;
  1660. end;
  1661. function tspecializesymtable.iscurrentunit: boolean;
  1662. begin
  1663. Result:=true;
  1664. end;
  1665. {****************************************************************************
  1666. TWITHSYMTABLE
  1667. ****************************************************************************}
  1668. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1669. begin
  1670. inherited create('');
  1671. symtabletype:=withsymtable;
  1672. withrefnode:=refnode;
  1673. { Replace SymList with the passed symlist }
  1674. SymList.free;
  1675. SymList:=ASymList;
  1676. defowner:=aowner;
  1677. end;
  1678. destructor twithsymtable.destroy;
  1679. begin
  1680. withrefnode.free;
  1681. { Disable SymList because we don't Own it }
  1682. SymList:=nil;
  1683. inherited destroy;
  1684. end;
  1685. procedure twithsymtable.clear;
  1686. begin
  1687. { remove no entry from a withsymtable as it is only a pointer to the
  1688. recorddef or objectdef symtable }
  1689. end;
  1690. procedure twithsymtable.insertdef(def:TDefEntry);
  1691. begin
  1692. { Definitions can't be registered in the withsymtable
  1693. because the withsymtable is removed after the with block.
  1694. We can't easily solve it here because the next symtable in the
  1695. stack is not known. }
  1696. internalerror(200602046);
  1697. end;
  1698. {****************************************************************************
  1699. TSTT_ExceptionSymtable
  1700. ****************************************************************************}
  1701. constructor tstt_excepTSymtable.create;
  1702. begin
  1703. inherited create('');
  1704. symtabletype:=stt_excepTSymtable;
  1705. end;
  1706. {****************************************************************************
  1707. TMacroSymtable
  1708. ****************************************************************************}
  1709. constructor tmacrosymtable.create(exported: boolean);
  1710. begin
  1711. inherited create('');
  1712. if exported then
  1713. symtabletype:=exportedmacrosymtable
  1714. else
  1715. symtabletype:=localmacrosymtable;
  1716. symtablelevel:=main_program_level;
  1717. end;
  1718. {****************************************************************************
  1719. TEnumSymtable
  1720. ****************************************************************************}
  1721. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1722. var
  1723. value: longint;
  1724. def: tenumdef;
  1725. begin
  1726. // defowner = nil only when we are loading from ppu
  1727. if defowner<>nil then
  1728. begin
  1729. { First entry? Then we need to set the minval }
  1730. value:=tenumsym(sym).value;
  1731. def:=tenumdef(defowner);
  1732. if SymList.count=0 then
  1733. begin
  1734. if value>0 then
  1735. def.has_jumps:=true;
  1736. def.setmin(value);
  1737. def.setmax(value);
  1738. end
  1739. else
  1740. begin
  1741. { check for jumps }
  1742. if value>def.max+1 then
  1743. def.has_jumps:=true;
  1744. { update low and high }
  1745. if def.min>value then
  1746. def.setmin(value);
  1747. if def.max<value then
  1748. def.setmax(value);
  1749. end;
  1750. end;
  1751. inherited insert(sym, checkdup);
  1752. end;
  1753. constructor tenumsymtable.create(adefowner: tdef);
  1754. begin
  1755. inherited Create('');
  1756. symtabletype:=enumsymtable;
  1757. defowner:=adefowner;
  1758. end;
  1759. {****************************************************************************
  1760. TArraySymtable
  1761. ****************************************************************************}
  1762. procedure tarraysymtable.insertdef(def: TDefEntry);
  1763. begin
  1764. { Enums must also be available outside the record scope,
  1765. insert in the owner of this symtable }
  1766. if def.typ=enumdef then
  1767. defowner.owner.insertdef(def)
  1768. else
  1769. inherited insertdef(def);
  1770. end;
  1771. constructor tarraysymtable.create(adefowner: tdef);
  1772. begin
  1773. inherited Create('');
  1774. symtabletype:=arraysymtable;
  1775. defowner:=adefowner;
  1776. end;
  1777. {*****************************************************************************
  1778. Helper Routines
  1779. *****************************************************************************}
  1780. function FullTypeName(def,otherdef:tdef):string;
  1781. var
  1782. s1,s2 : string;
  1783. begin
  1784. if def.typ in [objectdef,recorddef] then
  1785. s1:=tabstractrecorddef(def).RttiName
  1786. else
  1787. s1:=def.typename;
  1788. { When the names are the same try to include the unit name }
  1789. if assigned(otherdef) and
  1790. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1791. begin
  1792. s2:=otherdef.typename;
  1793. if upper(s1)=upper(s2) then
  1794. s1:=def.owner.realname^+'.'+s1;
  1795. end;
  1796. FullTypeName:=s1;
  1797. end;
  1798. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1799. begin
  1800. result:='';
  1801. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1802. begin
  1803. if (result='') then
  1804. if symtable.name<>nil then
  1805. result:=symtable.name^
  1806. else
  1807. else
  1808. if symtable.name<>nil then
  1809. result:=symtable.name^+delimiter+result
  1810. else
  1811. result:=delimiter+result;
  1812. symtable:=symtable.defowner.owner;
  1813. end;
  1814. end;
  1815. function generate_objectpascal_helper_key(def:tdef):string;
  1816. begin
  1817. if not assigned(def) then
  1818. internalerror(2013020501);
  1819. if def.typ in [recorddef,objectdef] then
  1820. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  1821. else
  1822. result:=make_mangledname('',def.owner,def.typesym.name);
  1823. end;
  1824. procedure incompatibletypes(def1,def2:tdef);
  1825. begin
  1826. { When there is an errordef there is already an error message show }
  1827. if (def2.typ=errordef) or
  1828. (def1.typ=errordef) then
  1829. exit;
  1830. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1831. end;
  1832. procedure hidesym(sym:TSymEntry);
  1833. begin
  1834. sym.realname:='$hidden'+sym.realname;
  1835. tsym(sym).visibility:=vis_hidden;
  1836. end;
  1837. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  1838. var
  1839. st : TSymtable;
  1840. filename : TIDString;
  1841. begin
  1842. if not warn then
  1843. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  1844. else
  1845. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  1846. { Write hint where the original symbol was found }
  1847. st:=finduniTSymtable(origsym.owner);
  1848. with tsym(origsym).fileinfo do
  1849. begin
  1850. if assigned(st) and
  1851. (st.symtabletype=globalsymtable) and
  1852. st.iscurrentunit then
  1853. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1854. else if assigned(st.name) then
  1855. begin
  1856. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  1857. if filename<>'' then
  1858. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  1859. else
  1860. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1861. end;
  1862. end;
  1863. { Rename duplicate sym to an unreachable name, but it can be
  1864. inserted in the symtable without errors }
  1865. inc(dupnr);
  1866. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1867. if assigned(dupsym) then
  1868. include(tsym(dupsym).symoptions,sp_implicitrename);
  1869. end;
  1870. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  1871. begin
  1872. result:=false;
  1873. if not assigned(sym) or not (sym is tstoredsym) then
  1874. Internalerror(2011081101);
  1875. { For generics a dummy symbol without the parameter count is created
  1876. if such a symbol not yet exists so that different parts of the
  1877. parser can find that symbol. If that symbol is still a
  1878. undefineddef we replace the generic dummy symbol's
  1879. name with a "dup" name and use the new symbol as the generic dummy
  1880. symbol }
  1881. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  1882. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  1883. (m_delphi in current_settings.modeswitches) then
  1884. begin
  1885. inc(dupnr);
  1886. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  1887. include(tsym(sym).symoptions,sp_implicitrename);
  1888. { we need to find the new symbol now if checking for a dummy }
  1889. include(symoptions,sp_generic_dummy);
  1890. result:=true;
  1891. end;
  1892. end;
  1893. function get_jumpbuf_size : longint;
  1894. var
  1895. srsym : ttypesym;
  1896. begin
  1897. if jmp_buf_size=-1 then
  1898. begin
  1899. srsym:=search_system_type('JMP_BUF');
  1900. jmp_buf_size:=srsym.typedef.size;
  1901. jmp_buf_align:=srsym.typedef.alignment;
  1902. end;
  1903. result:=jmp_buf_size;
  1904. end;
  1905. {*****************************************************************************
  1906. Search
  1907. *****************************************************************************}
  1908. procedure addsymref(sym:tsym);
  1909. var
  1910. owner: tsymtable;
  1911. begin
  1912. { symbol uses count }
  1913. sym.IncRefCount;
  1914. { unit uses count }
  1915. owner:=sym.owner;
  1916. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  1917. owner:=tdef(owner.defowner).owner;
  1918. if assigned(current_module) and
  1919. (owner.symtabletype=globalsymtable) then
  1920. begin
  1921. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  1922. internalerror(200501152);
  1923. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  1924. end;
  1925. end;
  1926. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  1927. begin
  1928. result:=nesteddef=ownerdef;
  1929. if not result and
  1930. { types declared locally in a record method are not defined in the
  1931. record itself }
  1932. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  1933. assigned(nesteddef.owner.defowner) then
  1934. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  1935. end;
  1936. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  1937. begin
  1938. result:=assigned(childsym) and (childsym.owner=symtable);
  1939. if not result and assigned(childsym) and
  1940. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1941. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  1942. end;
  1943. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  1944. begin
  1945. result:=false;
  1946. if not assigned(def1) or not assigned(def2) then
  1947. exit;
  1948. { for both defs walk to the topmost generic }
  1949. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  1950. def1:=tdef(def1.owner.defowner);
  1951. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  1952. def2:=tdef(def2.owner.defowner);
  1953. result:=def1=def2;
  1954. end;
  1955. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  1956. var
  1957. uname : string;
  1958. begin
  1959. { TODO : check regarding arrays and records declared as their type }
  1960. if not (def.typ in [recorddef,objectdef]) then
  1961. internalerror(2012051501);
  1962. uname:=upper(srsym.realname);
  1963. repeat
  1964. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  1965. begin
  1966. result:=def;
  1967. exit;
  1968. end;
  1969. def:=tdef(def.owner.defowner);
  1970. until not (def.typ in [recorddef,objectdef]);
  1971. result:=nil;
  1972. end;
  1973. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  1974. begin
  1975. { TODO : check regarding arrays and records declared as their type }
  1976. if not (nesteddef.typ in [recorddef,objectdef]) then
  1977. internalerror(2012051601);
  1978. repeat
  1979. if tstoreddef(nesteddef).genericdef=genericdef then
  1980. begin
  1981. resultdef:=nesteddef;
  1982. result:=true;
  1983. exit;
  1984. end;
  1985. nesteddef:=tdef(nesteddef.owner.defowner);
  1986. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  1987. resultdef:=nil;
  1988. result:=false;
  1989. end;
  1990. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  1991. symvisibility: visibility of the symbol
  1992. contextobjdef: via which def the symbol is accessed, e.g.:
  1993. fieldname:=1 -> contextobjdef = current_structdef
  1994. objfield.fieldname:=1 -> contextobjdef = def of objfield
  1995. }
  1996. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1997. var
  1998. symownerdef : tabstractrecorddef;
  1999. nonlocalst : tsymtable;
  2000. begin
  2001. result:=false;
  2002. { Get objdectdef owner of the symtable for the is_related checks }
  2003. if not assigned(symst) or
  2004. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2005. internalerror(200810285);
  2006. symownerdef:=tabstractrecorddef(symst.defowner);
  2007. { specializations might belong to a localsymtable or parasymtable }
  2008. nonlocalst:=symownerdef.owner;
  2009. if tstoreddef(symst.defowner).is_specialization then
  2010. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2011. nonlocalst:=nonlocalst.defowner.owner;
  2012. case symvisibility of
  2013. vis_private :
  2014. begin
  2015. { private symbols are allowed when we are in the same
  2016. module as they are defined }
  2017. result:=(
  2018. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2019. (nonlocalst.iscurrentunit)
  2020. ) or
  2021. ( // the case of specialize inside the generic declaration and nested types
  2022. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2023. (
  2024. assigned(current_structdef) and
  2025. (
  2026. (current_structdef=symownerdef) or
  2027. (current_structdef.owner.iscurrentunit)
  2028. )
  2029. ) or
  2030. (
  2031. not assigned(current_structdef) and
  2032. (symownerdef.owner.iscurrentunit)
  2033. )
  2034. );
  2035. end;
  2036. vis_strictprivate :
  2037. begin
  2038. result:=assigned(current_structdef) and
  2039. is_owned_by(current_structdef,symownerdef);
  2040. end;
  2041. vis_strictprotected :
  2042. begin
  2043. result:=(
  2044. { access from nested class }
  2045. assigned(current_structdef) and
  2046. is_owned_by(current_structdef,symownerdef)
  2047. ) or
  2048. (
  2049. { access from child class }
  2050. assigned(contextobjdef) and
  2051. assigned(current_structdef) and
  2052. def_is_related(contextobjdef,symownerdef) and
  2053. def_is_related(current_structdef,contextobjdef)
  2054. ) or
  2055. (
  2056. { helpers can access strict protected symbols }
  2057. is_objectpascal_helper(contextobjdef) and
  2058. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2059. ) or
  2060. (
  2061. { same as above, but from context of call node inside
  2062. helper method }
  2063. is_objectpascal_helper(current_structdef) and
  2064. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2065. );
  2066. end;
  2067. vis_protected :
  2068. begin
  2069. { protected symbols are visible in the module that defines them and
  2070. also visible to related objects. The related object must be defined
  2071. in the current module }
  2072. result:=(
  2073. (
  2074. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2075. (nonlocalst.iscurrentunit)
  2076. ) or
  2077. (
  2078. assigned(contextobjdef) and
  2079. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  2080. (contextobjdef.owner.iscurrentunit) and
  2081. def_is_related(contextobjdef,symownerdef)
  2082. ) or
  2083. ( // the case of specialize inside the generic declaration and nested types
  2084. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2085. (
  2086. assigned(current_structdef) and
  2087. (
  2088. (current_structdef=symownerdef) or
  2089. (current_structdef.owner.iscurrentunit)
  2090. )
  2091. ) or
  2092. (
  2093. not assigned(current_structdef) and
  2094. (symownerdef.owner.iscurrentunit)
  2095. ) or
  2096. (
  2097. { helpers can access protected symbols }
  2098. is_objectpascal_helper(contextobjdef) and
  2099. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2100. )
  2101. )
  2102. );
  2103. end;
  2104. vis_public,
  2105. vis_published :
  2106. result:=true;
  2107. end;
  2108. end;
  2109. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2110. begin
  2111. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2112. end;
  2113. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2114. var
  2115. i : longint;
  2116. pd : tprocdef;
  2117. begin
  2118. if sym.typ=procsym then
  2119. begin
  2120. { A procsym is visible, when there is at least one of the procdefs visible }
  2121. result:=false;
  2122. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2123. begin
  2124. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2125. if (pd.owner=sym.owner) and
  2126. is_visible_for_object(pd,contextobjdef) then
  2127. begin
  2128. result:=true;
  2129. exit;
  2130. end;
  2131. end;
  2132. end
  2133. else
  2134. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2135. end;
  2136. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2137. begin
  2138. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2139. end;
  2140. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2141. begin
  2142. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2143. end;
  2144. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2145. var
  2146. hashedid: THashedIDString;
  2147. contextstructdef: tabstractrecorddef;
  2148. stackitem: psymtablestackitem;
  2149. begin
  2150. result:=false;
  2151. hashedid.id:=s;
  2152. stackitem:=symtablestack.stack;
  2153. while assigned(stackitem) do
  2154. begin
  2155. srsymtable:=stackitem^.symtable;
  2156. if (srsymtable.symtabletype=objectsymtable) then
  2157. begin
  2158. { TODO : implement the search for an option in classes as well }
  2159. if ssf_search_option in flags then
  2160. begin
  2161. result:=false;
  2162. exit;
  2163. end;
  2164. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2165. begin
  2166. result:=true;
  2167. exit;
  2168. end;
  2169. end
  2170. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2171. (srsymtable.defowner.typ=undefineddef)) then
  2172. begin
  2173. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2174. { First check if it is a unit/namespace symbol.
  2175. They are visible only if they are from the current unit or
  2176. unit of generic of currently processed specialization. }
  2177. if assigned(srsym) and
  2178. (
  2179. not(srsym.typ in [unitsym,namespacesym]) or
  2180. srsymtable.iscurrentunit or
  2181. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2182. ) and
  2183. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  2184. begin
  2185. { use the class from withsymtable only when it is
  2186. defined in this unit }
  2187. if (srsymtable.symtabletype=withsymtable) and
  2188. assigned(srsymtable.defowner) and
  2189. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2190. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2191. (srsymtable.defowner.owner.iscurrentunit) then
  2192. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2193. else
  2194. contextstructdef:=current_structdef;
  2195. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2196. is_visible_for_object(srsym,contextstructdef) then
  2197. begin
  2198. { we need to know if a procedure references symbols
  2199. in the static symtable, because then it can't be
  2200. inlined from outside this unit }
  2201. if assigned(current_procinfo) and
  2202. (srsym.owner.symtabletype=staticsymtable) then
  2203. include(current_procinfo.flags,pi_uses_static_symtable);
  2204. if not (ssf_no_addsymref in flags) then
  2205. addsymref(srsym);
  2206. result:=true;
  2207. exit;
  2208. end;
  2209. end;
  2210. end;
  2211. stackitem:=stackitem^.next;
  2212. end;
  2213. srsym:=nil;
  2214. srsymtable:=nil;
  2215. end;
  2216. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2217. srsymtable:TSymtable;option:tsymoption):boolean;
  2218. begin
  2219. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  2220. end;
  2221. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2222. var
  2223. hashedid : THashedIDString;
  2224. stackitem : psymtablestackitem;
  2225. classh : tobjectdef;
  2226. begin
  2227. result:=false;
  2228. hashedid.id:=s;
  2229. stackitem:=symtablestack.stack;
  2230. while assigned(stackitem) do
  2231. begin
  2232. {
  2233. It is not possible to have type symbols in:
  2234. parameters
  2235. Exception are classes, objects, records, generic definitions and specializations
  2236. that have the parameterized types inserted in the symtable.
  2237. }
  2238. srsymtable:=stackitem^.symtable;
  2239. if (srsymtable.symtabletype=ObjectSymtable) then
  2240. begin
  2241. classh:=tobjectdef(srsymtable.defowner);
  2242. while assigned(classh) do
  2243. begin
  2244. srsymtable:=classh.symtable;
  2245. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2246. if assigned(srsym) and
  2247. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2248. is_visible_for_object(srsym,current_structdef) then
  2249. begin
  2250. addsymref(srsym);
  2251. result:=true;
  2252. exit;
  2253. end;
  2254. classh:=classh.childof;
  2255. end;
  2256. end
  2257. else
  2258. begin
  2259. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2260. if assigned(srsym) and
  2261. (
  2262. not(srsym.typ in [unitsym,namespacesym]) or
  2263. srsymtable.iscurrentunit or
  2264. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2265. ) and
  2266. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2267. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2268. begin
  2269. { we need to know if a procedure references symbols
  2270. in the static symtable, because then it can't be
  2271. inlined from outside this unit }
  2272. if assigned(current_procinfo) and
  2273. (srsym.owner.symtabletype=staticsymtable) then
  2274. include(current_procinfo.flags,pi_uses_static_symtable);
  2275. addsymref(srsym);
  2276. result:=true;
  2277. exit;
  2278. end;
  2279. end;
  2280. stackitem:=stackitem^.next;
  2281. end;
  2282. result:=false;
  2283. srsym:=nil;
  2284. srsymtable:=nil;
  2285. end;
  2286. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2287. var
  2288. pmod : tmodule;
  2289. begin
  2290. pmod:=tmodule(pm);
  2291. result:=false;
  2292. if assigned(pmod.globalsymtable) then
  2293. begin
  2294. srsym:=tsym(pmod.globalsymtable.Find(s));
  2295. if assigned(srsym) then
  2296. begin
  2297. srsymtable:=pmod.globalsymtable;
  2298. addsymref(srsym);
  2299. result:=true;
  2300. exit;
  2301. end;
  2302. end;
  2303. { If the module is the current unit we also need
  2304. to search the local symtable }
  2305. if (pmod=current_module) and
  2306. assigned(pmod.localsymtable) then
  2307. begin
  2308. srsym:=tsym(pmod.localsymtable.Find(s));
  2309. if assigned(srsym) then
  2310. begin
  2311. srsymtable:=pmod.localsymtable;
  2312. addsymref(srsym);
  2313. result:=true;
  2314. exit;
  2315. end;
  2316. end;
  2317. srsym:=nil;
  2318. srsymtable:=nil;
  2319. end;
  2320. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2321. var
  2322. stackitem : psymtablestackitem;
  2323. begin
  2324. result:=false;
  2325. stackitem:=symtablestack.stack;
  2326. while assigned(stackitem) do
  2327. begin
  2328. srsymtable:=stackitem^.symtable;
  2329. if (srsymtable.symtabletype=globalsymtable) and
  2330. (srsymtable.name^=unitname) then
  2331. begin
  2332. srsym:=tsym(srsymtable.find(symname));
  2333. if not assigned(srsym) then
  2334. break;
  2335. result:=true;
  2336. exit;
  2337. end;
  2338. stackitem:=stackitem^.next;
  2339. end;
  2340. { If the module is the current unit we also need
  2341. to search the local symtable }
  2342. if assigned(current_module.localsymtable) and
  2343. (current_module.localsymtable.name^=unitname) then
  2344. begin
  2345. srsymtable:=current_module.localsymtable;
  2346. srsym:=tsym(srsymtable.find(symname));
  2347. if assigned(srsym) then
  2348. begin
  2349. result:=true;
  2350. exit;
  2351. end;
  2352. end;
  2353. end;
  2354. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2355. begin
  2356. result:=pd;
  2357. if pd.typ<>objectdef then
  2358. exit;
  2359. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2360. end;
  2361. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2362. var
  2363. hashedid : THashedIDString;
  2364. stackitem : psymtablestackitem;
  2365. srsymtable : tsymtable;
  2366. srsym : tsym;
  2367. formalname,
  2368. foundname : shortstring;
  2369. formalnameptr,
  2370. foundnameptr: pshortstring;
  2371. begin
  2372. { not a formal definition -> return it }
  2373. if not(oo_is_formal in pd.objectoptions) then
  2374. begin
  2375. result:=pd;
  2376. exit;
  2377. end;
  2378. hashedid.id:=pd.typesym.name;
  2379. stackitem:=symtablestack.stack;
  2380. while assigned(stackitem) do
  2381. begin
  2382. srsymtable:=stackitem^.symtable;
  2383. { ObjC classes can't appear in generics or as nested class
  2384. definitions. Java classes can. }
  2385. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2386. (is_java_class_or_interface(pd) and
  2387. (srsymtable.symtabletype=ObjectSymtable)) then
  2388. begin
  2389. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2390. if assigned(srsym) and
  2391. (srsym.typ=typesym) and
  2392. (ttypesym(srsym).typedef.typ=objectdef) and
  2393. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2394. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2395. begin
  2396. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2397. begin
  2398. { the external name for the formal and the real
  2399. definition must match }
  2400. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2401. assigned(pd.import_lib) then
  2402. begin
  2403. if assigned(pd.import_lib) then
  2404. formalname:=pd.import_lib^+'.'
  2405. else
  2406. formalname:='';
  2407. formalname:=formalname+pd.objextname^;
  2408. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2409. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2410. else
  2411. foundname:='';
  2412. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2413. formalnameptr:=@formalname;
  2414. foundnameptr:=@foundname;
  2415. end
  2416. else
  2417. begin
  2418. formalnameptr:=pd.objextname;
  2419. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2420. end;
  2421. if foundnameptr^<>formalnameptr^ then
  2422. begin
  2423. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2424. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2425. end;
  2426. end;
  2427. result:=tobjectdef(ttypesym(srsym).typedef);
  2428. if assigned(current_procinfo) and
  2429. (srsym.owner.symtabletype=staticsymtable) then
  2430. include(current_procinfo.flags,pi_uses_static_symtable);
  2431. addsymref(srsym);
  2432. exit;
  2433. end;
  2434. end;
  2435. stackitem:=stackitem^.next;
  2436. end;
  2437. { nothing found: optionally give an error and return the original
  2438. (empty) one }
  2439. if erroronfailure then
  2440. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2441. result:=pd;
  2442. end;
  2443. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2444. var
  2445. hashedid : THashedIDString;
  2446. orgclass : tobjectdef;
  2447. i : longint;
  2448. hlpsrsym : tsym;
  2449. hlpsrsymtable : tsymtable;
  2450. begin
  2451. orgclass:=classh;
  2452. { in case this is a formal class, first find the real definition }
  2453. if assigned(classh) then
  2454. begin
  2455. if (oo_is_formal in classh.objectoptions) then
  2456. classh:=find_real_class_definition(classh,true);
  2457. { The contextclassh is used for visibility. The classh must be equal to
  2458. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2459. parent or a class helper. }
  2460. if not (def_is_related(contextclassh,classh) or
  2461. (is_classhelper(contextclassh) and
  2462. assigned(tobjectdef(contextclassh).extendeddef) and
  2463. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  2464. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  2465. internalerror(200811161);
  2466. end;
  2467. result:=false;
  2468. hashedid.id:=s;
  2469. { an Objective-C protocol or Java interface can inherit from multiple
  2470. other protocols/interfaces -> use ImplementedInterfaces instead }
  2471. if is_objcprotocol(classh) or
  2472. is_javainterface(classh) then
  2473. begin
  2474. srsymtable:=classh.symtable;
  2475. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2476. if assigned(srsym) and
  2477. is_visible_for_object(srsym,contextclassh) then
  2478. begin
  2479. if not (ssf_no_addsymref in flags) then
  2480. addsymref(srsym);
  2481. result:=true;
  2482. exit;
  2483. end;
  2484. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2485. begin
  2486. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  2487. begin
  2488. result:=true;
  2489. exit;
  2490. end;
  2491. end;
  2492. end
  2493. else
  2494. if is_objectpascal_helper(classh) then
  2495. begin
  2496. { helpers have their own obscure search logic... }
  2497. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  2498. if result then
  2499. exit;
  2500. end
  2501. else
  2502. begin
  2503. hlpsrsym:=nil;
  2504. hlpsrsymtable:=nil;
  2505. while assigned(classh) do
  2506. begin
  2507. { search for a class helper method first if this is an Object
  2508. Pascal class and we haven't yet found a helper symbol }
  2509. if is_class(classh) and
  2510. (ssf_search_helper in flags) and
  2511. not assigned(hlpsrsym) then
  2512. begin
  2513. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2514. if result then
  2515. { if the procsym is overloaded we need to use the
  2516. "original" symbol; the helper symbol will be found when
  2517. searching for overloads }
  2518. if (srsym.typ<>procsym) or
  2519. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2520. exit
  2521. else
  2522. begin
  2523. { remember the found symbol if the class hierarchy
  2524. should not contain the a method with that name }
  2525. hlpsrsym:=srsym;
  2526. hlpsrsymtable:=srsymtable;
  2527. end;
  2528. end;
  2529. srsymtable:=classh.symtable;
  2530. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2531. if assigned(srsym) and
  2532. is_visible_for_object(srsym,contextclassh) then
  2533. begin
  2534. if not (ssf_no_addsymref in flags) then
  2535. addsymref(srsym);
  2536. result:=true;
  2537. exit;
  2538. end;
  2539. classh:=classh.childof;
  2540. end;
  2541. { did we find a helper symbol, but no symbol with the same name in
  2542. the extended object's hierarchy? }
  2543. if assigned(hlpsrsym) then
  2544. begin
  2545. srsym:=hlpsrsym;
  2546. srsymtable:=hlpsrsymtable;
  2547. result:=true;
  2548. exit;
  2549. end;
  2550. end;
  2551. if is_objcclass(orgclass) then
  2552. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2553. else
  2554. begin
  2555. srsym:=nil;
  2556. srsymtable:=nil;
  2557. end;
  2558. end;
  2559. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2560. var
  2561. hashedid : THashedIDString;
  2562. hlpsrsym : tsym;
  2563. hlpsrsymtable : tsymtable;
  2564. begin
  2565. result:=false;
  2566. hlpsrsym:=nil;
  2567. hlpsrsymtable:=nil;
  2568. hashedid.id:=s;
  2569. { search for a record helper method first }
  2570. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2571. if result then
  2572. { if the procsym is overloaded we need to use the
  2573. "original" symbol; the helper symbol will be found when
  2574. searching for overloads }
  2575. if (srsym.typ<>procsym) or
  2576. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2577. exit
  2578. else
  2579. begin
  2580. { remember the found symbol if we should not find a symbol with
  2581. the same name in the extended record }
  2582. hlpsrsym:=srsym;
  2583. hlpsrsymtable:=srsymtable;
  2584. end;
  2585. srsymtable:=recordh.symtable;
  2586. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2587. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2588. begin
  2589. addsymref(srsym);
  2590. result:=true;
  2591. exit;
  2592. end;
  2593. srsym:=hlpsrsym;
  2594. srsymtable:=hlpsrsymtable;
  2595. result:=assigned(srsym);
  2596. end;
  2597. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2598. var
  2599. def : tdef;
  2600. i : longint;
  2601. begin
  2602. { in case this is a formal class, first find the real definition }
  2603. if assigned(classh) and
  2604. (oo_is_formal in classh.objectoptions) then
  2605. classh:=find_real_class_definition(classh,true);
  2606. result:=false;
  2607. def:=nil;
  2608. while assigned(classh) do
  2609. begin
  2610. for i:=0 to classh.symtable.DefList.Count-1 do
  2611. begin
  2612. def:=tstoreddef(classh.symtable.DefList[i]);
  2613. { Find also all hidden private methods to
  2614. be compatible with delphi, see tw6203 (PFV) }
  2615. if (def.typ=procdef) and
  2616. (po_msgint in tprocdef(def).procoptions) and
  2617. (tprocdef(def).messageinf.i=msgid) then
  2618. begin
  2619. srdef:=def;
  2620. srsym:=tprocdef(def).procsym;
  2621. srsymtable:=classh.symtable;
  2622. addsymref(srsym);
  2623. result:=true;
  2624. exit;
  2625. end;
  2626. end;
  2627. classh:=classh.childof;
  2628. end;
  2629. srdef:=nil;
  2630. srsym:=nil;
  2631. srsymtable:=nil;
  2632. end;
  2633. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2634. var
  2635. def : tdef;
  2636. i : longint;
  2637. begin
  2638. { in case this is a formal class, first find the real definition }
  2639. if assigned(classh) and
  2640. (oo_is_formal in classh.objectoptions) then
  2641. classh:=find_real_class_definition(classh,true);
  2642. result:=false;
  2643. def:=nil;
  2644. while assigned(classh) do
  2645. begin
  2646. for i:=0 to classh.symtable.DefList.Count-1 do
  2647. begin
  2648. def:=tstoreddef(classh.symtable.DefList[i]);
  2649. { Find also all hidden private methods to
  2650. be compatible with delphi, see tw6203 (PFV) }
  2651. if (def.typ=procdef) and
  2652. (po_msgstr in tprocdef(def).procoptions) and
  2653. (tprocdef(def).messageinf.str^=s) then
  2654. begin
  2655. srsym:=tprocdef(def).procsym;
  2656. srsymtable:=classh.symtable;
  2657. addsymref(srsym);
  2658. result:=true;
  2659. exit;
  2660. end;
  2661. end;
  2662. classh:=classh.childof;
  2663. end;
  2664. srsym:=nil;
  2665. srsymtable:=nil;
  2666. end;
  2667. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2668. var
  2669. hashedid : THashedIDString;
  2670. parentclassh : tobjectdef;
  2671. begin
  2672. result:=false;
  2673. if not is_objectpascal_helper(classh) then
  2674. Internalerror(2011030101);
  2675. hashedid.id:=s;
  2676. { in a helper things are a bit more complex:
  2677. 1. search the symbol in the helper (if not "inherited")
  2678. 2. search the symbol in the extended type
  2679. 3. search the symbol in the parent helpers
  2680. 4. only classes: search the symbol in the parents of the extended type
  2681. }
  2682. if not (ssf_has_inherited in flags) then
  2683. begin
  2684. { search in the helper itself }
  2685. srsymtable:=classh.symtable;
  2686. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2687. if assigned(srsym) and
  2688. is_visible_for_object(srsym,contextclassh) then
  2689. begin
  2690. if not (ssf_no_addsymref in flags) then
  2691. addsymref(srsym);
  2692. result:=true;
  2693. exit;
  2694. end;
  2695. end;
  2696. { now search in the extended type itself }
  2697. { Note: the extendeddef might be Nil if we are currently parsing the
  2698. extended type itself and the identifier was not found }
  2699. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  2700. begin
  2701. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2702. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2703. if assigned(srsym) and
  2704. is_visible_for_object(srsym,contextclassh) then
  2705. begin
  2706. if not (ssf_no_addsymref in flags) then
  2707. addsymref(srsym);
  2708. result:=true;
  2709. exit;
  2710. end;
  2711. end;
  2712. { now search in the parent helpers }
  2713. parentclassh:=classh.childof;
  2714. while assigned(parentclassh) do
  2715. begin
  2716. srsymtable:=parentclassh.symtable;
  2717. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2718. if assigned(srsym) and
  2719. is_visible_for_object(srsym,contextclassh) then
  2720. begin
  2721. if not (ssf_no_addsymref in flags) then
  2722. addsymref(srsym);
  2723. result:=true;
  2724. exit;
  2725. end;
  2726. parentclassh:=parentclassh.childof;
  2727. end;
  2728. if is_class(classh.extendeddef) then
  2729. { now search in the parents of the extended class (with helpers!) }
  2730. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  2731. { addsymref is already called by searchsym_in_class }
  2732. end;
  2733. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2734. var
  2735. sym : Tprocsym;
  2736. hashedid : THashedIDString;
  2737. curreq,
  2738. besteq : tequaltype;
  2739. currpd,
  2740. bestpd : tprocdef;
  2741. stackitem : psymtablestackitem;
  2742. begin
  2743. hashedid.id:=overloaded_names[assignment_type];
  2744. besteq:=te_incompatible;
  2745. bestpd:=nil;
  2746. stackitem:=symtablestack.stack;
  2747. while assigned(stackitem) do
  2748. begin
  2749. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2750. if sym<>nil then
  2751. begin
  2752. if sym.typ<>procsym then
  2753. internalerror(200402031);
  2754. { if the source type is an alias then this is only the second choice,
  2755. if you mess with this code, check tw4093 }
  2756. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2757. if curreq>besteq then
  2758. begin
  2759. besteq:=curreq;
  2760. bestpd:=currpd;
  2761. if (besteq=te_exact) then
  2762. break;
  2763. end;
  2764. end;
  2765. stackitem:=stackitem^.next;
  2766. end;
  2767. result:=bestpd;
  2768. end;
  2769. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2770. begin
  2771. { search record/object symtable first for a suitable operator }
  2772. if from_def.typ in [recorddef,objectdef] then
  2773. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2774. if to_def.typ in [recorddef,objectdef] then
  2775. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2776. { if type conversion is explicit then search first for explicit
  2777. operator overload and if not found then use implicit operator }
  2778. if explicit then
  2779. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2780. else
  2781. result:=nil;
  2782. if result=nil then
  2783. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2784. { restore symtable stack }
  2785. if to_def.typ in [recorddef,objectdef] then
  2786. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2787. if from_def.typ in [recorddef,objectdef] then
  2788. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2789. end;
  2790. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2791. var
  2792. sym : Tprocsym;
  2793. hashedid : THashedIDString;
  2794. curreq,
  2795. besteq : tequaltype;
  2796. currpd,
  2797. bestpd : tprocdef;
  2798. stackitem : psymtablestackitem;
  2799. begin
  2800. hashedid.id:='enumerator';
  2801. besteq:=te_incompatible;
  2802. bestpd:=nil;
  2803. stackitem:=symtablestack.stack;
  2804. while assigned(stackitem) do
  2805. begin
  2806. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2807. if sym<>nil then
  2808. begin
  2809. if sym.typ<>procsym then
  2810. internalerror(200910241);
  2811. { if the source type is an alias then this is only the second choice,
  2812. if you mess with this code, check tw4093 }
  2813. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2814. if curreq>besteq then
  2815. begin
  2816. besteq:=curreq;
  2817. bestpd:=currpd;
  2818. if (besteq=te_exact) then
  2819. break;
  2820. end;
  2821. end;
  2822. stackitem:=stackitem^.next;
  2823. end;
  2824. result:=bestpd;
  2825. end;
  2826. function search_system_type(const s: TIDString): ttypesym;
  2827. var
  2828. sym : tsym;
  2829. begin
  2830. sym:=tsym(systemunit.Find(s));
  2831. if not assigned(sym) or
  2832. (sym.typ<>typesym) then
  2833. message1(cg_f_unknown_system_type,s);
  2834. result:=ttypesym(sym);
  2835. end;
  2836. function try_search_system_type(const s: TIDString): ttypesym;
  2837. var
  2838. sym : tsym;
  2839. begin
  2840. sym:=tsym(systemunit.Find(s));
  2841. if not assigned(sym) then
  2842. result:=nil
  2843. else
  2844. begin
  2845. if sym.typ<>typesym then
  2846. message1(cg_f_unknown_system_type,s);
  2847. result:=ttypesym(sym);
  2848. end;
  2849. end;
  2850. function search_system_proc(const s: TIDString): tprocdef;
  2851. var
  2852. srsym: tsym;
  2853. begin
  2854. srsym:=tsym(systemunit.find(s));
  2855. if not assigned(srsym) and
  2856. (cs_compilesystem in current_settings.moduleswitches) then
  2857. srsym:=tsym(systemunit.Find(upper(s)));
  2858. if not assigned(srsym) or
  2859. (srsym.typ<>procsym) then
  2860. message1(cg_f_unknown_compilerproc,s);
  2861. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  2862. end;
  2863. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2864. var
  2865. srsymtable: tsymtable;
  2866. sym: tsym;
  2867. begin
  2868. sym:=nil;
  2869. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2870. (sym.typ=typesym) then
  2871. begin
  2872. result:=ttypesym(sym);
  2873. exit;
  2874. end
  2875. else
  2876. begin
  2877. if throwerror then
  2878. message2(cg_f_unknown_type_in_unit,typename,unitname);
  2879. result:=nil;
  2880. end;
  2881. end;
  2882. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2883. var
  2884. s: string;
  2885. list: TFPObjectList;
  2886. i: integer;
  2887. st: tsymtable;
  2888. begin
  2889. result:=false;
  2890. odef:=nil;
  2891. { when there are no helpers active currently then we don't need to do
  2892. anything }
  2893. if current_module.extendeddefs.count=0 then
  2894. exit;
  2895. { no helpers for anonymous types }
  2896. if ((pd.typ in [recorddef,objectdef]) and
  2897. (
  2898. not assigned(tabstractrecorddef(pd).objrealname) or
  2899. (tabstractrecorddef(pd).objrealname^='')
  2900. )
  2901. ) or
  2902. not assigned(pd.typesym) then
  2903. exit;
  2904. { if pd is defined inside a procedure we must not use make_mangledname
  2905. (as a helper may not be defined in a procedure this is no problem...)}
  2906. st:=pd.owner;
  2907. while st.symtabletype in [objectsymtable,recordsymtable] do
  2908. st:=st.defowner.owner;
  2909. if st.symtabletype=localsymtable then
  2910. exit;
  2911. { the mangled name is used as the key for tmodule.extendeddefs }
  2912. s:=generate_objectpascal_helper_key(pd);
  2913. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2914. if assigned(list) and (list.count>0) then
  2915. begin
  2916. i:=list.count-1;
  2917. repeat
  2918. odef:=tobjectdef(list[list.count-1]);
  2919. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2920. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2921. dec(i);
  2922. until result or (i<0);
  2923. if not result then
  2924. { just to be sure that noone uses odef }
  2925. odef:=nil;
  2926. end;
  2927. end;
  2928. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2929. var
  2930. hashedid : THashedIDString;
  2931. classh : tobjectdef;
  2932. i : integer;
  2933. pdef : tprocdef;
  2934. begin
  2935. result:=false;
  2936. { if there is no class helper for the class then there is no need to
  2937. search further }
  2938. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2939. exit;
  2940. hashedid.id:=s;
  2941. repeat
  2942. srsymtable:=classh.symtable;
  2943. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2944. if srsym<>nil then
  2945. begin
  2946. case srsym.typ of
  2947. procsym:
  2948. begin
  2949. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2950. begin
  2951. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2952. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2953. continue;
  2954. { we need to know if a procedure references symbols
  2955. in the static symtable, because then it can't be
  2956. inlined from outside this unit }
  2957. if assigned(current_procinfo) and
  2958. (srsym.owner.symtabletype=staticsymtable) then
  2959. include(current_procinfo.flags,pi_uses_static_symtable);
  2960. { the first found method wins }
  2961. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2962. srsymtable:=srsym.owner;
  2963. addsymref(srsym);
  2964. result:=true;
  2965. exit;
  2966. end;
  2967. end;
  2968. typesym,
  2969. fieldvarsym,
  2970. constsym,
  2971. enumsym,
  2972. undefinedsym,
  2973. propertysym:
  2974. begin
  2975. addsymref(srsym);
  2976. result:=true;
  2977. exit;
  2978. end;
  2979. else
  2980. internalerror(2014041101);
  2981. end;
  2982. end;
  2983. { try the helper parent if available }
  2984. classh:=classh.childof;
  2985. until classh=nil;
  2986. srsym:=nil;
  2987. srsymtable:=nil;
  2988. end;
  2989. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2990. var
  2991. hashedid : THashedIDString;
  2992. stackitem : psymtablestackitem;
  2993. i : longint;
  2994. defowner : tobjectdef;
  2995. begin
  2996. hashedid.id:=class_helper_prefix+s;
  2997. stackitem:=symtablestack.stack;
  2998. while assigned(stackitem) do
  2999. begin
  3000. srsymtable:=stackitem^.symtable;
  3001. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3002. if assigned(srsym) then
  3003. begin
  3004. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3005. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3006. (srsym.typ<>procsym) then
  3007. internalerror(2009111505);
  3008. { check whether this procsym includes a helper for this particular class }
  3009. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3010. begin
  3011. { does pd inherit from (or is the same as) the class
  3012. that this method's category extended?
  3013. Warning: this list contains both category and objcclass methods
  3014. (for id.randommethod), so only check category methods here
  3015. }
  3016. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3017. if (oo_is_classhelper in defowner.objectoptions) and
  3018. def_is_related(pd,defowner.childof) then
  3019. begin
  3020. { we need to know if a procedure references symbols
  3021. in the static symtable, because then it can't be
  3022. inlined from outside this unit }
  3023. if assigned(current_procinfo) and
  3024. (srsym.owner.symtabletype=staticsymtable) then
  3025. include(current_procinfo.flags,pi_uses_static_symtable);
  3026. { no need to keep looking. There might be other
  3027. categories that extend this, a parent or child
  3028. class with a method with the same name (either
  3029. overriding this one, or overridden by this one),
  3030. but that doesn't matter as far as the basic
  3031. procsym is concerned.
  3032. }
  3033. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3034. srsymtable:=srsym.owner;
  3035. addsymref(srsym);
  3036. result:=true;
  3037. exit;
  3038. end;
  3039. end;
  3040. end;
  3041. stackitem:=stackitem^.next;
  3042. end;
  3043. srsym:=nil;
  3044. srsymtable:=nil;
  3045. result:=false;
  3046. end;
  3047. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3048. var
  3049. hashedid : THashedIDString;
  3050. stackitem : psymtablestackitem;
  3051. i : longint;
  3052. begin
  3053. hashedid.id:=class_helper_prefix+s;
  3054. stackitem:=symtablestack.stack;
  3055. while assigned(stackitem) do
  3056. begin
  3057. srsymtable:=stackitem^.symtable;
  3058. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3059. if assigned(srsym) then
  3060. begin
  3061. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3062. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3063. (srsym.typ<>procsym) then
  3064. internalerror(2009112005);
  3065. { check whether this procsym includes a helper for this particular class }
  3066. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3067. begin
  3068. { we need to know if a procedure references symbols
  3069. in the static symtable, because then it can't be
  3070. inlined from outside this unit }
  3071. if assigned(current_procinfo) and
  3072. (srsym.owner.symtabletype=staticsymtable) then
  3073. include(current_procinfo.flags,pi_uses_static_symtable);
  3074. { no need to keep looking. There might be other
  3075. methods with the same name, but that doesn't matter
  3076. as far as the basic procsym is concerned.
  3077. }
  3078. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3079. { We need the symtable in which the classhelper-like sym
  3080. is located, not the objectdef. The reason is that the
  3081. callnode will climb the symtablestack until it encounters
  3082. this symtable to start looking for overloads (and it won't
  3083. find the objectsymtable in which this method sym is
  3084. located
  3085. srsymtable:=srsym.owner;
  3086. }
  3087. addsymref(srsym);
  3088. result:=true;
  3089. exit;
  3090. end;
  3091. end;
  3092. stackitem:=stackitem^.next;
  3093. end;
  3094. srsym:=nil;
  3095. srsymtable:=nil;
  3096. result:=false;
  3097. end;
  3098. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  3099. { searches n in symtable of pd and all anchestors }
  3100. var
  3101. srsymtable : tsymtable;
  3102. begin
  3103. { in case this is a formal class, first find the real definition }
  3104. if (oo_is_formal in pd.objectoptions) then
  3105. pd:=find_real_class_definition(tobjectdef(pd),true);
  3106. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  3107. exit;
  3108. result:=search_struct_member_no_helper(pd,s);
  3109. if assigned(result) then
  3110. exit;
  3111. { not found, now look for class helpers }
  3112. if is_objcclass(pd) then
  3113. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  3114. end;
  3115. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  3116. var
  3117. hashedid : THashedIDString;
  3118. srsym : tsym;
  3119. begin
  3120. hashedid.id:=s;
  3121. while assigned(pd) do
  3122. begin
  3123. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  3124. if assigned(srsym) then
  3125. begin
  3126. result:=srsym;
  3127. exit;
  3128. end;
  3129. if pd.typ=objectdef then
  3130. pd:=tobjectdef(pd).childof
  3131. else
  3132. pd:=nil;
  3133. end;
  3134. result:=nil;
  3135. end;
  3136. function search_macro(const s : string):tsym;
  3137. var
  3138. stackitem : psymtablestackitem;
  3139. hashedid : THashedIDString;
  3140. srsym : tsym;
  3141. begin
  3142. hashedid.id:=s;
  3143. { First search the localmacrosymtable before searching the
  3144. global macrosymtables from the units }
  3145. if assigned(current_module) then
  3146. begin
  3147. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  3148. if assigned(srsym) then
  3149. begin
  3150. result:= srsym;
  3151. exit;
  3152. end;
  3153. end;
  3154. stackitem:=macrosymtablestack.stack;
  3155. while assigned(stackitem) do
  3156. begin
  3157. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3158. if assigned(srsym) then
  3159. begin
  3160. result:= srsym;
  3161. exit;
  3162. end;
  3163. stackitem:=stackitem^.next;
  3164. end;
  3165. result:= nil;
  3166. end;
  3167. function defined_macro(const s : string):boolean;
  3168. var
  3169. mac: tmacro;
  3170. begin
  3171. mac:=tmacro(search_macro(s));
  3172. if assigned(mac) then
  3173. begin
  3174. mac.is_used:=true;
  3175. defined_macro:=mac.defined;
  3176. end
  3177. else
  3178. defined_macro:=false;
  3179. end;
  3180. {****************************************************************************
  3181. Object Helpers
  3182. ****************************************************************************}
  3183. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  3184. { returns the default property of a class, searches also anchestors }
  3185. var
  3186. _defaultprop : tpropertysym;
  3187. helperpd : tobjectdef;
  3188. begin
  3189. _defaultprop:=nil;
  3190. { first search in helper's hierarchy }
  3191. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3192. while assigned(helperpd) do
  3193. begin
  3194. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3195. if assigned(_defaultprop) then
  3196. break;
  3197. helperpd:=helperpd.childof;
  3198. end;
  3199. if assigned(_defaultprop) then
  3200. begin
  3201. search_default_property:=_defaultprop;
  3202. exit;
  3203. end;
  3204. { now search in the type's hierarchy itself }
  3205. while assigned(pd) do
  3206. begin
  3207. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3208. if assigned(_defaultprop) then
  3209. break;
  3210. if (pd.typ=objectdef) then
  3211. pd:=tobjectdef(pd).childof
  3212. else
  3213. break;
  3214. end;
  3215. search_default_property:=_defaultprop;
  3216. end;
  3217. {****************************************************************************
  3218. Macro Helpers
  3219. ****************************************************************************}
  3220. procedure def_system_macro(const name : string);
  3221. var
  3222. mac : tmacro;
  3223. s: string;
  3224. begin
  3225. if name = '' then
  3226. internalerror(2004121202);
  3227. s:= upper(name);
  3228. mac:=tmacro(search_macro(s));
  3229. if not assigned(mac) then
  3230. begin
  3231. mac:=tmacro.create(s);
  3232. if assigned(current_module) then
  3233. current_module.localmacrosymtable.insert(mac)
  3234. else
  3235. initialmacrosymtable.insert(mac);
  3236. end;
  3237. Message1(parser_c_macro_defined,mac.name);
  3238. mac.defined:=true;
  3239. end;
  3240. procedure set_system_macro(const name, value : string);
  3241. var
  3242. mac : tmacro;
  3243. s: string;
  3244. begin
  3245. if name = '' then
  3246. internalerror(2004121203);
  3247. s:= upper(name);
  3248. mac:=tmacro(search_macro(s));
  3249. if not assigned(mac) then
  3250. begin
  3251. mac:=tmacro.create(s);
  3252. if assigned(current_module) then
  3253. current_module.localmacrosymtable.insert(mac)
  3254. else
  3255. initialmacrosymtable.insert(mac);
  3256. end
  3257. else
  3258. begin
  3259. mac.is_compiler_var:=false;
  3260. if assigned(mac.buftext) then
  3261. freemem(mac.buftext,mac.buflen);
  3262. end;
  3263. Message2(parser_c_macro_set_to,mac.name,value);
  3264. mac.buflen:=length(value);
  3265. getmem(mac.buftext,mac.buflen);
  3266. move(value[1],mac.buftext^,mac.buflen);
  3267. mac.defined:=true;
  3268. end;
  3269. procedure set_system_compvar(const name, value : string);
  3270. var
  3271. mac : tmacro;
  3272. s: string;
  3273. begin
  3274. if name = '' then
  3275. internalerror(2004121204);
  3276. s:= upper(name);
  3277. mac:=tmacro(search_macro(s));
  3278. if not assigned(mac) then
  3279. begin
  3280. mac:=tmacro.create(s);
  3281. mac.is_compiler_var:=true;
  3282. if assigned(current_module) then
  3283. current_module.localmacrosymtable.insert(mac)
  3284. else
  3285. initialmacrosymtable.insert(mac);
  3286. end
  3287. else
  3288. begin
  3289. mac.is_compiler_var:=true;
  3290. if assigned(mac.buftext) then
  3291. freemem(mac.buftext,mac.buflen);
  3292. end;
  3293. Message2(parser_c_macro_set_to,mac.name,value);
  3294. mac.buflen:=length(value);
  3295. getmem(mac.buftext,mac.buflen);
  3296. move(value[1],mac.buftext^,mac.buflen);
  3297. mac.defined:=true;
  3298. end;
  3299. procedure undef_system_macro(const name : string);
  3300. var
  3301. mac : tmacro;
  3302. s: string;
  3303. begin
  3304. if name = '' then
  3305. internalerror(2004121205);
  3306. s:= upper(name);
  3307. mac:=tmacro(search_macro(s));
  3308. if not assigned(mac) then
  3309. {If not found, then it's already undefined.}
  3310. else
  3311. begin
  3312. Message1(parser_c_macro_undefined,mac.name);
  3313. mac.defined:=false;
  3314. mac.is_compiler_var:=false;
  3315. { delete old definition }
  3316. if assigned(mac.buftext) then
  3317. begin
  3318. freemem(mac.buftext,mac.buflen);
  3319. mac.buftext:=nil;
  3320. end;
  3321. end;
  3322. end;
  3323. {$ifdef UNITALIASES}
  3324. {****************************************************************************
  3325. TUNIT_ALIAS
  3326. ****************************************************************************}
  3327. constructor tunit_alias.create(const n:string);
  3328. var
  3329. i : longint;
  3330. begin
  3331. i:=pos('=',n);
  3332. if i=0 then
  3333. fail;
  3334. inherited createname(Copy(n,1,i-1));
  3335. newname:=stringdup(Copy(n,i+1,255));
  3336. end;
  3337. destructor tunit_alias.destroy;
  3338. begin
  3339. stringdispose(newname);
  3340. inherited destroy;
  3341. end;
  3342. procedure addunitalias(const n:string);
  3343. begin
  3344. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3345. end;
  3346. function getunitalias(const n:string):string;
  3347. var
  3348. p : punit_alias;
  3349. begin
  3350. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3351. if assigned(p) then
  3352. getunitalias:=punit_alias(p).newname^
  3353. else
  3354. getunitalias:=n;
  3355. end;
  3356. {$endif UNITALIASES}
  3357. {****************************************************************************
  3358. Init/Done Symtable
  3359. ****************************************************************************}
  3360. procedure InitSymtable;
  3361. begin
  3362. { Reset symbolstack }
  3363. symtablestack:=nil;
  3364. systemunit:=nil;
  3365. { create error syms and def }
  3366. generrorsym:=terrorsym.create;
  3367. generrordef:=cerrordef.create;
  3368. { macros }
  3369. initialmacrosymtable:=tmacrosymtable.create(false);
  3370. macrosymtablestack:=TSymtablestack.create;
  3371. macrosymtablestack.push(initialmacrosymtable);
  3372. {$ifdef UNITALIASES}
  3373. { unit aliases }
  3374. unitaliases:=TFPHashObjectList.create;
  3375. {$endif}
  3376. { set some global vars to nil, might be important for the ide }
  3377. class_tobject:=nil;
  3378. interface_iunknown:=nil;
  3379. interface_idispatch:=nil;
  3380. rec_tguid:=nil;
  3381. dupnr:=0;
  3382. end;
  3383. procedure DoneSymtable;
  3384. begin
  3385. generrorsym.owner:=nil;
  3386. generrorsym.free;
  3387. generrordef.owner:=nil;
  3388. generrordef.free;
  3389. initialmacrosymtable.free;
  3390. macrosymtablestack.free;
  3391. {$ifdef UNITALIASES}
  3392. unitaliases.free;
  3393. {$endif}
  3394. end;
  3395. end.