symtable.pas 154 KB

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