symtable.pas 94 KB

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