symtable.pas 140 KB

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