symtable.pas 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym,
  25. { ppu }
  26. ppu;
  27. {****************************************************************************
  28. Symtable types
  29. ****************************************************************************}
  30. type
  31. tstoredsymtable = class(TSymtable)
  32. private
  33. init_final_check_done : boolean;
  34. procedure _needs_init_final(sym:TObject;arg:pointer);
  35. procedure check_forward(sym:TObject;arg:pointer);
  36. procedure labeldefined(sym:TObject;arg:pointer);
  37. procedure varsymbolused(sym:TObject;arg:pointer);
  38. procedure TestPrivate(sym:TObject;arg:pointer);
  39. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  40. procedure loaddefs(ppufile:tcompilerppufile);
  41. procedure loadsyms(ppufile:tcompilerppufile);
  42. procedure writedefs(ppufile:tcompilerppufile);
  43. procedure writesyms(ppufile:tcompilerppufile);
  44. public
  45. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  46. procedure delete(sym:TSymEntry);override;
  47. { load/write }
  48. procedure ppuload(ppufile:tcompilerppufile);virtual;
  49. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  50. procedure buildderef;virtual;
  51. procedure buildderefimpl;virtual;
  52. procedure deref;virtual;
  53. procedure derefimpl;virtual;
  54. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  55. procedure allsymbolsused;
  56. procedure allprivatesused;
  57. procedure check_forwards;
  58. procedure checklabels;
  59. function needs_init_final : boolean;
  60. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  61. end;
  62. tabstractrecordsymtable = class(tstoredsymtable)
  63. public
  64. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  65. recordalignment, { alignment desired when inserting this record }
  66. fieldalignment, { alignment current alignment used when fields are inserted }
  67. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  68. constructor create(const n:string;usealign:shortint);
  69. procedure ppuload(ppufile:tcompilerppufile);override;
  70. procedure ppuwrite(ppufile:tcompilerppufile);override;
  71. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  72. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  73. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  74. procedure addalignmentpadding;
  75. procedure insertdef(def:TDefEntry);override;
  76. function is_packed: boolean;
  77. function has_single_field(out sym:tfieldvarsym): boolean;
  78. function get_unit_symtable: tsymtable;
  79. protected
  80. { size in bytes including padding }
  81. _datasize : asizeint;
  82. { size in bits of the data in case of bitpacked record. Only important during construction, }
  83. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  84. databitsize : asizeint;
  85. { size in bytes of padding }
  86. _paddingsize : word;
  87. procedure setdatasize(val: asizeint);
  88. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  89. public
  90. function iscurrentunit: boolean; override;
  91. property datasize : asizeint read _datasize write setdatasize;
  92. property paddingsize: word read _paddingsize write _paddingsize;
  93. end;
  94. trecordsymtable = class(tabstractrecordsymtable)
  95. public
  96. constructor create(const n:string;usealign:shortint);
  97. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  98. end;
  99. tObjectSymtable = class(tabstractrecordsymtable)
  100. public
  101. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  102. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  103. end;
  104. { tabstractlocalsymtable }
  105. tabstractlocalsymtable = class(tstoredsymtable)
  106. public
  107. procedure ppuwrite(ppufile:tcompilerppufile);override;
  108. function count_locals:longint;
  109. end;
  110. tlocalsymtable = class(tabstractlocalsymtable)
  111. public
  112. constructor create(adefowner:tdef;level:byte);
  113. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  114. end;
  115. { tparasymtable }
  116. tparasymtable = class(tabstractlocalsymtable)
  117. public
  118. readonly: boolean;
  119. constructor create(adefowner:tdef;level:byte);
  120. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  121. procedure insertdef(def:TDefEntry);override;
  122. end;
  123. tabstractuniTSymtable = class(tstoredsymtable)
  124. public
  125. constructor create(const n : string;id:word);
  126. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  127. function findnamespace(const n:string):TSymEntry;virtual;
  128. function iscurrentunit:boolean;override;
  129. procedure insertunit(sym:TSymEntry);
  130. end;
  131. tglobalsymtable = class(tabstractuniTSymtable)
  132. public
  133. unittypecount : word;
  134. constructor create(const n : string;id:word);
  135. procedure ppuload(ppufile:tcompilerppufile);override;
  136. procedure ppuwrite(ppufile:tcompilerppufile);override;
  137. end;
  138. tstaticsymtable = class(tabstractuniTSymtable)
  139. public
  140. constructor create(const n : string;id:word);
  141. procedure ppuload(ppufile:tcompilerppufile);override;
  142. procedure ppuwrite(ppufile:tcompilerppufile);override;
  143. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  144. function findnamespace(const n:string):TSymEntry;override;
  145. end;
  146. tspecializesymtable = class(tglobalsymtable)
  147. public
  148. constructor create(const n : string;id:word);
  149. function iscurrentunit:boolean;override;
  150. end;
  151. twithsymtable = class(TSymtable)
  152. withrefnode : tobject; { tnode }
  153. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  154. destructor destroy;override;
  155. procedure clear;override;
  156. procedure insertdef(def:TDefEntry);override;
  157. end;
  158. tstt_excepTSymtable = class(TSymtable)
  159. public
  160. constructor create;
  161. end;
  162. tmacrosymtable = class(tstoredsymtable)
  163. public
  164. constructor create(exported: boolean);
  165. end;
  166. { tenumsymtable }
  167. tenumsymtable = class(tstoredsymtable)
  168. public
  169. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  170. constructor create(adefowner:tdef);
  171. end;
  172. { tarraysymtable }
  173. tarraysymtable = class(tstoredsymtable)
  174. public
  175. procedure insertdef(def:TDefEntry);override;
  176. constructor create(adefowner:tdef);
  177. end;
  178. var
  179. systemunit : tglobalsymtable; { pointer to the system unit }
  180. type
  181. tsymbol_search_flag = (
  182. ssf_search_option,
  183. ssf_search_helper,
  184. ssf_has_inherited,
  185. ssf_no_addsymref
  186. );
  187. tsymbol_search_flags = set of tsymbol_search_flag;
  188. {****************************************************************************
  189. Functions
  190. ****************************************************************************}
  191. {*** Misc ***}
  192. function FullTypeName(def,otherdef:tdef):string;
  193. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  194. { def is the extended type of a helper }
  195. function generate_objectpascal_helper_key(def:tdef):string;
  196. procedure incompatibletypes(def1,def2:tdef);
  197. procedure hidesym(sym:TSymEntry);
  198. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  199. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  200. function get_jumpbuf_size : longint;
  201. {*** Search ***}
  202. procedure addsymref(sym:tsym);
  203. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  204. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  205. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  206. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  207. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  208. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  209. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  210. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  211. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  212. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  213. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  214. { searches for a symbol with the given name that has the given option in
  215. symoptions set }
  216. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  217. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  218. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  219. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  220. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  221. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  222. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  223. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  224. { searches symbols inside of a helper's implementation }
  225. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  226. function search_system_type(const s: TIDString): ttypesym;
  227. function try_search_system_type(const s: TIDString): ttypesym;
  228. function search_system_proc(const s: TIDString): tprocdef;
  229. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  230. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  231. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  232. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  233. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  234. { searches for the helper definition that's currently active for pd }
  235. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  236. { searches whether the symbol s is available in the currently active }
  237. { helper for pd }
  238. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  239. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  240. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  241. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  242. {and returns it if found. Returns nil otherwise.}
  243. function search_macro(const s : string):tsym;
  244. { Additionally to searching for a macro, also checks whether it's still }
  245. { actually defined (could be disable using "undef") }
  246. function defined_macro(const s : string):boolean;
  247. { Look for a system procedure (no overloads supported) }
  248. {*** Object Helpers ***}
  249. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  250. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  251. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  252. {*** Macro Helpers ***}
  253. {If called initially, the following procedures manipulate macros in }
  254. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  255. {Name can be given in any case (it will be converted to upper case).}
  256. procedure def_system_macro(const name : string);
  257. procedure set_system_macro(const name, value : string);
  258. procedure set_system_compvar(const name, value : string);
  259. procedure undef_system_macro(const name : string);
  260. {*** symtable stack ***}
  261. { $ifdef DEBUG
  262. procedure test_symtablestack;
  263. procedure list_symtablestack;
  264. $endif DEBUG}
  265. {$ifdef UNITALIASES}
  266. type
  267. punit_alias = ^tunit_alias;
  268. tunit_alias = object(TNamedIndexItem)
  269. newname : pshortstring;
  270. constructor init(const n:string);
  271. destructor done;virtual;
  272. end;
  273. var
  274. unitaliases : pdictionary;
  275. procedure addunitalias(const n:string);
  276. function getunitalias(const n:string):string;
  277. {$endif UNITALIASES}
  278. {*** Init / Done ***}
  279. procedure IniTSymtable;
  280. procedure DoneSymtable;
  281. const
  282. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  283. { NOTOKEN } 'error',
  284. { _PLUS } 'plus',
  285. { _MINUS } 'minus',
  286. { _STAR } 'star',
  287. { _SLASH } 'slash',
  288. { _EQ } 'equal',
  289. { _GT } 'greater',
  290. { _LT } 'lower',
  291. { _GTE } 'greater_or_equal',
  292. { _LTE } 'lower_or_equal',
  293. { _NE } 'not_equal',
  294. { _SYMDIF } 'sym_diff',
  295. { _STARSTAR } 'starstar',
  296. { _OP_AS } 'as',
  297. { _OP_IN } 'in',
  298. { _OP_IS } 'is',
  299. { _OP_OR } 'or',
  300. { _OP_AND } 'and',
  301. { _OP_DIV } 'div',
  302. { _OP_MOD } 'mod',
  303. { _OP_NOT } 'not',
  304. { _OP_SHL } 'shl',
  305. { _OP_SHR } 'shr',
  306. { _OP_XOR } 'xor',
  307. { _ASSIGNMENT } 'assign',
  308. { _OP_EXPLICIT } 'explicit',
  309. { _OP_ENUMERATOR } 'enumerator',
  310. { _OP_INC } 'inc',
  311. { _OP_DEC } 'dec');
  312. implementation
  313. uses
  314. { global }
  315. verbose,globals,
  316. { symtable }
  317. symutil,defutil,defcmp,
  318. { module }
  319. fmodule,
  320. { codegen }
  321. procinfo
  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,false);
  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, checking for duplicate names }
  1234. // unionst.SymList.List.List^[i].Data:=nil;
  1235. insert(sym);
  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. warn: boolean;
  1317. begin
  1318. result:=false;
  1319. if not assigned(defowner) then
  1320. internalerror(200602061);
  1321. { procsym and propertysym have special code
  1322. to override values in inherited classes. For other
  1323. symbols check for duplicates }
  1324. if not(sym.typ in [procsym,propertysym]) then
  1325. begin
  1326. { but private ids can be reused }
  1327. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1328. if assigned(hsym) and
  1329. (
  1330. (
  1331. not(m_delphi in current_settings.modeswitches) and
  1332. is_visible_for_object(hsym,tobjectdef(defowner))
  1333. ) or
  1334. (
  1335. { In Delphi, you can repeat members of a parent class. You can't }
  1336. { do this for objects however, and you (obviouly) can't }
  1337. { declare two fields with the same name in a single class }
  1338. (m_delphi in current_settings.modeswitches) and
  1339. (
  1340. is_object(tdef(defowner)) or
  1341. (hsym.owner = self)
  1342. )
  1343. )
  1344. ) then
  1345. begin
  1346. { only watn when a parameter/local variable in a method
  1347. conflicts with a category method, because this can easily
  1348. happen due to all possible categories being imported via
  1349. CocoaAll }
  1350. warn:=
  1351. (is_objccategory(tdef(hsym.owner.defowner)) or
  1352. is_classhelper(tdef(hsym.owner.defowner))) and
  1353. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1354. DuplicateSym(hashedid,sym,hsym,warn);
  1355. result:=true;
  1356. end;
  1357. end
  1358. else
  1359. result:=inherited checkduplicate(hashedid,sym);
  1360. end;
  1361. {****************************************************************************
  1362. TAbstractLocalSymtable
  1363. ****************************************************************************}
  1364. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1365. var
  1366. oldtyp : byte;
  1367. begin
  1368. oldtyp:=ppufile.entrytyp;
  1369. ppufile.entrytyp:=subentryid;
  1370. inherited ppuwrite(ppufile);
  1371. ppufile.entrytyp:=oldtyp;
  1372. end;
  1373. function tabstractlocalsymtable.count_locals:longint;
  1374. var
  1375. i : longint;
  1376. sym : tsym;
  1377. begin
  1378. result:=0;
  1379. for i:=0 to SymList.Count-1 do
  1380. begin
  1381. sym:=tsym(SymList[i]);
  1382. { Count only varsyms, but ignore the funcretsym }
  1383. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1384. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1385. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1386. (tstoredsym(sym).refs>0)) then
  1387. inc(result);
  1388. end;
  1389. end;
  1390. {****************************************************************************
  1391. TLocalSymtable
  1392. ****************************************************************************}
  1393. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1394. begin
  1395. inherited create('');
  1396. defowner:=adefowner;
  1397. symtabletype:=localsymtable;
  1398. symtablelevel:=level;
  1399. end;
  1400. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1401. var
  1402. hsym : tsym;
  1403. begin
  1404. if not assigned(defowner) or
  1405. (defowner.typ<>procdef) then
  1406. internalerror(200602042);
  1407. result:=false;
  1408. hsym:=tsym(FindWithHash(hashedid));
  1409. if assigned(hsym) then
  1410. begin
  1411. { a local and the function can have the same
  1412. name in TP and Delphi, but RESULT not }
  1413. if (m_duplicate_names in current_settings.modeswitches) and
  1414. (hsym.typ in [absolutevarsym,localvarsym]) and
  1415. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1416. not((m_result in current_settings.modeswitches) and
  1417. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1418. HideSym(hsym)
  1419. else
  1420. DuplicateSym(hashedid,sym,hsym,false);
  1421. result:=true;
  1422. exit;
  1423. end;
  1424. { check also parasymtable, this needs to be done here because
  1425. of the special situation with the funcret sym that needs to be
  1426. hidden for tp and delphi modes }
  1427. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1428. if assigned(hsym) then
  1429. begin
  1430. { a local and the function can have the same
  1431. name in TP and Delphi, but RESULT not }
  1432. if (m_duplicate_names in current_settings.modeswitches) and
  1433. (sym.typ in [absolutevarsym,localvarsym]) and
  1434. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1435. not((m_result in current_settings.modeswitches) and
  1436. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1437. Hidesym(sym)
  1438. else
  1439. DuplicateSym(hashedid,sym,hsym,false);
  1440. result:=true;
  1441. exit;
  1442. end;
  1443. { check ObjectSymtable, skip this for funcret sym because
  1444. that will always be positive because it has the same name
  1445. as the procsym }
  1446. if not is_funcret_sym(sym) and
  1447. (defowner.typ=procdef) and
  1448. assigned(tprocdef(defowner).struct) and
  1449. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1450. (
  1451. not(m_delphi in current_settings.modeswitches) or
  1452. is_object(tprocdef(defowner).struct)
  1453. ) then
  1454. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1455. end;
  1456. {****************************************************************************
  1457. TParaSymtable
  1458. ****************************************************************************}
  1459. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1460. begin
  1461. inherited create('');
  1462. readonly:=false;
  1463. defowner:=adefowner;
  1464. symtabletype:=parasymtable;
  1465. symtablelevel:=level;
  1466. end;
  1467. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1468. begin
  1469. result:=inherited checkduplicate(hashedid,sym);
  1470. if result then
  1471. exit;
  1472. if not(m_duplicate_names in current_settings.modeswitches) and
  1473. assigned(defowner) and (defowner.typ=procdef) and
  1474. assigned(tprocdef(defowner).struct) and
  1475. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1476. (
  1477. not(m_delphi in current_settings.modeswitches) or
  1478. is_object(tprocdef(defowner).struct)
  1479. ) then
  1480. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1481. end;
  1482. procedure tparasymtable.insertdef(def: TDefEntry);
  1483. begin
  1484. if readonly then
  1485. defowner.owner.insertdef(def)
  1486. else
  1487. inherited insertdef(def);
  1488. end;
  1489. {****************************************************************************
  1490. TAbstractUniTSymtable
  1491. ****************************************************************************}
  1492. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1493. begin
  1494. inherited create(n);
  1495. moduleid:=id;
  1496. end;
  1497. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1498. var
  1499. hsym : tsym;
  1500. begin
  1501. result:=false;
  1502. hsym:=tsym(FindWithHash(hashedid));
  1503. if assigned(hsym) then
  1504. begin
  1505. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  1506. exit;
  1507. if hsym.typ=symconst.namespacesym then
  1508. begin
  1509. case sym.typ of
  1510. symconst.namespacesym:;
  1511. symconst.unitsym:
  1512. begin
  1513. 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 }
  1514. tnamespacesym(hsym).unitsym:=tsym(sym);
  1515. end
  1516. else
  1517. HideSym(hsym);
  1518. end;
  1519. end
  1520. else
  1521. { In delphi (contrary to TP) you can have a symbol with the same name as the
  1522. unit, the unit can then not be accessed anymore using
  1523. <unit>.<id>, so we can hide the symbol.
  1524. Do the same if we add a namespace and there is a unit with the same name }
  1525. if (hsym.typ=symconst.unitsym) and
  1526. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  1527. begin
  1528. HideSym(hsym);
  1529. if sym.typ=symconst.namespacesym then
  1530. tnamespacesym(sym).unitsym:=tsym(hsym);
  1531. end
  1532. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  1533. in this case, copy the isoindex and make the original symbol invisible }
  1534. else if (m_iso in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  1535. and (tstaticvarsym(hsym).isoindex<>0) then
  1536. begin
  1537. HideSym(hsym);
  1538. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  1539. end
  1540. else
  1541. DuplicateSym(hashedid,sym,hsym,false);
  1542. result:=true;
  1543. exit;
  1544. end;
  1545. end;
  1546. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  1547. begin
  1548. result:=find(n);
  1549. if assigned(result)and(result.typ<>namespacesym)then
  1550. result:=nil;
  1551. end;
  1552. function tabstractuniTSymtable.iscurrentunit:boolean;
  1553. begin
  1554. result:=assigned(current_module) and
  1555. (
  1556. (current_module.globalsymtable=self) or
  1557. (current_module.localsymtable=self)
  1558. );
  1559. end;
  1560. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  1561. var
  1562. p:integer;
  1563. n,ns:string;
  1564. oldsym:TSymEntry;
  1565. begin
  1566. insert(sym);
  1567. n:=sym.realname;
  1568. p:=pos('.',n);
  1569. ns:='';
  1570. while p>0 do
  1571. begin
  1572. if ns='' then
  1573. ns:=copy(n,1,p-1)
  1574. else
  1575. ns:=ns+'.'+copy(n,1,p-1);
  1576. system.delete(n,1,p);
  1577. oldsym:=findnamespace(upper(ns));
  1578. if not assigned(oldsym) then
  1579. insert(cnamespacesym.create(ns));
  1580. p:=pos('.',n);
  1581. end;
  1582. end;
  1583. {****************************************************************************
  1584. TStaticSymtable
  1585. ****************************************************************************}
  1586. constructor tstaticsymtable.create(const n : string;id:word);
  1587. begin
  1588. inherited create(n,id);
  1589. symtabletype:=staticsymtable;
  1590. symtablelevel:=main_program_level;
  1591. currentvisibility:=vis_private;
  1592. end;
  1593. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1594. begin
  1595. inherited ppuload(ppufile);
  1596. { now we can deref the syms and defs }
  1597. deref;
  1598. end;
  1599. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1600. begin
  1601. inherited ppuwrite(ppufile);
  1602. end;
  1603. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1604. begin
  1605. result:=inherited checkduplicate(hashedid,sym);
  1606. if not result and
  1607. (current_module.localsymtable=self) and
  1608. assigned(current_module.globalsymtable) then
  1609. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1610. end;
  1611. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  1612. begin
  1613. result:=inherited findnamespace(n);
  1614. if not assigned(result) and
  1615. (current_module.localsymtable=self) and
  1616. assigned(current_module.globalsymtable) then
  1617. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  1618. end;
  1619. {****************************************************************************
  1620. TGlobalSymtable
  1621. ****************************************************************************}
  1622. constructor tglobalsymtable.create(const n : string;id:word);
  1623. begin
  1624. inherited create(n,id);
  1625. symtabletype:=globalsymtable;
  1626. symtablelevel:=main_program_level;
  1627. end;
  1628. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1629. begin
  1630. inherited ppuload(ppufile);
  1631. { now we can deref the syms and defs }
  1632. deref;
  1633. end;
  1634. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1635. begin
  1636. { write the symtable entries }
  1637. inherited ppuwrite(ppufile);
  1638. end;
  1639. {*****************************************************************************
  1640. tspecializesymtable
  1641. *****************************************************************************}
  1642. constructor tspecializesymtable.create(const n : string;id:word);
  1643. begin
  1644. inherited create(n,id);
  1645. { the specialize symtable does not own the syms and defs as they are all
  1646. moved to a different symtable before the symtable is destroyed; this
  1647. avoids calls to "extract" }
  1648. symlist.ownsobjects:=false;
  1649. deflist.ownsobjects:=false;
  1650. end;
  1651. function tspecializesymtable.iscurrentunit: boolean;
  1652. begin
  1653. Result:=true;
  1654. end;
  1655. {****************************************************************************
  1656. TWITHSYMTABLE
  1657. ****************************************************************************}
  1658. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1659. begin
  1660. inherited create('');
  1661. symtabletype:=withsymtable;
  1662. withrefnode:=refnode;
  1663. { Replace SymList with the passed symlist }
  1664. SymList.free;
  1665. SymList:=ASymList;
  1666. defowner:=aowner;
  1667. end;
  1668. destructor twithsymtable.destroy;
  1669. begin
  1670. withrefnode.free;
  1671. { Disable SymList because we don't Own it }
  1672. SymList:=nil;
  1673. inherited destroy;
  1674. end;
  1675. procedure twithsymtable.clear;
  1676. begin
  1677. { remove no entry from a withsymtable as it is only a pointer to the
  1678. recorddef or objectdef symtable }
  1679. end;
  1680. procedure twithsymtable.insertdef(def:TDefEntry);
  1681. begin
  1682. { Definitions can't be registered in the withsymtable
  1683. because the withsymtable is removed after the with block.
  1684. We can't easily solve it here because the next symtable in the
  1685. stack is not known. }
  1686. internalerror(200602046);
  1687. end;
  1688. {****************************************************************************
  1689. TSTT_ExceptionSymtable
  1690. ****************************************************************************}
  1691. constructor tstt_excepTSymtable.create;
  1692. begin
  1693. inherited create('');
  1694. symtabletype:=stt_excepTSymtable;
  1695. end;
  1696. {****************************************************************************
  1697. TMacroSymtable
  1698. ****************************************************************************}
  1699. constructor tmacrosymtable.create(exported: boolean);
  1700. begin
  1701. inherited create('');
  1702. if exported then
  1703. symtabletype:=exportedmacrosymtable
  1704. else
  1705. symtabletype:=localmacrosymtable;
  1706. symtablelevel:=main_program_level;
  1707. end;
  1708. {****************************************************************************
  1709. TEnumSymtable
  1710. ****************************************************************************}
  1711. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1712. var
  1713. value: longint;
  1714. def: tenumdef;
  1715. begin
  1716. // defowner = nil only when we are loading from ppu
  1717. if defowner<>nil then
  1718. begin
  1719. { First entry? Then we need to set the minval }
  1720. value:=tenumsym(sym).value;
  1721. def:=tenumdef(defowner);
  1722. if SymList.count=0 then
  1723. begin
  1724. if value>0 then
  1725. def.has_jumps:=true;
  1726. def.setmin(value);
  1727. def.setmax(value);
  1728. end
  1729. else
  1730. begin
  1731. { check for jumps }
  1732. if value>def.max+1 then
  1733. def.has_jumps:=true;
  1734. { update low and high }
  1735. if def.min>value then
  1736. def.setmin(value);
  1737. if def.max<value then
  1738. def.setmax(value);
  1739. end;
  1740. end;
  1741. inherited insert(sym, checkdup);
  1742. end;
  1743. constructor tenumsymtable.create(adefowner: tdef);
  1744. begin
  1745. inherited Create('');
  1746. symtabletype:=enumsymtable;
  1747. defowner:=adefowner;
  1748. end;
  1749. {****************************************************************************
  1750. TArraySymtable
  1751. ****************************************************************************}
  1752. procedure tarraysymtable.insertdef(def: TDefEntry);
  1753. begin
  1754. { Enums must also be available outside the record scope,
  1755. insert in the owner of this symtable }
  1756. if def.typ=enumdef then
  1757. defowner.owner.insertdef(def)
  1758. else
  1759. inherited insertdef(def);
  1760. end;
  1761. constructor tarraysymtable.create(adefowner: tdef);
  1762. begin
  1763. inherited Create('');
  1764. symtabletype:=arraysymtable;
  1765. defowner:=adefowner;
  1766. end;
  1767. {*****************************************************************************
  1768. Helper Routines
  1769. *****************************************************************************}
  1770. function FullTypeName(def,otherdef:tdef):string;
  1771. var
  1772. s1,s2 : string;
  1773. begin
  1774. if def.typ in [objectdef,recorddef] then
  1775. s1:=tabstractrecorddef(def).RttiName
  1776. else
  1777. s1:=def.typename;
  1778. { When the names are the same try to include the unit name }
  1779. if assigned(otherdef) and
  1780. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1781. begin
  1782. s2:=otherdef.typename;
  1783. if upper(s1)=upper(s2) then
  1784. s1:=def.owner.realname^+'.'+s1;
  1785. end;
  1786. FullTypeName:=s1;
  1787. end;
  1788. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1789. begin
  1790. result:='';
  1791. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1792. begin
  1793. if (result='') then
  1794. if symtable.name<>nil then
  1795. result:=symtable.name^
  1796. else
  1797. else
  1798. if symtable.name<>nil then
  1799. result:=symtable.name^+delimiter+result
  1800. else
  1801. result:=delimiter+result;
  1802. symtable:=symtable.defowner.owner;
  1803. end;
  1804. end;
  1805. function generate_objectpascal_helper_key(def:tdef):string;
  1806. begin
  1807. if not assigned(def) then
  1808. internalerror(2013020501);
  1809. if def.typ in [recorddef,objectdef] then
  1810. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  1811. else
  1812. result:=make_mangledname('',def.owner,def.typesym.name);
  1813. end;
  1814. procedure incompatibletypes(def1,def2:tdef);
  1815. begin
  1816. { When there is an errordef there is already an error message show }
  1817. if (def2.typ=errordef) or
  1818. (def1.typ=errordef) then
  1819. exit;
  1820. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1821. end;
  1822. procedure hidesym(sym:TSymEntry);
  1823. begin
  1824. sym.realname:='$hidden'+sym.realname;
  1825. tsym(sym).visibility:=vis_hidden;
  1826. end;
  1827. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  1828. var
  1829. st : TSymtable;
  1830. begin
  1831. if not warn then
  1832. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  1833. else
  1834. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  1835. { Write hint where the original symbol was found }
  1836. st:=finduniTSymtable(origsym.owner);
  1837. with tsym(origsym).fileinfo do
  1838. begin
  1839. if assigned(st) and
  1840. (st.symtabletype=globalsymtable) and
  1841. st.iscurrentunit then
  1842. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1843. else if assigned(st.name) then
  1844. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1845. end;
  1846. { Rename duplicate sym to an unreachable name, but it can be
  1847. inserted in the symtable without errors }
  1848. inc(dupnr);
  1849. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1850. if assigned(dupsym) then
  1851. include(tsym(dupsym).symoptions,sp_implicitrename);
  1852. end;
  1853. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  1854. begin
  1855. result:=false;
  1856. if not assigned(sym) or not (sym is tstoredsym) then
  1857. Internalerror(2011081101);
  1858. { For generics a dummy symbol without the parameter count is created
  1859. if such a symbol not yet exists so that different parts of the
  1860. parser can find that symbol. If that symbol is still a
  1861. undefineddef we replace the generic dummy symbol's
  1862. name with a "dup" name and use the new symbol as the generic dummy
  1863. symbol }
  1864. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  1865. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  1866. (m_delphi in current_settings.modeswitches) then
  1867. begin
  1868. inc(dupnr);
  1869. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  1870. include(tsym(sym).symoptions,sp_implicitrename);
  1871. { we need to find the new symbol now if checking for a dummy }
  1872. include(symoptions,sp_generic_dummy);
  1873. result:=true;
  1874. end;
  1875. end;
  1876. function get_jumpbuf_size : longint;
  1877. var
  1878. srsym : ttypesym;
  1879. begin
  1880. if jmp_buf_size=-1 then
  1881. begin
  1882. srsym:=search_system_type('JMP_BUF');
  1883. jmp_buf_size:=srsym.typedef.size;
  1884. jmp_buf_align:=srsym.typedef.alignment;
  1885. end;
  1886. result:=jmp_buf_size;
  1887. end;
  1888. {*****************************************************************************
  1889. Search
  1890. *****************************************************************************}
  1891. procedure addsymref(sym:tsym);
  1892. var
  1893. owner: tsymtable;
  1894. begin
  1895. { symbol uses count }
  1896. sym.IncRefCount;
  1897. { unit uses count }
  1898. owner:=sym.owner;
  1899. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  1900. owner:=tdef(owner.defowner).owner;
  1901. if assigned(current_module) and
  1902. (owner.symtabletype=globalsymtable) then
  1903. begin
  1904. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  1905. internalerror(200501152);
  1906. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  1907. end;
  1908. end;
  1909. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  1910. begin
  1911. result:=nesteddef=ownerdef;
  1912. if not result and
  1913. { types declared locally in a record method are not defined in the
  1914. record itself }
  1915. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  1916. assigned(nesteddef.owner.defowner) then
  1917. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  1918. end;
  1919. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  1920. begin
  1921. result:=assigned(childsym) and (childsym.owner=symtable);
  1922. if not result and assigned(childsym) and
  1923. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1924. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  1925. end;
  1926. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  1927. begin
  1928. result:=false;
  1929. if not assigned(def1) or not assigned(def2) then
  1930. exit;
  1931. { for both defs walk to the topmost generic }
  1932. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  1933. def1:=tdef(def1.owner.defowner);
  1934. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  1935. def2:=tdef(def2.owner.defowner);
  1936. result:=def1=def2;
  1937. end;
  1938. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  1939. var
  1940. uname : string;
  1941. begin
  1942. { TODO : check regarding arrays and records declared as their type }
  1943. if not (def.typ in [recorddef,objectdef]) then
  1944. internalerror(2012051501);
  1945. uname:=upper(srsym.realname);
  1946. repeat
  1947. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  1948. begin
  1949. result:=def;
  1950. exit;
  1951. end;
  1952. def:=tdef(def.owner.defowner);
  1953. until not (def.typ in [recorddef,objectdef]);
  1954. result:=nil;
  1955. end;
  1956. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  1957. begin
  1958. { TODO : check regarding arrays and records declared as their type }
  1959. if not (nesteddef.typ in [recorddef,objectdef]) then
  1960. internalerror(2012051601);
  1961. repeat
  1962. if tstoreddef(nesteddef).genericdef=genericdef then
  1963. begin
  1964. resultdef:=nesteddef;
  1965. result:=true;
  1966. exit;
  1967. end;
  1968. nesteddef:=tdef(nesteddef.owner.defowner);
  1969. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  1970. resultdef:=nil;
  1971. result:=false;
  1972. end;
  1973. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  1974. symvisibility: visibility of the symbol
  1975. contextobjdef: via which def the symbol is accessed, e.g.:
  1976. fieldname:=1 -> contextobjdef = current_structdef
  1977. objfield.fieldname:=1 -> contextobjdef = def of objfield
  1978. }
  1979. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1980. var
  1981. symownerdef : tabstractrecorddef;
  1982. nonlocalst : tsymtable;
  1983. begin
  1984. result:=false;
  1985. { Get objdectdef owner of the symtable for the is_related checks }
  1986. if not assigned(symst) or
  1987. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1988. internalerror(200810285);
  1989. symownerdef:=tabstractrecorddef(symst.defowner);
  1990. { specializations might belong to a localsymtable or parasymtable }
  1991. nonlocalst:=symownerdef.owner;
  1992. if tstoreddef(symst.defowner).is_specialization then
  1993. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  1994. nonlocalst:=nonlocalst.defowner.owner;
  1995. case symvisibility of
  1996. vis_private :
  1997. begin
  1998. { private symbols are allowed when we are in the same
  1999. module as they are defined }
  2000. result:=(
  2001. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2002. (nonlocalst.iscurrentunit)
  2003. ) or
  2004. ( // the case of specialize inside the generic declaration and nested types
  2005. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2006. (
  2007. assigned(current_structdef) and
  2008. (
  2009. (current_structdef=symownerdef) or
  2010. (current_structdef.owner.iscurrentunit)
  2011. )
  2012. ) or
  2013. (
  2014. not assigned(current_structdef) and
  2015. (symownerdef.owner.iscurrentunit)
  2016. )
  2017. );
  2018. end;
  2019. vis_strictprivate :
  2020. begin
  2021. result:=assigned(current_structdef) and
  2022. is_owned_by(current_structdef,symownerdef);
  2023. end;
  2024. vis_strictprotected :
  2025. begin
  2026. result:=(
  2027. { access from nested class }
  2028. assigned(current_structdef) and
  2029. is_owned_by(current_structdef,symownerdef)
  2030. ) or
  2031. (
  2032. { access from child class }
  2033. assigned(contextobjdef) and
  2034. assigned(current_structdef) and
  2035. def_is_related(contextobjdef,symownerdef) and
  2036. def_is_related(current_structdef,contextobjdef)
  2037. ) or
  2038. (
  2039. { helpers can access strict protected symbols }
  2040. is_objectpascal_helper(contextobjdef) and
  2041. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2042. ) or
  2043. (
  2044. { same as above, but from context of call node inside
  2045. helper method }
  2046. is_objectpascal_helper(current_structdef) and
  2047. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2048. );
  2049. end;
  2050. vis_protected :
  2051. begin
  2052. { protected symbols are visible in the module that defines them and
  2053. also visible to related objects. The related object must be defined
  2054. in the current module }
  2055. result:=(
  2056. (
  2057. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2058. (nonlocalst.iscurrentunit)
  2059. ) or
  2060. (
  2061. assigned(contextobjdef) and
  2062. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  2063. (contextobjdef.owner.iscurrentunit) and
  2064. def_is_related(contextobjdef,symownerdef)
  2065. ) or
  2066. ( // the case of specialize inside the generic declaration and nested types
  2067. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2068. (
  2069. assigned(current_structdef) and
  2070. (
  2071. (current_structdef=symownerdef) or
  2072. (current_structdef.owner.iscurrentunit)
  2073. )
  2074. ) or
  2075. (
  2076. not assigned(current_structdef) and
  2077. (symownerdef.owner.iscurrentunit)
  2078. ) or
  2079. (
  2080. { helpers can access protected symbols }
  2081. is_objectpascal_helper(contextobjdef) and
  2082. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2083. )
  2084. )
  2085. );
  2086. end;
  2087. vis_public,
  2088. vis_published :
  2089. result:=true;
  2090. end;
  2091. end;
  2092. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2093. begin
  2094. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2095. end;
  2096. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2097. var
  2098. i : longint;
  2099. pd : tprocdef;
  2100. begin
  2101. if sym.typ=procsym then
  2102. begin
  2103. { A procsym is visible, when there is at least one of the procdefs visible }
  2104. result:=false;
  2105. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2106. begin
  2107. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2108. if (pd.owner=sym.owner) and
  2109. is_visible_for_object(pd,contextobjdef) then
  2110. begin
  2111. result:=true;
  2112. exit;
  2113. end;
  2114. end;
  2115. end
  2116. else
  2117. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2118. end;
  2119. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2120. begin
  2121. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2122. end;
  2123. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2124. begin
  2125. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2126. end;
  2127. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2128. var
  2129. hashedid: THashedIDString;
  2130. contextstructdef: tabstractrecorddef;
  2131. stackitem: psymtablestackitem;
  2132. begin
  2133. result:=false;
  2134. hashedid.id:=s;
  2135. stackitem:=symtablestack.stack;
  2136. while assigned(stackitem) do
  2137. begin
  2138. srsymtable:=stackitem^.symtable;
  2139. if (srsymtable.symtabletype=objectsymtable) then
  2140. begin
  2141. { TODO : implement the search for an option in classes as well }
  2142. if ssf_search_option in flags then
  2143. begin
  2144. result:=false;
  2145. exit;
  2146. end;
  2147. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2148. begin
  2149. result:=true;
  2150. exit;
  2151. end;
  2152. end
  2153. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2154. (srsymtable.defowner.typ=undefineddef)) then
  2155. begin
  2156. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2157. { First check if it is a unit/namespace symbol.
  2158. They are visible only if they are from the current unit or
  2159. unit of generic of currently processed specialization. }
  2160. if assigned(srsym) and
  2161. (
  2162. not(srsym.typ in [unitsym,namespacesym]) or
  2163. srsymtable.iscurrentunit or
  2164. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2165. ) and
  2166. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  2167. begin
  2168. { use the class from withsymtable only when it is
  2169. defined in this unit }
  2170. if (srsymtable.symtabletype=withsymtable) and
  2171. assigned(srsymtable.defowner) and
  2172. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2173. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2174. (srsymtable.defowner.owner.iscurrentunit) then
  2175. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2176. else
  2177. contextstructdef:=current_structdef;
  2178. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2179. is_visible_for_object(srsym,contextstructdef) then
  2180. begin
  2181. { we need to know if a procedure references symbols
  2182. in the static symtable, because then it can't be
  2183. inlined from outside this unit }
  2184. if assigned(current_procinfo) and
  2185. (srsym.owner.symtabletype=staticsymtable) then
  2186. include(current_procinfo.flags,pi_uses_static_symtable);
  2187. if not (ssf_no_addsymref in flags) then
  2188. addsymref(srsym);
  2189. result:=true;
  2190. exit;
  2191. end;
  2192. end;
  2193. end;
  2194. stackitem:=stackitem^.next;
  2195. end;
  2196. srsym:=nil;
  2197. srsymtable:=nil;
  2198. end;
  2199. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2200. srsymtable:TSymtable;option:tsymoption):boolean;
  2201. begin
  2202. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  2203. end;
  2204. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2205. var
  2206. hashedid : THashedIDString;
  2207. stackitem : psymtablestackitem;
  2208. classh : tobjectdef;
  2209. begin
  2210. result:=false;
  2211. hashedid.id:=s;
  2212. stackitem:=symtablestack.stack;
  2213. while assigned(stackitem) do
  2214. begin
  2215. {
  2216. It is not possible to have type symbols in:
  2217. parameters
  2218. Exception are classes, objects, records, generic definitions and specializations
  2219. that have the parameterized types inserted in the symtable.
  2220. }
  2221. srsymtable:=stackitem^.symtable;
  2222. if (srsymtable.symtabletype=ObjectSymtable) then
  2223. begin
  2224. classh:=tobjectdef(srsymtable.defowner);
  2225. while assigned(classh) do
  2226. begin
  2227. srsymtable:=classh.symtable;
  2228. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2229. if assigned(srsym) and
  2230. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2231. is_visible_for_object(srsym,current_structdef) then
  2232. begin
  2233. addsymref(srsym);
  2234. result:=true;
  2235. exit;
  2236. end;
  2237. classh:=classh.childof;
  2238. end;
  2239. end
  2240. else
  2241. begin
  2242. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2243. if assigned(srsym) and
  2244. (
  2245. not(srsym.typ in [unitsym,namespacesym]) or
  2246. srsymtable.iscurrentunit or
  2247. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2248. ) and
  2249. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2250. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2251. begin
  2252. { we need to know if a procedure references symbols
  2253. in the static symtable, because then it can't be
  2254. inlined from outside this unit }
  2255. if assigned(current_procinfo) and
  2256. (srsym.owner.symtabletype=staticsymtable) then
  2257. include(current_procinfo.flags,pi_uses_static_symtable);
  2258. addsymref(srsym);
  2259. result:=true;
  2260. exit;
  2261. end;
  2262. end;
  2263. stackitem:=stackitem^.next;
  2264. end;
  2265. result:=false;
  2266. srsym:=nil;
  2267. srsymtable:=nil;
  2268. end;
  2269. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2270. var
  2271. pmod : tmodule;
  2272. begin
  2273. pmod:=tmodule(pm);
  2274. result:=false;
  2275. if assigned(pmod.globalsymtable) then
  2276. begin
  2277. srsym:=tsym(pmod.globalsymtable.Find(s));
  2278. if assigned(srsym) then
  2279. begin
  2280. srsymtable:=pmod.globalsymtable;
  2281. addsymref(srsym);
  2282. result:=true;
  2283. exit;
  2284. end;
  2285. end;
  2286. { If the module is the current unit we also need
  2287. to search the local symtable }
  2288. if (pmod=current_module) and
  2289. assigned(pmod.localsymtable) then
  2290. begin
  2291. srsym:=tsym(pmod.localsymtable.Find(s));
  2292. if assigned(srsym) then
  2293. begin
  2294. srsymtable:=pmod.localsymtable;
  2295. addsymref(srsym);
  2296. result:=true;
  2297. exit;
  2298. end;
  2299. end;
  2300. srsym:=nil;
  2301. srsymtable:=nil;
  2302. end;
  2303. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2304. var
  2305. stackitem : psymtablestackitem;
  2306. begin
  2307. result:=false;
  2308. stackitem:=symtablestack.stack;
  2309. while assigned(stackitem) do
  2310. begin
  2311. srsymtable:=stackitem^.symtable;
  2312. if (srsymtable.symtabletype=globalsymtable) and
  2313. (srsymtable.name^=unitname) then
  2314. begin
  2315. srsym:=tsym(srsymtable.find(symname));
  2316. if not assigned(srsym) then
  2317. break;
  2318. result:=true;
  2319. exit;
  2320. end;
  2321. stackitem:=stackitem^.next;
  2322. end;
  2323. { If the module is the current unit we also need
  2324. to search the local symtable }
  2325. if assigned(current_module.localsymtable) and
  2326. (current_module.localsymtable.name^=unitname) then
  2327. begin
  2328. srsymtable:=current_module.localsymtable;
  2329. srsym:=tsym(srsymtable.find(symname));
  2330. if assigned(srsym) then
  2331. begin
  2332. result:=true;
  2333. exit;
  2334. end;
  2335. end;
  2336. end;
  2337. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2338. begin
  2339. result:=pd;
  2340. if pd.typ<>objectdef then
  2341. exit;
  2342. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2343. end;
  2344. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2345. var
  2346. hashedid : THashedIDString;
  2347. stackitem : psymtablestackitem;
  2348. srsymtable : tsymtable;
  2349. srsym : tsym;
  2350. formalname,
  2351. foundname : shortstring;
  2352. formalnameptr,
  2353. foundnameptr: pshortstring;
  2354. begin
  2355. { not a formal definition -> return it }
  2356. if not(oo_is_formal in pd.objectoptions) then
  2357. begin
  2358. result:=pd;
  2359. exit;
  2360. end;
  2361. hashedid.id:=pd.typesym.name;
  2362. stackitem:=symtablestack.stack;
  2363. while assigned(stackitem) do
  2364. begin
  2365. srsymtable:=stackitem^.symtable;
  2366. { ObjC classes can't appear in generics or as nested class
  2367. definitions. Java classes can. }
  2368. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2369. (is_java_class_or_interface(pd) and
  2370. (srsymtable.symtabletype=ObjectSymtable)) then
  2371. begin
  2372. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2373. if assigned(srsym) and
  2374. (srsym.typ=typesym) and
  2375. (ttypesym(srsym).typedef.typ=objectdef) and
  2376. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2377. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2378. begin
  2379. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2380. begin
  2381. { the external name for the formal and the real
  2382. definition must match }
  2383. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2384. assigned(pd.import_lib) then
  2385. begin
  2386. if assigned(pd.import_lib) then
  2387. formalname:=pd.import_lib^+'.'
  2388. else
  2389. formalname:='';
  2390. formalname:=formalname+pd.objextname^;
  2391. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2392. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2393. else
  2394. foundname:='';
  2395. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2396. formalnameptr:=@formalname;
  2397. foundnameptr:=@foundname;
  2398. end
  2399. else
  2400. begin
  2401. formalnameptr:=pd.objextname;
  2402. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2403. end;
  2404. if foundnameptr^<>formalnameptr^ then
  2405. begin
  2406. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2407. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2408. end;
  2409. end;
  2410. result:=tobjectdef(ttypesym(srsym).typedef);
  2411. if assigned(current_procinfo) and
  2412. (srsym.owner.symtabletype=staticsymtable) then
  2413. include(current_procinfo.flags,pi_uses_static_symtable);
  2414. addsymref(srsym);
  2415. exit;
  2416. end;
  2417. end;
  2418. stackitem:=stackitem^.next;
  2419. end;
  2420. { nothing found: optionally give an error and return the original
  2421. (empty) one }
  2422. if erroronfailure then
  2423. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2424. result:=pd;
  2425. end;
  2426. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2427. var
  2428. hashedid : THashedIDString;
  2429. orgclass : tobjectdef;
  2430. i : longint;
  2431. hlpsrsym : tsym;
  2432. hlpsrsymtable : tsymtable;
  2433. begin
  2434. orgclass:=classh;
  2435. { in case this is a formal class, first find the real definition }
  2436. if assigned(classh) then
  2437. begin
  2438. if (oo_is_formal in classh.objectoptions) then
  2439. classh:=find_real_class_definition(classh,true);
  2440. { The contextclassh is used for visibility. The classh must be equal to
  2441. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2442. parent or a class helper. }
  2443. if not (def_is_related(contextclassh,classh) or
  2444. (is_classhelper(contextclassh) and
  2445. assigned(tobjectdef(contextclassh).extendeddef) and
  2446. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  2447. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  2448. internalerror(200811161);
  2449. end;
  2450. result:=false;
  2451. hashedid.id:=s;
  2452. { an Objective-C protocol or Java interface can inherit from multiple
  2453. other protocols/interfaces -> use ImplementedInterfaces instead }
  2454. if is_objcprotocol(classh) or
  2455. is_javainterface(classh) then
  2456. begin
  2457. srsymtable:=classh.symtable;
  2458. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2459. if assigned(srsym) and
  2460. is_visible_for_object(srsym,contextclassh) then
  2461. begin
  2462. if not (ssf_no_addsymref in flags) then
  2463. addsymref(srsym);
  2464. result:=true;
  2465. exit;
  2466. end;
  2467. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2468. begin
  2469. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  2470. begin
  2471. result:=true;
  2472. exit;
  2473. end;
  2474. end;
  2475. end
  2476. else
  2477. if is_objectpascal_helper(classh) then
  2478. begin
  2479. { helpers have their own obscure search logic... }
  2480. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  2481. if result then
  2482. exit;
  2483. end
  2484. else
  2485. begin
  2486. hlpsrsym:=nil;
  2487. hlpsrsymtable:=nil;
  2488. while assigned(classh) do
  2489. begin
  2490. { search for a class helper method first if this is an Object
  2491. Pascal class and we haven't yet found a helper symbol }
  2492. if is_class(classh) and
  2493. (ssf_search_helper in flags) and
  2494. not assigned(hlpsrsym) then
  2495. begin
  2496. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2497. if result then
  2498. { if the procsym is overloaded we need to use the
  2499. "original" symbol; the helper symbol will be found when
  2500. searching for overloads }
  2501. if (srsym.typ<>procsym) or
  2502. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2503. exit
  2504. else
  2505. begin
  2506. { remember the found symbol if the class hierarchy
  2507. should not contain the a method with that name }
  2508. hlpsrsym:=srsym;
  2509. hlpsrsymtable:=srsymtable;
  2510. end;
  2511. end;
  2512. srsymtable:=classh.symtable;
  2513. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2514. if assigned(srsym) and
  2515. is_visible_for_object(srsym,contextclassh) then
  2516. begin
  2517. if not (ssf_no_addsymref in flags) then
  2518. addsymref(srsym);
  2519. result:=true;
  2520. exit;
  2521. end;
  2522. classh:=classh.childof;
  2523. end;
  2524. { did we find a helper symbol, but no symbol with the same name in
  2525. the extended object's hierarchy? }
  2526. if assigned(hlpsrsym) then
  2527. begin
  2528. srsym:=hlpsrsym;
  2529. srsymtable:=hlpsrsymtable;
  2530. result:=true;
  2531. exit;
  2532. end;
  2533. end;
  2534. if is_objcclass(orgclass) then
  2535. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2536. else
  2537. begin
  2538. srsym:=nil;
  2539. srsymtable:=nil;
  2540. end;
  2541. end;
  2542. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2543. var
  2544. hashedid : THashedIDString;
  2545. hlpsrsym : tsym;
  2546. hlpsrsymtable : tsymtable;
  2547. begin
  2548. result:=false;
  2549. hlpsrsym:=nil;
  2550. hlpsrsymtable:=nil;
  2551. hashedid.id:=s;
  2552. { search for a record helper method first }
  2553. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2554. if result then
  2555. { if the procsym is overloaded we need to use the
  2556. "original" symbol; the helper symbol will be found when
  2557. searching for overloads }
  2558. if (srsym.typ<>procsym) or
  2559. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2560. exit
  2561. else
  2562. begin
  2563. { remember the found symbol if we should not find a symbol with
  2564. the same name in the extended record }
  2565. hlpsrsym:=srsym;
  2566. hlpsrsymtable:=srsymtable;
  2567. end;
  2568. srsymtable:=recordh.symtable;
  2569. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2570. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2571. begin
  2572. addsymref(srsym);
  2573. result:=true;
  2574. exit;
  2575. end;
  2576. srsym:=hlpsrsym;
  2577. srsymtable:=hlpsrsymtable;
  2578. result:=assigned(srsym);
  2579. end;
  2580. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2581. var
  2582. def : tdef;
  2583. i : longint;
  2584. begin
  2585. { in case this is a formal class, first find the real definition }
  2586. if assigned(classh) and
  2587. (oo_is_formal in classh.objectoptions) then
  2588. classh:=find_real_class_definition(classh,true);
  2589. result:=false;
  2590. def:=nil;
  2591. while assigned(classh) do
  2592. begin
  2593. for i:=0 to classh.symtable.DefList.Count-1 do
  2594. begin
  2595. def:=tstoreddef(classh.symtable.DefList[i]);
  2596. { Find also all hidden private methods to
  2597. be compatible with delphi, see tw6203 (PFV) }
  2598. if (def.typ=procdef) and
  2599. (po_msgint in tprocdef(def).procoptions) and
  2600. (tprocdef(def).messageinf.i=msgid) then
  2601. begin
  2602. srdef:=def;
  2603. srsym:=tprocdef(def).procsym;
  2604. srsymtable:=classh.symtable;
  2605. addsymref(srsym);
  2606. result:=true;
  2607. exit;
  2608. end;
  2609. end;
  2610. classh:=classh.childof;
  2611. end;
  2612. srdef:=nil;
  2613. srsym:=nil;
  2614. srsymtable:=nil;
  2615. end;
  2616. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2617. var
  2618. def : tdef;
  2619. i : longint;
  2620. begin
  2621. { in case this is a formal class, first find the real definition }
  2622. if assigned(classh) and
  2623. (oo_is_formal in classh.objectoptions) then
  2624. classh:=find_real_class_definition(classh,true);
  2625. result:=false;
  2626. def:=nil;
  2627. while assigned(classh) do
  2628. begin
  2629. for i:=0 to classh.symtable.DefList.Count-1 do
  2630. begin
  2631. def:=tstoreddef(classh.symtable.DefList[i]);
  2632. { Find also all hidden private methods to
  2633. be compatible with delphi, see tw6203 (PFV) }
  2634. if (def.typ=procdef) and
  2635. (po_msgstr in tprocdef(def).procoptions) and
  2636. (tprocdef(def).messageinf.str^=s) then
  2637. begin
  2638. srsym:=tprocdef(def).procsym;
  2639. srsymtable:=classh.symtable;
  2640. addsymref(srsym);
  2641. result:=true;
  2642. exit;
  2643. end;
  2644. end;
  2645. classh:=classh.childof;
  2646. end;
  2647. srsym:=nil;
  2648. srsymtable:=nil;
  2649. end;
  2650. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2651. var
  2652. hashedid : THashedIDString;
  2653. parentclassh : tobjectdef;
  2654. begin
  2655. result:=false;
  2656. if not is_objectpascal_helper(classh) then
  2657. Internalerror(2011030101);
  2658. hashedid.id:=s;
  2659. { in a helper things are a bit more complex:
  2660. 1. search the symbol in the helper (if not "inherited")
  2661. 2. search the symbol in the extended type
  2662. 3. search the symbol in the parent helpers
  2663. 4. only classes: search the symbol in the parents of the extended type
  2664. }
  2665. if not (ssf_has_inherited in flags) then
  2666. begin
  2667. { search in the helper itself }
  2668. srsymtable:=classh.symtable;
  2669. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2670. if assigned(srsym) and
  2671. is_visible_for_object(srsym,contextclassh) then
  2672. begin
  2673. if not (ssf_no_addsymref in flags) then
  2674. addsymref(srsym);
  2675. result:=true;
  2676. exit;
  2677. end;
  2678. end;
  2679. { now search in the extended type itself }
  2680. { Note: the extendeddef might be Nil if we are currently parsing the
  2681. extended type itself and the identifier was not found }
  2682. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  2683. begin
  2684. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2685. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2686. if assigned(srsym) and
  2687. is_visible_for_object(srsym,contextclassh) then
  2688. begin
  2689. if not (ssf_no_addsymref in flags) then
  2690. addsymref(srsym);
  2691. result:=true;
  2692. exit;
  2693. end;
  2694. end;
  2695. { now search in the parent helpers }
  2696. parentclassh:=classh.childof;
  2697. while assigned(parentclassh) do
  2698. begin
  2699. srsymtable:=parentclassh.symtable;
  2700. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2701. if assigned(srsym) and
  2702. is_visible_for_object(srsym,contextclassh) then
  2703. begin
  2704. if not (ssf_no_addsymref in flags) then
  2705. addsymref(srsym);
  2706. result:=true;
  2707. exit;
  2708. end;
  2709. parentclassh:=parentclassh.childof;
  2710. end;
  2711. if is_class(classh.extendeddef) then
  2712. { now search in the parents of the extended class (with helpers!) }
  2713. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  2714. { addsymref is already called by searchsym_in_class }
  2715. end;
  2716. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2717. var
  2718. sym : Tprocsym;
  2719. hashedid : THashedIDString;
  2720. curreq,
  2721. besteq : tequaltype;
  2722. currpd,
  2723. bestpd : tprocdef;
  2724. stackitem : psymtablestackitem;
  2725. begin
  2726. hashedid.id:=overloaded_names[assignment_type];
  2727. besteq:=te_incompatible;
  2728. bestpd:=nil;
  2729. stackitem:=symtablestack.stack;
  2730. while assigned(stackitem) do
  2731. begin
  2732. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2733. if sym<>nil then
  2734. begin
  2735. if sym.typ<>procsym then
  2736. internalerror(200402031);
  2737. { if the source type is an alias then this is only the second choice,
  2738. if you mess with this code, check tw4093 }
  2739. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2740. if curreq>besteq then
  2741. begin
  2742. besteq:=curreq;
  2743. bestpd:=currpd;
  2744. if (besteq=te_exact) then
  2745. break;
  2746. end;
  2747. end;
  2748. stackitem:=stackitem^.next;
  2749. end;
  2750. result:=bestpd;
  2751. end;
  2752. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2753. begin
  2754. { search record/object symtable first for a suitable operator }
  2755. if from_def.typ in [recorddef,objectdef] then
  2756. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2757. if to_def.typ in [recorddef,objectdef] then
  2758. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2759. { if type conversion is explicit then search first for explicit
  2760. operator overload and if not found then use implicit operator }
  2761. if explicit then
  2762. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2763. else
  2764. result:=nil;
  2765. if result=nil then
  2766. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2767. { restore symtable stack }
  2768. if to_def.typ in [recorddef,objectdef] then
  2769. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2770. if from_def.typ in [recorddef,objectdef] then
  2771. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2772. end;
  2773. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2774. var
  2775. sym : Tprocsym;
  2776. hashedid : THashedIDString;
  2777. curreq,
  2778. besteq : tequaltype;
  2779. currpd,
  2780. bestpd : tprocdef;
  2781. stackitem : psymtablestackitem;
  2782. begin
  2783. hashedid.id:='enumerator';
  2784. besteq:=te_incompatible;
  2785. bestpd:=nil;
  2786. stackitem:=symtablestack.stack;
  2787. while assigned(stackitem) do
  2788. begin
  2789. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2790. if sym<>nil then
  2791. begin
  2792. if sym.typ<>procsym then
  2793. internalerror(200910241);
  2794. { if the source type is an alias then this is only the second choice,
  2795. if you mess with this code, check tw4093 }
  2796. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2797. if curreq>besteq then
  2798. begin
  2799. besteq:=curreq;
  2800. bestpd:=currpd;
  2801. if (besteq=te_exact) then
  2802. break;
  2803. end;
  2804. end;
  2805. stackitem:=stackitem^.next;
  2806. end;
  2807. result:=bestpd;
  2808. end;
  2809. function search_system_type(const s: TIDString): ttypesym;
  2810. var
  2811. sym : tsym;
  2812. begin
  2813. sym:=tsym(systemunit.Find(s));
  2814. if not assigned(sym) or
  2815. (sym.typ<>typesym) then
  2816. message1(cg_f_unknown_system_type,s);
  2817. result:=ttypesym(sym);
  2818. end;
  2819. function try_search_system_type(const s: TIDString): ttypesym;
  2820. var
  2821. sym : tsym;
  2822. begin
  2823. sym:=tsym(systemunit.Find(s));
  2824. if not assigned(sym) then
  2825. result:=nil
  2826. else
  2827. begin
  2828. if sym.typ<>typesym then
  2829. message1(cg_f_unknown_system_type,s);
  2830. result:=ttypesym(sym);
  2831. end;
  2832. end;
  2833. function search_system_proc(const s: TIDString): tprocdef;
  2834. var
  2835. srsym: tsym;
  2836. begin
  2837. srsym:=tsym(systemunit.find(s));
  2838. if not assigned(srsym) and
  2839. (cs_compilesystem in current_settings.moduleswitches) then
  2840. srsym:=tsym(systemunit.Find(upper(s)));
  2841. if not assigned(srsym) or
  2842. (srsym.typ<>procsym) then
  2843. message1(cg_f_unknown_compilerproc,s);
  2844. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  2845. end;
  2846. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2847. var
  2848. srsymtable: tsymtable;
  2849. sym: tsym;
  2850. begin
  2851. sym:=nil;
  2852. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2853. (sym.typ=typesym) then
  2854. begin
  2855. result:=ttypesym(sym);
  2856. exit;
  2857. end
  2858. else
  2859. begin
  2860. if throwerror then
  2861. message2(cg_f_unknown_type_in_unit,typename,unitname);
  2862. result:=nil;
  2863. end;
  2864. end;
  2865. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2866. var
  2867. s: string;
  2868. list: TFPObjectList;
  2869. i: integer;
  2870. st: tsymtable;
  2871. begin
  2872. result:=false;
  2873. odef:=nil;
  2874. { when there are no helpers active currently then we don't need to do
  2875. anything }
  2876. if current_module.extendeddefs.count=0 then
  2877. exit;
  2878. { no helpers for anonymous types }
  2879. if ((pd.typ in [recorddef,objectdef]) and
  2880. (
  2881. not assigned(tabstractrecorddef(pd).objrealname) or
  2882. (tabstractrecorddef(pd).objrealname^='')
  2883. )
  2884. ) or
  2885. not assigned(pd.typesym) then
  2886. exit;
  2887. { if pd is defined inside a procedure we must not use make_mangledname
  2888. (as a helper may not be defined in a procedure this is no problem...)}
  2889. st:=pd.owner;
  2890. while st.symtabletype in [objectsymtable,recordsymtable] do
  2891. st:=st.defowner.owner;
  2892. if st.symtabletype=localsymtable then
  2893. exit;
  2894. { the mangled name is used as the key for tmodule.extendeddefs }
  2895. s:=generate_objectpascal_helper_key(pd);
  2896. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2897. if assigned(list) and (list.count>0) then
  2898. begin
  2899. i:=list.count-1;
  2900. repeat
  2901. odef:=tobjectdef(list[list.count-1]);
  2902. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2903. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2904. dec(i);
  2905. until result or (i<0);
  2906. if not result then
  2907. { just to be sure that noone uses odef }
  2908. odef:=nil;
  2909. end;
  2910. end;
  2911. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2912. var
  2913. hashedid : THashedIDString;
  2914. classh : tobjectdef;
  2915. i : integer;
  2916. pdef : tprocdef;
  2917. begin
  2918. result:=false;
  2919. { if there is no class helper for the class then there is no need to
  2920. search further }
  2921. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2922. exit;
  2923. hashedid.id:=s;
  2924. repeat
  2925. srsymtable:=classh.symtable;
  2926. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2927. if srsym<>nil then
  2928. begin
  2929. case srsym.typ of
  2930. procsym:
  2931. begin
  2932. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2933. begin
  2934. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2935. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2936. continue;
  2937. { we need to know if a procedure references symbols
  2938. in the static symtable, because then it can't be
  2939. inlined from outside this unit }
  2940. if assigned(current_procinfo) and
  2941. (srsym.owner.symtabletype=staticsymtable) then
  2942. include(current_procinfo.flags,pi_uses_static_symtable);
  2943. { the first found method wins }
  2944. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2945. srsymtable:=srsym.owner;
  2946. addsymref(srsym);
  2947. result:=true;
  2948. exit;
  2949. end;
  2950. end;
  2951. typesym,
  2952. fieldvarsym,
  2953. constsym,
  2954. enumsym,
  2955. undefinedsym,
  2956. propertysym:
  2957. begin
  2958. addsymref(srsym);
  2959. result:=true;
  2960. exit;
  2961. end;
  2962. else
  2963. internalerror(2014041101);
  2964. end;
  2965. end;
  2966. { try the helper parent if available }
  2967. classh:=classh.childof;
  2968. until classh=nil;
  2969. srsym:=nil;
  2970. srsymtable:=nil;
  2971. end;
  2972. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2973. var
  2974. hashedid : THashedIDString;
  2975. stackitem : psymtablestackitem;
  2976. i : longint;
  2977. defowner : tobjectdef;
  2978. begin
  2979. hashedid.id:=class_helper_prefix+s;
  2980. stackitem:=symtablestack.stack;
  2981. while assigned(stackitem) do
  2982. begin
  2983. srsymtable:=stackitem^.symtable;
  2984. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2985. if assigned(srsym) then
  2986. begin
  2987. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2988. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2989. (srsym.typ<>procsym) then
  2990. internalerror(2009111505);
  2991. { check whether this procsym includes a helper for this particular class }
  2992. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2993. begin
  2994. { does pd inherit from (or is the same as) the class
  2995. that this method's category extended?
  2996. Warning: this list contains both category and objcclass methods
  2997. (for id.randommethod), so only check category methods here
  2998. }
  2999. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3000. if is_objccategory(defowner) and
  3001. def_is_related(pd,defowner.childof) then
  3002. begin
  3003. { we need to know if a procedure references symbols
  3004. in the static symtable, because then it can't be
  3005. inlined from outside this unit }
  3006. if assigned(current_procinfo) and
  3007. (srsym.owner.symtabletype=staticsymtable) then
  3008. include(current_procinfo.flags,pi_uses_static_symtable);
  3009. { no need to keep looking. There might be other
  3010. categories that extend this, a parent or child
  3011. class with a method with the same name (either
  3012. overriding this one, or overridden by this one),
  3013. but that doesn't matter as far as the basic
  3014. procsym is concerned.
  3015. }
  3016. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3017. srsymtable:=srsym.owner;
  3018. addsymref(srsym);
  3019. result:=true;
  3020. exit;
  3021. end;
  3022. end;
  3023. end;
  3024. stackitem:=stackitem^.next;
  3025. end;
  3026. srsym:=nil;
  3027. srsymtable:=nil;
  3028. result:=false;
  3029. end;
  3030. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3031. var
  3032. hashedid : THashedIDString;
  3033. stackitem : psymtablestackitem;
  3034. i : longint;
  3035. begin
  3036. hashedid.id:=class_helper_prefix+s;
  3037. stackitem:=symtablestack.stack;
  3038. while assigned(stackitem) do
  3039. begin
  3040. srsymtable:=stackitem^.symtable;
  3041. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3042. if assigned(srsym) then
  3043. begin
  3044. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3045. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3046. (srsym.typ<>procsym) then
  3047. internalerror(2009112005);
  3048. { check whether this procsym includes a helper for this particular class }
  3049. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3050. begin
  3051. { we need to know if a procedure references symbols
  3052. in the static symtable, because then it can't be
  3053. inlined from outside this unit }
  3054. if assigned(current_procinfo) and
  3055. (srsym.owner.symtabletype=staticsymtable) then
  3056. include(current_procinfo.flags,pi_uses_static_symtable);
  3057. { no need to keep looking. There might be other
  3058. methods with the same name, but that doesn't matter
  3059. as far as the basic procsym is concerned.
  3060. }
  3061. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3062. { We need the symtable in which the classhelper-like sym
  3063. is located, not the objectdef. The reason is that the
  3064. callnode will climb the symtablestack until it encounters
  3065. this symtable to start looking for overloads (and it won't
  3066. find the objectsymtable in which this method sym is
  3067. located
  3068. srsymtable:=srsym.owner;
  3069. }
  3070. addsymref(srsym);
  3071. result:=true;
  3072. exit;
  3073. end;
  3074. end;
  3075. stackitem:=stackitem^.next;
  3076. end;
  3077. srsym:=nil;
  3078. srsymtable:=nil;
  3079. result:=false;
  3080. end;
  3081. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  3082. { searches n in symtable of pd and all anchestors }
  3083. var
  3084. srsymtable : tsymtable;
  3085. begin
  3086. { in case this is a formal class, first find the real definition }
  3087. if (oo_is_formal in pd.objectoptions) then
  3088. pd:=find_real_class_definition(tobjectdef(pd),true);
  3089. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  3090. exit;
  3091. result:=search_struct_member_no_helper(pd,s);
  3092. if assigned(result) then
  3093. exit;
  3094. { not found, now look for class helpers }
  3095. if is_objcclass(pd) then
  3096. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  3097. end;
  3098. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  3099. var
  3100. hashedid : THashedIDString;
  3101. srsym : tsym;
  3102. begin
  3103. hashedid.id:=s;
  3104. while assigned(pd) do
  3105. begin
  3106. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  3107. if assigned(srsym) then
  3108. begin
  3109. result:=srsym;
  3110. exit;
  3111. end;
  3112. if pd.typ=objectdef then
  3113. pd:=tobjectdef(pd).childof
  3114. else
  3115. pd:=nil;
  3116. end;
  3117. result:=nil;
  3118. end;
  3119. function search_macro(const s : string):tsym;
  3120. var
  3121. stackitem : psymtablestackitem;
  3122. hashedid : THashedIDString;
  3123. srsym : tsym;
  3124. begin
  3125. hashedid.id:=s;
  3126. { First search the localmacrosymtable before searching the
  3127. global macrosymtables from the units }
  3128. if assigned(current_module) then
  3129. begin
  3130. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  3131. if assigned(srsym) then
  3132. begin
  3133. result:= srsym;
  3134. exit;
  3135. end;
  3136. end;
  3137. stackitem:=macrosymtablestack.stack;
  3138. while assigned(stackitem) do
  3139. begin
  3140. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3141. if assigned(srsym) then
  3142. begin
  3143. result:= srsym;
  3144. exit;
  3145. end;
  3146. stackitem:=stackitem^.next;
  3147. end;
  3148. result:= nil;
  3149. end;
  3150. function defined_macro(const s : string):boolean;
  3151. var
  3152. mac: tmacro;
  3153. begin
  3154. mac:=tmacro(search_macro(s));
  3155. if assigned(mac) then
  3156. begin
  3157. mac.is_used:=true;
  3158. defined_macro:=mac.defined;
  3159. end
  3160. else
  3161. defined_macro:=false;
  3162. end;
  3163. {****************************************************************************
  3164. Object Helpers
  3165. ****************************************************************************}
  3166. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  3167. { returns the default property of a class, searches also anchestors }
  3168. var
  3169. _defaultprop : tpropertysym;
  3170. helperpd : tobjectdef;
  3171. begin
  3172. _defaultprop:=nil;
  3173. { first search in helper's hierarchy }
  3174. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3175. while assigned(helperpd) do
  3176. begin
  3177. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3178. if assigned(_defaultprop) then
  3179. break;
  3180. helperpd:=helperpd.childof;
  3181. end;
  3182. if assigned(_defaultprop) then
  3183. begin
  3184. search_default_property:=_defaultprop;
  3185. exit;
  3186. end;
  3187. { now search in the type's hierarchy itself }
  3188. while assigned(pd) do
  3189. begin
  3190. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3191. if assigned(_defaultprop) then
  3192. break;
  3193. if (pd.typ=objectdef) then
  3194. pd:=tobjectdef(pd).childof
  3195. else
  3196. break;
  3197. end;
  3198. search_default_property:=_defaultprop;
  3199. end;
  3200. {****************************************************************************
  3201. Macro Helpers
  3202. ****************************************************************************}
  3203. procedure def_system_macro(const name : string);
  3204. var
  3205. mac : tmacro;
  3206. s: string;
  3207. begin
  3208. if name = '' then
  3209. internalerror(2004121202);
  3210. s:= upper(name);
  3211. mac:=tmacro(search_macro(s));
  3212. if not assigned(mac) then
  3213. begin
  3214. mac:=tmacro.create(s);
  3215. if assigned(current_module) then
  3216. current_module.localmacrosymtable.insert(mac)
  3217. else
  3218. initialmacrosymtable.insert(mac);
  3219. end;
  3220. Message1(parser_c_macro_defined,mac.name);
  3221. mac.defined:=true;
  3222. end;
  3223. procedure set_system_macro(const name, value : string);
  3224. var
  3225. mac : tmacro;
  3226. s: string;
  3227. begin
  3228. if name = '' then
  3229. internalerror(2004121203);
  3230. s:= upper(name);
  3231. mac:=tmacro(search_macro(s));
  3232. if not assigned(mac) then
  3233. begin
  3234. mac:=tmacro.create(s);
  3235. if assigned(current_module) then
  3236. current_module.localmacrosymtable.insert(mac)
  3237. else
  3238. initialmacrosymtable.insert(mac);
  3239. end
  3240. else
  3241. begin
  3242. mac.is_compiler_var:=false;
  3243. if assigned(mac.buftext) then
  3244. freemem(mac.buftext,mac.buflen);
  3245. end;
  3246. Message2(parser_c_macro_set_to,mac.name,value);
  3247. mac.buflen:=length(value);
  3248. getmem(mac.buftext,mac.buflen);
  3249. move(value[1],mac.buftext^,mac.buflen);
  3250. mac.defined:=true;
  3251. end;
  3252. procedure set_system_compvar(const name, value : string);
  3253. var
  3254. mac : tmacro;
  3255. s: string;
  3256. begin
  3257. if name = '' then
  3258. internalerror(2004121204);
  3259. s:= upper(name);
  3260. mac:=tmacro(search_macro(s));
  3261. if not assigned(mac) then
  3262. begin
  3263. mac:=tmacro.create(s);
  3264. mac.is_compiler_var:=true;
  3265. if assigned(current_module) then
  3266. current_module.localmacrosymtable.insert(mac)
  3267. else
  3268. initialmacrosymtable.insert(mac);
  3269. end
  3270. else
  3271. begin
  3272. mac.is_compiler_var:=true;
  3273. if assigned(mac.buftext) then
  3274. freemem(mac.buftext,mac.buflen);
  3275. end;
  3276. Message2(parser_c_macro_set_to,mac.name,value);
  3277. mac.buflen:=length(value);
  3278. getmem(mac.buftext,mac.buflen);
  3279. move(value[1],mac.buftext^,mac.buflen);
  3280. mac.defined:=true;
  3281. end;
  3282. procedure undef_system_macro(const name : string);
  3283. var
  3284. mac : tmacro;
  3285. s: string;
  3286. begin
  3287. if name = '' then
  3288. internalerror(2004121205);
  3289. s:= upper(name);
  3290. mac:=tmacro(search_macro(s));
  3291. if not assigned(mac) then
  3292. {If not found, then it's already undefined.}
  3293. else
  3294. begin
  3295. Message1(parser_c_macro_undefined,mac.name);
  3296. mac.defined:=false;
  3297. mac.is_compiler_var:=false;
  3298. { delete old definition }
  3299. if assigned(mac.buftext) then
  3300. begin
  3301. freemem(mac.buftext,mac.buflen);
  3302. mac.buftext:=nil;
  3303. end;
  3304. end;
  3305. end;
  3306. {$ifdef UNITALIASES}
  3307. {****************************************************************************
  3308. TUNIT_ALIAS
  3309. ****************************************************************************}
  3310. constructor tunit_alias.create(const n:string);
  3311. var
  3312. i : longint;
  3313. begin
  3314. i:=pos('=',n);
  3315. if i=0 then
  3316. fail;
  3317. inherited createname(Copy(n,1,i-1));
  3318. newname:=stringdup(Copy(n,i+1,255));
  3319. end;
  3320. destructor tunit_alias.destroy;
  3321. begin
  3322. stringdispose(newname);
  3323. inherited destroy;
  3324. end;
  3325. procedure addunitalias(const n:string);
  3326. begin
  3327. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3328. end;
  3329. function getunitalias(const n:string):string;
  3330. var
  3331. p : punit_alias;
  3332. begin
  3333. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3334. if assigned(p) then
  3335. getunitalias:=punit_alias(p).newname^
  3336. else
  3337. getunitalias:=n;
  3338. end;
  3339. {$endif UNITALIASES}
  3340. {****************************************************************************
  3341. Init/Done Symtable
  3342. ****************************************************************************}
  3343. procedure InitSymtable;
  3344. begin
  3345. { Reset symbolstack }
  3346. symtablestack:=nil;
  3347. systemunit:=nil;
  3348. { create error syms and def }
  3349. generrorsym:=terrorsym.create;
  3350. generrordef:=cerrordef.create;
  3351. { macros }
  3352. initialmacrosymtable:=tmacrosymtable.create(false);
  3353. macrosymtablestack:=TSymtablestack.create;
  3354. macrosymtablestack.push(initialmacrosymtable);
  3355. {$ifdef UNITALIASES}
  3356. { unit aliases }
  3357. unitaliases:=TFPHashObjectList.create;
  3358. {$endif}
  3359. { set some global vars to nil, might be important for the ide }
  3360. class_tobject:=nil;
  3361. interface_iunknown:=nil;
  3362. interface_idispatch:=nil;
  3363. rec_tguid:=nil;
  3364. dupnr:=0;
  3365. end;
  3366. procedure DoneSymtable;
  3367. begin
  3368. generrorsym.owner:=nil;
  3369. generrorsym.free;
  3370. generrordef.owner:=nil;
  3371. generrordef.free;
  3372. initialmacrosymtable.free;
  3373. macrosymtablestack.free;
  3374. {$ifdef UNITALIASES}
  3375. unitaliases.free;
  3376. {$endif}
  3377. end;
  3378. end.