symtable.pas 98 KB

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