symtable.pas 138 KB

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