symtable.pas 102 KB

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