symtable.pas 172 KB

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