symtable.pas 133 KB

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