symtable.pas 99 KB

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