symtable.pas 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  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: 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. if symtable.name<>nil then
  1739. result:=symtable.name^
  1740. else
  1741. else
  1742. if symtable.name<>nil then
  1743. result:=symtable.name^+delimiter+result
  1744. else
  1745. result:=delimiter+result;
  1746. symtable:=symtable.defowner.owner;
  1747. end;
  1748. end;
  1749. procedure incompatibletypes(def1,def2:tdef);
  1750. begin
  1751. { When there is an errordef there is already an error message show }
  1752. if (def2.typ=errordef) or
  1753. (def1.typ=errordef) then
  1754. exit;
  1755. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1756. end;
  1757. procedure hidesym(sym:TSymEntry);
  1758. begin
  1759. sym.realname:='$hidden'+sym.realname;
  1760. tsym(sym).visibility:=vis_hidden;
  1761. end;
  1762. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1763. var
  1764. st : TSymtable;
  1765. begin
  1766. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1767. { Write hint where the original symbol was found }
  1768. st:=finduniTSymtable(origsym.owner);
  1769. with tsym(origsym).fileinfo do
  1770. begin
  1771. if assigned(st) and
  1772. (st.symtabletype=globalsymtable) and
  1773. st.iscurrentunit then
  1774. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1775. else if assigned(st.name) then
  1776. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1777. end;
  1778. { Rename duplicate sym to an unreachable name, but it can be
  1779. inserted in the symtable without errors }
  1780. inc(dupnr);
  1781. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1782. if assigned(dupsym) then
  1783. include(tsym(dupsym).symoptions,sp_implicitrename);
  1784. end;
  1785. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  1786. begin
  1787. result:=false;
  1788. if not assigned(sym) or not (sym is tstoredsym) then
  1789. Internalerror(2011081101);
  1790. { For generics a dummy symbol without the parameter count is created
  1791. if such a symbol not yet exists so that different parts of the
  1792. parser can find that symbol. If that symbol is still a
  1793. undefineddef we replace the generic dummy symbol's
  1794. name with a "dup" name and use the new symbol as the generic dummy
  1795. symbol }
  1796. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  1797. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  1798. (m_delphi in current_settings.modeswitches) then
  1799. begin
  1800. inc(dupnr);
  1801. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  1802. include(tsym(sym).symoptions,sp_implicitrename);
  1803. { we need to find the new symbol now if checking for a dummy }
  1804. include(symoptions,sp_generic_dummy);
  1805. result:=true;
  1806. end;
  1807. end;
  1808. {*****************************************************************************
  1809. Search
  1810. *****************************************************************************}
  1811. procedure addsymref(sym:tsym);
  1812. var
  1813. owner: tsymtable;
  1814. begin
  1815. { symbol uses count }
  1816. sym.IncRefCount;
  1817. { unit uses count }
  1818. owner:=sym.owner;
  1819. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  1820. owner:=tdef(owner.defowner).owner;
  1821. if assigned(current_module) and
  1822. (owner.symtabletype=globalsymtable) then
  1823. begin
  1824. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  1825. internalerror(200501152);
  1826. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  1827. end;
  1828. end;
  1829. function is_owned_by(childdef,ownerdef:tdef):boolean;
  1830. begin
  1831. result:=childdef=ownerdef;
  1832. if not result and assigned(childdef.owner.defowner) then
  1833. result:=is_owned_by(tdef(childdef.owner.defowner),ownerdef);
  1834. end;
  1835. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  1836. begin
  1837. result:=assigned(childsym) and (childsym.owner=symtable);
  1838. if not result and assigned(childsym) and
  1839. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1840. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  1841. end;
  1842. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  1843. begin
  1844. result:=false;
  1845. if not assigned(def1) or not assigned(def2) then
  1846. exit;
  1847. { for both defs walk to the topmost generic }
  1848. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  1849. def1:=tdef(def1.owner.defowner);
  1850. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  1851. def2:=tdef(def2.owner.defowner);
  1852. result:=def1=def2;
  1853. end;
  1854. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  1855. var
  1856. uname : string;
  1857. begin
  1858. { TODO : check regarding arrays and records declared as their type }
  1859. if not (def.typ in [recorddef,objectdef]) then
  1860. internalerror(2012051501);
  1861. uname:=upper(srsym.realname);
  1862. repeat
  1863. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  1864. begin
  1865. result:=def;
  1866. exit;
  1867. end;
  1868. def:=tdef(def.owner.defowner);
  1869. until not (def.typ in [recorddef,objectdef]);
  1870. result:=nil;
  1871. end;
  1872. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  1873. begin
  1874. { TODO : check regarding arrays and records declared as their type }
  1875. if not (nesteddef.typ in [recorddef,objectdef]) then
  1876. internalerror(2012051601);
  1877. repeat
  1878. if tstoreddef(nesteddef).genericdef=genericdef then
  1879. begin
  1880. resultdef:=nesteddef;
  1881. result:=true;
  1882. exit;
  1883. end;
  1884. nesteddef:=tdef(nesteddef.owner.defowner);
  1885. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  1886. resultdef:=nil;
  1887. result:=false;
  1888. end;
  1889. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  1890. symvisibility: visibility of the symbol
  1891. contextobjdef: via which def the symbol is accessed, e.g.:
  1892. fieldname:=1 -> contextobjdef = current_structdef
  1893. objfield.fieldname:=1 -> contextobjdef = def of objfield
  1894. }
  1895. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1896. var
  1897. symownerdef : tabstractrecorddef;
  1898. begin
  1899. result:=false;
  1900. { Get objdectdef owner of the symtable for the is_related checks }
  1901. if not assigned(symst) or
  1902. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1903. internalerror(200810285);
  1904. symownerdef:=tabstractrecorddef(symst.defowner);
  1905. case symvisibility of
  1906. vis_private :
  1907. begin
  1908. { private symbols are allowed when we are in the same
  1909. module as they are defined }
  1910. result:=(
  1911. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1912. (symownerdef.owner.iscurrentunit)
  1913. ) or
  1914. ( // the case of specialize inside the generic declaration and nested types
  1915. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1916. (
  1917. assigned(current_structdef) and
  1918. (
  1919. (current_structdef=symownerdef) or
  1920. (current_structdef.owner.iscurrentunit)
  1921. )
  1922. ) or
  1923. (
  1924. not assigned(current_structdef) and
  1925. (symownerdef.owner.iscurrentunit)
  1926. )
  1927. );
  1928. end;
  1929. vis_strictprivate :
  1930. begin
  1931. result:=assigned(current_structdef) and
  1932. is_owned_by(current_structdef,symownerdef);
  1933. end;
  1934. vis_strictprotected :
  1935. begin
  1936. result:=(
  1937. { access from nested class }
  1938. assigned(current_structdef) and
  1939. is_owned_by(current_structdef,symownerdef)
  1940. ) or
  1941. (
  1942. { access from child class }
  1943. assigned(contextobjdef) and
  1944. assigned(current_structdef) and
  1945. contextobjdef.is_related(symownerdef) and
  1946. current_structdef.is_related(contextobjdef)
  1947. ) or
  1948. (
  1949. { helpers can access strict protected symbols }
  1950. is_objectpascal_helper(contextobjdef) and
  1951. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1952. ) or
  1953. (
  1954. { same as above, but from context of call node inside
  1955. helper method }
  1956. is_objectpascal_helper(current_structdef) and
  1957. tobjectdef(current_structdef).extendeddef.is_related(symownerdef)
  1958. );
  1959. end;
  1960. vis_protected :
  1961. begin
  1962. { protected symbols are visible in the module that defines them and
  1963. also visible to related objects. The related object must be defined
  1964. in the current module }
  1965. result:=(
  1966. (
  1967. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1968. (symownerdef.owner.iscurrentunit)
  1969. ) or
  1970. (
  1971. assigned(contextobjdef) and
  1972. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1973. (contextobjdef.owner.iscurrentunit) and
  1974. contextobjdef.is_related(symownerdef)
  1975. ) or
  1976. ( // the case of specialize inside the generic declaration and nested types
  1977. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1978. (
  1979. assigned(current_structdef) and
  1980. (
  1981. (current_structdef=symownerdef) or
  1982. (current_structdef.owner.iscurrentunit)
  1983. )
  1984. ) or
  1985. (
  1986. not assigned(current_structdef) and
  1987. (symownerdef.owner.iscurrentunit)
  1988. ) or
  1989. (
  1990. { helpers can access protected symbols }
  1991. is_objectpascal_helper(contextobjdef) and
  1992. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1993. )
  1994. )
  1995. );
  1996. end;
  1997. vis_public,
  1998. vis_published :
  1999. result:=true;
  2000. end;
  2001. end;
  2002. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2003. begin
  2004. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2005. end;
  2006. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2007. var
  2008. i : longint;
  2009. pd : tprocdef;
  2010. begin
  2011. if sym.typ=procsym then
  2012. begin
  2013. { A procsym is visible, when there is at least one of the procdefs visible }
  2014. result:=false;
  2015. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2016. begin
  2017. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2018. if (pd.owner=sym.owner) and
  2019. is_visible_for_object(pd,contextobjdef) then
  2020. begin
  2021. result:=true;
  2022. exit;
  2023. end;
  2024. end;
  2025. end
  2026. else
  2027. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2028. end;
  2029. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2030. begin
  2031. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,false,sp_none);
  2032. end;
  2033. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchoption:boolean;option:tsymoption):boolean;
  2034. var
  2035. hashedid : THashedIDString;
  2036. contextstructdef : tabstractrecorddef;
  2037. stackitem : psymtablestackitem;
  2038. begin
  2039. result:=false;
  2040. hashedid.id:=s;
  2041. stackitem:=symtablestack.stack;
  2042. while assigned(stackitem) do
  2043. begin
  2044. srsymtable:=stackitem^.symtable;
  2045. if (srsymtable.symtabletype=objectsymtable) then
  2046. begin
  2047. { TODO : implement the search for an option in classes as well }
  2048. if searchoption then
  2049. begin
  2050. result:=false;
  2051. exit;
  2052. end;
  2053. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,true) then
  2054. begin
  2055. result:=true;
  2056. exit;
  2057. end;
  2058. end
  2059. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2060. (srsymtable.defowner.typ=undefineddef)) then
  2061. begin
  2062. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2063. if assigned(srsym) then
  2064. begin
  2065. { use the class from withsymtable only when it is
  2066. defined in this unit }
  2067. if (srsymtable.symtabletype=withsymtable) and
  2068. assigned(srsymtable.defowner) and
  2069. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2070. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2071. (srsymtable.defowner.owner.iscurrentunit) then
  2072. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2073. else
  2074. contextstructdef:=current_structdef;
  2075. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2076. is_visible_for_object(srsym,contextstructdef) and
  2077. (not searchoption or (option in srsym.symoptions)) then
  2078. begin
  2079. { we need to know if a procedure references symbols
  2080. in the static symtable, because then it can't be
  2081. inlined from outside this unit }
  2082. if assigned(current_procinfo) and
  2083. (srsym.owner.symtabletype=staticsymtable) then
  2084. include(current_procinfo.flags,pi_uses_static_symtable);
  2085. addsymref(srsym);
  2086. result:=true;
  2087. exit;
  2088. end;
  2089. end;
  2090. end;
  2091. stackitem:=stackitem^.next;
  2092. end;
  2093. srsym:=nil;
  2094. srsymtable:=nil;
  2095. end;
  2096. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2097. srsymtable:TSymtable;option:tsymoption):boolean;
  2098. begin
  2099. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,true,option);
  2100. end;
  2101. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2102. var
  2103. hashedid : THashedIDString;
  2104. stackitem : psymtablestackitem;
  2105. classh : tobjectdef;
  2106. begin
  2107. result:=false;
  2108. hashedid.id:=s;
  2109. stackitem:=symtablestack.stack;
  2110. while assigned(stackitem) do
  2111. begin
  2112. {
  2113. It is not possible to have type symbols in:
  2114. parameters
  2115. Exception are classes, objects, records, generic definitions and specializations
  2116. that have the parameterized types inserted in the symtable.
  2117. }
  2118. srsymtable:=stackitem^.symtable;
  2119. if (srsymtable.symtabletype=ObjectSymtable) then
  2120. begin
  2121. classh:=tobjectdef(srsymtable.defowner);
  2122. while assigned(classh) do
  2123. begin
  2124. srsymtable:=classh.symtable;
  2125. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2126. if assigned(srsym) and
  2127. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2128. is_visible_for_object(srsym,current_structdef) then
  2129. begin
  2130. addsymref(srsym);
  2131. result:=true;
  2132. exit;
  2133. end;
  2134. classh:=classh.childof;
  2135. end;
  2136. end
  2137. else
  2138. begin
  2139. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2140. if assigned(srsym) and
  2141. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2142. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2143. begin
  2144. { we need to know if a procedure references symbols
  2145. in the static symtable, because then it can't be
  2146. inlined from outside this unit }
  2147. if assigned(current_procinfo) and
  2148. (srsym.owner.symtabletype=staticsymtable) then
  2149. include(current_procinfo.flags,pi_uses_static_symtable);
  2150. addsymref(srsym);
  2151. result:=true;
  2152. exit;
  2153. end;
  2154. end;
  2155. stackitem:=stackitem^.next;
  2156. end;
  2157. result:=false;
  2158. srsym:=nil;
  2159. srsymtable:=nil;
  2160. end;
  2161. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2162. var
  2163. pmod : tmodule;
  2164. begin
  2165. pmod:=tmodule(pm);
  2166. result:=false;
  2167. if assigned(pmod.globalsymtable) then
  2168. begin
  2169. srsym:=tsym(pmod.globalsymtable.Find(s));
  2170. if assigned(srsym) then
  2171. begin
  2172. srsymtable:=pmod.globalsymtable;
  2173. addsymref(srsym);
  2174. result:=true;
  2175. exit;
  2176. end;
  2177. end;
  2178. { If the module is the current unit we also need
  2179. to search the local symtable }
  2180. if (pmod=current_module) and
  2181. assigned(pmod.localsymtable) then
  2182. begin
  2183. srsym:=tsym(pmod.localsymtable.Find(s));
  2184. if assigned(srsym) then
  2185. begin
  2186. srsymtable:=pmod.localsymtable;
  2187. addsymref(srsym);
  2188. result:=true;
  2189. exit;
  2190. end;
  2191. end;
  2192. srsym:=nil;
  2193. srsymtable:=nil;
  2194. end;
  2195. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2196. var
  2197. stackitem : psymtablestackitem;
  2198. begin
  2199. result:=false;
  2200. stackitem:=symtablestack.stack;
  2201. while assigned(stackitem) do
  2202. begin
  2203. srsymtable:=stackitem^.symtable;
  2204. if (srsymtable.symtabletype=globalsymtable) and
  2205. (srsymtable.name^=unitname) then
  2206. begin
  2207. srsym:=tsym(srsymtable.find(symname));
  2208. if not assigned(srsym) then
  2209. break;
  2210. result:=true;
  2211. exit;
  2212. end;
  2213. stackitem:=stackitem^.next;
  2214. end;
  2215. { If the module is the current unit we also need
  2216. to search the local symtable }
  2217. if assigned(current_module.localsymtable) and
  2218. (current_module.localsymtable.name^=unitname) then
  2219. begin
  2220. srsymtable:=current_module.localsymtable;
  2221. srsym:=tsym(srsymtable.find(symname));
  2222. if assigned(srsym) then
  2223. begin
  2224. result:=true;
  2225. exit;
  2226. end;
  2227. end;
  2228. end;
  2229. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2230. begin
  2231. result:=pd;
  2232. if pd.typ<>objectdef then
  2233. exit;
  2234. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2235. end;
  2236. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2237. var
  2238. hashedid : THashedIDString;
  2239. stackitem : psymtablestackitem;
  2240. srsymtable : tsymtable;
  2241. srsym : tsym;
  2242. formalname,
  2243. foundname : shortstring;
  2244. formalnameptr,
  2245. foundnameptr: pshortstring;
  2246. begin
  2247. { not a formal definition -> return it }
  2248. if not(oo_is_formal in pd.objectoptions) then
  2249. begin
  2250. result:=pd;
  2251. exit;
  2252. end;
  2253. hashedid.id:=pd.typesym.name;
  2254. stackitem:=symtablestack.stack;
  2255. while assigned(stackitem) do
  2256. begin
  2257. srsymtable:=stackitem^.symtable;
  2258. { ObjC classes can't appear in generics or as nested class
  2259. definitions. Java classes can. }
  2260. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2261. (is_java_class_or_interface(pd) and
  2262. (srsymtable.symtabletype=ObjectSymtable)) then
  2263. begin
  2264. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2265. if assigned(srsym) and
  2266. (srsym.typ=typesym) and
  2267. (ttypesym(srsym).typedef.typ=objectdef) and
  2268. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2269. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2270. begin
  2271. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2272. begin
  2273. { the external name for the formal and the real
  2274. definition must match }
  2275. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2276. assigned(pd.import_lib) then
  2277. begin
  2278. if assigned(pd.import_lib) then
  2279. formalname:=pd.import_lib^+'.'
  2280. else
  2281. formalname:='';
  2282. formalname:=formalname+pd.objextname^;
  2283. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2284. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2285. else
  2286. foundname:='';
  2287. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2288. formalnameptr:=@formalname;
  2289. foundnameptr:=@foundname;
  2290. end
  2291. else
  2292. begin
  2293. formalnameptr:=pd.objextname;
  2294. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2295. end;
  2296. if foundnameptr^<>formalnameptr^ then
  2297. begin
  2298. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2299. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2300. end;
  2301. end;
  2302. result:=tobjectdef(ttypesym(srsym).typedef);
  2303. if assigned(current_procinfo) and
  2304. (srsym.owner.symtabletype=staticsymtable) then
  2305. include(current_procinfo.flags,pi_uses_static_symtable);
  2306. addsymref(srsym);
  2307. exit;
  2308. end;
  2309. end;
  2310. stackitem:=stackitem^.next;
  2311. end;
  2312. { nothing found: optionally give an error and return the original
  2313. (empty) one }
  2314. if erroronfailure then
  2315. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2316. result:=pd;
  2317. end;
  2318. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  2319. var
  2320. hashedid : THashedIDString;
  2321. orgclass : tobjectdef;
  2322. i : longint;
  2323. hlpsrsym : tsym;
  2324. hlpsrsymtable : tsymtable;
  2325. begin
  2326. orgclass:=classh;
  2327. { in case this is a formal class, first find the real definition }
  2328. if assigned(classh) then
  2329. begin
  2330. if (oo_is_formal in classh.objectoptions) then
  2331. classh:=find_real_class_definition(classh,true);
  2332. { The contextclassh is used for visibility. The classh must be equal to
  2333. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2334. parent or a class helper. }
  2335. if not (contextclassh.is_related(classh) or
  2336. (is_classhelper(contextclassh) and
  2337. assigned(tobjectdef(contextclassh).extendeddef) and
  2338. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  2339. tobjectdef(contextclassh).extendeddef.is_related(classh))) then
  2340. internalerror(200811161);
  2341. end;
  2342. result:=false;
  2343. hashedid.id:=s;
  2344. { an Objective-C protocol or Java interface can inherit from multiple
  2345. other protocols/interfaces -> use ImplementedInterfaces instead }
  2346. if is_objcprotocol(classh) or
  2347. is_javainterface(classh) then
  2348. begin
  2349. srsymtable:=classh.symtable;
  2350. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2351. if assigned(srsym) and
  2352. is_visible_for_object(srsym,contextclassh) then
  2353. begin
  2354. addsymref(srsym);
  2355. result:=true;
  2356. exit;
  2357. end;
  2358. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2359. begin
  2360. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,false) then
  2361. begin
  2362. result:=true;
  2363. exit;
  2364. end;
  2365. end;
  2366. end
  2367. else
  2368. if is_objectpascal_helper(classh) then
  2369. begin
  2370. { helpers have their own obscure search logic... }
  2371. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,false);
  2372. if result then
  2373. exit;
  2374. end
  2375. else
  2376. begin
  2377. hlpsrsym:=nil;
  2378. hlpsrsymtable:=nil;
  2379. while assigned(classh) do
  2380. begin
  2381. { search for a class helper method first if this is an Object
  2382. Pascal class and we haven't yet found a helper symbol }
  2383. if is_class(classh) and searchhelper and not assigned(hlpsrsym) then
  2384. begin
  2385. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2386. if result then
  2387. { if the procsym is overloaded we need to use the
  2388. "original" symbol; the helper symbol will be found when
  2389. searching for overloads }
  2390. if (srsym.typ<>procsym) or
  2391. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2392. exit
  2393. else
  2394. begin
  2395. { remember the found symbol if the class hierarchy
  2396. should not contain the a method with that name }
  2397. hlpsrsym:=srsym;
  2398. hlpsrsymtable:=srsymtable;
  2399. end;
  2400. end;
  2401. srsymtable:=classh.symtable;
  2402. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2403. if assigned(srsym) and
  2404. is_visible_for_object(srsym,contextclassh) then
  2405. begin
  2406. addsymref(srsym);
  2407. result:=true;
  2408. exit;
  2409. end;
  2410. classh:=classh.childof;
  2411. end;
  2412. { did we find a helper symbol, but no symbol with the same name in
  2413. the extended object's hierarchy? }
  2414. if assigned(hlpsrsym) then
  2415. begin
  2416. srsym:=hlpsrsym;
  2417. srsymtable:=hlpsrsymtable;
  2418. result:=true;
  2419. exit;
  2420. end;
  2421. end;
  2422. if is_objcclass(orgclass) then
  2423. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2424. else
  2425. begin
  2426. srsym:=nil;
  2427. srsymtable:=nil;
  2428. end;
  2429. end;
  2430. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2431. var
  2432. hashedid : THashedIDString;
  2433. hlpsrsym : tsym;
  2434. hlpsrsymtable : tsymtable;
  2435. begin
  2436. result:=false;
  2437. hlpsrsym:=nil;
  2438. hlpsrsymtable:=nil;
  2439. hashedid.id:=s;
  2440. { search for a record helper method first }
  2441. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2442. if result then
  2443. { if the procsym is overloaded we need to use the
  2444. "original" symbol; the helper symbol will be found when
  2445. searching for overloads }
  2446. if (srsym.typ<>procsym) or
  2447. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2448. exit
  2449. else
  2450. begin
  2451. { remember the found symbol if we should not find a symbol with
  2452. the same name in the extended record }
  2453. hlpsrsym:=srsym;
  2454. hlpsrsymtable:=srsymtable;
  2455. end;
  2456. srsymtable:=recordh.symtable;
  2457. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2458. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2459. begin
  2460. addsymref(srsym);
  2461. result:=true;
  2462. exit;
  2463. end;
  2464. srsym:=hlpsrsym;
  2465. srsymtable:=hlpsrsymtable;
  2466. result:=assigned(srsym);
  2467. end;
  2468. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2469. var
  2470. def : tdef;
  2471. i : longint;
  2472. begin
  2473. { in case this is a formal class, first find the real definition }
  2474. if assigned(classh) and
  2475. (oo_is_formal in classh.objectoptions) then
  2476. classh:=find_real_class_definition(classh,true);
  2477. result:=false;
  2478. def:=nil;
  2479. while assigned(classh) do
  2480. begin
  2481. for i:=0 to classh.symtable.DefList.Count-1 do
  2482. begin
  2483. def:=tstoreddef(classh.symtable.DefList[i]);
  2484. { Find also all hidden private methods to
  2485. be compatible with delphi, see tw6203 (PFV) }
  2486. if (def.typ=procdef) and
  2487. (po_msgint in tprocdef(def).procoptions) and
  2488. (tprocdef(def).messageinf.i=msgid) then
  2489. begin
  2490. srdef:=def;
  2491. srsym:=tprocdef(def).procsym;
  2492. srsymtable:=classh.symtable;
  2493. addsymref(srsym);
  2494. result:=true;
  2495. exit;
  2496. end;
  2497. end;
  2498. classh:=classh.childof;
  2499. end;
  2500. srdef:=nil;
  2501. srsym:=nil;
  2502. srsymtable:=nil;
  2503. end;
  2504. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2505. var
  2506. def : tdef;
  2507. i : longint;
  2508. begin
  2509. { in case this is a formal class, first find the real definition }
  2510. if assigned(classh) and
  2511. (oo_is_formal in classh.objectoptions) then
  2512. classh:=find_real_class_definition(classh,true);
  2513. result:=false;
  2514. def:=nil;
  2515. while assigned(classh) do
  2516. begin
  2517. for i:=0 to classh.symtable.DefList.Count-1 do
  2518. begin
  2519. def:=tstoreddef(classh.symtable.DefList[i]);
  2520. { Find also all hidden private methods to
  2521. be compatible with delphi, see tw6203 (PFV) }
  2522. if (def.typ=procdef) and
  2523. (po_msgstr in tprocdef(def).procoptions) and
  2524. (tprocdef(def).messageinf.str^=s) then
  2525. begin
  2526. srsym:=tprocdef(def).procsym;
  2527. srsymtable:=classh.symtable;
  2528. addsymref(srsym);
  2529. result:=true;
  2530. exit;
  2531. end;
  2532. end;
  2533. classh:=classh.childof;
  2534. end;
  2535. srsym:=nil;
  2536. srsymtable:=nil;
  2537. end;
  2538. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2539. var
  2540. hashedid : THashedIDString;
  2541. parentclassh : tobjectdef;
  2542. begin
  2543. result:=false;
  2544. if not is_objectpascal_helper(classh) then
  2545. Internalerror(2011030101);
  2546. hashedid.id:=s;
  2547. { in a helper things are a bit more complex:
  2548. 1. search the symbol in the helper (if not "inherited")
  2549. 2. search the symbol in the extended type
  2550. 3. search the symbol in the parent helpers
  2551. 4. only classes: search the symbol in the parents of the extended type
  2552. }
  2553. if not aHasInherited then
  2554. begin
  2555. { search in the helper itself }
  2556. srsymtable:=classh.symtable;
  2557. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2558. if assigned(srsym) and
  2559. is_visible_for_object(srsym,contextclassh) then
  2560. begin
  2561. addsymref(srsym);
  2562. result:=true;
  2563. exit;
  2564. end;
  2565. end;
  2566. { now search in the extended type itself }
  2567. if classh.extendeddef.typ in [recorddef,objectdef] then
  2568. begin
  2569. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2570. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2571. if assigned(srsym) and
  2572. is_visible_for_object(srsym,contextclassh) then
  2573. begin
  2574. addsymref(srsym);
  2575. result:=true;
  2576. exit;
  2577. end;
  2578. end;
  2579. { now search in the parent helpers }
  2580. parentclassh:=classh.childof;
  2581. while assigned(parentclassh) do
  2582. begin
  2583. srsymtable:=parentclassh.symtable;
  2584. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2585. if assigned(srsym) and
  2586. is_visible_for_object(srsym,contextclassh) then
  2587. begin
  2588. addsymref(srsym);
  2589. result:=true;
  2590. exit;
  2591. end;
  2592. parentclassh:=parentclassh.childof;
  2593. end;
  2594. if is_class(classh.extendeddef) then
  2595. { now search in the parents of the extended class (with helpers!) }
  2596. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,true);
  2597. { addsymref is already called by searchsym_in_class }
  2598. end;
  2599. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2600. var
  2601. sym : Tprocsym;
  2602. hashedid : THashedIDString;
  2603. curreq,
  2604. besteq : tequaltype;
  2605. currpd,
  2606. bestpd : tprocdef;
  2607. stackitem : psymtablestackitem;
  2608. begin
  2609. hashedid.id:=overloaded_names[assignment_type];
  2610. besteq:=te_incompatible;
  2611. bestpd:=nil;
  2612. stackitem:=symtablestack.stack;
  2613. while assigned(stackitem) do
  2614. begin
  2615. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2616. if sym<>nil then
  2617. begin
  2618. if sym.typ<>procsym then
  2619. internalerror(200402031);
  2620. { if the source type is an alias then this is only the second choice,
  2621. if you mess with this code, check tw4093 }
  2622. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2623. if curreq>besteq then
  2624. begin
  2625. besteq:=curreq;
  2626. bestpd:=currpd;
  2627. if (besteq=te_exact) then
  2628. break;
  2629. end;
  2630. end;
  2631. stackitem:=stackitem^.next;
  2632. end;
  2633. result:=bestpd;
  2634. end;
  2635. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2636. begin
  2637. { search record/object symtable first for a suitable operator }
  2638. if from_def.typ in [recorddef,objectdef] then
  2639. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2640. if to_def.typ in [recorddef,objectdef] then
  2641. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2642. { if type conversion is explicit then search first for explicit
  2643. operator overload and if not found then use implicit operator }
  2644. if explicit then
  2645. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2646. else
  2647. result:=nil;
  2648. if result=nil then
  2649. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2650. { restore symtable stack }
  2651. if to_def.typ in [recorddef,objectdef] then
  2652. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2653. if from_def.typ in [recorddef,objectdef] then
  2654. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2655. end;
  2656. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2657. var
  2658. sym : Tprocsym;
  2659. hashedid : THashedIDString;
  2660. curreq,
  2661. besteq : tequaltype;
  2662. currpd,
  2663. bestpd : tprocdef;
  2664. stackitem : psymtablestackitem;
  2665. begin
  2666. hashedid.id:='enumerator';
  2667. besteq:=te_incompatible;
  2668. bestpd:=nil;
  2669. stackitem:=symtablestack.stack;
  2670. while assigned(stackitem) do
  2671. begin
  2672. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2673. if sym<>nil then
  2674. begin
  2675. if sym.typ<>procsym then
  2676. internalerror(200910241);
  2677. { if the source type is an alias then this is only the second choice,
  2678. if you mess with this code, check tw4093 }
  2679. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2680. if curreq>besteq then
  2681. begin
  2682. besteq:=curreq;
  2683. bestpd:=currpd;
  2684. if (besteq=te_exact) then
  2685. break;
  2686. end;
  2687. end;
  2688. stackitem:=stackitem^.next;
  2689. end;
  2690. result:=bestpd;
  2691. end;
  2692. function search_system_type(const s: TIDString): ttypesym;
  2693. var
  2694. sym : tsym;
  2695. begin
  2696. sym:=tsym(systemunit.Find(s));
  2697. if not assigned(sym) or
  2698. (sym.typ<>typesym) then
  2699. cgmessage1(cg_f_unknown_system_type,s);
  2700. result:=ttypesym(sym);
  2701. end;
  2702. function try_search_system_type(const s: TIDString): ttypesym;
  2703. var
  2704. sym : tsym;
  2705. begin
  2706. sym:=tsym(systemunit.Find(s));
  2707. if not assigned(sym) then
  2708. result:=nil
  2709. else
  2710. begin
  2711. if sym.typ<>typesym then
  2712. cgmessage1(cg_f_unknown_system_type,s);
  2713. result:=ttypesym(sym);
  2714. end;
  2715. end;
  2716. function search_system_proc(const s: TIDString): tprocdef;
  2717. var
  2718. srsym: tsym;
  2719. begin
  2720. srsym:=tsym(systemunit.find(s));
  2721. if not assigned(srsym) and
  2722. (cs_compilesystem in current_settings.moduleswitches) then
  2723. srsym:=tsym(systemunit.Find(upper(s)));
  2724. if not assigned(srsym) or
  2725. (srsym.typ<>procsym) then
  2726. cgmessage1(cg_f_unknown_compilerproc,s);
  2727. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  2728. end;
  2729. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2730. var
  2731. srsymtable: tsymtable;
  2732. sym: tsym;
  2733. begin
  2734. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2735. (sym.typ=typesym) then
  2736. begin
  2737. result:=ttypesym(sym);
  2738. exit;
  2739. end
  2740. else
  2741. begin
  2742. if throwerror then
  2743. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  2744. result:=nil;
  2745. end;
  2746. end;
  2747. function search_last_objectpascal_helper(pd,contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2748. var
  2749. s: string;
  2750. list: TFPObjectList;
  2751. i: integer;
  2752. st: tsymtable;
  2753. begin
  2754. result:=false;
  2755. odef:=nil;
  2756. { when there are no helpers active currently then we don't need to do
  2757. anything }
  2758. if current_module.extendeddefs.count=0 then
  2759. exit;
  2760. { no helpers for anonymous types }
  2761. if not assigned(pd.objrealname) or (pd.objrealname^='') then
  2762. exit;
  2763. { if pd is defined inside a procedure we must not use make_mangledname
  2764. (as a helper may not be defined in a procedure this is no problem...)}
  2765. st:=pd.owner;
  2766. while st.symtabletype in [objectsymtable,recordsymtable] do
  2767. st:=st.defowner.owner;
  2768. if st.symtabletype=localsymtable then
  2769. exit;
  2770. { the mangled name is used as the key for tmodule.extendeddefs }
  2771. s:=make_mangledname('',pd.symtable,'');
  2772. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2773. if assigned(list) and (list.count>0) then
  2774. begin
  2775. i:=list.count-1;
  2776. repeat
  2777. odef:=tobjectdef(list[list.count-1]);
  2778. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2779. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2780. dec(i);
  2781. until result or (i<0);
  2782. if not result then
  2783. { just to be sure that noone uses odef }
  2784. odef:=nil;
  2785. end;
  2786. end;
  2787. function search_objectpascal_helper(pd,contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2788. var
  2789. hashedid : THashedIDString;
  2790. classh : tobjectdef;
  2791. i : integer;
  2792. pdef : tprocdef;
  2793. begin
  2794. result:=false;
  2795. { if there is no class helper for the class then there is no need to
  2796. search further }
  2797. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2798. exit;
  2799. hashedid.id:=s;
  2800. repeat
  2801. srsymtable:=classh.symtable;
  2802. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2803. if srsym<>nil then
  2804. begin
  2805. if srsym.typ=propertysym then
  2806. begin
  2807. result:=true;
  2808. exit;
  2809. end;
  2810. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2811. begin
  2812. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2813. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2814. continue;
  2815. { we need to know if a procedure references symbols
  2816. in the static symtable, because then it can't be
  2817. inlined from outside this unit }
  2818. if assigned(current_procinfo) and
  2819. (srsym.owner.symtabletype=staticsymtable) then
  2820. include(current_procinfo.flags,pi_uses_static_symtable);
  2821. { the first found method wins }
  2822. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2823. srsymtable:=srsym.owner;
  2824. addsymref(srsym);
  2825. result:=true;
  2826. exit;
  2827. end;
  2828. end;
  2829. { try the helper parent if available }
  2830. classh:=classh.childof;
  2831. until classh=nil;
  2832. srsym:=nil;
  2833. srsymtable:=nil;
  2834. end;
  2835. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2836. var
  2837. hashedid : THashedIDString;
  2838. stackitem : psymtablestackitem;
  2839. i : longint;
  2840. defowner : tobjectdef;
  2841. begin
  2842. hashedid.id:=class_helper_prefix+s;
  2843. stackitem:=symtablestack.stack;
  2844. while assigned(stackitem) do
  2845. begin
  2846. srsymtable:=stackitem^.symtable;
  2847. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2848. if assigned(srsym) then
  2849. begin
  2850. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2851. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2852. (srsym.typ<>procsym) then
  2853. internalerror(2009111505);
  2854. { check whether this procsym includes a helper for this particular class }
  2855. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2856. begin
  2857. { does pd inherit from (or is the same as) the class
  2858. that this method's category extended?
  2859. Warning: this list contains both category and objcclass methods
  2860. (for id.randommethod), so only check category methods here
  2861. }
  2862. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  2863. if (oo_is_classhelper in defowner.objectoptions) and
  2864. pd.is_related(defowner.childof) then
  2865. begin
  2866. { we need to know if a procedure references symbols
  2867. in the static symtable, because then it can't be
  2868. inlined from outside this unit }
  2869. if assigned(current_procinfo) and
  2870. (srsym.owner.symtabletype=staticsymtable) then
  2871. include(current_procinfo.flags,pi_uses_static_symtable);
  2872. { no need to keep looking. There might be other
  2873. categories that extend this, a parent or child
  2874. class with a method with the same name (either
  2875. overriding this one, or overridden by this one),
  2876. but that doesn't matter as far as the basic
  2877. procsym is concerned.
  2878. }
  2879. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2880. srsymtable:=srsym.owner;
  2881. addsymref(srsym);
  2882. result:=true;
  2883. exit;
  2884. end;
  2885. end;
  2886. end;
  2887. stackitem:=stackitem^.next;
  2888. end;
  2889. srsym:=nil;
  2890. srsymtable:=nil;
  2891. result:=false;
  2892. end;
  2893. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2894. var
  2895. hashedid : THashedIDString;
  2896. stackitem : psymtablestackitem;
  2897. i : longint;
  2898. begin
  2899. hashedid.id:=class_helper_prefix+s;
  2900. stackitem:=symtablestack.stack;
  2901. while assigned(stackitem) do
  2902. begin
  2903. srsymtable:=stackitem^.symtable;
  2904. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2905. if assigned(srsym) then
  2906. begin
  2907. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2908. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2909. (srsym.typ<>procsym) then
  2910. internalerror(2009112005);
  2911. { check whether this procsym includes a helper for this particular class }
  2912. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2913. begin
  2914. { we need to know if a procedure references symbols
  2915. in the static symtable, because then it can't be
  2916. inlined from outside this unit }
  2917. if assigned(current_procinfo) and
  2918. (srsym.owner.symtabletype=staticsymtable) then
  2919. include(current_procinfo.flags,pi_uses_static_symtable);
  2920. { no need to keep looking. There might be other
  2921. methods with the same name, but that doesn't matter
  2922. as far as the basic procsym is concerned.
  2923. }
  2924. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2925. { We need the symtable in which the classhelper-like sym
  2926. is located, not the objectdef. The reason is that the
  2927. callnode will climb the symtablestack until it encounters
  2928. this symtable to start looking for overloads (and it won't
  2929. find the objectsymtable in which this method sym is
  2930. located
  2931. srsymtable:=srsym.owner;
  2932. }
  2933. addsymref(srsym);
  2934. result:=true;
  2935. exit;
  2936. end;
  2937. end;
  2938. stackitem:=stackitem^.next;
  2939. end;
  2940. srsym:=nil;
  2941. srsymtable:=nil;
  2942. result:=false;
  2943. end;
  2944. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  2945. { searches n in symtable of pd and all anchestors }
  2946. var
  2947. srsymtable : tsymtable;
  2948. begin
  2949. { in case this is a formal class, first find the real definition }
  2950. if (oo_is_formal in pd.objectoptions) then
  2951. pd:=find_real_class_definition(tobjectdef(pd),true);
  2952. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  2953. exit;
  2954. result:=search_struct_member_no_helper(pd,s);
  2955. if assigned(result) then
  2956. exit;
  2957. { not found, now look for class helpers }
  2958. if is_objcclass(pd) then
  2959. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  2960. end;
  2961. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  2962. var
  2963. hashedid : THashedIDString;
  2964. srsym : tsym;
  2965. begin
  2966. hashedid.id:=s;
  2967. while assigned(pd) do
  2968. begin
  2969. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2970. if assigned(srsym) then
  2971. begin
  2972. result:=srsym;
  2973. exit;
  2974. end;
  2975. if pd.typ=objectdef then
  2976. pd:=tobjectdef(pd).childof
  2977. else
  2978. pd:=nil;
  2979. end;
  2980. result:=nil;
  2981. end;
  2982. function search_macro(const s : string):tsym;
  2983. var
  2984. stackitem : psymtablestackitem;
  2985. hashedid : THashedIDString;
  2986. srsym : tsym;
  2987. begin
  2988. hashedid.id:=s;
  2989. { First search the localmacrosymtable before searching the
  2990. global macrosymtables from the units }
  2991. if assigned(current_module) then
  2992. begin
  2993. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2994. if assigned(srsym) then
  2995. begin
  2996. result:= srsym;
  2997. exit;
  2998. end;
  2999. end;
  3000. stackitem:=macrosymtablestack.stack;
  3001. while assigned(stackitem) do
  3002. begin
  3003. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3004. if assigned(srsym) then
  3005. begin
  3006. result:= srsym;
  3007. exit;
  3008. end;
  3009. stackitem:=stackitem^.next;
  3010. end;
  3011. result:= nil;
  3012. end;
  3013. function defined_macro(const s : string):boolean;
  3014. var
  3015. mac: tmacro;
  3016. begin
  3017. mac:=tmacro(search_macro(s));
  3018. if assigned(mac) then
  3019. begin
  3020. mac.is_used:=true;
  3021. defined_macro:=mac.defined;
  3022. end
  3023. else
  3024. defined_macro:=false;
  3025. end;
  3026. {****************************************************************************
  3027. Object Helpers
  3028. ****************************************************************************}
  3029. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  3030. { returns the default property of a class, searches also anchestors }
  3031. var
  3032. _defaultprop : tpropertysym;
  3033. helperpd : tobjectdef;
  3034. begin
  3035. _defaultprop:=nil;
  3036. { first search in helper's hierarchy }
  3037. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3038. while assigned(helperpd) do
  3039. begin
  3040. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3041. if assigned(_defaultprop) then
  3042. break;
  3043. helperpd:=helperpd.childof;
  3044. end;
  3045. if assigned(_defaultprop) then
  3046. begin
  3047. search_default_property:=_defaultprop;
  3048. exit;
  3049. end;
  3050. { now search in the type's hierarchy itself }
  3051. while assigned(pd) do
  3052. begin
  3053. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3054. if assigned(_defaultprop) then
  3055. break;
  3056. if (pd.typ=objectdef) then
  3057. pd:=tobjectdef(pd).childof
  3058. else
  3059. break;
  3060. end;
  3061. search_default_property:=_defaultprop;
  3062. end;
  3063. {****************************************************************************
  3064. Macro Helpers
  3065. ****************************************************************************}
  3066. procedure def_system_macro(const name : string);
  3067. var
  3068. mac : tmacro;
  3069. s: string;
  3070. begin
  3071. if name = '' then
  3072. internalerror(2004121202);
  3073. s:= upper(name);
  3074. mac:=tmacro(search_macro(s));
  3075. if not assigned(mac) then
  3076. begin
  3077. mac:=tmacro.create(s);
  3078. if assigned(current_module) then
  3079. current_module.localmacrosymtable.insert(mac)
  3080. else
  3081. initialmacrosymtable.insert(mac);
  3082. end;
  3083. Message1(parser_c_macro_defined,mac.name);
  3084. mac.defined:=true;
  3085. end;
  3086. procedure set_system_macro(const name, value : string);
  3087. var
  3088. mac : tmacro;
  3089. s: string;
  3090. begin
  3091. if name = '' then
  3092. internalerror(2004121203);
  3093. s:= upper(name);
  3094. mac:=tmacro(search_macro(s));
  3095. if not assigned(mac) then
  3096. begin
  3097. mac:=tmacro.create(s);
  3098. if assigned(current_module) then
  3099. current_module.localmacrosymtable.insert(mac)
  3100. else
  3101. initialmacrosymtable.insert(mac);
  3102. end
  3103. else
  3104. begin
  3105. mac.is_compiler_var:=false;
  3106. if assigned(mac.buftext) then
  3107. freemem(mac.buftext,mac.buflen);
  3108. end;
  3109. Message2(parser_c_macro_set_to,mac.name,value);
  3110. mac.buflen:=length(value);
  3111. getmem(mac.buftext,mac.buflen);
  3112. move(value[1],mac.buftext^,mac.buflen);
  3113. mac.defined:=true;
  3114. end;
  3115. procedure set_system_compvar(const name, value : string);
  3116. var
  3117. mac : tmacro;
  3118. s: string;
  3119. begin
  3120. if name = '' then
  3121. internalerror(2004121204);
  3122. s:= upper(name);
  3123. mac:=tmacro(search_macro(s));
  3124. if not assigned(mac) then
  3125. begin
  3126. mac:=tmacro.create(s);
  3127. mac.is_compiler_var:=true;
  3128. if assigned(current_module) then
  3129. current_module.localmacrosymtable.insert(mac)
  3130. else
  3131. initialmacrosymtable.insert(mac);
  3132. end
  3133. else
  3134. begin
  3135. mac.is_compiler_var:=true;
  3136. if assigned(mac.buftext) then
  3137. freemem(mac.buftext,mac.buflen);
  3138. end;
  3139. Message2(parser_c_macro_set_to,mac.name,value);
  3140. mac.buflen:=length(value);
  3141. getmem(mac.buftext,mac.buflen);
  3142. move(value[1],mac.buftext^,mac.buflen);
  3143. mac.defined:=true;
  3144. end;
  3145. procedure undef_system_macro(const name : string);
  3146. var
  3147. mac : tmacro;
  3148. s: string;
  3149. begin
  3150. if name = '' then
  3151. internalerror(2004121205);
  3152. s:= upper(name);
  3153. mac:=tmacro(search_macro(s));
  3154. if not assigned(mac) then
  3155. {If not found, then it's already undefined.}
  3156. else
  3157. begin
  3158. Message1(parser_c_macro_undefined,mac.name);
  3159. mac.defined:=false;
  3160. mac.is_compiler_var:=false;
  3161. { delete old definition }
  3162. if assigned(mac.buftext) then
  3163. begin
  3164. freemem(mac.buftext,mac.buflen);
  3165. mac.buftext:=nil;
  3166. end;
  3167. end;
  3168. end;
  3169. {$ifdef UNITALIASES}
  3170. {****************************************************************************
  3171. TUNIT_ALIAS
  3172. ****************************************************************************}
  3173. constructor tunit_alias.create(const n:string);
  3174. var
  3175. i : longint;
  3176. begin
  3177. i:=pos('=',n);
  3178. if i=0 then
  3179. fail;
  3180. inherited createname(Copy(n,1,i-1));
  3181. newname:=stringdup(Copy(n,i+1,255));
  3182. end;
  3183. destructor tunit_alias.destroy;
  3184. begin
  3185. stringdispose(newname);
  3186. inherited destroy;
  3187. end;
  3188. procedure addunitalias(const n:string);
  3189. begin
  3190. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3191. end;
  3192. function getunitalias(const n:string):string;
  3193. var
  3194. p : punit_alias;
  3195. begin
  3196. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3197. if assigned(p) then
  3198. getunitalias:=punit_alias(p).newname^
  3199. else
  3200. getunitalias:=n;
  3201. end;
  3202. {$endif UNITALIASES}
  3203. {****************************************************************************
  3204. Init/Done Symtable
  3205. ****************************************************************************}
  3206. procedure InitSymtable;
  3207. begin
  3208. { Reset symbolstack }
  3209. symtablestack:=nil;
  3210. systemunit:=nil;
  3211. { create error syms and def }
  3212. generrorsym:=terrorsym.create;
  3213. generrordef:=terrordef.create;
  3214. { macros }
  3215. initialmacrosymtable:=tmacrosymtable.create(false);
  3216. macrosymtablestack:=TSymtablestack.create;
  3217. macrosymtablestack.push(initialmacrosymtable);
  3218. {$ifdef UNITALIASES}
  3219. { unit aliases }
  3220. unitaliases:=TFPHashObjectList.create;
  3221. {$endif}
  3222. { set some global vars to nil, might be important for the ide }
  3223. class_tobject:=nil;
  3224. interface_iunknown:=nil;
  3225. interface_idispatch:=nil;
  3226. rec_tguid:=nil;
  3227. dupnr:=0;
  3228. end;
  3229. procedure DoneSymtable;
  3230. begin
  3231. generrorsym.owner:=nil;
  3232. generrorsym.free;
  3233. generrordef.owner:=nil;
  3234. generrordef.free;
  3235. initialmacrosymtable.free;
  3236. macrosymtablestack.free;
  3237. {$ifdef UNITALIASES}
  3238. unitaliases.free;
  3239. {$endif}
  3240. end;
  3241. end.