symtable.pas 101 KB

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