symtable.pas 98 KB

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