symtable.pas 133 KB

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