symtable.pas 134 KB

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