symtable.pas 193 KB

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