symtable.pas 118 KB

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