symtable.pas 158 KB

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