symtable.pas 156 KB

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