symtable.pas 166 KB

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