symtable.pas 164 KB

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