2
0

symtable.pas 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066
  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,
  23. { global }
  24. cpuinfo,globtype,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,symsym,
  27. { ppu }
  28. ppu,
  29. { assembler }
  30. aasmtai,aasmdata
  31. ;
  32. {****************************************************************************
  33. Symtable types
  34. ****************************************************************************}
  35. type
  36. tstoredsymtable = class(TSymtable)
  37. private
  38. b_needs_init_final : boolean;
  39. procedure _needs_init_final(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  51. procedure delete(sym:TSymEntry);override;
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure buildderef;virtual;
  56. procedure buildderefimpl;virtual;
  57. procedure deref;virtual;
  58. procedure derefimpl;virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean;
  65. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  66. end;
  67. tabstractrecordsymtable = class(tstoredsymtable)
  68. public
  69. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  70. recordalignment, { alignment desired when inserting this record }
  71. fieldalignment, { alignment current alignment used when fields are inserted }
  72. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  73. constructor create(const n:string;usealign:shortint);
  74. procedure ppuload(ppufile:tcompilerppufile);override;
  75. procedure ppuwrite(ppufile:tcompilerppufile);override;
  76. procedure alignrecord(fieldoffset:aint;varalign:shortint);
  77. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  78. procedure addalignmentpadding;
  79. procedure insertdef(def:TDefEntry);override;
  80. function is_packed: boolean;
  81. function has_single_field(out sym:tfieldvarsym): boolean;
  82. function get_unit_symtable: tsymtable;
  83. protected
  84. _datasize : aint;
  85. { size in bits of the data in case of bitpacked record. Only important during construction, }
  86. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  87. databitsize : aint;
  88. procedure setdatasize(val: aint);
  89. public
  90. function iscurrentunit: boolean; override;
  91. property datasize : aint read _datasize write setdatasize;
  92. end;
  93. trecordsymtable = class(tabstractrecordsymtable)
  94. public
  95. constructor create(const n:string;usealign:shortint);
  96. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  97. end;
  98. tObjectSymtable = class(tabstractrecordsymtable)
  99. public
  100. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  101. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  102. end;
  103. { tabstractlocalsymtable }
  104. tabstractlocalsymtable = class(tstoredsymtable)
  105. public
  106. procedure ppuwrite(ppufile:tcompilerppufile);override;
  107. function count_locals:longint;
  108. end;
  109. tlocalsymtable = class(tabstractlocalsymtable)
  110. public
  111. constructor create(adefowner:tdef;level:byte);
  112. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  113. end;
  114. { tparasymtable }
  115. tparasymtable = class(tabstractlocalsymtable)
  116. public
  117. readonly: boolean;
  118. constructor create(adefowner:tdef;level:byte);
  119. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  120. procedure insertdef(def:TDefEntry);override;
  121. end;
  122. tabstractuniTSymtable = class(tstoredsymtable)
  123. public
  124. constructor create(const n : string;id:word);
  125. function iscurrentunit:boolean;override;
  126. end;
  127. tglobalsymtable = class(tabstractuniTSymtable)
  128. public
  129. unittypecount : word;
  130. constructor create(const n : string;id:word);
  131. procedure ppuload(ppufile:tcompilerppufile);override;
  132. procedure ppuwrite(ppufile:tcompilerppufile);override;
  133. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  134. end;
  135. tstaticsymtable = class(tabstractuniTSymtable)
  136. public
  137. constructor create(const n : string;id:word);
  138. procedure ppuload(ppufile:tcompilerppufile);override;
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  141. end;
  142. twithsymtable = class(TSymtable)
  143. withrefnode : tobject; { tnode }
  144. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  145. destructor destroy;override;
  146. procedure clear;override;
  147. procedure insertdef(def:TDefEntry);override;
  148. end;
  149. tstt_excepTSymtable = class(TSymtable)
  150. public
  151. constructor create;
  152. end;
  153. tmacrosymtable = class(tstoredsymtable)
  154. public
  155. constructor create(exported: boolean);
  156. end;
  157. { tenumsymtable }
  158. tenumsymtable = class(tstoredsymtable)
  159. public
  160. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  161. constructor create(adefowner:tdef);
  162. end;
  163. { tarraysymtable }
  164. tarraysymtable = class(tstoredsymtable)
  165. public
  166. procedure insertdef(def:TDefEntry);override;
  167. constructor create(adefowner:tdef);
  168. end;
  169. var
  170. systemunit : tglobalsymtable; { pointer to the system unit }
  171. {****************************************************************************
  172. Functions
  173. ****************************************************************************}
  174. {*** Misc ***}
  175. function FullTypeName(def,otherdef:tdef):string;
  176. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  177. procedure incompatibletypes(def1,def2:tdef);
  178. procedure hidesym(sym:TSymEntry);
  179. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  180. {*** Search ***}
  181. procedure addsymref(sym:tsym);
  182. function is_owned_by(childdef,ownerdef:tabstractrecorddef):boolean;
  183. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  184. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  185. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  186. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  187. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  188. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  189. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  190. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:thelpersearch):boolean;
  191. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  192. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  193. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  194. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  195. function search_system_type(const s: TIDString): ttypesym;
  196. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  197. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  198. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  199. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  200. function search_last_objectpascal_helper(pd : tabstractrecorddef;out odef : tobjectdef):boolean;
  201. function search_objectpascal_class_helper(pd,contextclassh : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  202. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  203. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  204. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  205. {and returns it if found. Returns nil otherwise.}
  206. function search_macro(const s : string):tsym;
  207. { Additionally to searching for a macro, also checks whether it's still }
  208. { actually defined (could be disable using "undef") }
  209. function defined_macro(const s : string):boolean;
  210. {*** Object Helpers ***}
  211. function search_default_property(pd : tobjectdef) : tpropertysym;
  212. function find_real_objcclass_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  213. {*** Macro Helpers ***}
  214. {If called initially, the following procedures manipulate macros in }
  215. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  216. {Name can be given in any case (it will be converted to upper case).}
  217. procedure def_system_macro(const name : string);
  218. procedure set_system_macro(const name, value : string);
  219. procedure set_system_compvar(const name, value : string);
  220. procedure undef_system_macro(const name : string);
  221. {*** symtable stack ***}
  222. { $ifdef DEBUG
  223. procedure test_symtablestack;
  224. procedure list_symtablestack;
  225. $endif DEBUG}
  226. {$ifdef UNITALIASES}
  227. type
  228. punit_alias = ^tunit_alias;
  229. tunit_alias = object(TNamedIndexItem)
  230. newname : pshortstring;
  231. constructor init(const n:string);
  232. destructor done;virtual;
  233. end;
  234. var
  235. unitaliases : pdictionary;
  236. procedure addunitalias(const n:string);
  237. function getunitalias(const n:string):string;
  238. {$endif UNITALIASES}
  239. {*** Init / Done ***}
  240. procedure IniTSymtable;
  241. procedure DoneSymtable;
  242. const
  243. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  244. { NOTOKEN } 'error',
  245. { _PLUS } 'plus',
  246. { _MINUS } 'minus',
  247. { _STAR } 'star',
  248. { _SLASH } 'slash',
  249. { _EQ } 'equal',
  250. { _GT } 'greater',
  251. { _LT } 'lower',
  252. { _GTE } 'greater_or_equal',
  253. { _LTE } 'lower_or_equal',
  254. { _NE } 'not_equal',
  255. { _SYMDIF } 'sym_diff',
  256. { _STARSTAR } 'starstar',
  257. { _OP_AS } 'as',
  258. { _OP_IN } 'in',
  259. { _OP_IS } 'is',
  260. { _OP_OR } 'or',
  261. { _OP_AND } 'and',
  262. { _OP_DIV } 'div',
  263. { _OP_MOD } 'mod',
  264. { _OP_NOT } 'not',
  265. { _OP_SHL } 'shl',
  266. { _OP_SHR } 'shr',
  267. { _OP_XOR } 'xor',
  268. { _ASSIGNMENT } 'assign',
  269. { _OP_EXPLICIT } 'explicit',
  270. { _OP_ENUMERATOR } 'enumerator',
  271. { _OP_INC } 'inc',
  272. { _OP_DEC } 'dec');
  273. implementation
  274. uses
  275. { global }
  276. verbose,globals,
  277. { target }
  278. systems,
  279. { symtable }
  280. symutil,defcmp,defutil,
  281. { module }
  282. fmodule,
  283. { codegen }
  284. procinfo
  285. ;
  286. var
  287. dupnr : longint; { unique number for duplicate symbols }
  288. {*****************************************************************************
  289. TStoredSymtable
  290. *****************************************************************************}
  291. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  292. begin
  293. inherited insert(sym,checkdup);
  294. end;
  295. procedure tstoredsymtable.delete(sym:TSymEntry);
  296. begin
  297. inherited delete(sym);
  298. end;
  299. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  300. begin
  301. { load the table's flags }
  302. if ppufile.readentry<>ibsymtableoptions then
  303. Message(unit_f_ppu_read_error);
  304. ppufile.getsmallset(tableoptions);
  305. { load definitions }
  306. loaddefs(ppufile);
  307. { load symbols }
  308. loadsyms(ppufile);
  309. end;
  310. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  311. begin
  312. { write the table's flags }
  313. ppufile.putsmallset(tableoptions);
  314. ppufile.writeentry(ibsymtableoptions);
  315. { write definitions }
  316. writedefs(ppufile);
  317. { write symbols }
  318. writesyms(ppufile);
  319. end;
  320. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  321. var
  322. def : tdef;
  323. b : byte;
  324. begin
  325. { load start of definition section, which holds the amount of defs }
  326. if ppufile.readentry<>ibstartdefs then
  327. Message(unit_f_ppu_read_error);
  328. { read definitions }
  329. repeat
  330. b:=ppufile.readentry;
  331. case b of
  332. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  333. ibarraydef : def:=tarraydef.ppuload(ppufile);
  334. iborddef : def:=torddef.ppuload(ppufile);
  335. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  336. ibprocdef : def:=tprocdef.ppuload(ppufile);
  337. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  338. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  339. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  340. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  341. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  342. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  343. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  344. ibenumdef : def:=tenumdef.ppuload(ppufile);
  345. ibsetdef : def:=tsetdef.ppuload(ppufile);
  346. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  347. ibfiledef : def:=tfiledef.ppuload(ppufile);
  348. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  349. ibformaldef : def:=tformaldef.ppuload(ppufile);
  350. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  351. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  352. ibenddefs : break;
  353. ibend : Message(unit_f_ppu_read_error);
  354. else
  355. Message1(unit_f_ppu_invalid_entry,tostr(b));
  356. end;
  357. InsertDef(def);
  358. until false;
  359. end;
  360. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  361. var
  362. b : byte;
  363. sym : tsym;
  364. begin
  365. { load start of definition section, which holds the amount of defs }
  366. if ppufile.readentry<>ibstartsyms then
  367. Message(unit_f_ppu_read_error);
  368. { now read the symbols }
  369. repeat
  370. b:=ppufile.readentry;
  371. case b of
  372. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  373. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  374. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  375. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  376. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  377. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  378. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  379. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  380. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  381. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  382. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  383. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  384. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  385. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  386. ibendsyms : break;
  387. ibend : Message(unit_f_ppu_read_error);
  388. else
  389. Message1(unit_f_ppu_invalid_entry,tostr(b));
  390. end;
  391. Insert(sym,false);
  392. until false;
  393. end;
  394. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  395. var
  396. i : longint;
  397. def : tstoreddef;
  398. begin
  399. { each definition get a number, write then the amount of defs to the
  400. ibstartdef entry }
  401. ppufile.putlongint(DefList.count);
  402. ppufile.writeentry(ibstartdefs);
  403. { now write the definition }
  404. for i:=0 to DefList.Count-1 do
  405. begin
  406. def:=tstoreddef(DefList[i]);
  407. def.ppuwrite(ppufile);
  408. end;
  409. { write end of definitions }
  410. ppufile.writeentry(ibenddefs);
  411. end;
  412. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  413. var
  414. i : longint;
  415. sym : Tstoredsym;
  416. begin
  417. { each definition get a number, write then the amount of syms and the
  418. datasize to the ibsymdef entry }
  419. ppufile.putlongint(SymList.count);
  420. ppufile.writeentry(ibstartsyms);
  421. { foreach is used to write all symbols }
  422. for i:=0 to SymList.Count-1 do
  423. begin
  424. sym:=tstoredsym(SymList[i]);
  425. sym.ppuwrite(ppufile);
  426. end;
  427. { end of symbols }
  428. ppufile.writeentry(ibendsyms);
  429. end;
  430. procedure tstoredsymtable.buildderef;
  431. var
  432. i : longint;
  433. def : tstoreddef;
  434. sym : tstoredsym;
  435. begin
  436. { interface definitions }
  437. for i:=0 to DefList.Count-1 do
  438. begin
  439. def:=tstoreddef(DefList[i]);
  440. def.buildderef;
  441. end;
  442. { interface symbols }
  443. for i:=0 to SymList.Count-1 do
  444. begin
  445. sym:=tstoredsym(SymList[i]);
  446. sym.buildderef;
  447. end;
  448. end;
  449. procedure tstoredsymtable.buildderefimpl;
  450. var
  451. i : longint;
  452. def : tstoreddef;
  453. begin
  454. { implementation definitions }
  455. for i:=0 to DefList.Count-1 do
  456. begin
  457. def:=tstoreddef(DefList[i]);
  458. def.buildderefimpl;
  459. end;
  460. end;
  461. procedure tstoredsymtable.deref;
  462. var
  463. i : longint;
  464. def : tstoreddef;
  465. sym : tstoredsym;
  466. begin
  467. { first deref the interface ttype symbols. This is needs
  468. to be done before the interface defs are derefed, because
  469. the interface defs can contain references to the type symbols
  470. which then already need to contain a resolved typedef field (PFV) }
  471. for i:=0 to SymList.Count-1 do
  472. begin
  473. sym:=tstoredsym(SymList[i]);
  474. if sym.typ=typesym then
  475. sym.deref;
  476. end;
  477. { interface definitions }
  478. for i:=0 to DefList.Count-1 do
  479. begin
  480. def:=tstoreddef(DefList[i]);
  481. def.deref;
  482. end;
  483. { interface symbols }
  484. for i:=0 to SymList.Count-1 do
  485. begin
  486. sym:=tstoredsym(SymList[i]);
  487. if sym.typ<>typesym then
  488. sym.deref;
  489. end;
  490. end;
  491. procedure tstoredsymtable.derefimpl;
  492. var
  493. i : longint;
  494. def : tstoreddef;
  495. begin
  496. { implementation definitions }
  497. for i:=0 to DefList.Count-1 do
  498. begin
  499. def:=tstoreddef(DefList[i]);
  500. def.derefimpl;
  501. end;
  502. end;
  503. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  504. var
  505. hsym : tsym;
  506. begin
  507. hsym:=tsym(FindWithHash(hashedid));
  508. if assigned(hsym) then
  509. DuplicateSym(hashedid,sym,hsym);
  510. result:=assigned(hsym);
  511. end;
  512. {**************************************
  513. Callbacks
  514. **************************************}
  515. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  516. begin
  517. if tsym(sym).typ=procsym then
  518. tprocsym(sym).check_forward
  519. { check also object method table }
  520. { we needn't to test the def list }
  521. { because each object has to have a type sym,
  522. only test objects declarations, not type renamings }
  523. else
  524. if (tsym(sym).typ=typesym) and
  525. assigned(ttypesym(sym).typedef) and
  526. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  527. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  528. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  529. end;
  530. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  531. begin
  532. if (tsym(sym).typ=labelsym) and
  533. not(tlabelsym(sym).defined) then
  534. begin
  535. if tlabelsym(sym).used then
  536. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  537. else
  538. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  539. end;
  540. end;
  541. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  542. begin
  543. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  544. ((tsym(sym).owner.symtabletype in
  545. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  546. begin
  547. { unused symbol should be reported only if no }
  548. { error is reported }
  549. { if the symbol is in a register it is used }
  550. { also don't count the value parameters which have local copies }
  551. { also don't claim for high param of open parameters (PM) }
  552. if (Errorcount<>0) or
  553. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  554. (sp_internal in tsym(sym).symoptions) then
  555. exit;
  556. if (tstoredsym(sym).refs=0) then
  557. begin
  558. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  559. begin
  560. { don't warn about the result of constructors }
  561. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  562. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  563. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  564. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  565. end
  566. else if (tsym(sym).owner.symtabletype=parasymtable) then
  567. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  568. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  569. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname)
  570. else
  571. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  572. end
  573. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  574. begin
  575. if (tsym(sym).owner.symtabletype=parasymtable) then
  576. begin
  577. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  578. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  579. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  580. end
  581. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  582. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname)
  583. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  584. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  585. end
  586. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  587. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  588. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  589. end
  590. else if ((tsym(sym).owner.symtabletype in
  591. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  592. begin
  593. if (Errorcount<>0) or
  594. (sp_internal in tsym(sym).symoptions) then
  595. exit;
  596. { do not claim for inherited private fields !! }
  597. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  598. case tsym(sym).typ of
  599. typesym:
  600. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname);
  601. constsym:
  602. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname);
  603. propertysym:
  604. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname);
  605. else
  606. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).RttiName,tsym(sym).prettyname);
  607. end
  608. { units references are problematic }
  609. else
  610. begin
  611. if (tsym(sym).refs=0) and
  612. not(tsym(sym).typ in [enumsym,unitsym]) and
  613. not(is_funcret_sym(tsym(sym))) and
  614. { don't complain about compiler generated syms for specializations, see also #13405 }
  615. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  616. (pos('$',ttypesym(sym).Realname)<>0)) and
  617. (
  618. (tsym(sym).typ<>procsym) or
  619. ((tsym(sym).owner.symtabletype=staticsymtable) and
  620. not current_module.is_unit)
  621. ) and
  622. { don't complain about alias for hidden _cmd parameter to
  623. obj-c methods }
  624. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  625. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  626. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  627. end;
  628. end;
  629. end;
  630. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  631. begin
  632. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  633. varsymbolused(sym,arg);
  634. end;
  635. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  636. begin
  637. {
  638. Don't test simple object aliases PM
  639. }
  640. if (tsym(sym).typ=typesym) and
  641. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  642. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  643. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  644. end;
  645. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  646. begin
  647. if (tsym(sym).typ=propertysym) and
  648. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  649. ppointer(arg)^:=sym;
  650. end;
  651. {***********************************************
  652. Process all entries
  653. ***********************************************}
  654. { checks, if all procsyms and methods are defined }
  655. procedure tstoredsymtable.check_forwards;
  656. begin
  657. SymList.ForEachCall(@check_forward,nil);
  658. end;
  659. procedure tstoredsymtable.checklabels;
  660. begin
  661. SymList.ForEachCall(@labeldefined,nil);
  662. end;
  663. procedure tstoredsymtable.allsymbolsused;
  664. begin
  665. SymList.ForEachCall(@varsymbolused,nil);
  666. end;
  667. procedure tstoredsymtable.allprivatesused;
  668. begin
  669. SymList.ForEachCall(@objectprivatesymbolused,nil);
  670. end;
  671. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  672. begin
  673. if b_needs_init_final then
  674. exit;
  675. { don't check static symbols - they can be present in structures only and
  676. always have a reference to a symbol defined on unit level }
  677. if sp_static in tsym(sym).symoptions then
  678. exit;
  679. case tsym(sym).typ of
  680. fieldvarsym,
  681. staticvarsym,
  682. localvarsym,
  683. paravarsym :
  684. begin
  685. if is_managed_type(tabstractvarsym(sym).vardef) then
  686. b_needs_init_final:=true;
  687. end;
  688. end;
  689. end;
  690. { returns true, if p contains data which needs init/final code }
  691. function tstoredsymtable.needs_init_final : boolean;
  692. begin
  693. b_needs_init_final:=false;
  694. SymList.ForEachCall(@_needs_init_final,nil);
  695. needs_init_final:=b_needs_init_final;
  696. end;
  697. {****************************************************************************
  698. TAbstractRecordSymtable
  699. ****************************************************************************}
  700. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  701. begin
  702. inherited create(n);
  703. moduleid:=current_module.moduleid;
  704. _datasize:=0;
  705. databitsize:=0;
  706. recordalignment:=1;
  707. usefieldalignment:=usealign;
  708. padalignment:=1;
  709. { recordalign C_alignment means C record packing, that starts
  710. with an alignment of 1 }
  711. case usealign of
  712. C_alignment,
  713. bit_alignment:
  714. fieldalignment:=1;
  715. mac68k_alignment:
  716. fieldalignment:=2;
  717. else
  718. fieldalignment:=usealign;
  719. end;
  720. end;
  721. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  722. begin
  723. inherited ppuload(ppufile);
  724. end;
  725. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  726. var
  727. oldtyp : byte;
  728. begin
  729. oldtyp:=ppufile.entrytyp;
  730. ppufile.entrytyp:=subentryid;
  731. inherited ppuwrite(ppufile);
  732. ppufile.entrytyp:=oldtyp;
  733. end;
  734. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  735. begin
  736. { optimal alignment of the record when declaring a variable of this }
  737. { type is independent of the packrecords setting }
  738. if (fieldoffs mod fieldalign) = 0 then
  739. result:=fieldalign
  740. else if (fieldalign >= 16) and
  741. ((fieldoffs mod 16) = 0) and
  742. ((fieldalign mod 16) = 0) then
  743. result:=16
  744. else if (fieldalign >= 8) and
  745. ((fieldoffs mod 8) = 0) and
  746. ((fieldalign mod 8) = 0) then
  747. result:=8
  748. else if (fieldalign >= 4) and
  749. ((fieldoffs mod 4) = 0) and
  750. ((fieldalign mod 4) = 0) then
  751. result:=4
  752. else if (fieldalign >= 2) and
  753. ((fieldoffs mod 2) = 0) and
  754. ((fieldalign mod 2) = 0) then
  755. result:=2
  756. else
  757. result:=1;
  758. end;
  759. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  760. var
  761. varalignrecord: shortint;
  762. begin
  763. case usefieldalignment of
  764. C_alignment:
  765. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  766. mac68k_alignment:
  767. varalignrecord:=2;
  768. else
  769. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  770. end;
  771. recordalignment:=max(recordalignment,varalignrecord);
  772. end;
  773. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  774. var
  775. l : aint;
  776. varalignfield,
  777. varalign : shortint;
  778. vardef : tdef;
  779. begin
  780. if (sym.owner<>self) then
  781. internalerror(200602031);
  782. if sym.fieldoffset<>-1 then
  783. internalerror(200602032);
  784. { set visibility for the symbol }
  785. sym.visibility:=vis;
  786. { this symbol can't be loaded to a register }
  787. sym.varregable:=vr_none;
  788. { Calculate field offset }
  789. l:=sym.getsize;
  790. vardef:=sym.vardef;
  791. varalign:=vardef.alignment;
  792. case usefieldalignment of
  793. bit_alignment:
  794. begin
  795. { bitpacking only happens for ordinals, the rest is aligned at }
  796. { 1 byte (compatible with GPC/GCC) }
  797. if is_ordinal(vardef) then
  798. begin
  799. sym.fieldoffset:=databitsize;
  800. l:=sym.getpackedbitsize;
  801. end
  802. else
  803. begin
  804. databitsize:=_datasize*8;
  805. sym.fieldoffset:=databitsize;
  806. if (l>high(aint) div 8) then
  807. Message(sym_e_segment_too_large);
  808. l:=l*8;
  809. end;
  810. if varalign=0 then
  811. varalign:=size_2_align(l);
  812. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  813. { bit packed records are limited to high(aint) bits }
  814. { instead of bytes to avoid double precision }
  815. { arithmetic in offset calculations }
  816. if int64(l)>high(aint)-sym.fieldoffset then
  817. begin
  818. Message(sym_e_segment_too_large);
  819. _datasize:=high(aint);
  820. databitsize:=high(aint);
  821. end
  822. else
  823. begin
  824. databitsize:=sym.fieldoffset+l;
  825. _datasize:=(databitsize+7) div 8;
  826. end;
  827. { rest is not applicable }
  828. exit;
  829. end;
  830. { Calc the alignment size for C style records }
  831. C_alignment:
  832. begin
  833. if (varalign>4) and
  834. ((varalign mod 4)<>0) and
  835. (vardef.typ=arraydef) then
  836. Message1(sym_w_wrong_C_pack,vardef.typename);
  837. if varalign=0 then
  838. varalign:=l;
  839. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  840. begin
  841. if (varalign>16) and (fieldalignment<32) then
  842. fieldalignment:=32
  843. else if (varalign>12) and (fieldalignment<16) then
  844. fieldalignment:=16
  845. { 12 is needed for long double }
  846. else if (varalign>8) and (fieldalignment<12) then
  847. fieldalignment:=12
  848. else if (varalign>4) and (fieldalignment<8) then
  849. fieldalignment:=8
  850. else if (varalign>2) and (fieldalignment<4) then
  851. fieldalignment:=4
  852. else if (varalign>1) and (fieldalignment<2) then
  853. fieldalignment:=2;
  854. end;
  855. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  856. end;
  857. mac68k_alignment:
  858. begin
  859. { mac68k alignment (C description):
  860. * char is aligned to 1 byte
  861. * everything else (except vector) is aligned to 2 bytes
  862. * vector is aligned to 16 bytes
  863. }
  864. if l>1 then
  865. fieldalignment:=2
  866. else
  867. fieldalignment:=1;
  868. varalign:=2;
  869. end;
  870. end;
  871. if varalign=0 then
  872. varalign:=size_2_align(l);
  873. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  874. sym.fieldoffset:=align(_datasize,varalignfield);
  875. if l>high(aint)-sym.fieldoffset then
  876. begin
  877. Message(sym_e_segment_too_large);
  878. _datasize:=high(aint);
  879. end
  880. else
  881. _datasize:=sym.fieldoffset+l;
  882. { Calc alignment needed for this record }
  883. alignrecord(sym.fieldoffset,varalign);
  884. end;
  885. procedure tabstractrecordsymtable.addalignmentpadding;
  886. begin
  887. { make the record size aligned correctly so it can be
  888. used as elements in an array. For C records we
  889. use the fieldalignment, because that is updated with the
  890. used alignment. }
  891. if (padalignment = 1) then
  892. case usefieldalignment of
  893. C_alignment:
  894. padalignment:=fieldalignment;
  895. { bitpacked }
  896. bit_alignment:
  897. padalignment:=1;
  898. { mac68k: always round to multiple of 2 }
  899. mac68k_alignment:
  900. padalignment:=2;
  901. { default/no packrecords specified }
  902. 0:
  903. padalignment:=recordalignment
  904. { specific packrecords setting -> use as upper limit }
  905. else
  906. padalignment:=min(recordalignment,usefieldalignment);
  907. end;
  908. _datasize:=align(_datasize,padalignment);
  909. end;
  910. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  911. begin
  912. { Enums must also be available outside the record scope,
  913. insert in the owner of this symtable }
  914. if def.typ=enumdef then
  915. defowner.owner.insertdef(def)
  916. else
  917. inherited insertdef(def);
  918. end;
  919. function tabstractrecordsymtable.is_packed: boolean;
  920. begin
  921. result:=usefieldalignment=bit_alignment;
  922. end;
  923. function tabstractrecordsymtable.has_single_field(out sym: tfieldvarsym): boolean;
  924. var
  925. i: longint;
  926. begin
  927. result:=false;
  928. { If a record contains a union, it does not contain a "single
  929. non-composite field" in the context of certain ABIs requiring
  930. special treatment for such records }
  931. if (defowner.typ=recorddef) and
  932. trecorddef(defowner).isunion then
  933. exit;
  934. { a record/object can contain other things than fields }
  935. for i:=0 to SymList.Count-1 do
  936. begin
  937. if tsym(symlist[i]).typ=fieldvarsym then
  938. begin
  939. if result then
  940. begin
  941. result:=false;
  942. exit;
  943. end;
  944. result:=true;
  945. sym:=tfieldvarsym(symlist[i])
  946. end;
  947. end;
  948. end;
  949. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  950. begin
  951. result:=defowner.owner;
  952. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  953. result:=result.defowner.owner;
  954. end;
  955. procedure tabstractrecordsymtable.setdatasize(val: aint);
  956. begin
  957. _datasize:=val;
  958. if (usefieldalignment=bit_alignment) then
  959. { can overflow in non bitpacked records }
  960. databitsize:=val*8;
  961. end;
  962. function tabstractrecordsymtable.iscurrentunit: boolean;
  963. begin
  964. Result := Assigned(current_module) and (current_module.moduleid=moduleid);
  965. end;
  966. {****************************************************************************
  967. TRecordSymtable
  968. ****************************************************************************}
  969. constructor trecordsymtable.create(const n:string;usealign:shortint);
  970. begin
  971. inherited create(n,usealign);
  972. symtabletype:=recordsymtable;
  973. end;
  974. { this procedure is reserved for inserting case variant into
  975. a record symtable }
  976. { the offset is the location of the start of the variant
  977. and datasize and dataalignment corresponds to
  978. the complete size (see code in pdecl unit) PM }
  979. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  980. var
  981. sym : tsym;
  982. def : tdef;
  983. i : integer;
  984. varalignrecord,varalign,
  985. storesize,storealign : aint;
  986. bitsize: aint;
  987. begin
  988. storesize:=_datasize;
  989. storealign:=fieldalignment;
  990. _datasize:=offset;
  991. if (usefieldalignment=bit_alignment) then
  992. databitsize:=offset*8;
  993. { We move the ownership of the defs and symbols to the new recordsymtable.
  994. The old unionsymtable keeps the references, but doesn't own the
  995. objects anymore }
  996. unionst.DefList.OwnsObjects:=false;
  997. unionst.SymList.OwnsObjects:=false;
  998. { copy symbols }
  999. for i:=0 to unionst.SymList.Count-1 do
  1000. begin
  1001. sym:=TSym(unionst.SymList[i]);
  1002. if sym.typ<>fieldvarsym then
  1003. internalerror(200601272);
  1004. if tfieldvarsym(sym).fieldoffset=0 then
  1005. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1006. { add to this record symtable }
  1007. // unionst.SymList.List.List^[i].Data:=nil;
  1008. sym.ChangeOwner(self);
  1009. varalign:=tfieldvarsym(sym).vardef.alignment;
  1010. if varalign=0 then
  1011. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1012. { retrieve size }
  1013. if (usefieldalignment=bit_alignment) then
  1014. begin
  1015. { bit packed records are limited to high(aint) bits }
  1016. { instead of bytes to avoid double precision }
  1017. { arithmetic in offset calculations }
  1018. if is_ordinal(tfieldvarsym(sym).vardef) then
  1019. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1020. else
  1021. begin
  1022. bitsize:=tfieldvarsym(sym).getsize;
  1023. if (bitsize>high(aint) div 8) then
  1024. Message(sym_e_segment_too_large);
  1025. bitsize:=bitsize*8;
  1026. end;
  1027. if bitsize>high(aint)-databitsize then
  1028. begin
  1029. Message(sym_e_segment_too_large);
  1030. _datasize:=high(aint);
  1031. databitsize:=high(aint);
  1032. end
  1033. else
  1034. begin
  1035. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1036. _datasize:=(databitsize+7) div 8;
  1037. end;
  1038. tfieldvarsym(sym).fieldoffset:=databitsize;
  1039. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1040. end
  1041. else
  1042. begin
  1043. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  1044. begin
  1045. Message(sym_e_segment_too_large);
  1046. _datasize:=high(aint);
  1047. end
  1048. else
  1049. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1050. { update address }
  1051. tfieldvarsym(sym).fieldoffset:=_datasize;
  1052. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1053. end;
  1054. { update alignment of this record }
  1055. if (usefieldalignment<>C_alignment) and
  1056. (usefieldalignment<>mac68k_alignment) then
  1057. recordalignment:=max(recordalignment,varalignrecord);
  1058. end;
  1059. { update alignment for C records }
  1060. if (usefieldalignment=C_alignment) and
  1061. (usefieldalignment<>mac68k_alignment) then
  1062. recordalignment:=max(recordalignment,unionst.recordalignment);
  1063. { Register defs in the new record symtable }
  1064. for i:=0 to unionst.DefList.Count-1 do
  1065. begin
  1066. def:=TDef(unionst.DefList[i]);
  1067. def.ChangeOwner(self);
  1068. end;
  1069. _datasize:=storesize;
  1070. fieldalignment:=storealign;
  1071. { If a record contains a union, it does not contain a "single
  1072. non-composite field" in the context of certain ABIs requiring
  1073. special treatment for such records }
  1074. if defowner.typ=recorddef then
  1075. trecorddef(defowner).isunion:=true;
  1076. end;
  1077. {****************************************************************************
  1078. TObjectSymtable
  1079. ****************************************************************************}
  1080. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  1081. begin
  1082. inherited create(n,usealign);
  1083. symtabletype:=ObjectSymtable;
  1084. defowner:=adefowner;
  1085. end;
  1086. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1087. var
  1088. hsym : tsym;
  1089. begin
  1090. result:=false;
  1091. if not assigned(defowner) then
  1092. internalerror(200602061);
  1093. { procsym and propertysym have special code
  1094. to override values in inherited classes. For other
  1095. symbols check for duplicates }
  1096. if not(sym.typ in [procsym,propertysym]) then
  1097. begin
  1098. { but private ids can be reused }
  1099. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1100. if assigned(hsym) and
  1101. (
  1102. (
  1103. not(m_delphi in current_settings.modeswitches) and
  1104. is_visible_for_object(hsym,tobjectdef(defowner))
  1105. ) or
  1106. (
  1107. { In Delphi, you can repeat members of a parent class. You can't }
  1108. { do this for objects however, and you (obviouly) can't }
  1109. { declare two fields with the same name in a single class }
  1110. (m_delphi in current_settings.modeswitches) and
  1111. (
  1112. is_object(tdef(defowner)) or
  1113. (hsym.owner = self)
  1114. )
  1115. )
  1116. ) then
  1117. begin
  1118. DuplicateSym(hashedid,sym,hsym);
  1119. result:=true;
  1120. end;
  1121. end
  1122. else
  1123. begin
  1124. if not(m_duplicate_names in current_settings.modeswitches) then
  1125. result:=inherited checkduplicate(hashedid,sym);
  1126. end;
  1127. end;
  1128. {****************************************************************************
  1129. TAbstractLocalSymtable
  1130. ****************************************************************************}
  1131. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1132. var
  1133. oldtyp : byte;
  1134. begin
  1135. oldtyp:=ppufile.entrytyp;
  1136. ppufile.entrytyp:=subentryid;
  1137. inherited ppuwrite(ppufile);
  1138. ppufile.entrytyp:=oldtyp;
  1139. end;
  1140. function tabstractlocalsymtable.count_locals:longint;
  1141. var
  1142. i : longint;
  1143. sym : tsym;
  1144. begin
  1145. result:=0;
  1146. for i:=0 to SymList.Count-1 do
  1147. begin
  1148. sym:=tsym(SymList[i]);
  1149. { Count only varsyms, but ignore the funcretsym }
  1150. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1151. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1152. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1153. (tstoredsym(sym).refs>0)) then
  1154. inc(result);
  1155. end;
  1156. end;
  1157. {****************************************************************************
  1158. TLocalSymtable
  1159. ****************************************************************************}
  1160. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1161. begin
  1162. inherited create('');
  1163. defowner:=adefowner;
  1164. symtabletype:=localsymtable;
  1165. symtablelevel:=level;
  1166. end;
  1167. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1168. var
  1169. hsym : tsym;
  1170. begin
  1171. if not assigned(defowner) or
  1172. (defowner.typ<>procdef) then
  1173. internalerror(200602042);
  1174. result:=false;
  1175. hsym:=tsym(FindWithHash(hashedid));
  1176. if assigned(hsym) then
  1177. begin
  1178. { a local and the function can have the same
  1179. name in TP and Delphi, but RESULT not }
  1180. if (m_duplicate_names in current_settings.modeswitches) and
  1181. (hsym.typ in [absolutevarsym,localvarsym]) and
  1182. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1183. not((m_result in current_settings.modeswitches) and
  1184. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1185. HideSym(hsym)
  1186. else
  1187. DuplicateSym(hashedid,sym,hsym);
  1188. result:=true;
  1189. exit;
  1190. end;
  1191. { check also parasymtable, this needs to be done here because
  1192. of the special situation with the funcret sym that needs to be
  1193. hidden for tp and delphi modes }
  1194. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1195. if assigned(hsym) then
  1196. begin
  1197. { a local and the function can have the same
  1198. name in TP and Delphi, but RESULT not }
  1199. if (m_duplicate_names in current_settings.modeswitches) and
  1200. (sym.typ in [absolutevarsym,localvarsym]) and
  1201. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1202. not((m_result in current_settings.modeswitches) and
  1203. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1204. Hidesym(sym)
  1205. else
  1206. DuplicateSym(hashedid,sym,hsym);
  1207. result:=true;
  1208. exit;
  1209. end;
  1210. { check ObjectSymtable, skip this for funcret sym because
  1211. that will always be positive because it has the same name
  1212. as the procsym }
  1213. if not is_funcret_sym(sym) and
  1214. (defowner.typ=procdef) and
  1215. assigned(tprocdef(defowner).struct) and
  1216. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1217. (
  1218. not(m_delphi in current_settings.modeswitches) or
  1219. is_object(tprocdef(defowner).struct)
  1220. ) then
  1221. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1222. end;
  1223. {****************************************************************************
  1224. TParaSymtable
  1225. ****************************************************************************}
  1226. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1227. begin
  1228. inherited create('');
  1229. readonly:=false;
  1230. defowner:=adefowner;
  1231. symtabletype:=parasymtable;
  1232. symtablelevel:=level;
  1233. end;
  1234. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1235. begin
  1236. result:=inherited checkduplicate(hashedid,sym);
  1237. if result then
  1238. exit;
  1239. if not(m_duplicate_names in current_settings.modeswitches) and
  1240. (defowner.typ=procdef) and
  1241. assigned(tprocdef(defowner).struct) and
  1242. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1243. (
  1244. not(m_delphi in current_settings.modeswitches) or
  1245. is_object(tprocdef(defowner).struct)
  1246. ) then
  1247. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1248. end;
  1249. procedure tparasymtable.insertdef(def: TDefEntry);
  1250. begin
  1251. if readonly then
  1252. defowner.owner.insertdef(def)
  1253. else
  1254. inherited insertdef(def);
  1255. end;
  1256. {****************************************************************************
  1257. TAbstractUniTSymtable
  1258. ****************************************************************************}
  1259. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1260. begin
  1261. inherited create(n);
  1262. moduleid:=id;
  1263. end;
  1264. function tabstractuniTSymtable.iscurrentunit:boolean;
  1265. begin
  1266. result:=assigned(current_module) and
  1267. (
  1268. (current_module.globalsymtable=self) or
  1269. (current_module.localsymtable=self)
  1270. );
  1271. end;
  1272. {****************************************************************************
  1273. TStaticSymtable
  1274. ****************************************************************************}
  1275. constructor tstaticsymtable.create(const n : string;id:word);
  1276. begin
  1277. inherited create(n,id);
  1278. symtabletype:=staticsymtable;
  1279. symtablelevel:=main_program_level;
  1280. end;
  1281. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1282. begin
  1283. inherited ppuload(ppufile);
  1284. { now we can deref the syms and defs }
  1285. deref;
  1286. end;
  1287. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1288. begin
  1289. inherited ppuwrite(ppufile);
  1290. end;
  1291. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1292. var
  1293. hsym : tsym;
  1294. begin
  1295. result:=false;
  1296. hsym:=tsym(FindWithHash(hashedid));
  1297. if assigned(hsym) then
  1298. begin
  1299. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1300. unit, the unit can then not be accessed anymore using
  1301. <unit>.<id>, so we can hide the symbol }
  1302. if (m_delphi in current_settings.modeswitches) and
  1303. (hsym.typ=symconst.unitsym) then
  1304. HideSym(hsym)
  1305. else
  1306. DuplicateSym(hashedid,sym,hsym);
  1307. result:=true;
  1308. exit;
  1309. end;
  1310. if (current_module.localsymtable=self) and
  1311. assigned(current_module.globalsymtable) then
  1312. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1313. end;
  1314. {****************************************************************************
  1315. TGlobalSymtable
  1316. ****************************************************************************}
  1317. constructor tglobalsymtable.create(const n : string;id:word);
  1318. begin
  1319. inherited create(n,id);
  1320. symtabletype:=globalsymtable;
  1321. symtablelevel:=main_program_level;
  1322. end;
  1323. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1324. begin
  1325. inherited ppuload(ppufile);
  1326. { now we can deref the syms and defs }
  1327. deref;
  1328. end;
  1329. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1330. begin
  1331. { write the symtable entries }
  1332. inherited ppuwrite(ppufile);
  1333. end;
  1334. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1335. var
  1336. hsym : tsym;
  1337. begin
  1338. result:=false;
  1339. hsym:=tsym(FindWithHash(hashedid));
  1340. if assigned(hsym) then
  1341. begin
  1342. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1343. unit, the unit can then not be accessed anymore using
  1344. <unit>.<id>, so we can hide the symbol }
  1345. if (m_delphi in current_settings.modeswitches) and
  1346. (hsym.typ=symconst.unitsym) then
  1347. HideSym(hsym)
  1348. else
  1349. DuplicateSym(hashedid,sym,hsym);
  1350. result:=true;
  1351. exit;
  1352. end;
  1353. end;
  1354. {****************************************************************************
  1355. TWITHSYMTABLE
  1356. ****************************************************************************}
  1357. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1358. begin
  1359. inherited create('');
  1360. symtabletype:=withsymtable;
  1361. withrefnode:=refnode;
  1362. { Replace SymList with the passed symlist }
  1363. SymList.free;
  1364. SymList:=ASymList;
  1365. defowner:=aowner;
  1366. end;
  1367. destructor twithsymtable.destroy;
  1368. begin
  1369. withrefnode.free;
  1370. { Disable SymList because we don't Own it }
  1371. SymList:=nil;
  1372. inherited destroy;
  1373. end;
  1374. procedure twithsymtable.clear;
  1375. begin
  1376. { remove no entry from a withsymtable as it is only a pointer to the
  1377. recorddef or objectdef symtable }
  1378. end;
  1379. procedure twithsymtable.insertdef(def:TDefEntry);
  1380. begin
  1381. { Definitions can't be registered in the withsymtable
  1382. because the withsymtable is removed after the with block.
  1383. We can't easily solve it here because the next symtable in the
  1384. stack is not known. }
  1385. internalerror(200602046);
  1386. end;
  1387. {****************************************************************************
  1388. TSTT_ExceptionSymtable
  1389. ****************************************************************************}
  1390. constructor tstt_excepTSymtable.create;
  1391. begin
  1392. inherited create('');
  1393. symtabletype:=stt_excepTSymtable;
  1394. end;
  1395. {****************************************************************************
  1396. TMacroSymtable
  1397. ****************************************************************************}
  1398. constructor tmacrosymtable.create(exported: boolean);
  1399. begin
  1400. inherited create('');
  1401. if exported then
  1402. symtabletype:=exportedmacrosymtable
  1403. else
  1404. symtabletype:=localmacrosymtable;
  1405. symtablelevel:=main_program_level;
  1406. end;
  1407. {****************************************************************************
  1408. TEnumSymtable
  1409. ****************************************************************************}
  1410. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1411. var
  1412. value: longint;
  1413. def: tenumdef;
  1414. begin
  1415. // defowner = nil only when we are loading from ppu
  1416. if defowner<>nil then
  1417. begin
  1418. { First entry? Then we need to set the minval }
  1419. value:=tenumsym(sym).value;
  1420. def:=tenumdef(defowner);
  1421. if SymList.count=0 then
  1422. begin
  1423. if value>0 then
  1424. def.has_jumps:=true;
  1425. def.setmin(value);
  1426. def.setmax(value);
  1427. end
  1428. else
  1429. begin
  1430. { check for jumps }
  1431. if value>def.max+1 then
  1432. def.has_jumps:=true;
  1433. { update low and high }
  1434. if def.min>value then
  1435. def.setmin(value);
  1436. if def.max<value then
  1437. def.setmax(value);
  1438. end;
  1439. end;
  1440. inherited insert(sym, checkdup);
  1441. end;
  1442. constructor tenumsymtable.create(adefowner: tdef);
  1443. begin
  1444. inherited Create('');
  1445. symtabletype:=enumsymtable;
  1446. defowner:=adefowner;
  1447. end;
  1448. {****************************************************************************
  1449. TArraySymtable
  1450. ****************************************************************************}
  1451. procedure tarraysymtable.insertdef(def: TDefEntry);
  1452. begin
  1453. { Enums must also be available outside the record scope,
  1454. insert in the owner of this symtable }
  1455. if def.typ=enumdef then
  1456. defowner.owner.insertdef(def)
  1457. else
  1458. inherited insertdef(def);
  1459. end;
  1460. constructor tarraysymtable.create(adefowner: tdef);
  1461. begin
  1462. inherited Create('');
  1463. symtabletype:=arraysymtable;
  1464. defowner:=adefowner;
  1465. end;
  1466. {*****************************************************************************
  1467. Helper Routines
  1468. *****************************************************************************}
  1469. function FullTypeName(def,otherdef:tdef):string;
  1470. var
  1471. s1,s2 : string;
  1472. begin
  1473. if def.typ in [objectdef,recorddef] then
  1474. s1:=tabstractrecorddef(def).RttiName
  1475. else
  1476. s1:=def.typename;
  1477. { When the names are the same try to include the unit name }
  1478. if assigned(otherdef) and
  1479. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1480. begin
  1481. s2:=otherdef.typename;
  1482. if upper(s1)=upper(s2) then
  1483. s1:=def.owner.realname^+'.'+s1;
  1484. end;
  1485. FullTypeName:=s1;
  1486. end;
  1487. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1488. begin
  1489. result:='';
  1490. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1491. begin
  1492. if (result='') then
  1493. result:=symtable.name^
  1494. else
  1495. result:=symtable.name^+delimiter+result;
  1496. symtable:=symtable.defowner.owner;
  1497. end;
  1498. end;
  1499. procedure incompatibletypes(def1,def2:tdef);
  1500. begin
  1501. { When there is an errordef there is already an error message show }
  1502. if (def2.typ=errordef) or
  1503. (def1.typ=errordef) then
  1504. exit;
  1505. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1506. end;
  1507. procedure hidesym(sym:TSymEntry);
  1508. begin
  1509. sym.realname:='$hidden'+sym.realname;
  1510. tsym(sym).visibility:=vis_hidden;
  1511. end;
  1512. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1513. var
  1514. st : TSymtable;
  1515. begin
  1516. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1517. { Write hint where the original symbol was found }
  1518. st:=finduniTSymtable(origsym.owner);
  1519. with tsym(origsym).fileinfo do
  1520. begin
  1521. if assigned(st) and
  1522. (st.symtabletype=globalsymtable) and
  1523. st.iscurrentunit then
  1524. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1525. else if assigned(st.name) then
  1526. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1527. end;
  1528. { Rename duplicate sym to an unreachable name, but it can be
  1529. inserted in the symtable without errors }
  1530. inc(dupnr);
  1531. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1532. if assigned(dupsym) then
  1533. include(tsym(dupsym).symoptions,sp_implicitrename);
  1534. end;
  1535. {*****************************************************************************
  1536. Search
  1537. *****************************************************************************}
  1538. procedure addsymref(sym:tsym);
  1539. begin
  1540. { symbol uses count }
  1541. sym.IncRefCount;
  1542. { unit uses count }
  1543. if assigned(current_module) and
  1544. (sym.owner.symtabletype=globalsymtable) then
  1545. begin
  1546. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1547. internalerror(200501152);
  1548. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1549. end;
  1550. end;
  1551. function is_owned_by(childdef,ownerdef:tabstractrecorddef):boolean;
  1552. begin
  1553. result:=childdef=ownerdef;
  1554. if not result and (childdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1555. result:=is_owned_by(tabstractrecorddef(childdef.owner.defowner),ownerdef);
  1556. end;
  1557. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1558. var
  1559. symownerdef : tabstractrecorddef;
  1560. begin
  1561. result:=false;
  1562. { Get objdectdef owner of the symtable for the is_related checks }
  1563. if not assigned(symst) or
  1564. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1565. internalerror(200810285);
  1566. symownerdef:=tabstractrecorddef(symst.defowner);
  1567. case symvisibility of
  1568. vis_private :
  1569. begin
  1570. { private symbols are allowed when we are in the same
  1571. module as they are defined }
  1572. result:=(
  1573. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1574. (symownerdef.owner.iscurrentunit)
  1575. ) or
  1576. ( // the case of specialize inside the generic declaration
  1577. (symownerdef.owner.symtabletype = objectsymtable) and
  1578. (
  1579. assigned(current_structdef) and
  1580. (
  1581. (current_structdef=symownerdef) or
  1582. (current_structdef.owner.iscurrentunit)
  1583. )
  1584. ) or
  1585. (
  1586. not assigned(current_structdef) and
  1587. (symownerdef.owner.iscurrentunit)
  1588. )
  1589. );
  1590. end;
  1591. vis_strictprivate :
  1592. begin
  1593. result:=assigned(current_structdef) and
  1594. is_owned_by(current_structdef,symownerdef);
  1595. end;
  1596. vis_strictprotected :
  1597. begin
  1598. result:=assigned(current_structdef) and
  1599. (current_structdef.is_related(symownerdef) or
  1600. is_owned_by(current_structdef,symownerdef));
  1601. end;
  1602. vis_protected :
  1603. begin
  1604. { protected symbols are visible in the module that defines them and
  1605. also visible to related objects. The related object must be defined
  1606. in the current module }
  1607. result:=(
  1608. (
  1609. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1610. (symownerdef.owner.iscurrentunit)
  1611. ) or
  1612. (
  1613. assigned(contextobjdef) and
  1614. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1615. (contextobjdef.owner.iscurrentunit) and
  1616. contextobjdef.is_related(symownerdef)
  1617. ) or
  1618. ( // the case of specialize inside the generic declaration
  1619. (symownerdef.owner.symtabletype = objectsymtable) and
  1620. (
  1621. assigned(current_structdef) and
  1622. (
  1623. (current_structdef=symownerdef) or
  1624. (current_structdef.owner.iscurrentunit)
  1625. )
  1626. ) or
  1627. (
  1628. not assigned(current_structdef) and
  1629. (symownerdef.owner.iscurrentunit)
  1630. )
  1631. )
  1632. );
  1633. end;
  1634. vis_public,
  1635. vis_published :
  1636. result:=true;
  1637. end;
  1638. end;
  1639. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  1640. begin
  1641. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1642. end;
  1643. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  1644. var
  1645. i : longint;
  1646. pd : tprocdef;
  1647. begin
  1648. if sym.typ=procsym then
  1649. begin
  1650. { A procsym is visible, when there is at least one of the procdefs visible }
  1651. result:=false;
  1652. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1653. begin
  1654. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1655. if (pd.owner=sym.owner) and
  1656. is_visible_for_object(pd,contextobjdef) then
  1657. begin
  1658. result:=true;
  1659. exit;
  1660. end;
  1661. end;
  1662. end
  1663. else
  1664. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1665. end;
  1666. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1667. var
  1668. hashedid : THashedIDString;
  1669. contextstructdef : tabstractrecorddef;
  1670. stackitem : psymtablestackitem;
  1671. begin
  1672. result:=false;
  1673. hashedid.id:=s;
  1674. stackitem:=symtablestack.stack;
  1675. while assigned(stackitem) do
  1676. begin
  1677. srsymtable:=stackitem^.symtable;
  1678. if (srsymtable.symtabletype=objectsymtable) then
  1679. begin
  1680. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,hs_searchfirst) then
  1681. begin
  1682. result:=true;
  1683. exit;
  1684. end;
  1685. end
  1686. else
  1687. begin
  1688. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1689. if assigned(srsym) then
  1690. begin
  1691. { use the class from withsymtable only when it is
  1692. defined in this unit }
  1693. if (srsymtable.symtabletype=withsymtable) and
  1694. assigned(srsymtable.defowner) and
  1695. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  1696. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1697. (srsymtable.defowner.owner.iscurrentunit) then
  1698. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  1699. else
  1700. contextstructdef:=current_structdef;
  1701. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1702. is_visible_for_object(srsym,contextstructdef) then
  1703. begin
  1704. { we need to know if a procedure references symbols
  1705. in the static symtable, because then it can't be
  1706. inlined from outside this unit }
  1707. if assigned(current_procinfo) and
  1708. (srsym.owner.symtabletype=staticsymtable) then
  1709. include(current_procinfo.flags,pi_uses_static_symtable);
  1710. addsymref(srsym);
  1711. result:=true;
  1712. exit;
  1713. end;
  1714. end;
  1715. end;
  1716. stackitem:=stackitem^.next;
  1717. end;
  1718. srsym:=nil;
  1719. srsymtable:=nil;
  1720. end;
  1721. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1722. var
  1723. hashedid : THashedIDString;
  1724. stackitem : psymtablestackitem;
  1725. classh : tobjectdef;
  1726. begin
  1727. result:=false;
  1728. hashedid.id:=s;
  1729. stackitem:=symtablestack.stack;
  1730. while assigned(stackitem) do
  1731. begin
  1732. {
  1733. It is not possible to have type symbols in:
  1734. parameters
  1735. Exception are classes, objects, records, generic definitions and specializations
  1736. that have the parameterized types inserted in the symtable.
  1737. }
  1738. srsymtable:=stackitem^.symtable;
  1739. if (srsymtable.symtabletype=ObjectSymtable) then
  1740. begin
  1741. classh:=tobjectdef(srsymtable.defowner);
  1742. while assigned(classh) do
  1743. begin
  1744. srsymtable:=classh.symtable;
  1745. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1746. if assigned(srsym) and
  1747. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1748. is_visible_for_object(srsym,current_structdef) then
  1749. begin
  1750. addsymref(srsym);
  1751. result:=true;
  1752. exit;
  1753. end;
  1754. classh:=classh.childof;
  1755. end;
  1756. end
  1757. else
  1758. begin
  1759. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1760. if assigned(srsym) and
  1761. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1762. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  1763. begin
  1764. { we need to know if a procedure references symbols
  1765. in the static symtable, because then it can't be
  1766. inlined from outside this unit }
  1767. if assigned(current_procinfo) and
  1768. (srsym.owner.symtabletype=staticsymtable) then
  1769. include(current_procinfo.flags,pi_uses_static_symtable);
  1770. addsymref(srsym);
  1771. result:=true;
  1772. exit;
  1773. end;
  1774. end;
  1775. stackitem:=stackitem^.next;
  1776. end;
  1777. result:=false;
  1778. srsym:=nil;
  1779. srsymtable:=nil;
  1780. end;
  1781. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1782. var
  1783. pmod : tmodule;
  1784. begin
  1785. pmod:=tmodule(pm);
  1786. result:=false;
  1787. if assigned(pmod.globalsymtable) then
  1788. begin
  1789. srsym:=tsym(pmod.globalsymtable.Find(s));
  1790. if assigned(srsym) then
  1791. begin
  1792. srsymtable:=pmod.globalsymtable;
  1793. addsymref(srsym);
  1794. result:=true;
  1795. exit;
  1796. end;
  1797. end;
  1798. { If the module is the current unit we also need
  1799. to search the local symtable }
  1800. if (pmod=current_module) and
  1801. assigned(pmod.localsymtable) then
  1802. begin
  1803. srsym:=tsym(pmod.localsymtable.Find(s));
  1804. if assigned(srsym) then
  1805. begin
  1806. srsymtable:=pmod.localsymtable;
  1807. addsymref(srsym);
  1808. result:=true;
  1809. exit;
  1810. end;
  1811. end;
  1812. srsym:=nil;
  1813. srsymtable:=nil;
  1814. end;
  1815. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1816. var
  1817. stackitem : psymtablestackitem;
  1818. begin
  1819. result:=false;
  1820. stackitem:=symtablestack.stack;
  1821. while assigned(stackitem) do
  1822. begin
  1823. srsymtable:=stackitem^.symtable;
  1824. if (srsymtable.symtabletype=globalsymtable) and
  1825. (srsymtable.name^=unitname) then
  1826. begin
  1827. srsym:=tsym(srsymtable.find(symname));
  1828. if not assigned(srsym) then
  1829. break;
  1830. result:=true;
  1831. exit;
  1832. end;
  1833. stackitem:=stackitem^.next;
  1834. end;
  1835. { If the module is the current unit we also need
  1836. to search the local symtable }
  1837. if (current_module.globalsymtable=srsymtable) and
  1838. assigned(current_module.localsymtable) then
  1839. begin
  1840. srsymtable:=current_module.localsymtable;
  1841. srsym:=tsym(srsymtable.find(symname));
  1842. if assigned(srsym) then
  1843. begin
  1844. result:=true;
  1845. exit;
  1846. end;
  1847. end;
  1848. end;
  1849. function find_real_objcclass_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  1850. var
  1851. hashedid : THashedIDString;
  1852. stackitem : psymtablestackitem;
  1853. srsymtable : tsymtable;
  1854. srsym : tsym;
  1855. begin
  1856. { not a formal definition -> return it }
  1857. if not(oo_is_formal in pd.objectoptions) then
  1858. begin
  1859. result:=pd;
  1860. exit;
  1861. end;
  1862. hashedid.id:=pd.typesym.name;
  1863. stackitem:=symtablestack.stack;
  1864. while assigned(stackitem) do
  1865. begin
  1866. srsymtable:=stackitem^.symtable;
  1867. { ObjC classes can't appear in generics or as nested class
  1868. definitions }
  1869. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) then
  1870. begin
  1871. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1872. if assigned(srsym) and
  1873. (srsym.typ=typesym) and
  1874. is_objcclass(ttypesym(srsym).typedef) and
  1875. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  1876. begin
  1877. { the external name for the formal and the real definition must match }
  1878. if tobjectdef(ttypesym(srsym).typedef).objextname^<>pd.objextname^ then
  1879. begin
  1880. Message2(sym_e_external_class_name_mismatch1,pd.objextname^,pd.typename);
  1881. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,tobjectdef(ttypesym(srsym).typedef).objextname^);
  1882. end;
  1883. result:=tobjectdef(ttypesym(srsym).typedef);
  1884. if assigned(current_procinfo) and
  1885. (srsym.owner.symtabletype=staticsymtable) then
  1886. include(current_procinfo.flags,pi_uses_static_symtable);
  1887. addsymref(srsym);
  1888. exit;
  1889. end;
  1890. end;
  1891. stackitem:=stackitem^.next;
  1892. end;
  1893. { nothing found: optionally give an error and return the original
  1894. (empty) one }
  1895. if erroronfailure then
  1896. Message1(sym_e_objc_formal_class_not_resolved,pd.objrealname^);
  1897. result:=pd;
  1898. end;
  1899. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:thelpersearch):boolean;
  1900. var
  1901. hashedid : THashedIDString;
  1902. exdef : tabstractrecorddef;
  1903. orgclass : tobjectdef;
  1904. i : longint;
  1905. begin
  1906. { search for a class helper method first if this is an Object Pascal
  1907. class }
  1908. if is_class(classh) and (searchhelper = hs_searchfirst) then
  1909. begin
  1910. result:=search_objectpascal_class_helper(classh,contextclassh,s,srsym,srsymtable);
  1911. if result then
  1912. begin
  1913. { if the procsym is overloaded we need to use the "original"
  1914. symbol; the helper symbol will be find when searching for
  1915. overloads }
  1916. if (srsym.typ<>procsym) or
  1917. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  1918. Exit;
  1919. end;
  1920. end;
  1921. orgclass:=classh;
  1922. { in case this is a formal objcclass, first find the real definition }
  1923. if assigned(classh) then
  1924. begin
  1925. if (oo_is_formal in classh.objectoptions) then
  1926. classh:=find_real_objcclass_definition(classh,true);
  1927. { The contextclassh is used for visibility. The classh must be equal to
  1928. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1929. parent or a class helper. }
  1930. if not (contextclassh.is_related(classh) or
  1931. (contextclassh.extendeddef=classh)) then
  1932. internalerror(200811161);
  1933. end;
  1934. result:=false;
  1935. hashedid.id:=s;
  1936. { an Objective-C protocol can inherit from multiple other protocols
  1937. -> uses ImplementedInterfaces instead }
  1938. if is_objcprotocol(classh) then
  1939. begin
  1940. srsymtable:=classh.symtable;
  1941. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1942. if assigned(srsym) and
  1943. is_visible_for_object(srsym,contextclassh) then
  1944. begin
  1945. addsymref(srsym);
  1946. result:=true;
  1947. exit;
  1948. end;
  1949. for i:=0 to classh.ImplementedInterfaces.count-1 do
  1950. begin
  1951. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,hs_nosearch) then
  1952. begin
  1953. result:=true;
  1954. exit;
  1955. end;
  1956. end;
  1957. end
  1958. else
  1959. if is_objectpascal_helper(classh) then
  1960. begin
  1961. { helpers have their own obscure search logic... }
  1962. result:=searchsym_in_helper(classh,contextclassh,s,srsym,srsymtable,false);
  1963. if result then
  1964. exit;
  1965. end
  1966. else
  1967. begin
  1968. while assigned(classh) do
  1969. begin
  1970. srsymtable:=classh.symtable;
  1971. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1972. if assigned(srsym) and
  1973. is_visible_for_object(srsym,contextclassh) then
  1974. begin
  1975. addsymref(srsym);
  1976. result:=true;
  1977. exit;
  1978. end;
  1979. classh:=classh.childof;
  1980. end;
  1981. end;
  1982. if is_objcclass(orgclass) then
  1983. result:=search_class_helper(orgclass,s,srsym,srsymtable)
  1984. else
  1985. { this is currently not used, so maybe this can be removed again... }
  1986. if is_class(orgclass) and (searchhelper = hs_searchlast) then
  1987. result:=search_objectpascal_class_helper(orgclass,contextclassh,s,srsym,srsymtable)
  1988. else
  1989. begin
  1990. srsym:=nil;
  1991. srsymtable:=nil;
  1992. end;
  1993. end;
  1994. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1995. var
  1996. hashedid : THashedIDString;
  1997. begin
  1998. hashedid.id:=s;
  1999. srsymtable:=recordh.symtable;
  2000. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2001. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2002. begin
  2003. addsymref(srsym);
  2004. result:=true;
  2005. exit;
  2006. end;
  2007. srsym:=nil;
  2008. srsymtable:=nil;
  2009. end;
  2010. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2011. var
  2012. def : tdef;
  2013. i : longint;
  2014. begin
  2015. { in case this is a formal objcclass, first find the real definition }
  2016. if assigned(classh) and
  2017. (oo_is_formal in classh.objectoptions) then
  2018. classh:=find_real_objcclass_definition(classh,true);
  2019. result:=false;
  2020. def:=nil;
  2021. while assigned(classh) do
  2022. begin
  2023. for i:=0 to classh.symtable.DefList.Count-1 do
  2024. begin
  2025. def:=tstoreddef(classh.symtable.DefList[i]);
  2026. { Find also all hidden private methods to
  2027. be compatible with delphi, see tw6203 (PFV) }
  2028. if (def.typ=procdef) and
  2029. (po_msgint in tprocdef(def).procoptions) and
  2030. (tprocdef(def).messageinf.i=msgid) then
  2031. begin
  2032. srdef:=def;
  2033. srsym:=tprocdef(def).procsym;
  2034. srsymtable:=classh.symtable;
  2035. addsymref(srsym);
  2036. result:=true;
  2037. exit;
  2038. end;
  2039. end;
  2040. classh:=classh.childof;
  2041. end;
  2042. srdef:=nil;
  2043. srsym:=nil;
  2044. srsymtable:=nil;
  2045. end;
  2046. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2047. var
  2048. def : tdef;
  2049. i : longint;
  2050. begin
  2051. { in case this is a formal objcclass, first find the real definition }
  2052. if assigned(classh) and
  2053. (oo_is_formal in classh.objectoptions) then
  2054. classh:=find_real_objcclass_definition(classh,true);
  2055. result:=false;
  2056. def:=nil;
  2057. while assigned(classh) do
  2058. begin
  2059. for i:=0 to classh.symtable.DefList.Count-1 do
  2060. begin
  2061. def:=tstoreddef(classh.symtable.DefList[i]);
  2062. { Find also all hidden private methods to
  2063. be compatible with delphi, see tw6203 (PFV) }
  2064. if (def.typ=procdef) and
  2065. (po_msgstr in tprocdef(def).procoptions) and
  2066. (tprocdef(def).messageinf.str^=s) then
  2067. begin
  2068. srsym:=tprocdef(def).procsym;
  2069. srsymtable:=classh.symtable;
  2070. addsymref(srsym);
  2071. result:=true;
  2072. exit;
  2073. end;
  2074. end;
  2075. classh:=classh.childof;
  2076. end;
  2077. srsym:=nil;
  2078. srsymtable:=nil;
  2079. end;
  2080. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2081. var
  2082. hashedid : THashedIDString;
  2083. tmpsrsym : tsym;
  2084. tmpsrsymtable : tsymtable;
  2085. found : boolean;
  2086. begin
  2087. if not is_objectpascal_helper(classh) then
  2088. Internalerror(2011030101);
  2089. hashedid.id:=s;
  2090. if is_class(classh.extendeddef) then
  2091. begin
  2092. { in a class helper things are a bit more complex:
  2093. 1. search the symbol in the helper (if not "inherited")
  2094. 2. search the symbol in the extended type
  2095. 3. search the symbol in the parent helpers
  2096. 4. search the symbol in the parents of the extended type
  2097. Thus we search in the hierarchy of the extended type first and
  2098. check whether the returned symtable (if any) belongs to the
  2099. extended type or one of its parents. If the latter we first
  2100. check whether one of the helper parents contains a suitable
  2101. symbol
  2102. }
  2103. if not aHasInherited then
  2104. begin
  2105. { search in the helper itself }
  2106. srsymtable:=classh.symtable;
  2107. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2108. if assigned(srsym) and
  2109. is_visible_for_object(srsym,contextclassh) then
  2110. begin
  2111. addsymref(srsym);
  2112. result:=true;
  2113. exit;
  2114. end;
  2115. end;
  2116. { search in the hierarchy of the extended class }
  2117. found:=searchsym_in_class(tobjectdef(classh.extendeddef),tobjectdef(classh.extendeddef),s,tmpsrsym,tmpsrsymtable,hs_nosearch);
  2118. if not found then
  2119. begin
  2120. if assigned(classh.childof) then
  2121. begin
  2122. { the symbol isn't in the extended type's hierarchy,
  2123. so search in the parents of the helper }
  2124. result:=searchsym_in_class(classh.childof,contextclassh,s,srsym,srsymtable,hs_nosearch);
  2125. if result and is_visible_for_object(srsym,contextclassh) then
  2126. addsymref(srsym);
  2127. end;
  2128. end
  2129. else
  2130. begin
  2131. if (tmpsrsymtable.defowner=classh.extendeddef) and
  2132. is_visible_for_object(tmpsrsym,contextclassh) then
  2133. begin
  2134. { the symbol was found in the extended type }
  2135. result:=true;
  2136. srsym:=tmpsrsym;
  2137. srsymtable:=tmpsrsymtable;
  2138. exit;
  2139. end
  2140. else
  2141. begin
  2142. result:=false;
  2143. { search in the helper's parents first }
  2144. if assigned(classh.childof) then
  2145. result:=searchsym_in_class(classh.childof,contextclassh,s,srsym,srsymtable,hs_nosearch);
  2146. if not result then
  2147. begin
  2148. { we use the symbol found in one of the extended
  2149. type's ancestors }
  2150. result:=true;
  2151. srsym:=tmpsrsym;
  2152. srsymtable:=tmpsrsymtable;
  2153. end;
  2154. if assigned(srsym) and is_visible_for_object(srsym,contextclassh) then
  2155. addsymref(srsym);
  2156. end;
  2157. end;
  2158. end
  2159. else if is_record(classh.extendeddef) and
  2160. searchsym_in_record(classh.extendeddef, s, srsym, srsymtable) then
  2161. begin
  2162. result:=true;
  2163. exit;
  2164. end;
  2165. end;
  2166. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2167. var
  2168. sym : Tprocsym;
  2169. hashedid : THashedIDString;
  2170. curreq,
  2171. besteq : tequaltype;
  2172. currpd,
  2173. bestpd : tprocdef;
  2174. stackitem : psymtablestackitem;
  2175. begin
  2176. hashedid.id:=overloaded_names[assignment_type];
  2177. besteq:=te_incompatible;
  2178. bestpd:=nil;
  2179. stackitem:=symtablestack.stack;
  2180. while assigned(stackitem) do
  2181. begin
  2182. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2183. if sym<>nil then
  2184. begin
  2185. if sym.typ<>procsym then
  2186. internalerror(200402031);
  2187. { if the source type is an alias then this is only the second choice,
  2188. if you mess with this code, check tw4093 }
  2189. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2190. if curreq>besteq then
  2191. begin
  2192. besteq:=curreq;
  2193. bestpd:=currpd;
  2194. if (besteq=te_exact) then
  2195. break;
  2196. end;
  2197. end;
  2198. stackitem:=stackitem^.next;
  2199. end;
  2200. result:=bestpd;
  2201. end;
  2202. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2203. begin
  2204. { search record/object symtable first for a suitable operator }
  2205. if from_def.typ in [recorddef,objectdef] then
  2206. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2207. if to_def.typ in [recorddef,objectdef] then
  2208. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2209. { if type conversion is explicit then search first for explicit
  2210. operator overload and if not found then use implicit operator }
  2211. if explicit then
  2212. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2213. else
  2214. result:=nil;
  2215. if result=nil then
  2216. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2217. { restore symtable stack }
  2218. if to_def.typ in [recorddef,objectdef] then
  2219. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2220. if from_def.typ in [recorddef,objectdef] then
  2221. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2222. end;
  2223. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2224. var
  2225. sym : Tprocsym;
  2226. hashedid : THashedIDString;
  2227. curreq,
  2228. besteq : tequaltype;
  2229. currpd,
  2230. bestpd : tprocdef;
  2231. stackitem : psymtablestackitem;
  2232. begin
  2233. hashedid.id:='enumerator';
  2234. besteq:=te_incompatible;
  2235. bestpd:=nil;
  2236. stackitem:=symtablestack.stack;
  2237. while assigned(stackitem) do
  2238. begin
  2239. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2240. if sym<>nil then
  2241. begin
  2242. if sym.typ<>procsym then
  2243. internalerror(200910241);
  2244. { if the source type is an alias then this is only the second choice,
  2245. if you mess with this code, check tw4093 }
  2246. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2247. if curreq>besteq then
  2248. begin
  2249. besteq:=curreq;
  2250. bestpd:=currpd;
  2251. if (besteq=te_exact) then
  2252. break;
  2253. end;
  2254. end;
  2255. stackitem:=stackitem^.next;
  2256. end;
  2257. result:=bestpd;
  2258. end;
  2259. function search_system_type(const s: TIDString): ttypesym;
  2260. var
  2261. sym : tsym;
  2262. begin
  2263. sym:=tsym(systemunit.Find(s));
  2264. if not assigned(sym) or
  2265. (sym.typ<>typesym) then
  2266. cgmessage1(cg_f_unknown_system_type,s);
  2267. result:=ttypesym(sym);
  2268. end;
  2269. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2270. var
  2271. srsymtable: tsymtable;
  2272. sym: tsym;
  2273. begin
  2274. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2275. (sym.typ=typesym) then
  2276. begin
  2277. result:=ttypesym(sym);
  2278. exit;
  2279. end
  2280. else
  2281. begin
  2282. if throwerror then
  2283. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  2284. result:=nil;
  2285. end;
  2286. end;
  2287. function search_last_objectpascal_helper(pd : tabstractrecorddef;out odef : tobjectdef):boolean;
  2288. var
  2289. stackitem : psymtablestackitem;
  2290. i : integer;
  2291. srsymtable : tsymtable;
  2292. begin
  2293. result:=false;
  2294. stackitem:=symtablestack.stack;
  2295. while assigned(stackitem) do
  2296. begin
  2297. srsymtable:=stackitem^.symtable;
  2298. { only check symtables that contain a class helper }
  2299. if (srsymtable.symtabletype in [staticsymtable,globalsymtable]) and
  2300. (sto_has_classhelper in srsymtable.tableoptions) then
  2301. begin
  2302. { we need to search from last to first }
  2303. for i:=srsymtable.symlist.count-1 downto 0 do
  2304. begin
  2305. if not (srsymtable.symlist[i] is ttypesym) then
  2306. continue;
  2307. if not is_objectpascal_helper(ttypesym(srsymtable.symlist[i]).typedef) then
  2308. continue;
  2309. odef:=tobjectdef(ttypesym(srsymtable.symlist[i]).typedef);
  2310. { does the class helper extend the correct class? }
  2311. result:=odef.extendeddef=pd;
  2312. if result then
  2313. exit
  2314. else
  2315. odef:=nil;
  2316. end;
  2317. end;
  2318. stackitem:=stackitem^.next;
  2319. end;
  2320. end;
  2321. function search_objectpascal_class_helper(pd,contextclassh : tobjectdef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2322. var
  2323. hashedid : THashedIDString;
  2324. classh : tobjectdef;
  2325. i : integer;
  2326. pdef : tprocdef;
  2327. begin
  2328. result:=false;
  2329. { if there is no class helper for the class then there is no need to
  2330. search further }
  2331. if not search_last_objectpascal_helper(pd,classh) then
  2332. exit;
  2333. hashedid.id:=s;
  2334. repeat
  2335. srsymtable:=classh.symtable;
  2336. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2337. if srsym<>nil then
  2338. begin
  2339. if srsym.typ=propertysym then
  2340. begin
  2341. result:=true;
  2342. exit;
  2343. end;
  2344. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2345. begin
  2346. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2347. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2348. continue;
  2349. { we need to know if a procedure references symbols
  2350. in the static symtable, because then it can't be
  2351. inlined from outside this unit }
  2352. if assigned(current_procinfo) and
  2353. (srsym.owner.symtabletype=staticsymtable) then
  2354. include(current_procinfo.flags,pi_uses_static_symtable);
  2355. { no need to keep looking. There might be other
  2356. categories that extend this, a parent or child
  2357. class with a method with the same name (either
  2358. overriding this one, or overridden by this one),
  2359. but that doesn't matter as far as the basic
  2360. procsym is concerned.
  2361. }
  2362. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2363. srsymtable:=srsym.owner;
  2364. addsymref(srsym);
  2365. result:=true;
  2366. exit;
  2367. end;
  2368. end;
  2369. { try the class helper parent if available }
  2370. classh:=classh.childof;
  2371. until classh=nil;
  2372. srsym:=nil;
  2373. srsymtable:=nil;
  2374. end;
  2375. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2376. var
  2377. hashedid : THashedIDString;
  2378. stackitem : psymtablestackitem;
  2379. i : longint;
  2380. defowner : tobjectdef;
  2381. begin
  2382. hashedid.id:=class_helper_prefix+s;
  2383. stackitem:=symtablestack.stack;
  2384. while assigned(stackitem) do
  2385. begin
  2386. srsymtable:=stackitem^.symtable;
  2387. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2388. if assigned(srsym) then
  2389. begin
  2390. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2391. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2392. (srsym.typ<>procsym) then
  2393. internalerror(2009111505);
  2394. { check whether this procsym includes a helper for this particular class }
  2395. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2396. begin
  2397. { does pd inherit from (or is the same as) the class
  2398. that this method's category extended?
  2399. Warning: this list contains both category and objcclass methods
  2400. (for id.randommethod), so only check category methods here
  2401. }
  2402. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  2403. if (oo_is_classhelper in defowner.objectoptions) and
  2404. pd.is_related(defowner.childof) then
  2405. begin
  2406. { we need to know if a procedure references symbols
  2407. in the static symtable, because then it can't be
  2408. inlined from outside this unit }
  2409. if assigned(current_procinfo) and
  2410. (srsym.owner.symtabletype=staticsymtable) then
  2411. include(current_procinfo.flags,pi_uses_static_symtable);
  2412. { no need to keep looking. There might be other
  2413. categories that extend this, a parent or child
  2414. class with a method with the same name (either
  2415. overriding this one, or overridden by this one),
  2416. but that doesn't matter as far as the basic
  2417. procsym is concerned.
  2418. }
  2419. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2420. srsymtable:=srsym.owner;
  2421. addsymref(srsym);
  2422. result:=true;
  2423. exit;
  2424. end;
  2425. end;
  2426. end;
  2427. stackitem:=stackitem^.next;
  2428. end;
  2429. srsym:=nil;
  2430. srsymtable:=nil;
  2431. result:=false;
  2432. end;
  2433. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2434. var
  2435. hashedid : THashedIDString;
  2436. stackitem : psymtablestackitem;
  2437. i : longint;
  2438. begin
  2439. hashedid.id:=class_helper_prefix+s;
  2440. stackitem:=symtablestack.stack;
  2441. while assigned(stackitem) do
  2442. begin
  2443. srsymtable:=stackitem^.symtable;
  2444. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2445. if assigned(srsym) then
  2446. begin
  2447. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2448. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2449. (srsym.typ<>procsym) then
  2450. internalerror(2009112005);
  2451. { check whether this procsym includes a helper for this particular class }
  2452. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2453. begin
  2454. { we need to know if a procedure references symbols
  2455. in the static symtable, because then it can't be
  2456. inlined from outside this unit }
  2457. if assigned(current_procinfo) and
  2458. (srsym.owner.symtabletype=staticsymtable) then
  2459. include(current_procinfo.flags,pi_uses_static_symtable);
  2460. { no need to keep looking. There might be other
  2461. methods with the same name, but that doesn't matter
  2462. as far as the basic procsym is concerned.
  2463. }
  2464. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2465. { We need the symtable in which the classhelper-like sym
  2466. is located, not the objectdef. The reason is that the
  2467. callnode will climb the symtablestack until it encounters
  2468. this symtable to start looking for overloads (and it won't
  2469. find the objectsymtable in which this method sym is
  2470. located
  2471. srsymtable:=srsym.owner;
  2472. }
  2473. addsymref(srsym);
  2474. result:=true;
  2475. exit;
  2476. end;
  2477. end;
  2478. stackitem:=stackitem^.next;
  2479. end;
  2480. srsym:=nil;
  2481. srsymtable:=nil;
  2482. result:=false;
  2483. end;
  2484. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  2485. { searches n in symtable of pd and all anchestors }
  2486. var
  2487. hashedid : THashedIDString;
  2488. srsym : tsym;
  2489. orgpd : tabstractrecorddef;
  2490. srsymtable : tsymtable;
  2491. begin
  2492. { in case this is a formal objcclass, first find the real definition }
  2493. if (oo_is_formal in pd.objectoptions) then
  2494. pd:=find_real_objcclass_definition(tobjectdef(pd),true);
  2495. hashedid.id:=s;
  2496. orgpd:=pd;
  2497. while assigned(pd) do
  2498. begin
  2499. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2500. if assigned(srsym) then
  2501. begin
  2502. search_struct_member:=srsym;
  2503. exit;
  2504. end;
  2505. if pd.typ=objectdef then
  2506. pd:=tobjectdef(pd).childof
  2507. else
  2508. pd:=nil;
  2509. end;
  2510. { not found, now look for class helpers }
  2511. if is_objcclass(pd) then
  2512. search_class_helper(tobjectdef(orgpd),s,result,srsymtable)
  2513. else
  2514. result:=nil;
  2515. end;
  2516. function search_macro(const s : string):tsym;
  2517. var
  2518. stackitem : psymtablestackitem;
  2519. hashedid : THashedIDString;
  2520. srsym : tsym;
  2521. begin
  2522. hashedid.id:=s;
  2523. { First search the localmacrosymtable before searching the
  2524. global macrosymtables from the units }
  2525. if assigned(current_module) then
  2526. begin
  2527. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2528. if assigned(srsym) then
  2529. begin
  2530. result:= srsym;
  2531. exit;
  2532. end;
  2533. end;
  2534. stackitem:=macrosymtablestack.stack;
  2535. while assigned(stackitem) do
  2536. begin
  2537. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2538. if assigned(srsym) then
  2539. begin
  2540. result:= srsym;
  2541. exit;
  2542. end;
  2543. stackitem:=stackitem^.next;
  2544. end;
  2545. result:= nil;
  2546. end;
  2547. function defined_macro(const s : string):boolean;
  2548. var
  2549. mac: tmacro;
  2550. begin
  2551. mac:=tmacro(search_macro(s));
  2552. if assigned(mac) then
  2553. begin
  2554. mac.is_used:=true;
  2555. defined_macro:=mac.defined;
  2556. end
  2557. else
  2558. defined_macro:=false;
  2559. end;
  2560. {****************************************************************************
  2561. Object Helpers
  2562. ****************************************************************************}
  2563. function search_default_property(pd : tobjectdef) : tpropertysym;
  2564. { returns the default property of a class, searches also anchestors }
  2565. var
  2566. _defaultprop : tpropertysym;
  2567. begin
  2568. _defaultprop:=nil;
  2569. while assigned(pd) do
  2570. begin
  2571. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2572. if assigned(_defaultprop) then
  2573. break;
  2574. pd:=pd.childof;
  2575. end;
  2576. search_default_property:=_defaultprop;
  2577. end;
  2578. {****************************************************************************
  2579. Macro Helpers
  2580. ****************************************************************************}
  2581. procedure def_system_macro(const name : string);
  2582. var
  2583. mac : tmacro;
  2584. s: string;
  2585. begin
  2586. if name = '' then
  2587. internalerror(2004121202);
  2588. s:= upper(name);
  2589. mac:=tmacro(search_macro(s));
  2590. if not assigned(mac) then
  2591. begin
  2592. mac:=tmacro.create(s);
  2593. if assigned(current_module) then
  2594. current_module.localmacrosymtable.insert(mac)
  2595. else
  2596. initialmacrosymtable.insert(mac);
  2597. end;
  2598. if not mac.defined then
  2599. Message1(parser_c_macro_defined,mac.name);
  2600. mac.defined:=true;
  2601. end;
  2602. procedure set_system_macro(const name, value : string);
  2603. var
  2604. mac : tmacro;
  2605. s: string;
  2606. begin
  2607. if name = '' then
  2608. internalerror(2004121203);
  2609. s:= upper(name);
  2610. mac:=tmacro(search_macro(s));
  2611. if not assigned(mac) then
  2612. begin
  2613. mac:=tmacro.create(s);
  2614. if assigned(current_module) then
  2615. current_module.localmacrosymtable.insert(mac)
  2616. else
  2617. initialmacrosymtable.insert(mac);
  2618. end
  2619. else
  2620. begin
  2621. mac.is_compiler_var:=false;
  2622. if assigned(mac.buftext) then
  2623. freemem(mac.buftext,mac.buflen);
  2624. end;
  2625. Message2(parser_c_macro_set_to,mac.name,value);
  2626. mac.buflen:=length(value);
  2627. getmem(mac.buftext,mac.buflen);
  2628. move(value[1],mac.buftext^,mac.buflen);
  2629. mac.defined:=true;
  2630. end;
  2631. procedure set_system_compvar(const name, value : string);
  2632. var
  2633. mac : tmacro;
  2634. s: string;
  2635. begin
  2636. if name = '' then
  2637. internalerror(2004121204);
  2638. s:= upper(name);
  2639. mac:=tmacro(search_macro(s));
  2640. if not assigned(mac) then
  2641. begin
  2642. mac:=tmacro.create(s);
  2643. mac.is_compiler_var:=true;
  2644. if assigned(current_module) then
  2645. current_module.localmacrosymtable.insert(mac)
  2646. else
  2647. initialmacrosymtable.insert(mac);
  2648. end
  2649. else
  2650. begin
  2651. mac.is_compiler_var:=true;
  2652. if assigned(mac.buftext) then
  2653. freemem(mac.buftext,mac.buflen);
  2654. end;
  2655. Message2(parser_c_macro_set_to,mac.name,value);
  2656. mac.buflen:=length(value);
  2657. getmem(mac.buftext,mac.buflen);
  2658. move(value[1],mac.buftext^,mac.buflen);
  2659. mac.defined:=true;
  2660. end;
  2661. procedure undef_system_macro(const name : string);
  2662. var
  2663. mac : tmacro;
  2664. s: string;
  2665. begin
  2666. if name = '' then
  2667. internalerror(2004121205);
  2668. s:= upper(name);
  2669. mac:=tmacro(search_macro(s));
  2670. if not assigned(mac) then
  2671. {If not found, then it's already undefined.}
  2672. else
  2673. begin
  2674. if mac.defined then
  2675. Message1(parser_c_macro_undefined,mac.name);
  2676. mac.defined:=false;
  2677. mac.is_compiler_var:=false;
  2678. { delete old definition }
  2679. if assigned(mac.buftext) then
  2680. begin
  2681. freemem(mac.buftext,mac.buflen);
  2682. mac.buftext:=nil;
  2683. end;
  2684. end;
  2685. end;
  2686. {$ifdef UNITALIASES}
  2687. {****************************************************************************
  2688. TUNIT_ALIAS
  2689. ****************************************************************************}
  2690. constructor tunit_alias.create(const n:string);
  2691. var
  2692. i : longint;
  2693. begin
  2694. i:=pos('=',n);
  2695. if i=0 then
  2696. fail;
  2697. inherited createname(Copy(n,1,i-1));
  2698. newname:=stringdup(Copy(n,i+1,255));
  2699. end;
  2700. destructor tunit_alias.destroy;
  2701. begin
  2702. stringdispose(newname);
  2703. inherited destroy;
  2704. end;
  2705. procedure addunitalias(const n:string);
  2706. begin
  2707. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2708. end;
  2709. function getunitalias(const n:string):string;
  2710. var
  2711. p : punit_alias;
  2712. begin
  2713. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2714. if assigned(p) then
  2715. getunitalias:=punit_alias(p).newname^
  2716. else
  2717. getunitalias:=n;
  2718. end;
  2719. {$endif UNITALIASES}
  2720. {****************************************************************************
  2721. Init/Done Symtable
  2722. ****************************************************************************}
  2723. procedure InitSymtable;
  2724. begin
  2725. { Reset symbolstack }
  2726. symtablestack:=nil;
  2727. systemunit:=nil;
  2728. { create error syms and def }
  2729. generrorsym:=terrorsym.create;
  2730. generrordef:=terrordef.create;
  2731. { macros }
  2732. initialmacrosymtable:=tmacrosymtable.create(false);
  2733. macrosymtablestack:=TSymtablestack.create;
  2734. macrosymtablestack.push(initialmacrosymtable);
  2735. {$ifdef UNITALIASES}
  2736. { unit aliases }
  2737. unitaliases:=TFPHashObjectList.create;
  2738. {$endif}
  2739. { set some global vars to nil, might be important for the ide }
  2740. class_tobject:=nil;
  2741. interface_iunknown:=nil;
  2742. interface_idispatch:=nil;
  2743. rec_tguid:=nil;
  2744. dupnr:=0;
  2745. end;
  2746. procedure DoneSymtable;
  2747. begin
  2748. generrorsym.owner:=nil;
  2749. generrorsym.free;
  2750. generrordef.owner:=nil;
  2751. generrordef.free;
  2752. initialmacrosymtable.free;
  2753. macrosymtablestack.free;
  2754. {$ifdef UNITALIASES}
  2755. unitaliases.free;
  2756. {$endif}
  2757. end;
  2758. end.