symtable.pas 142 KB

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