symtable.pas 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328
  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.symid-field2.symid;
  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. { sort the non-class fields to minimise losses due to alignment }
  1125. list.sort(@field_alignment_compare);
  1126. { now fill up gaps caused by alignment skips with smaller fields
  1127. where possible }
  1128. repeat
  1129. i:=0;
  1130. base:=_datasize;
  1131. globalfieldalignment:=fieldalignment;
  1132. changed:=false;
  1133. while i<list.count do
  1134. begin
  1135. fieldvs:=tfieldvarsym(list[i]);
  1136. if sp_static in fieldvs.symoptions then
  1137. begin
  1138. inc(i);
  1139. continue;
  1140. end;
  1141. prevglobalfieldalignment:=globalfieldalignment;
  1142. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1143. newfieldalignment:=globalfieldalignment;
  1144. { size of the gap between the end of the previous field and
  1145. the start of the current one }
  1146. space:=fieldoffset-base;
  1147. bestspaceleft:=space;
  1148. while space>0 do
  1149. begin
  1150. bestfieldindex:=-1;
  1151. bestinsertfieldoffset:=-1;
  1152. for j:=i+1 to list.count-1 do
  1153. begin
  1154. insertfieldvs:=tfieldvarsym(list[j]);
  1155. if sp_static in insertfieldvs.symoptions then
  1156. continue;
  1157. insertfieldsize:=insertfieldvs.getsize;
  1158. { can the new field fit possibly in the gap? }
  1159. if insertfieldsize<=space then
  1160. begin
  1161. { restore globalfieldalignment to situation before
  1162. the original field was inserted }
  1163. globalfieldalignment:=prevglobalfieldalignment;
  1164. { at what offset would it be inserted? (this new
  1165. field has its own alignment requirements, which
  1166. may make it impossible to fit after all) }
  1167. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1168. globalfieldalignment:=prevglobalfieldalignment;
  1169. { taking into account the alignment, does it still
  1170. fit and if so, does it fit better than the
  1171. previously found best fit? }
  1172. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1173. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1174. begin
  1175. { new best fit }
  1176. bestfieldindex:=j;
  1177. bestinsertfieldoffset:=insertfieldoffset;
  1178. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1179. if bestspaceleft=0 then
  1180. break;
  1181. end;
  1182. end;
  1183. end;
  1184. { if we didn't find any field to fit, stop trying for this
  1185. gap }
  1186. if bestfieldindex=-1 then
  1187. break;
  1188. changed:=true;
  1189. { we found a field to insert -> adjust the new base
  1190. address }
  1191. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1192. { update globalfieldalignment for this newly inserted
  1193. field }
  1194. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1195. { move the new field before the current one }
  1196. list.move(bestfieldindex,i);
  1197. { and skip the new field (which is now at position i) }
  1198. inc(i);
  1199. { there may be more space left -> continue }
  1200. space:=bestspaceleft;
  1201. end;
  1202. if base>fieldoffset then
  1203. internalerror(2012071302);
  1204. { check the next field }
  1205. base:=fieldoffset+fieldvs.getsize;
  1206. { since the original field had the same or greater alignment
  1207. than anything we inserted before it, the global field
  1208. alignment is still the same now as it was originally after
  1209. inserting that field }
  1210. globalfieldalignment:=newfieldalignment;
  1211. inc(i);
  1212. end;
  1213. { there may be small gaps left *before* inserted fields }
  1214. until not changed;
  1215. end;
  1216. { finally, set the actual field offsets }
  1217. for i:=0 to list.count-1 do
  1218. begin
  1219. fieldvs:=tfieldvarsym(list[i]);
  1220. { static data fields are already inserted in the globalsymtable }
  1221. if not(sp_static in fieldvs.symoptions) then
  1222. begin
  1223. { read_record_fields already set the visibility of the fields,
  1224. because a single list can contain symbols with different
  1225. visibility }
  1226. addfield(fieldvs,fieldvs.visibility);
  1227. end;
  1228. end;
  1229. end;
  1230. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1231. var
  1232. i: longint;
  1233. sym: tsym;
  1234. begin
  1235. { there could be multiple fields in case of a variant record }
  1236. if (defowner.typ=recorddef) and
  1237. trecorddef(defowner).isunion then
  1238. internalerror(2014090403);
  1239. for i:=0 to SymList.count-1 do
  1240. begin
  1241. sym:=tsym(symlist[i]);
  1242. if (sym.typ=fieldvarsym) and
  1243. not(sp_static in sym.symoptions) and
  1244. (tfieldvarsym(sym).fieldoffset>=offset) then
  1245. begin
  1246. result:=tfieldvarsym(sym);
  1247. exit;
  1248. end;
  1249. end;
  1250. result:=nil;
  1251. end;
  1252. procedure tabstractrecordsymtable.addalignmentpadding;
  1253. var
  1254. padded_datasize: asizeint;
  1255. begin
  1256. { make the record size aligned correctly so it can be
  1257. used as elements in an array. For C records we
  1258. use the fieldalignment, because that is updated with the
  1259. used alignment. }
  1260. if (padalignment = 1) then
  1261. case usefieldalignment of
  1262. C_alignment:
  1263. padalignment:=fieldalignment;
  1264. { bitpacked }
  1265. bit_alignment:
  1266. padalignment:=1;
  1267. { mac68k: always round to multiple of 2 }
  1268. mac68k_alignment:
  1269. padalignment:=2;
  1270. { default/no packrecords specified }
  1271. 0:
  1272. padalignment:=recordalignment
  1273. { specific packrecords setting -> use as upper limit }
  1274. else
  1275. padalignment:=min(recordalignment,usefieldalignment);
  1276. end;
  1277. padded_datasize:=align(_datasize,padalignment);
  1278. _paddingsize:=padded_datasize-_datasize;
  1279. _datasize:=padded_datasize;
  1280. end;
  1281. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1282. begin
  1283. { Enums must also be available outside the record scope,
  1284. insert in the owner of this symtable }
  1285. if def.typ=enumdef then
  1286. defowner.owner.insertdef(def)
  1287. else
  1288. inherited insertdef(def);
  1289. end;
  1290. function tabstractrecordsymtable.is_packed: boolean;
  1291. begin
  1292. result:=usefieldalignment=bit_alignment;
  1293. end;
  1294. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1295. var
  1296. i: longint;
  1297. currentsymlist: TFPHashObjectList;
  1298. currentdef: tdef;
  1299. sym: tfieldvarsym;
  1300. begin
  1301. result:=false;
  1302. { If a record contains a union, it does not contain a "single
  1303. non-composite field" in the context of certain ABIs requiring
  1304. special treatment for such records }
  1305. if (defowner.typ=recorddef) and
  1306. trecorddef(defowner).isunion then
  1307. exit;
  1308. { a record/object can contain other things than fields }
  1309. currentsymlist:=symlist;
  1310. { recurse in arrays and records }
  1311. sym:=nil;
  1312. repeat
  1313. { record has one field? }
  1314. for i:=0 to currentsymlist.Count-1 do
  1315. begin
  1316. if (tsym(symlist[i]).typ=fieldvarsym) and
  1317. not(sp_static in tsym(symlist[i]).symoptions) then
  1318. begin
  1319. if result then
  1320. begin
  1321. result:=false;
  1322. exit;
  1323. end;
  1324. result:=true;
  1325. sym:=tfieldvarsym(symlist[i])
  1326. end;
  1327. end;
  1328. if assigned(sym) then
  1329. begin
  1330. { if the field is an array, does it contain one element? }
  1331. currentdef:=sym.vardef;
  1332. while (currentdef.typ=arraydef) and
  1333. not is_special_array(currentdef) do
  1334. begin
  1335. if tarraydef(currentdef).elecount<>1 then
  1336. begin
  1337. result:=false;
  1338. exit;
  1339. end;
  1340. currentdef:=tarraydef(currentdef).elementdef;
  1341. end;
  1342. { if the array element is again a record, continue descending }
  1343. if currentdef.typ=recorddef then
  1344. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1345. else
  1346. begin
  1347. { otherwise we found the type of the single element }
  1348. def:=currentdef;
  1349. exit;
  1350. end;
  1351. end
  1352. else
  1353. exit
  1354. until false;
  1355. end;
  1356. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1357. begin
  1358. result:=defowner.owner;
  1359. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1360. result:=result.defowner.owner;
  1361. end;
  1362. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1363. begin
  1364. _datasize:=val;
  1365. if (usefieldalignment=bit_alignment) then
  1366. { can overflow in non bitpacked records }
  1367. databitsize:=val*8;
  1368. end;
  1369. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1370. var
  1371. l : asizeint;
  1372. varalignfield,
  1373. varalign : shortint;
  1374. vardef : tdef;
  1375. begin
  1376. { Calculate field offset }
  1377. l:=sym.getsize;
  1378. vardef:=sym.vardef;
  1379. varalign:=vardef.structalignment;
  1380. case usefieldalignment of
  1381. bit_alignment:
  1382. { has to be handled separately }
  1383. internalerror(2012071301);
  1384. C_alignment:
  1385. begin
  1386. { Calc the alignment size for C style records }
  1387. if (varalign>4) and
  1388. ((varalign mod 4)<>0) and
  1389. (vardef.typ=arraydef) then
  1390. Message1(sym_w_wrong_C_pack,vardef.typename);
  1391. if varalign=0 then
  1392. varalign:=l;
  1393. if (globalfieldalignment<maxCrecordalign) then
  1394. begin
  1395. if (varalign>16) and (globalfieldalignment<32) then
  1396. globalfieldalignment:=32
  1397. else if (varalign>12) and (globalfieldalignment<16) then
  1398. globalfieldalignment:=16
  1399. { 12 is needed for long double }
  1400. else if (varalign>8) and (globalfieldalignment<12) then
  1401. globalfieldalignment:=12
  1402. else if (varalign>4) and (globalfieldalignment<8) then
  1403. globalfieldalignment:=8
  1404. else if (varalign>2) and (globalfieldalignment<4) then
  1405. globalfieldalignment:=4
  1406. else if (varalign>1) and (globalfieldalignment<2) then
  1407. globalfieldalignment:=2;
  1408. end;
  1409. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1410. end;
  1411. mac68k_alignment:
  1412. begin
  1413. { mac68k alignment (C description):
  1414. * char is aligned to 1 byte
  1415. * everything else (except vector) is aligned to 2 bytes
  1416. * vector is aligned to 16 bytes
  1417. }
  1418. if l>1 then
  1419. globalfieldalignment:=2
  1420. else
  1421. globalfieldalignment:=1;
  1422. varalign:=2;
  1423. end;
  1424. end;
  1425. if varalign=0 then
  1426. varalign:=size_2_align(l);
  1427. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1428. result:=align(base,varalignfield);
  1429. end;
  1430. function tabstractrecordsymtable.iscurrentunit: boolean;
  1431. begin
  1432. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1433. end;
  1434. {****************************************************************************
  1435. TRecordSymtable
  1436. ****************************************************************************}
  1437. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1438. begin
  1439. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1440. symtabletype:=recordsymtable;
  1441. end;
  1442. { this procedure is reserved for inserting case variant into
  1443. a record symtable }
  1444. { the offset is the location of the start of the variant
  1445. and datasize and dataalignment corresponds to
  1446. the complete size (see code in pdecl unit) PM }
  1447. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1448. var
  1449. sym : tsym;
  1450. def : tdef;
  1451. i : integer;
  1452. varalignrecord,varalign,
  1453. storesize,storealign : aint;
  1454. bitsize: tcgint;
  1455. begin
  1456. storesize:=_datasize;
  1457. storealign:=fieldalignment;
  1458. _datasize:=offset;
  1459. if (usefieldalignment=bit_alignment) then
  1460. databitsize:=offset*8;
  1461. { We move the ownership of the defs and symbols to the new recordsymtable.
  1462. The old unionsymtable keeps the references, but doesn't own the
  1463. objects anymore }
  1464. unionst.DefList.OwnsObjects:=false;
  1465. unionst.SymList.OwnsObjects:=false;
  1466. { copy symbols }
  1467. for i:=0 to unionst.SymList.Count-1 do
  1468. begin
  1469. sym:=TSym(unionst.SymList[i]);
  1470. if sym.typ<>fieldvarsym then
  1471. internalerror(200601272);
  1472. if tfieldvarsym(sym).fieldoffset=0 then
  1473. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1474. { add to this record symtable, checking for duplicate names }
  1475. // unionst.SymList.List.List^[i].Data:=nil;
  1476. insert(sym);
  1477. varalign:=tfieldvarsym(sym).vardef.alignment;
  1478. if varalign=0 then
  1479. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1480. { retrieve size }
  1481. if (usefieldalignment=bit_alignment) then
  1482. begin
  1483. { bit packed records are limited to high(aint) bits }
  1484. { instead of bytes to avoid double precision }
  1485. { arithmetic in offset calculations }
  1486. if is_ordinal(tfieldvarsym(sym).vardef) then
  1487. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1488. else
  1489. begin
  1490. bitsize:=tfieldvarsym(sym).getsize;
  1491. if (bitsize>high(asizeint) div 8) then
  1492. Message(sym_e_segment_too_large);
  1493. bitsize:=bitsize*8;
  1494. end;
  1495. if bitsize>high(asizeint)-databitsize then
  1496. begin
  1497. Message(sym_e_segment_too_large);
  1498. _datasize:=high(asizeint);
  1499. databitsize:=high(asizeint);
  1500. end
  1501. else
  1502. begin
  1503. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1504. _datasize:=(databitsize+7) div 8;
  1505. end;
  1506. tfieldvarsym(sym).fieldoffset:=databitsize;
  1507. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1508. end
  1509. else
  1510. begin
  1511. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1512. begin
  1513. Message(sym_e_segment_too_large);
  1514. _datasize:=high(asizeint);
  1515. end
  1516. else
  1517. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1518. { update address }
  1519. tfieldvarsym(sym).fieldoffset:=_datasize;
  1520. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1521. end;
  1522. { update alignment of this record }
  1523. if (usefieldalignment<>C_alignment) and
  1524. (usefieldalignment<>mac68k_alignment) then
  1525. recordalignment:=max(recordalignment,varalignrecord);
  1526. end;
  1527. { update alignment for C records }
  1528. if (usefieldalignment=C_alignment) and
  1529. (usefieldalignment<>mac68k_alignment) then
  1530. recordalignment:=max(recordalignment,unionst.recordalignment);
  1531. { Register defs in the new record symtable }
  1532. for i:=0 to unionst.DefList.Count-1 do
  1533. begin
  1534. def:=TDef(unionst.DefList[i]);
  1535. def.ChangeOwner(self);
  1536. end;
  1537. _datasize:=storesize;
  1538. fieldalignment:=storealign;
  1539. { If a record contains a union, it does not contain a "single
  1540. non-composite field" in the context of certain ABIs requiring
  1541. special treatment for such records }
  1542. if defowner.typ=recorddef then
  1543. trecorddef(defowner).isunion:=true;
  1544. end;
  1545. {****************************************************************************
  1546. TObjectSymtable
  1547. ****************************************************************************}
  1548. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1549. begin
  1550. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1551. symtabletype:=ObjectSymtable;
  1552. defowner:=adefowner;
  1553. end;
  1554. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1555. var
  1556. hsym: tsym;
  1557. warn: boolean;
  1558. begin
  1559. result:=false;
  1560. if not assigned(defowner) then
  1561. internalerror(200602061);
  1562. { procsym and propertysym have special code
  1563. to override values in inherited classes. For other
  1564. symbols check for duplicates }
  1565. if not(sym.typ in [procsym,propertysym]) then
  1566. begin
  1567. { but private ids can be reused }
  1568. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1569. if assigned(hsym) and
  1570. (
  1571. (
  1572. not(m_delphi in current_settings.modeswitches) and
  1573. is_visible_for_object(hsym,tobjectdef(defowner))
  1574. ) or
  1575. (
  1576. { In Delphi, you can repeat members of a parent class. You can't }
  1577. { do this for objects however, and you (obviouly) can't }
  1578. { declare two fields with the same name in a single class }
  1579. (m_delphi in current_settings.modeswitches) and
  1580. (
  1581. is_object(tdef(defowner)) or
  1582. (hsym.owner = self)
  1583. )
  1584. )
  1585. ) then
  1586. begin
  1587. { only watn when a parameter/local variable in a method
  1588. conflicts with a category method, because this can easily
  1589. happen due to all possible categories being imported via
  1590. CocoaAll }
  1591. warn:=
  1592. (is_objccategory(tdef(hsym.owner.defowner)) or
  1593. is_classhelper(tdef(hsym.owner.defowner))) and
  1594. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1595. DuplicateSym(hashedid,sym,hsym,warn);
  1596. result:=true;
  1597. end;
  1598. end
  1599. else
  1600. result:=inherited checkduplicate(hashedid,sym);
  1601. end;
  1602. {$ifdef llvm}
  1603. {****************************************************************************
  1604. tLlvmShadowSymtableEntry
  1605. ****************************************************************************}
  1606. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1607. begin
  1608. fdef:=def;
  1609. ffieldoffset:=fieldoffset;
  1610. end;
  1611. {****************************************************************************
  1612. TLlvmShadowSymtable
  1613. ****************************************************************************}
  1614. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1615. begin
  1616. result:=tllvmshadowsymtableentry(symdeflist[f.llvmfieldnr])
  1617. end;
  1618. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1619. begin
  1620. equivst:=st;
  1621. curroffset:=0;
  1622. symdeflist:=tfpobjectlist.create(true);
  1623. generate;
  1624. end;
  1625. destructor tllvmshadowsymtable.destroy;
  1626. begin
  1627. symdeflist.free;
  1628. end;
  1629. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1630. var
  1631. sizectr,
  1632. tmpsize: aint;
  1633. begin
  1634. case equivst.usefieldalignment of
  1635. C_alignment:
  1636. { default for llvm, don't add explicit padding }
  1637. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1638. bit_alignment:
  1639. begin
  1640. { curoffset: bit address after the previous field. }
  1641. { llvm has no special support for bitfields in records, }
  1642. { so we replace them with plain bytes. }
  1643. { as soon as a single bit of a byte is allocated, we }
  1644. { allocate the byte in the llvm shadow record }
  1645. if (fieldoffset>curroffset) then
  1646. curroffset:=align(curroffset,8);
  1647. { fields in bitpacked records always start either right }
  1648. { after the previous one, or at the next byte boundary. }
  1649. if (curroffset<>fieldoffset) then
  1650. internalerror(2008051002);
  1651. if is_ordinal(vardef) and
  1652. (vardef.packedbitsize mod 8 <> 0) then
  1653. begin
  1654. tmpsize:=vardef.packedbitsize;
  1655. sizectr:=tmpsize+7;
  1656. repeat
  1657. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+(tmpsize+7)-sizectr));
  1658. dec(sizectr,8);
  1659. until (sizectr<=0);
  1660. inc(curroffset,tmpsize);
  1661. end
  1662. else
  1663. begin
  1664. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1665. if not(derefclass) then
  1666. inc(curroffset,vardef.size*8)
  1667. else
  1668. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1669. end;
  1670. end
  1671. else
  1672. begin
  1673. { curoffset: address right after the previous field }
  1674. while (fieldoffset>curroffset) do
  1675. begin
  1676. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1677. inc(curroffset);
  1678. end;
  1679. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1680. if not(derefclass) then
  1681. inc(curroffset,vardef.size)
  1682. else
  1683. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1684. end
  1685. end
  1686. end;
  1687. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1688. begin
  1689. case equivst.usefieldalignment of
  1690. { already correct in this case }
  1691. bit_alignment,
  1692. { handled by llvm }
  1693. C_alignment:
  1694. ;
  1695. else
  1696. begin
  1697. { add padding fields }
  1698. while (finalsize>curroffset) do
  1699. begin
  1700. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1701. inc(curroffset);
  1702. end;
  1703. end;
  1704. end;
  1705. end;
  1706. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1707. var
  1708. sym: tfieldvarsym;
  1709. lastoffset: aint;
  1710. newalignment: aint;
  1711. i, j: longint;
  1712. begin
  1713. i:=0;
  1714. while (i<equivst.symlist.count) do
  1715. begin
  1716. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1717. begin
  1718. inc(i);
  1719. continue;
  1720. end;
  1721. sym:=tfieldvarsym(equivst.symlist[i]);
  1722. { a "better" algorithm might be to use the largest }
  1723. { variant in case of (bit)packing, since then }
  1724. { alignment doesn't matter }
  1725. if (vo_is_first_field in sym.varoptions) then
  1726. begin
  1727. { we assume that all fields are processed in order. }
  1728. if (variantstarts.count<>0) then
  1729. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1730. else
  1731. lastoffset:=-1;
  1732. { new variant at same level as last one: use if higher alignment }
  1733. if (lastoffset=sym.fieldoffset) then
  1734. begin
  1735. if (equivst.fieldalignment<>bit_alignment) then
  1736. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1737. else
  1738. newalignment:=1;
  1739. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1740. variantstarts[variantstarts.count-1]:=sym;
  1741. end
  1742. { variant at deeper level than last one -> add }
  1743. else if (lastoffset<sym.fieldoffset) then
  1744. variantstarts.add(sym)
  1745. else
  1746. begin
  1747. { a variant at a less deep level, so backtrack }
  1748. j:=variantstarts.count-2;
  1749. while (j>=0) do
  1750. begin
  1751. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1752. break;
  1753. dec(j);
  1754. end;
  1755. if (j<0) then
  1756. internalerror(2008051003);
  1757. { new variant has higher alignment? }
  1758. if (equivst.fieldalignment<>bit_alignment) then
  1759. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1760. else
  1761. newalignment:=1;
  1762. { yes, replace and remove previous nested variants }
  1763. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1764. begin
  1765. variantstarts[j]:=sym;
  1766. variantstarts.count:=j+1;
  1767. end
  1768. { no, skip this variant }
  1769. else
  1770. begin
  1771. inc(i);
  1772. while (i<equivst.symlist.count) and
  1773. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1774. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  1775. inc(i);
  1776. continue;
  1777. end;
  1778. end;
  1779. end;
  1780. inc(i);
  1781. end;
  1782. end;
  1783. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1784. var
  1785. lastvaroffsetprocessed: aint;
  1786. i, equivcount, varcount: longint;
  1787. begin
  1788. { if it's an object/class, the first entry is the parent (if there is one) }
  1789. if (equivst.symtabletype=objectsymtable) and
  1790. assigned(tobjectdef(equivst.defowner).childof) then
  1791. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  1792. equivcount:=equivst.symlist.count;
  1793. varcount:=0;
  1794. i:=0;
  1795. lastvaroffsetprocessed:=-1;
  1796. while (i<equivcount) do
  1797. begin
  1798. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1799. begin
  1800. inc(i);
  1801. continue;
  1802. end;
  1803. { start of a new variant? }
  1804. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1805. begin
  1806. { if we want to process the same variant offset twice, it means that we }
  1807. { got to the end and are trying to process the next variant part -> stop }
  1808. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  1809. break;
  1810. if (varcount>=variantstarts.count) then
  1811. internalerror(2008051005);
  1812. { new variant part -> use the one with the biggest alignment }
  1813. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1814. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1815. inc(varcount);
  1816. if (i<0) then
  1817. internalerror(2008051004);
  1818. end;
  1819. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  1820. inc(i);
  1821. end;
  1822. addalignmentpadding(equivst.datasize);
  1823. end;
  1824. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1825. var
  1826. i, varcount: longint;
  1827. shadowindex: longint;
  1828. equivcount : longint;
  1829. begin
  1830. varcount:=0;
  1831. shadowindex:=0;
  1832. equivcount:=equivst.symlist.count;
  1833. i:=0;
  1834. while (i < equivcount) do
  1835. begin
  1836. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1837. begin
  1838. inc(i);
  1839. continue;
  1840. end;
  1841. { start of a new variant? }
  1842. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1843. begin
  1844. { back up to a less deeply nested variant level? }
  1845. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  1846. dec(varcount);
  1847. { it's possible that some variants are more deeply nested than the
  1848. one we recorded in the shadowsymtable (since we recorded the one
  1849. with the biggest alignment, not necessarily the biggest one in size
  1850. }
  1851. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1852. varcount:=variantstarts.count-1
  1853. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1854. internalerror(2008051006);
  1855. { reset the shadowindex to the start of this variant. }
  1856. { in case the llvmfieldnr is not (yet) set for this }
  1857. { field, shadowindex will simply be reset to zero and }
  1858. { we'll start searching from the start of the record }
  1859. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1860. if (varcount<pred(variantstarts.count)) then
  1861. inc(varcount);
  1862. end;
  1863. { find the last shadowfield whose offset <= the current field's offset }
  1864. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1865. (shadowindex<symdeflist.count-1) and
  1866. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1867. inc(shadowindex);
  1868. { set the field number and potential offset from that field (in case }
  1869. { of overlapping variants) }
  1870. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1871. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1872. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1873. inc(i);
  1874. end;
  1875. end;
  1876. procedure tllvmshadowsymtable.generate;
  1877. var
  1878. variantstarts: tfplist;
  1879. begin
  1880. variantstarts:=tfplist.create;
  1881. { first go through the entire record and }
  1882. { store the fieldvarsyms of the variants }
  1883. { with the highest alignment }
  1884. findvariantstarts(variantstarts);
  1885. { now go through the regular fields and the selected variants, }
  1886. { and add them to the llvm shadow record symtable }
  1887. buildtable(variantstarts);
  1888. { finally map all original fields to the llvm definition }
  1889. buildmapping(variantstarts);
  1890. variantstarts.free;
  1891. end;
  1892. {$endif llvm}
  1893. {****************************************************************************
  1894. TAbstractSubSymtable
  1895. ****************************************************************************}
  1896. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  1897. var
  1898. oldtyp : byte;
  1899. begin
  1900. oldtyp:=ppufile.entrytyp;
  1901. ppufile.entrytyp:=subentryid;
  1902. inherited ppuwrite(ppufile);
  1903. ppufile.entrytyp:=oldtyp;
  1904. end;
  1905. {****************************************************************************
  1906. TAbstractLocalSymtable
  1907. ****************************************************************************}
  1908. function tabstractlocalsymtable.count_locals:longint;
  1909. var
  1910. i : longint;
  1911. sym : tsym;
  1912. begin
  1913. result:=0;
  1914. for i:=0 to SymList.Count-1 do
  1915. begin
  1916. sym:=tsym(SymList[i]);
  1917. { Count only varsyms, but ignore the funcretsym }
  1918. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1919. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1920. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1921. (tstoredsym(sym).refs>0)) then
  1922. inc(result);
  1923. end;
  1924. end;
  1925. function tabstractlocalsymtable.iscurrentunit: boolean;
  1926. begin
  1927. Result:=
  1928. assigned(defowner) and
  1929. defowner.owner.iscurrentunit;
  1930. end;
  1931. {****************************************************************************
  1932. TLocalSymtable
  1933. ****************************************************************************}
  1934. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1935. begin
  1936. inherited create('');
  1937. defowner:=adefowner;
  1938. symtabletype:=localsymtable;
  1939. symtablelevel:=level;
  1940. end;
  1941. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1942. var
  1943. hsym : tsym;
  1944. begin
  1945. if not assigned(defowner) or
  1946. (defowner.typ<>procdef) then
  1947. internalerror(200602042);
  1948. result:=false;
  1949. hsym:=tsym(FindWithHash(hashedid));
  1950. if assigned(hsym) then
  1951. begin
  1952. { a local and the function can have the same
  1953. name in TP and Delphi, but RESULT not }
  1954. if (m_duplicate_names in current_settings.modeswitches) and
  1955. (hsym.typ in [absolutevarsym,localvarsym]) and
  1956. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1957. not((m_result in current_settings.modeswitches) and
  1958. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1959. HideSym(hsym)
  1960. else
  1961. DuplicateSym(hashedid,sym,hsym,false);
  1962. result:=true;
  1963. exit;
  1964. end;
  1965. { check also parasymtable, this needs to be done here because
  1966. of the special situation with the funcret sym that needs to be
  1967. hidden for tp and delphi modes }
  1968. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1969. if assigned(hsym) then
  1970. begin
  1971. { a local and the function can have the same
  1972. name in TP and Delphi, but RESULT not }
  1973. if (m_duplicate_names in current_settings.modeswitches) and
  1974. (sym.typ in [absolutevarsym,localvarsym]) and
  1975. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1976. not((m_result in current_settings.modeswitches) and
  1977. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1978. Hidesym(sym)
  1979. else
  1980. DuplicateSym(hashedid,sym,hsym,false);
  1981. result:=true;
  1982. exit;
  1983. end;
  1984. { check ObjectSymtable, skip this for funcret sym because
  1985. that will always be positive because it has the same name
  1986. as the procsym }
  1987. if not is_funcret_sym(sym) and
  1988. (defowner.typ=procdef) and
  1989. assigned(tprocdef(defowner).struct) and
  1990. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1991. (
  1992. not(m_delphi in current_settings.modeswitches) or
  1993. is_object(tprocdef(defowner).struct)
  1994. ) then
  1995. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1996. end;
  1997. {****************************************************************************
  1998. TParaSymtable
  1999. ****************************************************************************}
  2000. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2001. begin
  2002. inherited create('');
  2003. readonly:=false;
  2004. defowner:=adefowner;
  2005. symtabletype:=parasymtable;
  2006. symtablelevel:=level;
  2007. end;
  2008. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2009. begin
  2010. result:=inherited checkduplicate(hashedid,sym);
  2011. if result then
  2012. exit;
  2013. if not(m_duplicate_names in current_settings.modeswitches) and
  2014. assigned(defowner) and (defowner.typ=procdef) and
  2015. assigned(tprocdef(defowner).struct) and
  2016. assigned(tprocdef(defowner).owner) and
  2017. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2018. (
  2019. not(m_delphi in current_settings.modeswitches) or
  2020. is_object(tprocdef(defowner).struct)
  2021. ) then
  2022. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2023. end;
  2024. procedure tparasymtable.insertdef(def: TDefEntry);
  2025. begin
  2026. if readonly then
  2027. defowner.owner.insertdef(def)
  2028. else
  2029. inherited insertdef(def);
  2030. end;
  2031. {****************************************************************************
  2032. TAbstractUniTSymtable
  2033. ****************************************************************************}
  2034. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2035. begin
  2036. inherited create(n);
  2037. moduleid:=id;
  2038. end;
  2039. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2040. var
  2041. hsym : tsym;
  2042. begin
  2043. result:=false;
  2044. hsym:=tsym(FindWithHash(hashedid));
  2045. if assigned(hsym) then
  2046. begin
  2047. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2048. exit;
  2049. if hsym.typ=symconst.namespacesym then
  2050. begin
  2051. case sym.typ of
  2052. symconst.namespacesym:;
  2053. symconst.unitsym:
  2054. begin
  2055. 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 }
  2056. tnamespacesym(hsym).unitsym:=tsym(sym);
  2057. end
  2058. else
  2059. HideSym(hsym);
  2060. end;
  2061. end
  2062. else
  2063. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2064. unit, the unit can then not be accessed anymore using
  2065. <unit>.<id>, so we can hide the symbol.
  2066. Do the same if we add a namespace and there is a unit with the same name }
  2067. if (hsym.typ=symconst.unitsym) and
  2068. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2069. begin
  2070. HideSym(hsym);
  2071. if sym.typ=symconst.namespacesym then
  2072. tnamespacesym(sym).unitsym:=tsym(hsym);
  2073. end
  2074. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2075. in this case, copy the isoindex and make the original symbol invisible }
  2076. else if (m_iso in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2077. and (tprogramparasym(hsym).isoindex<>0) then
  2078. begin
  2079. HideSym(hsym);
  2080. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2081. end
  2082. else
  2083. DuplicateSym(hashedid,sym,hsym,false);
  2084. result:=true;
  2085. exit;
  2086. end;
  2087. end;
  2088. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2089. begin
  2090. result:=find(n);
  2091. if assigned(result)and(result.typ<>namespacesym)then
  2092. result:=nil;
  2093. end;
  2094. function tabstractuniTSymtable.iscurrentunit:boolean;
  2095. begin
  2096. result:=assigned(current_module) and
  2097. (
  2098. (current_module.globalsymtable=self) or
  2099. (current_module.localsymtable=self)
  2100. );
  2101. end;
  2102. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2103. var
  2104. p:integer;
  2105. n,ns:string;
  2106. oldsym:TSymEntry;
  2107. begin
  2108. insert(sym);
  2109. n:=sym.realname;
  2110. p:=pos('.',n);
  2111. ns:='';
  2112. while p>0 do
  2113. begin
  2114. if ns='' then
  2115. ns:=copy(n,1,p-1)
  2116. else
  2117. ns:=ns+'.'+copy(n,1,p-1);
  2118. system.delete(n,1,p);
  2119. oldsym:=findnamespace(upper(ns));
  2120. if not assigned(oldsym) then
  2121. insert(cnamespacesym.create(ns));
  2122. p:=pos('.',n);
  2123. end;
  2124. end;
  2125. {****************************************************************************
  2126. TStaticSymtable
  2127. ****************************************************************************}
  2128. constructor tstaticsymtable.create(const n : string;id:word);
  2129. begin
  2130. inherited create(n,id);
  2131. symtabletype:=staticsymtable;
  2132. symtablelevel:=main_program_level;
  2133. currentvisibility:=vis_private;
  2134. end;
  2135. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2136. begin
  2137. inherited ppuload(ppufile);
  2138. { now we can deref the syms and defs }
  2139. deref(false);
  2140. end;
  2141. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2142. begin
  2143. inherited ppuwrite(ppufile);
  2144. end;
  2145. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2146. begin
  2147. result:=inherited checkduplicate(hashedid,sym);
  2148. if not result and
  2149. (current_module.localsymtable=self) and
  2150. assigned(current_module.globalsymtable) then
  2151. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2152. end;
  2153. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2154. begin
  2155. result:=inherited findnamespace(n);
  2156. if not assigned(result) and
  2157. (current_module.localsymtable=self) and
  2158. assigned(current_module.globalsymtable) then
  2159. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2160. end;
  2161. {****************************************************************************
  2162. TGlobalSymtable
  2163. ****************************************************************************}
  2164. constructor tglobalsymtable.create(const n : string;id:word);
  2165. begin
  2166. inherited create(n,id);
  2167. symtabletype:=globalsymtable;
  2168. symtablelevel:=main_program_level;
  2169. end;
  2170. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2171. begin
  2172. inherited ppuload(ppufile);
  2173. { now we can deref the syms and defs }
  2174. deref(false);
  2175. end;
  2176. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2177. begin
  2178. { write the symtable entries }
  2179. inherited ppuwrite(ppufile);
  2180. end;
  2181. {*****************************************************************************
  2182. tspecializesymtable
  2183. *****************************************************************************}
  2184. constructor tspecializesymtable.create(const n : string;id:word);
  2185. begin
  2186. inherited create(n,id);
  2187. { the specialize symtable does not own the syms and defs as they are all
  2188. moved to a different symtable before the symtable is destroyed; this
  2189. avoids calls to "extract" }
  2190. symlist.ownsobjects:=false;
  2191. deflist.ownsobjects:=false;
  2192. end;
  2193. function tspecializesymtable.iscurrentunit: boolean;
  2194. begin
  2195. Result:=true;
  2196. end;
  2197. {****************************************************************************
  2198. TWITHSYMTABLE
  2199. ****************************************************************************}
  2200. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2201. begin
  2202. inherited create('');
  2203. symtabletype:=withsymtable;
  2204. withrefnode:=refnode;
  2205. { Replace SymList with the passed symlist }
  2206. SymList.free;
  2207. SymList:=ASymList;
  2208. defowner:=aowner;
  2209. end;
  2210. destructor twithsymtable.destroy;
  2211. begin
  2212. withrefnode.free;
  2213. { Disable SymList because we don't Own it }
  2214. SymList:=nil;
  2215. inherited destroy;
  2216. end;
  2217. procedure twithsymtable.clear;
  2218. begin
  2219. { remove no entry from a withsymtable as it is only a pointer to the
  2220. recorddef or objectdef symtable }
  2221. end;
  2222. procedure twithsymtable.insertdef(def:TDefEntry);
  2223. begin
  2224. { Definitions can't be registered in the withsymtable
  2225. because the withsymtable is removed after the with block.
  2226. We can't easily solve it here because the next symtable in the
  2227. stack is not known. }
  2228. internalerror(200602046);
  2229. end;
  2230. {****************************************************************************
  2231. TSTT_ExceptionSymtable
  2232. ****************************************************************************}
  2233. constructor tstt_excepTSymtable.create;
  2234. begin
  2235. inherited create('');
  2236. symtabletype:=stt_excepTSymtable;
  2237. end;
  2238. {****************************************************************************
  2239. TMacroSymtable
  2240. ****************************************************************************}
  2241. constructor tmacrosymtable.create(exported: boolean);
  2242. begin
  2243. inherited create('');
  2244. if exported then
  2245. symtabletype:=exportedmacrosymtable
  2246. else
  2247. symtabletype:=localmacrosymtable;
  2248. symtablelevel:=main_program_level;
  2249. end;
  2250. {****************************************************************************
  2251. TEnumSymtable
  2252. ****************************************************************************}
  2253. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2254. var
  2255. value: longint;
  2256. def: tenumdef;
  2257. begin
  2258. // defowner = nil only when we are loading from ppu
  2259. if defowner<>nil then
  2260. begin
  2261. { First entry? Then we need to set the minval }
  2262. value:=tenumsym(sym).value;
  2263. def:=tenumdef(defowner);
  2264. if SymList.count=0 then
  2265. begin
  2266. if value>0 then
  2267. def.has_jumps:=true;
  2268. def.setmin(value);
  2269. def.setmax(value);
  2270. end
  2271. else
  2272. begin
  2273. { check for jumps }
  2274. if value>def.max+1 then
  2275. def.has_jumps:=true;
  2276. { update low and high }
  2277. if def.min>value then
  2278. def.setmin(value);
  2279. if def.max<value then
  2280. def.setmax(value);
  2281. end;
  2282. end;
  2283. inherited insert(sym, checkdup);
  2284. end;
  2285. constructor tenumsymtable.create(adefowner: tdef);
  2286. begin
  2287. inherited Create('');
  2288. symtabletype:=enumsymtable;
  2289. defowner:=adefowner;
  2290. end;
  2291. {****************************************************************************
  2292. TArraySymtable
  2293. ****************************************************************************}
  2294. procedure tarraysymtable.insertdef(def: TDefEntry);
  2295. begin
  2296. { Enums must also be available outside the record scope,
  2297. insert in the owner of this symtable }
  2298. if def.typ=enumdef then
  2299. defowner.owner.insertdef(def)
  2300. else
  2301. inherited insertdef(def);
  2302. end;
  2303. constructor tarraysymtable.create(adefowner: tdef);
  2304. begin
  2305. inherited Create('');
  2306. symtabletype:=arraysymtable;
  2307. defowner:=adefowner;
  2308. end;
  2309. {*****************************************************************************
  2310. Helper Routines
  2311. *****************************************************************************}
  2312. function FullTypeName(def,otherdef:tdef):string;
  2313. var
  2314. s1,s2 : string;
  2315. begin
  2316. if def.typ in [objectdef,recorddef] then
  2317. s1:=tabstractrecorddef(def).RttiName
  2318. else
  2319. s1:=def.typename;
  2320. { When the names are the same try to include the unit name }
  2321. if assigned(otherdef) and
  2322. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2323. begin
  2324. s2:=otherdef.typename;
  2325. if upper(s1)=upper(s2) then
  2326. s1:=def.owner.realname^+'.'+s1;
  2327. end;
  2328. FullTypeName:=s1;
  2329. end;
  2330. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2331. begin
  2332. result:='';
  2333. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2334. begin
  2335. if (result='') then
  2336. if symtable.name<>nil then
  2337. result:=symtable.name^
  2338. else
  2339. else
  2340. if symtable.name<>nil then
  2341. result:=symtable.name^+delimiter+result
  2342. else
  2343. result:=delimiter+result;
  2344. symtable:=symtable.defowner.owner;
  2345. end;
  2346. end;
  2347. function generate_objectpascal_helper_key(def:tdef):string;
  2348. begin
  2349. if not assigned(def) then
  2350. internalerror(2013020501);
  2351. if def.typ in [recorddef,objectdef] then
  2352. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2353. else
  2354. result:=make_mangledname('',def.owner,def.typesym.name);
  2355. end;
  2356. procedure incompatibletypes(def1,def2:tdef);
  2357. begin
  2358. { When there is an errordef there is already an error message show }
  2359. if (def2.typ=errordef) or
  2360. (def1.typ=errordef) then
  2361. exit;
  2362. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2363. end;
  2364. procedure hidesym(sym:TSymEntry);
  2365. begin
  2366. sym.realname:='$hidden'+sym.realname;
  2367. tsym(sym).visibility:=vis_hidden;
  2368. end;
  2369. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2370. var
  2371. st : TSymtable;
  2372. filename : TIDString;
  2373. begin
  2374. if not warn then
  2375. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2376. else
  2377. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2378. { Write hint where the original symbol was found }
  2379. st:=finduniTSymtable(origsym.owner);
  2380. with tsym(origsym).fileinfo do
  2381. begin
  2382. if assigned(st) and
  2383. (st.symtabletype=globalsymtable) and
  2384. st.iscurrentunit then
  2385. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2386. else if assigned(st.name) then
  2387. begin
  2388. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2389. if filename<>'' then
  2390. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2391. else
  2392. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2393. end;
  2394. end;
  2395. { Rename duplicate sym to an unreachable name, but it can be
  2396. inserted in the symtable without errors }
  2397. inc(dupnr);
  2398. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2399. if assigned(dupsym) then
  2400. include(tsym(dupsym).symoptions,sp_implicitrename);
  2401. end;
  2402. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2403. begin
  2404. result:=false;
  2405. if not assigned(sym) or not (sym is tstoredsym) then
  2406. Internalerror(2011081101);
  2407. { For generics a dummy symbol without the parameter count is created
  2408. if such a symbol not yet exists so that different parts of the
  2409. parser can find that symbol. If that symbol is still a
  2410. undefineddef we replace the generic dummy symbol's
  2411. name with a "dup" name and use the new symbol as the generic dummy
  2412. symbol }
  2413. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2414. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2415. (m_delphi in current_settings.modeswitches) then
  2416. begin
  2417. inc(dupnr);
  2418. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2419. include(tsym(sym).symoptions,sp_implicitrename);
  2420. { we need to find the new symbol now if checking for a dummy }
  2421. include(symoptions,sp_generic_dummy);
  2422. result:=true;
  2423. end;
  2424. end;
  2425. {*****************************************************************************
  2426. Search
  2427. *****************************************************************************}
  2428. procedure addsymref(sym:tsym);
  2429. var
  2430. owner: tsymtable;
  2431. begin
  2432. { symbol uses count }
  2433. sym.IncRefCount;
  2434. { unit uses count }
  2435. owner:=sym.owner;
  2436. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2437. owner:=tdef(owner.defowner).owner;
  2438. if assigned(current_module) and
  2439. (owner.symtabletype=globalsymtable) then
  2440. begin
  2441. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2442. internalerror(200501152);
  2443. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2444. end;
  2445. end;
  2446. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2447. begin
  2448. result:=nesteddef=ownerdef;
  2449. if not result and
  2450. { types declared locally in a record method are not defined in the
  2451. record itself }
  2452. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2453. assigned(nesteddef.owner.defowner) then
  2454. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2455. end;
  2456. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2457. begin
  2458. result:=assigned(childsym) and (childsym.owner=symtable);
  2459. if not result and assigned(childsym) and
  2460. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2461. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2462. end;
  2463. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2464. begin
  2465. result:=false;
  2466. if not assigned(def1) or not assigned(def2) then
  2467. exit;
  2468. { for both defs walk to the topmost generic }
  2469. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2470. def1:=tdef(def1.owner.defowner);
  2471. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2472. def2:=tdef(def2.owner.defowner);
  2473. result:=def1=def2;
  2474. end;
  2475. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2476. var
  2477. uname : string;
  2478. begin
  2479. { TODO : check regarding arrays and records declared as their type }
  2480. if not (def.typ in [recorddef,objectdef]) then
  2481. internalerror(2012051501);
  2482. uname:=upper(srsym.realname);
  2483. repeat
  2484. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2485. begin
  2486. result:=def;
  2487. exit;
  2488. end;
  2489. def:=tdef(def.owner.defowner);
  2490. until not (def.typ in [recorddef,objectdef]);
  2491. result:=nil;
  2492. end;
  2493. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2494. begin
  2495. { TODO : check regarding arrays and records declared as their type }
  2496. if not (nesteddef.typ in [recorddef,objectdef]) then
  2497. internalerror(2012051601);
  2498. repeat
  2499. if tstoreddef(nesteddef).genericdef=genericdef then
  2500. begin
  2501. resultdef:=nesteddef;
  2502. result:=true;
  2503. exit;
  2504. end;
  2505. nesteddef:=tdef(nesteddef.owner.defowner);
  2506. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2507. resultdef:=nil;
  2508. result:=false;
  2509. end;
  2510. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2511. symvisibility: visibility of the symbol
  2512. contextobjdef: via which def the symbol is accessed, e.g.:
  2513. fieldname:=1 -> contextobjdef = current_structdef
  2514. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2515. }
  2516. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2517. var
  2518. symownerdef : tabstractrecorddef;
  2519. nonlocalst : tsymtable;
  2520. begin
  2521. result:=false;
  2522. { Get objdectdef owner of the symtable for the is_related checks }
  2523. if not assigned(symst) or
  2524. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2525. internalerror(200810285);
  2526. symownerdef:=tabstractrecorddef(symst.defowner);
  2527. { specializations might belong to a localsymtable or parasymtable }
  2528. nonlocalst:=symownerdef.owner;
  2529. if tstoreddef(symst.defowner).is_specialization then
  2530. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2531. nonlocalst:=nonlocalst.defowner.owner;
  2532. case symvisibility of
  2533. vis_private :
  2534. begin
  2535. { private symbols are allowed when we are in the same
  2536. module as they are defined }
  2537. result:=(
  2538. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2539. (nonlocalst.iscurrentunit)
  2540. ) or
  2541. ( // the case of specialize inside the generic declaration and nested types
  2542. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2543. (
  2544. assigned(current_structdef) and
  2545. (
  2546. (current_structdef=symownerdef) or
  2547. (current_structdef.owner.iscurrentunit)
  2548. )
  2549. ) or
  2550. (
  2551. not assigned(current_structdef) and
  2552. (symownerdef.owner.iscurrentunit)
  2553. )
  2554. );
  2555. end;
  2556. vis_strictprivate :
  2557. begin
  2558. result:=assigned(current_structdef) and
  2559. is_owned_by(current_structdef,symownerdef);
  2560. end;
  2561. vis_strictprotected :
  2562. begin
  2563. result:=(
  2564. { access from nested class }
  2565. assigned(current_structdef) and
  2566. is_owned_by(current_structdef,symownerdef)
  2567. ) or
  2568. (
  2569. { access from child class }
  2570. assigned(contextobjdef) and
  2571. assigned(current_structdef) and
  2572. def_is_related(contextobjdef,symownerdef) and
  2573. def_is_related(current_structdef,contextobjdef)
  2574. ) or
  2575. (
  2576. { helpers can access strict protected symbols }
  2577. is_objectpascal_helper(contextobjdef) and
  2578. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2579. ) or
  2580. (
  2581. { same as above, but from context of call node inside
  2582. helper method }
  2583. is_objectpascal_helper(current_structdef) and
  2584. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2585. );
  2586. end;
  2587. vis_protected :
  2588. begin
  2589. { protected symbols are visible in the module that defines them and
  2590. also visible to related objects. The related object must be defined
  2591. in the current module }
  2592. result:=(
  2593. (
  2594. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2595. (nonlocalst.iscurrentunit)
  2596. ) or
  2597. (
  2598. assigned(contextobjdef) and
  2599. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  2600. (contextobjdef.owner.iscurrentunit) and
  2601. def_is_related(contextobjdef,symownerdef)
  2602. ) or
  2603. ( // the case of specialize inside the generic declaration and nested types
  2604. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2605. (
  2606. assigned(current_structdef) and
  2607. (
  2608. (current_structdef=symownerdef) or
  2609. (current_structdef.owner.iscurrentunit)
  2610. )
  2611. ) or
  2612. (
  2613. not assigned(current_structdef) and
  2614. (symownerdef.owner.iscurrentunit)
  2615. ) or
  2616. (
  2617. { helpers can access protected symbols }
  2618. is_objectpascal_helper(contextobjdef) and
  2619. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2620. )
  2621. )
  2622. );
  2623. end;
  2624. vis_public,
  2625. vis_published :
  2626. result:=true;
  2627. end;
  2628. end;
  2629. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2630. begin
  2631. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2632. end;
  2633. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2634. var
  2635. i : longint;
  2636. pd : tprocdef;
  2637. begin
  2638. if sym.typ=procsym then
  2639. begin
  2640. { A procsym is visible, when there is at least one of the procdefs visible }
  2641. result:=false;
  2642. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2643. begin
  2644. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2645. if (pd.owner=sym.owner) and
  2646. is_visible_for_object(pd,contextobjdef) then
  2647. begin
  2648. result:=true;
  2649. exit;
  2650. end;
  2651. end;
  2652. end
  2653. else
  2654. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2655. end;
  2656. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2657. begin
  2658. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2659. end;
  2660. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2661. begin
  2662. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2663. end;
  2664. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2665. var
  2666. hashedid: THashedIDString;
  2667. contextstructdef: tabstractrecorddef;
  2668. stackitem: psymtablestackitem;
  2669. begin
  2670. result:=false;
  2671. hashedid.id:=s;
  2672. stackitem:=symtablestack.stack;
  2673. while assigned(stackitem) do
  2674. begin
  2675. srsymtable:=stackitem^.symtable;
  2676. if (srsymtable.symtabletype=objectsymtable) then
  2677. begin
  2678. { TODO : implement the search for an option in classes as well }
  2679. if ssf_search_option in flags then
  2680. begin
  2681. result:=false;
  2682. exit;
  2683. end;
  2684. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2685. begin
  2686. result:=true;
  2687. exit;
  2688. end;
  2689. end
  2690. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2691. (srsymtable.defowner.typ=undefineddef)) then
  2692. begin
  2693. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2694. { First check if it is a unit/namespace symbol.
  2695. They are visible only if they are from the current unit or
  2696. unit of generic of currently processed specialization. }
  2697. if assigned(srsym) and
  2698. (
  2699. not(srsym.typ in [unitsym,namespacesym]) or
  2700. srsymtable.iscurrentunit or
  2701. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2702. ) and
  2703. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  2704. begin
  2705. { use the class from withsymtable only when it is
  2706. defined in this unit }
  2707. if (srsymtable.symtabletype=withsymtable) and
  2708. assigned(srsymtable.defowner) and
  2709. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2710. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2711. (srsymtable.defowner.owner.iscurrentunit) then
  2712. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2713. else
  2714. contextstructdef:=current_structdef;
  2715. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2716. is_visible_for_object(srsym,contextstructdef) then
  2717. begin
  2718. { we need to know if a procedure references symbols
  2719. in the static symtable, because then it can't be
  2720. inlined from outside this unit }
  2721. if assigned(current_procinfo) and
  2722. (srsym.owner.symtabletype=staticsymtable) then
  2723. include(current_procinfo.flags,pi_uses_static_symtable);
  2724. if not (ssf_no_addsymref in flags) then
  2725. addsymref(srsym);
  2726. result:=true;
  2727. exit;
  2728. end;
  2729. end;
  2730. end;
  2731. stackitem:=stackitem^.next;
  2732. end;
  2733. srsym:=nil;
  2734. srsymtable:=nil;
  2735. end;
  2736. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2737. srsymtable:TSymtable;option:tsymoption):boolean;
  2738. begin
  2739. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  2740. end;
  2741. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2742. var
  2743. hashedid : THashedIDString;
  2744. stackitem : psymtablestackitem;
  2745. classh : tobjectdef;
  2746. begin
  2747. result:=false;
  2748. hashedid.id:=s;
  2749. stackitem:=symtablestack.stack;
  2750. while assigned(stackitem) do
  2751. begin
  2752. {
  2753. It is not possible to have type symbols in:
  2754. parameters
  2755. Exception are classes, objects, records, generic definitions and specializations
  2756. that have the parameterized types inserted in the symtable.
  2757. }
  2758. srsymtable:=stackitem^.symtable;
  2759. if (srsymtable.symtabletype=ObjectSymtable) then
  2760. begin
  2761. classh:=tobjectdef(srsymtable.defowner);
  2762. while assigned(classh) do
  2763. begin
  2764. srsymtable:=classh.symtable;
  2765. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2766. if assigned(srsym) and
  2767. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2768. is_visible_for_object(srsym,current_structdef) then
  2769. begin
  2770. addsymref(srsym);
  2771. result:=true;
  2772. exit;
  2773. end;
  2774. classh:=classh.childof;
  2775. end;
  2776. end
  2777. else
  2778. begin
  2779. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2780. if assigned(srsym) and
  2781. (
  2782. not(srsym.typ in [unitsym,namespacesym]) or
  2783. srsymtable.iscurrentunit or
  2784. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2785. ) and
  2786. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2787. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2788. begin
  2789. { we need to know if a procedure references symbols
  2790. in the static symtable, because then it can't be
  2791. inlined from outside this unit }
  2792. if assigned(current_procinfo) and
  2793. (srsym.owner.symtabletype=staticsymtable) then
  2794. include(current_procinfo.flags,pi_uses_static_symtable);
  2795. addsymref(srsym);
  2796. result:=true;
  2797. exit;
  2798. end;
  2799. end;
  2800. stackitem:=stackitem^.next;
  2801. end;
  2802. result:=false;
  2803. srsym:=nil;
  2804. srsymtable:=nil;
  2805. end;
  2806. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2807. var
  2808. pmod : tmodule;
  2809. begin
  2810. pmod:=tmodule(pm);
  2811. result:=false;
  2812. if assigned(pmod.globalsymtable) then
  2813. begin
  2814. srsym:=tsym(pmod.globalsymtable.Find(s));
  2815. if assigned(srsym) then
  2816. begin
  2817. srsymtable:=pmod.globalsymtable;
  2818. addsymref(srsym);
  2819. result:=true;
  2820. exit;
  2821. end;
  2822. end;
  2823. { If the module is the current unit we also need
  2824. to search the local symtable }
  2825. if (pmod=current_module) and
  2826. assigned(pmod.localsymtable) then
  2827. begin
  2828. srsym:=tsym(pmod.localsymtable.Find(s));
  2829. if assigned(srsym) then
  2830. begin
  2831. srsymtable:=pmod.localsymtable;
  2832. addsymref(srsym);
  2833. result:=true;
  2834. exit;
  2835. end;
  2836. end;
  2837. srsym:=nil;
  2838. srsymtable:=nil;
  2839. end;
  2840. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2841. var
  2842. stackitem : psymtablestackitem;
  2843. begin
  2844. result:=false;
  2845. stackitem:=symtablestack.stack;
  2846. while assigned(stackitem) do
  2847. begin
  2848. srsymtable:=stackitem^.symtable;
  2849. if (srsymtable.symtabletype=globalsymtable) and
  2850. (srsymtable.name^=unitname) then
  2851. begin
  2852. srsym:=tsym(srsymtable.find(symname));
  2853. if not assigned(srsym) then
  2854. break;
  2855. result:=true;
  2856. exit;
  2857. end;
  2858. stackitem:=stackitem^.next;
  2859. end;
  2860. { If the module is the current unit we also need
  2861. to search the local symtable }
  2862. if assigned(current_module.localsymtable) and
  2863. (current_module.localsymtable.name^=unitname) then
  2864. begin
  2865. srsymtable:=current_module.localsymtable;
  2866. srsym:=tsym(srsymtable.find(symname));
  2867. if assigned(srsym) then
  2868. begin
  2869. result:=true;
  2870. exit;
  2871. end;
  2872. end;
  2873. end;
  2874. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2875. begin
  2876. result:=pd;
  2877. if pd.typ<>objectdef then
  2878. exit;
  2879. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2880. end;
  2881. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2882. var
  2883. hashedid : THashedIDString;
  2884. stackitem : psymtablestackitem;
  2885. srsymtable : tsymtable;
  2886. srsym : tsym;
  2887. formalname,
  2888. foundname : shortstring;
  2889. formalnameptr,
  2890. foundnameptr: pshortstring;
  2891. begin
  2892. { not a formal definition -> return it }
  2893. if not(oo_is_formal in pd.objectoptions) then
  2894. begin
  2895. result:=pd;
  2896. exit;
  2897. end;
  2898. hashedid.id:=pd.typesym.name;
  2899. stackitem:=symtablestack.stack;
  2900. while assigned(stackitem) do
  2901. begin
  2902. srsymtable:=stackitem^.symtable;
  2903. { ObjC classes can't appear in generics or as nested class
  2904. definitions. Java classes can. }
  2905. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2906. (is_java_class_or_interface(pd) and
  2907. (srsymtable.symtabletype=ObjectSymtable)) then
  2908. begin
  2909. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2910. if assigned(srsym) and
  2911. (srsym.typ=typesym) and
  2912. (ttypesym(srsym).typedef.typ=objectdef) and
  2913. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2914. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2915. begin
  2916. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2917. begin
  2918. { the external name for the formal and the real
  2919. definition must match }
  2920. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2921. assigned(pd.import_lib) then
  2922. begin
  2923. if assigned(pd.import_lib) then
  2924. formalname:=pd.import_lib^+'.'
  2925. else
  2926. formalname:='';
  2927. formalname:=formalname+pd.objextname^;
  2928. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2929. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2930. else
  2931. foundname:='';
  2932. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2933. formalnameptr:=@formalname;
  2934. foundnameptr:=@foundname;
  2935. end
  2936. else
  2937. begin
  2938. formalnameptr:=pd.objextname;
  2939. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2940. end;
  2941. if foundnameptr^<>formalnameptr^ then
  2942. begin
  2943. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2944. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2945. end;
  2946. end;
  2947. result:=tobjectdef(ttypesym(srsym).typedef);
  2948. if assigned(current_procinfo) and
  2949. (srsym.owner.symtabletype=staticsymtable) then
  2950. include(current_procinfo.flags,pi_uses_static_symtable);
  2951. addsymref(srsym);
  2952. exit;
  2953. end;
  2954. end;
  2955. stackitem:=stackitem^.next;
  2956. end;
  2957. { nothing found: optionally give an error and return the original
  2958. (empty) one }
  2959. if erroronfailure then
  2960. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2961. result:=pd;
  2962. end;
  2963. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2964. var
  2965. hashedid : THashedIDString;
  2966. orgclass : tobjectdef;
  2967. i : longint;
  2968. hlpsrsym : tsym;
  2969. hlpsrsymtable : tsymtable;
  2970. begin
  2971. orgclass:=classh;
  2972. { in case this is a formal class, first find the real definition }
  2973. if assigned(classh) then
  2974. begin
  2975. if (oo_is_formal in classh.objectoptions) then
  2976. classh:=find_real_class_definition(classh,true);
  2977. { The contextclassh is used for visibility. The classh must be equal to
  2978. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2979. parent or a class helper. }
  2980. if not (def_is_related(contextclassh,classh) or
  2981. (is_classhelper(contextclassh) and
  2982. assigned(tobjectdef(contextclassh).extendeddef) and
  2983. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  2984. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  2985. internalerror(200811161);
  2986. end;
  2987. result:=false;
  2988. hashedid.id:=s;
  2989. { an Objective-C protocol or Java interface can inherit from multiple
  2990. other protocols/interfaces -> use ImplementedInterfaces instead }
  2991. if is_objcprotocol(classh) or
  2992. is_javainterface(classh) then
  2993. begin
  2994. srsymtable:=classh.symtable;
  2995. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2996. if assigned(srsym) and
  2997. is_visible_for_object(srsym,contextclassh) then
  2998. begin
  2999. if not (ssf_no_addsymref in flags) then
  3000. addsymref(srsym);
  3001. result:=true;
  3002. exit;
  3003. end;
  3004. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3005. begin
  3006. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3007. begin
  3008. result:=true;
  3009. exit;
  3010. end;
  3011. end;
  3012. end
  3013. else
  3014. if is_objectpascal_helper(classh) then
  3015. begin
  3016. { helpers have their own obscure search logic... }
  3017. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3018. if result then
  3019. exit;
  3020. end
  3021. else
  3022. begin
  3023. hlpsrsym:=nil;
  3024. hlpsrsymtable:=nil;
  3025. while assigned(classh) do
  3026. begin
  3027. { search for a class helper method first if this is an Object
  3028. Pascal class and we haven't yet found a helper symbol }
  3029. if is_class(classh) and
  3030. (ssf_search_helper in flags) and
  3031. not assigned(hlpsrsym) then
  3032. begin
  3033. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3034. if result then
  3035. { if the procsym is overloaded we need to use the
  3036. "original" symbol; the helper symbol will be found when
  3037. searching for overloads }
  3038. if (srsym.typ<>procsym) or
  3039. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3040. exit
  3041. else
  3042. begin
  3043. { remember the found symbol if the class hierarchy
  3044. should not contain the a method with that name }
  3045. hlpsrsym:=srsym;
  3046. hlpsrsymtable:=srsymtable;
  3047. end;
  3048. end;
  3049. srsymtable:=classh.symtable;
  3050. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3051. if assigned(srsym) and
  3052. is_visible_for_object(srsym,contextclassh) then
  3053. begin
  3054. if not (ssf_no_addsymref in flags) then
  3055. addsymref(srsym);
  3056. result:=true;
  3057. exit;
  3058. end;
  3059. classh:=classh.childof;
  3060. end;
  3061. { did we find a helper symbol, but no symbol with the same name in
  3062. the extended object's hierarchy? }
  3063. if assigned(hlpsrsym) then
  3064. begin
  3065. srsym:=hlpsrsym;
  3066. srsymtable:=hlpsrsymtable;
  3067. result:=true;
  3068. exit;
  3069. end;
  3070. end;
  3071. if is_objcclass(orgclass) then
  3072. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3073. else
  3074. begin
  3075. srsym:=nil;
  3076. srsymtable:=nil;
  3077. end;
  3078. end;
  3079. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3080. var
  3081. hashedid : THashedIDString;
  3082. hlpsrsym : tsym;
  3083. hlpsrsymtable : tsymtable;
  3084. begin
  3085. result:=false;
  3086. hlpsrsym:=nil;
  3087. hlpsrsymtable:=nil;
  3088. hashedid.id:=s;
  3089. { search for a record helper method first }
  3090. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3091. if result then
  3092. { if the procsym is overloaded we need to use the
  3093. "original" symbol; the helper symbol will be found when
  3094. searching for overloads }
  3095. if (srsym.typ<>procsym) or
  3096. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3097. exit
  3098. else
  3099. begin
  3100. { remember the found symbol if we should not find a symbol with
  3101. the same name in the extended record }
  3102. hlpsrsym:=srsym;
  3103. hlpsrsymtable:=srsymtable;
  3104. end;
  3105. srsymtable:=recordh.symtable;
  3106. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3107. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3108. begin
  3109. addsymref(srsym);
  3110. result:=true;
  3111. exit;
  3112. end;
  3113. srsym:=hlpsrsym;
  3114. srsymtable:=hlpsrsymtable;
  3115. result:=assigned(srsym);
  3116. end;
  3117. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3118. var
  3119. def : tdef;
  3120. i : longint;
  3121. begin
  3122. { in case this is a formal class, first find the real definition }
  3123. if assigned(classh) and
  3124. (oo_is_formal in classh.objectoptions) then
  3125. classh:=find_real_class_definition(classh,true);
  3126. result:=false;
  3127. def:=nil;
  3128. while assigned(classh) do
  3129. begin
  3130. for i:=0 to classh.symtable.DefList.Count-1 do
  3131. begin
  3132. def:=tstoreddef(classh.symtable.DefList[i]);
  3133. { Find also all hidden private methods to
  3134. be compatible with delphi, see tw6203 (PFV) }
  3135. if (def.typ=procdef) and
  3136. (po_msgint in tprocdef(def).procoptions) and
  3137. (tprocdef(def).messageinf.i=msgid) then
  3138. begin
  3139. srdef:=def;
  3140. srsym:=tprocdef(def).procsym;
  3141. srsymtable:=classh.symtable;
  3142. addsymref(srsym);
  3143. result:=true;
  3144. exit;
  3145. end;
  3146. end;
  3147. classh:=classh.childof;
  3148. end;
  3149. srdef:=nil;
  3150. srsym:=nil;
  3151. srsymtable:=nil;
  3152. end;
  3153. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3154. var
  3155. def : tdef;
  3156. i : longint;
  3157. begin
  3158. { in case this is a formal class, first find the real definition }
  3159. if assigned(classh) and
  3160. (oo_is_formal in classh.objectoptions) then
  3161. classh:=find_real_class_definition(classh,true);
  3162. result:=false;
  3163. def:=nil;
  3164. while assigned(classh) do
  3165. begin
  3166. for i:=0 to classh.symtable.DefList.Count-1 do
  3167. begin
  3168. def:=tstoreddef(classh.symtable.DefList[i]);
  3169. { Find also all hidden private methods to
  3170. be compatible with delphi, see tw6203 (PFV) }
  3171. if (def.typ=procdef) and
  3172. (po_msgstr in tprocdef(def).procoptions) and
  3173. (tprocdef(def).messageinf.str^=s) then
  3174. begin
  3175. srsym:=tprocdef(def).procsym;
  3176. srsymtable:=classh.symtable;
  3177. addsymref(srsym);
  3178. result:=true;
  3179. exit;
  3180. end;
  3181. end;
  3182. classh:=classh.childof;
  3183. end;
  3184. srsym:=nil;
  3185. srsymtable:=nil;
  3186. end;
  3187. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3188. var
  3189. hashedid : THashedIDString;
  3190. parentclassh : tobjectdef;
  3191. begin
  3192. result:=false;
  3193. if not is_objectpascal_helper(classh) then
  3194. Internalerror(2011030101);
  3195. hashedid.id:=s;
  3196. { in a helper things are a bit more complex:
  3197. 1. search the symbol in the helper (if not "inherited")
  3198. 2. search the symbol in the extended type
  3199. 3. search the symbol in the parent helpers
  3200. 4. only classes: search the symbol in the parents of the extended type
  3201. }
  3202. if not (ssf_has_inherited in flags) then
  3203. begin
  3204. { search in the helper itself }
  3205. srsymtable:=classh.symtable;
  3206. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3207. if assigned(srsym) and
  3208. is_visible_for_object(srsym,contextclassh) then
  3209. begin
  3210. if not (ssf_no_addsymref in flags) then
  3211. addsymref(srsym);
  3212. result:=true;
  3213. exit;
  3214. end;
  3215. end;
  3216. { now search in the extended type itself }
  3217. { Note: the extendeddef might be Nil if we are currently parsing the
  3218. extended type itself and the identifier was not found }
  3219. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3220. begin
  3221. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3222. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3223. if assigned(srsym) and
  3224. is_visible_for_object(srsym,contextclassh) then
  3225. begin
  3226. if not (ssf_no_addsymref in flags) then
  3227. addsymref(srsym);
  3228. result:=true;
  3229. exit;
  3230. end;
  3231. end;
  3232. { now search in the parent helpers }
  3233. parentclassh:=classh.childof;
  3234. while assigned(parentclassh) do
  3235. begin
  3236. srsymtable:=parentclassh.symtable;
  3237. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3238. if assigned(srsym) and
  3239. is_visible_for_object(srsym,contextclassh) then
  3240. begin
  3241. if not (ssf_no_addsymref in flags) then
  3242. addsymref(srsym);
  3243. result:=true;
  3244. exit;
  3245. end;
  3246. parentclassh:=parentclassh.childof;
  3247. end;
  3248. if is_class(classh.extendeddef) then
  3249. { now search in the parents of the extended class (with helpers!) }
  3250. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3251. { addsymref is already called by searchsym_in_class }
  3252. end;
  3253. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3254. var
  3255. sym : Tprocsym;
  3256. hashedid : THashedIDString;
  3257. curreq,
  3258. besteq : tequaltype;
  3259. currpd,
  3260. bestpd : tprocdef;
  3261. stackitem : psymtablestackitem;
  3262. begin
  3263. hashedid.id:=overloaded_names[assignment_type];
  3264. besteq:=te_incompatible;
  3265. bestpd:=nil;
  3266. stackitem:=symtablestack.stack;
  3267. while assigned(stackitem) do
  3268. begin
  3269. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3270. if sym<>nil then
  3271. begin
  3272. if sym.typ<>procsym then
  3273. internalerror(200402031);
  3274. { if the source type is an alias then this is only the second choice,
  3275. if you mess with this code, check tw4093 }
  3276. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  3277. if curreq>besteq then
  3278. begin
  3279. besteq:=curreq;
  3280. bestpd:=currpd;
  3281. if (besteq=te_exact) then
  3282. break;
  3283. end;
  3284. end;
  3285. stackitem:=stackitem^.next;
  3286. end;
  3287. result:=bestpd;
  3288. end;
  3289. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3290. begin
  3291. { search record/object symtable first for a suitable operator }
  3292. if from_def.typ in [recorddef,objectdef] then
  3293. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3294. if to_def.typ in [recorddef,objectdef] then
  3295. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3296. { if type conversion is explicit then search first for explicit
  3297. operator overload and if not found then use implicit operator }
  3298. if explicit then
  3299. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3300. else
  3301. result:=nil;
  3302. if result=nil then
  3303. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3304. { restore symtable stack }
  3305. if to_def.typ in [recorddef,objectdef] then
  3306. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3307. if from_def.typ in [recorddef,objectdef] then
  3308. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3309. end;
  3310. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3311. var
  3312. sym : Tprocsym;
  3313. hashedid : THashedIDString;
  3314. curreq,
  3315. besteq : tequaltype;
  3316. currpd,
  3317. bestpd : tprocdef;
  3318. stackitem : psymtablestackitem;
  3319. begin
  3320. hashedid.id:='enumerator';
  3321. besteq:=te_incompatible;
  3322. bestpd:=nil;
  3323. stackitem:=symtablestack.stack;
  3324. while assigned(stackitem) do
  3325. begin
  3326. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3327. if sym<>nil then
  3328. begin
  3329. if sym.typ<>procsym then
  3330. internalerror(200910241);
  3331. { if the source type is an alias then this is only the second choice,
  3332. if you mess with this code, check tw4093 }
  3333. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3334. if curreq>besteq then
  3335. begin
  3336. besteq:=curreq;
  3337. bestpd:=currpd;
  3338. if (besteq=te_exact) then
  3339. break;
  3340. end;
  3341. end;
  3342. stackitem:=stackitem^.next;
  3343. end;
  3344. result:=bestpd;
  3345. end;
  3346. function search_system_type(const s: TIDString): ttypesym;
  3347. var
  3348. sym : tsym;
  3349. begin
  3350. sym:=tsym(systemunit.Find(s));
  3351. if not assigned(sym) or
  3352. (sym.typ<>typesym) then
  3353. message1(cg_f_unknown_system_type,s);
  3354. result:=ttypesym(sym);
  3355. end;
  3356. function try_search_system_type(const s: TIDString): ttypesym;
  3357. var
  3358. sym : tsym;
  3359. begin
  3360. sym:=tsym(systemunit.Find(s));
  3361. if not assigned(sym) then
  3362. result:=nil
  3363. else
  3364. begin
  3365. if sym.typ<>typesym then
  3366. message1(cg_f_unknown_system_type,s);
  3367. result:=ttypesym(sym);
  3368. end;
  3369. end;
  3370. function try_search_current_module_type(const s: TIDString): ttypesym;
  3371. var
  3372. found: boolean;
  3373. srsymtable: tsymtable;
  3374. srsym: tsym;
  3375. begin
  3376. if s[1]='$' then
  3377. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  3378. else
  3379. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  3380. if found then
  3381. begin
  3382. if (srsym.typ<>typesym) then
  3383. internalerror(2014091207);
  3384. result:=ttypesym(srsym);
  3385. end
  3386. else
  3387. result:=nil;
  3388. end;
  3389. function search_system_proc(const s: TIDString): tprocdef;
  3390. var
  3391. srsym: tsym;
  3392. begin
  3393. srsym:=tsym(systemunit.find(s));
  3394. if not assigned(srsym) and
  3395. (cs_compilesystem in current_settings.moduleswitches) then
  3396. srsym:=tsym(systemunit.Find(upper(s)));
  3397. if not assigned(srsym) or
  3398. (srsym.typ<>procsym) then
  3399. message1(cg_f_unknown_compilerproc,s);
  3400. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3401. end;
  3402. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  3403. var
  3404. srsymtable: tsymtable;
  3405. sym: tsym;
  3406. begin
  3407. sym:=nil;
  3408. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  3409. (sym.typ=typesym) then
  3410. begin
  3411. result:=ttypesym(sym);
  3412. exit;
  3413. end
  3414. else
  3415. begin
  3416. if throwerror then
  3417. message2(cg_f_unknown_type_in_unit,typename,unitname);
  3418. result:=nil;
  3419. end;
  3420. end;
  3421. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  3422. var
  3423. s: string;
  3424. list: TFPObjectList;
  3425. i: integer;
  3426. st: tsymtable;
  3427. begin
  3428. result:=false;
  3429. odef:=nil;
  3430. { when there are no helpers active currently then we don't need to do
  3431. anything }
  3432. if current_module.extendeddefs.count=0 then
  3433. exit;
  3434. { no helpers for anonymous types }
  3435. if ((pd.typ in [recorddef,objectdef]) and
  3436. (
  3437. not assigned(tabstractrecorddef(pd).objrealname) or
  3438. (tabstractrecorddef(pd).objrealname^='')
  3439. )
  3440. ) or
  3441. not assigned(pd.typesym) then
  3442. exit;
  3443. { if pd is defined inside a procedure we must not use make_mangledname
  3444. (as a helper may not be defined in a procedure this is no problem...)}
  3445. st:=pd.owner;
  3446. while st.symtabletype in [objectsymtable,recordsymtable] do
  3447. st:=st.defowner.owner;
  3448. if st.symtabletype=localsymtable then
  3449. exit;
  3450. { the mangled name is used as the key for tmodule.extendeddefs }
  3451. s:=generate_objectpascal_helper_key(pd);
  3452. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  3453. if assigned(list) and (list.count>0) then
  3454. begin
  3455. i:=list.count-1;
  3456. repeat
  3457. odef:=tobjectdef(list[list.count-1]);
  3458. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3459. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3460. dec(i);
  3461. until result or (i<0);
  3462. if not result then
  3463. { just to be sure that noone uses odef }
  3464. odef:=nil;
  3465. end;
  3466. end;
  3467. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3468. var
  3469. hashedid : THashedIDString;
  3470. classh : tobjectdef;
  3471. i : integer;
  3472. pdef : tprocdef;
  3473. begin
  3474. result:=false;
  3475. { if there is no class helper for the class then there is no need to
  3476. search further }
  3477. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  3478. exit;
  3479. hashedid.id:=s;
  3480. repeat
  3481. srsymtable:=classh.symtable;
  3482. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3483. if srsym<>nil then
  3484. begin
  3485. case srsym.typ of
  3486. procsym:
  3487. begin
  3488. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3489. begin
  3490. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  3491. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  3492. continue;
  3493. { we need to know if a procedure references symbols
  3494. in the static symtable, because then it can't be
  3495. inlined from outside this unit }
  3496. if assigned(current_procinfo) and
  3497. (srsym.owner.symtabletype=staticsymtable) then
  3498. include(current_procinfo.flags,pi_uses_static_symtable);
  3499. { the first found method wins }
  3500. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3501. srsymtable:=srsym.owner;
  3502. addsymref(srsym);
  3503. result:=true;
  3504. exit;
  3505. end;
  3506. end;
  3507. typesym,
  3508. fieldvarsym,
  3509. constsym,
  3510. enumsym,
  3511. undefinedsym,
  3512. propertysym:
  3513. begin
  3514. addsymref(srsym);
  3515. result:=true;
  3516. exit;
  3517. end;
  3518. else
  3519. internalerror(2014041101);
  3520. end;
  3521. end;
  3522. { try the helper parent if available }
  3523. classh:=classh.childof;
  3524. until classh=nil;
  3525. srsym:=nil;
  3526. srsymtable:=nil;
  3527. end;
  3528. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3529. var
  3530. hashedid : THashedIDString;
  3531. stackitem : psymtablestackitem;
  3532. i : longint;
  3533. defowner : tobjectdef;
  3534. begin
  3535. hashedid.id:=class_helper_prefix+s;
  3536. stackitem:=symtablestack.stack;
  3537. while assigned(stackitem) do
  3538. begin
  3539. srsymtable:=stackitem^.symtable;
  3540. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3541. if assigned(srsym) then
  3542. begin
  3543. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3544. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3545. (srsym.typ<>procsym) then
  3546. internalerror(2009111505);
  3547. { check whether this procsym includes a helper for this particular class }
  3548. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3549. begin
  3550. { does pd inherit from (or is the same as) the class
  3551. that this method's category extended?
  3552. Warning: this list contains both category and objcclass methods
  3553. (for id.randommethod), so only check category methods here
  3554. }
  3555. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3556. if (oo_is_classhelper in defowner.objectoptions) and
  3557. def_is_related(pd,defowner.childof) then
  3558. begin
  3559. { we need to know if a procedure references symbols
  3560. in the static symtable, because then it can't be
  3561. inlined from outside this unit }
  3562. if assigned(current_procinfo) and
  3563. (srsym.owner.symtabletype=staticsymtable) then
  3564. include(current_procinfo.flags,pi_uses_static_symtable);
  3565. { no need to keep looking. There might be other
  3566. categories that extend this, a parent or child
  3567. class with a method with the same name (either
  3568. overriding this one, or overridden by this one),
  3569. but that doesn't matter as far as the basic
  3570. procsym is concerned.
  3571. }
  3572. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3573. srsymtable:=srsym.owner;
  3574. addsymref(srsym);
  3575. result:=true;
  3576. exit;
  3577. end;
  3578. end;
  3579. end;
  3580. stackitem:=stackitem^.next;
  3581. end;
  3582. srsym:=nil;
  3583. srsymtable:=nil;
  3584. result:=false;
  3585. end;
  3586. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3587. var
  3588. hashedid : THashedIDString;
  3589. stackitem : psymtablestackitem;
  3590. i : longint;
  3591. begin
  3592. hashedid.id:=class_helper_prefix+s;
  3593. stackitem:=symtablestack.stack;
  3594. while assigned(stackitem) do
  3595. begin
  3596. srsymtable:=stackitem^.symtable;
  3597. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3598. if assigned(srsym) then
  3599. begin
  3600. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3601. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3602. (srsym.typ<>procsym) then
  3603. internalerror(2009112005);
  3604. { check whether this procsym includes a helper for this particular class }
  3605. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3606. begin
  3607. { we need to know if a procedure references symbols
  3608. in the static symtable, because then it can't be
  3609. inlined from outside this unit }
  3610. if assigned(current_procinfo) and
  3611. (srsym.owner.symtabletype=staticsymtable) then
  3612. include(current_procinfo.flags,pi_uses_static_symtable);
  3613. { no need to keep looking. There might be other
  3614. methods with the same name, but that doesn't matter
  3615. as far as the basic procsym is concerned.
  3616. }
  3617. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3618. { We need the symtable in which the classhelper-like sym
  3619. is located, not the objectdef. The reason is that the
  3620. callnode will climb the symtablestack until it encounters
  3621. this symtable to start looking for overloads (and it won't
  3622. find the objectsymtable in which this method sym is
  3623. located
  3624. srsymtable:=srsym.owner;
  3625. }
  3626. addsymref(srsym);
  3627. result:=true;
  3628. exit;
  3629. end;
  3630. end;
  3631. stackitem:=stackitem^.next;
  3632. end;
  3633. srsym:=nil;
  3634. srsymtable:=nil;
  3635. result:=false;
  3636. end;
  3637. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  3638. { searches n in symtable of pd and all anchestors }
  3639. var
  3640. srsymtable : tsymtable;
  3641. begin
  3642. { in case this is a formal class, first find the real definition }
  3643. if (oo_is_formal in pd.objectoptions) then
  3644. pd:=find_real_class_definition(tobjectdef(pd),true);
  3645. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  3646. exit;
  3647. result:=search_struct_member_no_helper(pd,s);
  3648. if assigned(result) then
  3649. exit;
  3650. { not found, now look for class helpers }
  3651. if is_objcclass(pd) then
  3652. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  3653. end;
  3654. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  3655. var
  3656. hashedid : THashedIDString;
  3657. srsym : tsym;
  3658. begin
  3659. hashedid.id:=s;
  3660. while assigned(pd) do
  3661. begin
  3662. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  3663. if assigned(srsym) then
  3664. begin
  3665. result:=srsym;
  3666. exit;
  3667. end;
  3668. if pd.typ=objectdef then
  3669. pd:=tobjectdef(pd).childof
  3670. else
  3671. pd:=nil;
  3672. end;
  3673. result:=nil;
  3674. end;
  3675. function search_macro(const s : string):tsym;
  3676. var
  3677. stackitem : psymtablestackitem;
  3678. hashedid : THashedIDString;
  3679. srsym : tsym;
  3680. begin
  3681. hashedid.id:=s;
  3682. { First search the localmacrosymtable before searching the
  3683. global macrosymtables from the units }
  3684. if assigned(current_module) then
  3685. begin
  3686. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  3687. if assigned(srsym) then
  3688. begin
  3689. result:= srsym;
  3690. exit;
  3691. end;
  3692. end;
  3693. stackitem:=macrosymtablestack.stack;
  3694. while assigned(stackitem) do
  3695. begin
  3696. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3697. if assigned(srsym) then
  3698. begin
  3699. result:= srsym;
  3700. exit;
  3701. end;
  3702. stackitem:=stackitem^.next;
  3703. end;
  3704. result:= nil;
  3705. end;
  3706. function defined_macro(const s : string):boolean;
  3707. var
  3708. mac: tmacro;
  3709. begin
  3710. mac:=tmacro(search_macro(s));
  3711. if assigned(mac) then
  3712. begin
  3713. mac.is_used:=true;
  3714. defined_macro:=mac.defined;
  3715. end
  3716. else
  3717. defined_macro:=false;
  3718. end;
  3719. {****************************************************************************
  3720. Object Helpers
  3721. ****************************************************************************}
  3722. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  3723. { returns the default property of a class, searches also anchestors }
  3724. var
  3725. _defaultprop : tpropertysym;
  3726. helperpd : tobjectdef;
  3727. begin
  3728. _defaultprop:=nil;
  3729. { first search in helper's hierarchy }
  3730. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3731. while assigned(helperpd) do
  3732. begin
  3733. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3734. if assigned(_defaultprop) then
  3735. break;
  3736. helperpd:=helperpd.childof;
  3737. end;
  3738. if assigned(_defaultprop) then
  3739. begin
  3740. search_default_property:=_defaultprop;
  3741. exit;
  3742. end;
  3743. { now search in the type's hierarchy itself }
  3744. while assigned(pd) do
  3745. begin
  3746. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3747. if assigned(_defaultprop) then
  3748. break;
  3749. if (pd.typ=objectdef) then
  3750. pd:=tobjectdef(pd).childof
  3751. else
  3752. break;
  3753. end;
  3754. search_default_property:=_defaultprop;
  3755. end;
  3756. {****************************************************************************
  3757. Macro Helpers
  3758. ****************************************************************************}
  3759. procedure def_system_macro(const name : string);
  3760. var
  3761. mac : tmacro;
  3762. s: string;
  3763. begin
  3764. if name = '' then
  3765. internalerror(2004121202);
  3766. s:= upper(name);
  3767. mac:=tmacro(search_macro(s));
  3768. if not assigned(mac) then
  3769. begin
  3770. mac:=tmacro.create(s);
  3771. if assigned(current_module) then
  3772. current_module.localmacrosymtable.insert(mac)
  3773. else
  3774. initialmacrosymtable.insert(mac);
  3775. end;
  3776. Message1(parser_c_macro_defined,mac.name);
  3777. mac.defined:=true;
  3778. end;
  3779. procedure set_system_macro(const name, value : string);
  3780. var
  3781. mac : tmacro;
  3782. s: string;
  3783. begin
  3784. if name = '' then
  3785. internalerror(2004121203);
  3786. s:= upper(name);
  3787. mac:=tmacro(search_macro(s));
  3788. if not assigned(mac) then
  3789. begin
  3790. mac:=tmacro.create(s);
  3791. if assigned(current_module) then
  3792. current_module.localmacrosymtable.insert(mac)
  3793. else
  3794. initialmacrosymtable.insert(mac);
  3795. end
  3796. else
  3797. begin
  3798. mac.is_compiler_var:=false;
  3799. if assigned(mac.buftext) then
  3800. freemem(mac.buftext,mac.buflen);
  3801. end;
  3802. Message2(parser_c_macro_set_to,mac.name,value);
  3803. mac.buflen:=length(value);
  3804. getmem(mac.buftext,mac.buflen);
  3805. move(value[1],mac.buftext^,mac.buflen);
  3806. mac.defined:=true;
  3807. end;
  3808. procedure set_system_compvar(const name, value : string);
  3809. var
  3810. mac : tmacro;
  3811. s: string;
  3812. begin
  3813. if name = '' then
  3814. internalerror(2004121204);
  3815. s:= upper(name);
  3816. mac:=tmacro(search_macro(s));
  3817. if not assigned(mac) then
  3818. begin
  3819. mac:=tmacro.create(s);
  3820. mac.is_compiler_var:=true;
  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:=true;
  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 undef_system_macro(const name : string);
  3839. var
  3840. mac : tmacro;
  3841. s: string;
  3842. begin
  3843. if name = '' then
  3844. internalerror(2004121205);
  3845. s:= upper(name);
  3846. mac:=tmacro(search_macro(s));
  3847. if not assigned(mac) then
  3848. {If not found, then it's already undefined.}
  3849. else
  3850. begin
  3851. Message1(parser_c_macro_undefined,mac.name);
  3852. mac.defined:=false;
  3853. mac.is_compiler_var:=false;
  3854. { delete old definition }
  3855. if assigned(mac.buftext) then
  3856. begin
  3857. freemem(mac.buftext,mac.buflen);
  3858. mac.buftext:=nil;
  3859. end;
  3860. end;
  3861. end;
  3862. {$ifdef UNITALIASES}
  3863. {****************************************************************************
  3864. TUNIT_ALIAS
  3865. ****************************************************************************}
  3866. constructor tunit_alias.create(const n:string);
  3867. var
  3868. i : longint;
  3869. begin
  3870. i:=pos('=',n);
  3871. if i=0 then
  3872. fail;
  3873. inherited createname(Copy(n,1,i-1));
  3874. newname:=stringdup(Copy(n,i+1,255));
  3875. end;
  3876. destructor tunit_alias.destroy;
  3877. begin
  3878. stringdispose(newname);
  3879. inherited destroy;
  3880. end;
  3881. procedure addunitalias(const n:string);
  3882. begin
  3883. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3884. end;
  3885. function getunitalias(const n:string):string;
  3886. var
  3887. p : punit_alias;
  3888. begin
  3889. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3890. if assigned(p) then
  3891. getunitalias:=punit_alias(p).newname^
  3892. else
  3893. getunitalias:=n;
  3894. end;
  3895. {$endif UNITALIASES}
  3896. {****************************************************************************
  3897. Init/Done Symtable
  3898. ****************************************************************************}
  3899. procedure InitSymtable;
  3900. begin
  3901. { Reset symbolstack }
  3902. symtablestack:=nil;
  3903. systemunit:=nil;
  3904. { create error syms and def }
  3905. generrorsym:=terrorsym.create;
  3906. generrordef:=cerrordef.create;
  3907. { macros }
  3908. initialmacrosymtable:=tmacrosymtable.create(false);
  3909. macrosymtablestack:=TSymtablestack.create;
  3910. macrosymtablestack.push(initialmacrosymtable);
  3911. {$ifdef UNITALIASES}
  3912. { unit aliases }
  3913. unitaliases:=TFPHashObjectList.create;
  3914. {$endif}
  3915. { set some global vars to nil, might be important for the ide }
  3916. class_tobject:=nil;
  3917. interface_iunknown:=nil;
  3918. interface_idispatch:=nil;
  3919. rec_tguid:=nil;
  3920. dupnr:=0;
  3921. end;
  3922. procedure DoneSymtable;
  3923. begin
  3924. generrorsym.owner:=nil;
  3925. generrorsym.free;
  3926. generrordef.owner:=nil;
  3927. generrordef.free;
  3928. initialmacrosymtable.free;
  3929. macrosymtablestack.free;
  3930. {$ifdef UNITALIASES}
  3931. unitaliases.free;
  3932. {$endif}
  3933. end;
  3934. end.