symtable.pas 187 KB

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