symtable.pas 164 KB

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