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