symtable.pas 112 KB

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