symtable.pas 114 KB

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