symtable.pas 109 KB

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