symtable.pas 166 KB

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