symtable.pas 113 KB

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