symtable.pas 166 KB

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