symdef.pas 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  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 symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:asizeint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function OwnerHierarchyName: string; override;
  69. function in_currentunit: boolean;
  70. { regvars }
  71. function is_intregable : boolean;
  72. function is_fpuregable : boolean;
  73. { generics }
  74. procedure initgeneric;
  75. private
  76. savesize : asizeuint;
  77. end;
  78. tfiletyp = (ft_text,ft_typed,ft_untyped);
  79. tfiledef = class(tstoreddef)
  80. filetyp : tfiletyp;
  81. typedfiledef : tdef;
  82. typedfiledefderef : tderef;
  83. constructor createtext;
  84. constructor createuntyped;
  85. constructor createtyped(def : tdef);
  86. constructor ppuload(ppufile:tcompilerppufile);
  87. function getcopy : tstoreddef;override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderef;override;
  90. procedure deref;override;
  91. function GetTypeName:string;override;
  92. function getmangledparaname:string;override;
  93. procedure setsize;
  94. end;
  95. tvariantdef = class(tstoreddef)
  96. varianttype : tvarianttype;
  97. constructor create(v : tvarianttype);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. function getcopy : tstoreddef;override;
  100. function GetTypeName:string;override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. function getvardef:longint;override;
  103. procedure setsize;
  104. function is_publishable : boolean;override;
  105. function needs_inittable : boolean;override;
  106. end;
  107. tformaldef = class(tstoreddef)
  108. typed:boolean;
  109. constructor create(Atyped:boolean);
  110. constructor ppuload(ppufile:tcompilerppufile);
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. function GetTypeName:string;override;
  113. end;
  114. tforwarddef = class(tstoreddef)
  115. tosymname : pshortstring;
  116. forwardpos : tfileposinfo;
  117. constructor create(const s:string;const pos:tfileposinfo);
  118. destructor destroy;override;
  119. function getcopy:tstoreddef;override;
  120. function GetTypeName:string;override;
  121. end;
  122. tundefineddef = class(tstoreddef)
  123. constructor create;
  124. constructor ppuload(ppufile:tcompilerppufile);
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. function GetTypeName:string;override;
  127. end;
  128. terrordef = class(tstoreddef)
  129. constructor create;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function GetTypeName:string;override;
  132. function getmangledparaname : string;override;
  133. end;
  134. tabstractpointerdef = class(tstoreddef)
  135. pointeddef : tdef;
  136. pointeddefderef : tderef;
  137. constructor create(dt:tdeftyp;def:tdef);
  138. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderef;override;
  141. procedure deref;override;
  142. end;
  143. tpointerdef = class(tabstractpointerdef)
  144. is_far : boolean;
  145. has_pointer_math : boolean;
  146. constructor create(def:tdef);
  147. constructor createfar(def:tdef);
  148. function getcopy:tstoreddef;override;
  149. constructor ppuload(ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function GetTypeName:string;override;
  152. end;
  153. tprocdef = class;
  154. { tabstractrecorddef }
  155. tabstractrecorddef= class(tstoreddef)
  156. objname,
  157. objrealname : PShortString;
  158. symtable : TSymtable;
  159. cloneddef : tabstractrecorddef;
  160. cloneddefderef : tderef;
  161. objectoptions : tobjectoptions;
  162. constructor create(const n:string; dt:tdeftyp);
  163. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. destructor destroy; override;
  166. procedure check_forwards; virtual;
  167. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  168. function GetSymtable(t:tGetSymtable):TSymtable;override;
  169. function is_packed:boolean;
  170. function RttiName: string;
  171. { enumerator support }
  172. function search_enumerator_get: tprocdef; virtual;
  173. function search_enumerator_move: tprocdef; virtual;
  174. function search_enumerator_current: tsym; virtual;
  175. end;
  176. trecorddef = class(tabstractrecorddef)
  177. public
  178. isunion : boolean;
  179. constructor create(const n:string; p:TSymtable);
  180. constructor ppuload(ppufile:tcompilerppufile);
  181. destructor destroy;override;
  182. function getcopy : tstoreddef;override;
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. procedure buildderef;override;
  185. procedure deref;override;
  186. function size:asizeint;override;
  187. function alignment : shortint;override;
  188. function padalignment: shortint;
  189. function GetTypeName:string;override;
  190. { debug }
  191. function needs_inittable : boolean;override;
  192. end;
  193. tobjectdef = class;
  194. { TImplementedInterface }
  195. TImplementedInterface = class
  196. IntfDef : tobjectdef;
  197. IntfDefDeref : tderef;
  198. IType : tinterfaceentrytype;
  199. IOffset : longint;
  200. VtblImplIntf : TImplementedInterface;
  201. NameMappings : TFPHashList;
  202. ProcDefs : TFPObjectList;
  203. ImplementsGetter : tsym;
  204. constructor create(aintf: tobjectdef);
  205. constructor create_deref(d:tderef);
  206. destructor destroy; override;
  207. function getcopy:TImplementedInterface;
  208. procedure buildderef;
  209. procedure deref;
  210. procedure AddMapping(const origname, newname: string);
  211. function GetMapping(const origname: string):string;
  212. procedure AddImplProc(pd:tprocdef);
  213. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  214. end;
  215. { tvmtentry }
  216. tvmtentry = record
  217. procdef : tprocdef;
  218. procdefderef : tderef;
  219. visibility : tvisibility;
  220. end;
  221. pvmtentry = ^tvmtentry;
  222. { tobjectdef }
  223. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  224. pmvcallstaticinfo = ^tmvcallstaticinfo;
  225. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  226. tobjectdef = class(tabstractrecorddef)
  227. private
  228. fcurrent_dispid: longint;
  229. public
  230. dwarf_struct_lab : tasmsymbol;
  231. childof : tobjectdef;
  232. childofderef : tderef;
  233. { for Object Pascal helpers }
  234. extendeddef : tabstractrecorddef;
  235. extendeddefderef: tderef;
  236. { for C++ classes: name of the library this class is imported from }
  237. import_lib,
  238. { for Objective-C: protocols and classes can have the same name there }
  239. objextname : pshortstring;
  240. { to be able to have a variable vmt position }
  241. { and no vmt field for objects without virtuals }
  242. vmtentries : TFPList;
  243. vmcallstaticinfo : pmvcallstaticinfo;
  244. vmt_offset : longint;
  245. objecttype : tobjecttyp;
  246. iidguid : pguid;
  247. iidstr : pshortstring;
  248. writing_class_record_dbginfo,
  249. { a class of this type has been created in this module }
  250. created_in_current_module,
  251. { a loadvmtnode for this class has been created in this
  252. module, so if a classrefdef variable of this or a parent
  253. class is used somewhere to instantiate a class, then this
  254. class may be instantiated
  255. }
  256. maybe_created_in_current_module,
  257. { a "class of" this particular class has been created in
  258. this module
  259. }
  260. classref_created_in_current_module : boolean;
  261. { store implemented interfaces defs and name mappings }
  262. ImplementedInterfaces : TFPObjectList;
  263. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  264. constructor ppuload(ppufile:tcompilerppufile);
  265. destructor destroy;override;
  266. function getcopy : tstoreddef;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);override;
  268. function GetTypeName:string;override;
  269. procedure buildderef;override;
  270. procedure deref;override;
  271. procedure buildderefimpl;override;
  272. procedure derefimpl;override;
  273. procedure resetvmtentries;
  274. procedure copyvmtentries(objdef:tobjectdef);
  275. function getparentdef:tdef;override;
  276. function size : asizeint;override;
  277. function alignment:shortint;override;
  278. function vmtmethodoffset(index:longint):longint;
  279. function members_need_inittable : boolean;
  280. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  281. { this should be called when this class implements an interface }
  282. procedure prepareguid;
  283. function is_publishable : boolean;override;
  284. function is_related(d : tdef) : boolean;override;
  285. function needs_inittable : boolean;override;
  286. function rtti_mangledname(rt:trttitype):string;override;
  287. function vmt_mangledname : string;
  288. procedure check_forwards; override;
  289. procedure insertvmt;
  290. procedure set_parent(c : tobjectdef);
  291. function find_destructor: tprocdef;
  292. function implements_any_interfaces: boolean;
  293. { dispinterface support }
  294. function get_next_dispid: longint;
  295. { enumerator support }
  296. function search_enumerator_get: tprocdef; override;
  297. function search_enumerator_move: tprocdef; override;
  298. function search_enumerator_current: tsym; override;
  299. { WPO }
  300. procedure register_created_object_type;override;
  301. procedure register_maybe_created_object_type;
  302. procedure register_created_classref_type;
  303. procedure register_vmt_call(index:longint);
  304. { ObjC }
  305. procedure finish_objc_data;
  306. function check_objc_types: boolean;
  307. { C++ }
  308. procedure finish_cpp_data;
  309. end;
  310. tclassrefdef = class(tabstractpointerdef)
  311. constructor create(def:tdef);
  312. constructor ppuload(ppufile:tcompilerppufile);
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;
  314. function getcopy:tstoreddef;override;
  315. function GetTypeName:string;override;
  316. function is_publishable : boolean;override;
  317. function rtti_mangledname(rt:trttitype):string;override;
  318. procedure register_created_object_type;override;
  319. end;
  320. tarraydef = class(tstoreddef)
  321. lowrange,
  322. highrange : asizeint;
  323. rangedef : tdef;
  324. rangedefderef : tderef;
  325. arrayoptions : tarraydefoptions;
  326. symtable : TSymtable;
  327. protected
  328. _elementdef : tdef;
  329. _elementdefderef : tderef;
  330. procedure setelementdef(def:tdef);
  331. public
  332. function elesize : asizeint;
  333. function elepackedbitsize : asizeint;
  334. function elecount : asizeuint;
  335. constructor create_from_pointer(def:tdef);
  336. constructor create(l,h:asizeint;def:tdef);
  337. constructor ppuload(ppufile:tcompilerppufile);
  338. destructor destroy; override;
  339. function getcopy : tstoreddef;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. function GetTypeName:string;override;
  342. function getmangledparaname : string;override;
  343. procedure buildderef;override;
  344. procedure deref;override;
  345. function size : asizeint;override;
  346. function alignment : shortint;override;
  347. { returns the label of the range check string }
  348. function needs_inittable : boolean;override;
  349. property elementdef : tdef read _elementdef write setelementdef;
  350. function is_publishable : boolean;override;
  351. end;
  352. torddef = class(tstoreddef)
  353. low,high : TConstExprInt;
  354. ordtype : tordtype;
  355. constructor create(t : tordtype;v,b : TConstExprInt);
  356. constructor ppuload(ppufile:tcompilerppufile);
  357. function getcopy : tstoreddef;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. function is_publishable : boolean;override;
  360. function GetTypeName:string;override;
  361. function alignment:shortint;override;
  362. procedure setsize;
  363. function packedbitsize: asizeint; override;
  364. function getvardef : longint;override;
  365. end;
  366. tfloatdef = class(tstoreddef)
  367. floattype : tfloattype;
  368. constructor create(t : tfloattype);
  369. constructor ppuload(ppufile:tcompilerppufile);
  370. function getcopy : tstoreddef;override;
  371. procedure ppuwrite(ppufile:tcompilerppufile);override;
  372. function GetTypeName:string;override;
  373. function is_publishable : boolean;override;
  374. function alignment:shortint;override;
  375. procedure setsize;
  376. function getvardef:longint;override;
  377. end;
  378. { tabstractprocdef }
  379. tabstractprocdef = class(tstoreddef)
  380. { saves a definition to the return type }
  381. returndef : tdef;
  382. returndefderef : tderef;
  383. parast : TSymtable;
  384. paras : tparalist;
  385. proctypeoption : tproctypeoption;
  386. proccalloption : tproccalloption;
  387. procoptions : tprocoptions;
  388. callerargareasize,
  389. calleeargareasize: pint;
  390. { number of user visibile parameters }
  391. maxparacount,
  392. minparacount : byte;
  393. {$ifdef m68k}
  394. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  395. {$endif}
  396. funcretloc : array[tcallercallee] of TCGPara;
  397. has_paraloc_info : tcallercallee; { paraloc info is available }
  398. constructor create(dt:tdeftyp;level:byte);
  399. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  400. destructor destroy;override;
  401. procedure ppuwrite(ppufile:tcompilerppufile);override;
  402. procedure buildderef;override;
  403. procedure deref;override;
  404. procedure calcparas;
  405. function typename_paras(showhidden:boolean): string;
  406. function is_methodpointer:boolean;virtual;
  407. function is_addressonly:boolean;virtual;
  408. function no_self_node:boolean;
  409. procedure check_mark_as_nested;
  410. procedure init_paraloc_info(side: tcallercallee);
  411. function stack_tainting_parameter(side: tcallercallee): boolean;
  412. private
  413. procedure count_para(p:TObject;arg:pointer);
  414. procedure insert_para(p:TObject;arg:pointer);
  415. end;
  416. tprocvardef = class(tabstractprocdef)
  417. constructor create(level:byte);
  418. constructor ppuload(ppufile:tcompilerppufile);
  419. function getcopy : tstoreddef;override;
  420. procedure ppuwrite(ppufile:tcompilerppufile);override;
  421. function GetSymtable(t:tGetSymtable):TSymtable;override;
  422. function size : asizeint;override;
  423. function GetTypeName:string;override;
  424. function is_publishable : boolean;override;
  425. function is_methodpointer:boolean;override;
  426. function is_addressonly:boolean;override;
  427. function getmangledparaname:string;override;
  428. end;
  429. tmessageinf = record
  430. case integer of
  431. 0 : (str : pshortstring);
  432. 1 : (i : longint);
  433. end;
  434. tinlininginfo = record
  435. { node tree }
  436. code : tnode;
  437. flags : tprocinfoflags;
  438. end;
  439. pinlininginfo = ^tinlininginfo;
  440. {$ifdef oldregvars}
  441. { register variables }
  442. pregvarinfo = ^tregvarinfo;
  443. tregvarinfo = record
  444. regvars : array[1..maxvarregs] of tsym;
  445. regvars_para : array[1..maxvarregs] of boolean;
  446. regvars_refs : array[1..maxvarregs] of longint;
  447. fpuregvars : array[1..maxfpuvarregs] of tsym;
  448. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  449. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  450. end;
  451. {$endif oldregvars}
  452. { tprocdef }
  453. tprocdef = class(tabstractprocdef)
  454. private
  455. _mangledname : pshortstring;
  456. public
  457. messageinf : tmessageinf;
  458. dispid : longint;
  459. {$ifndef EXTDEBUG}
  460. { where is this function defined and what were the symbol
  461. flags, needed here because there
  462. is only one symbol for all overloaded functions
  463. EXTDEBUG has fileinfo in tdef (PFV) }
  464. fileinfo : tfileposinfo;
  465. {$endif}
  466. symoptions : tsymoptions;
  467. deprecatedmsg : pshortstring;
  468. { symbol owning this definition }
  469. procsym : tsym;
  470. procsymderef : tderef;
  471. { alias names }
  472. aliasnames : TCmdStrList;
  473. { symtables }
  474. localst : TSymtable;
  475. funcretsym : tsym;
  476. funcretsymderef : tderef;
  477. struct : tabstractrecorddef;
  478. structderef : tderef;
  479. {$if defined(powerpc) or defined(m68k)}
  480. { library symbol for AmigaOS/MorphOS }
  481. libsym : tsym;
  482. libsymderef : tderef;
  483. {$endif powerpc or m68k}
  484. { name of the result variable to insert in the localsymtable }
  485. resultname : pshortstring;
  486. { import info }
  487. import_dll,
  488. import_name : pshortstring;
  489. { info for inlining the subroutine, if this pointer is nil,
  490. the procedure can't be inlined }
  491. inlininginfo : pinlininginfo;
  492. {$ifdef oldregvars}
  493. regvarinfo: pregvarinfo;
  494. {$endif oldregvars}
  495. { First/last assembler symbol/instruction in aasmoutput list.
  496. Note: initialised after compiling the code for the procdef, but
  497. not saved to/restored from ppu. Used when inserting debug info }
  498. procstarttai,
  499. procendtai : tai;
  500. import_nr : word;
  501. extnumber : word;
  502. {$ifdef i386}
  503. fpu_used : byte;
  504. {$endif i386}
  505. visibility : tvisibility;
  506. { true, if the procedure is only declared
  507. (forward procedure) }
  508. forwarddef,
  509. { true if the procedure is declared in the interface }
  510. interfacedef : boolean;
  511. { true if the procedure has a forward declaration }
  512. hasforward : boolean;
  513. constructor create(level:byte);
  514. constructor ppuload(ppufile:tcompilerppufile);
  515. destructor destroy;override;
  516. procedure ppuwrite(ppufile:tcompilerppufile);override;
  517. procedure buildderef;override;
  518. procedure buildderefimpl;override;
  519. procedure deref;override;
  520. procedure derefimpl;override;
  521. function GetSymtable(t:tGetSymtable):TSymtable;override;
  522. function GetTypeName : string;override;
  523. function mangledname : string;
  524. procedure setmangledname(const s : string);
  525. function fullprocname(showhidden:boolean):string;
  526. function cplusplusmangledname : string;
  527. function objcmangledname : string;
  528. function is_methodpointer:boolean;override;
  529. function is_addressonly:boolean;override;
  530. procedure make_external;
  531. end;
  532. { single linked list of overloaded procs }
  533. pprocdeflist = ^tprocdeflist;
  534. tprocdeflist = record
  535. def : tprocdef;
  536. defderef : tderef;
  537. next : pprocdeflist;
  538. end;
  539. tstringdef = class(tstoreddef)
  540. stringtype : tstringtype;
  541. len : asizeint;
  542. constructor createshort(l : byte);
  543. constructor loadshort(ppufile:tcompilerppufile);
  544. constructor createlong(l : asizeint);
  545. constructor loadlong(ppufile:tcompilerppufile);
  546. constructor createansi;
  547. constructor loadansi(ppufile:tcompilerppufile);
  548. constructor createwide;
  549. constructor loadwide(ppufile:tcompilerppufile);
  550. constructor createunicode;
  551. constructor loadunicode(ppufile:tcompilerppufile);
  552. function getcopy : tstoreddef;override;
  553. function stringtypname:string;
  554. procedure ppuwrite(ppufile:tcompilerppufile);override;
  555. function GetTypeName:string;override;
  556. function getmangledparaname:string;override;
  557. function is_publishable : boolean;override;
  558. function alignment : shortint;override;
  559. function needs_inittable : boolean;override;
  560. function getvardef:longint;override;
  561. end;
  562. { tenumdef }
  563. tenumdef = class(tstoreddef)
  564. minval,
  565. maxval : asizeint;
  566. has_jumps : boolean;
  567. basedef : tenumdef;
  568. basedefderef : tderef;
  569. symtable : TSymtable;
  570. constructor create;
  571. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  572. constructor ppuload(ppufile:tcompilerppufile);
  573. destructor destroy;override;
  574. function getcopy : tstoreddef;override;
  575. procedure ppuwrite(ppufile:tcompilerppufile);override;
  576. procedure buildderef;override;
  577. procedure deref;override;
  578. function GetTypeName:string;override;
  579. function is_publishable : boolean;override;
  580. procedure calcsavesize;
  581. function packedbitsize: asizeint; override;
  582. procedure setmax(_max:asizeint);
  583. procedure setmin(_min:asizeint);
  584. function min:asizeint;
  585. function max:asizeint;
  586. function getfirstsym:tsym;
  587. end;
  588. tsetdef = class(tstoreddef)
  589. elementdef : tdef;
  590. elementdefderef : tderef;
  591. setbase,
  592. setmax : aword;
  593. constructor create(def:tdef;low, high : asizeint);
  594. constructor ppuload(ppufile:tcompilerppufile);
  595. function getcopy : tstoreddef;override;
  596. procedure ppuwrite(ppufile:tcompilerppufile);override;
  597. procedure buildderef;override;
  598. procedure deref;override;
  599. function GetTypeName:string;override;
  600. function is_publishable : boolean;override;
  601. end;
  602. tdefawaresymtablestack = class(TSymtablestack)
  603. private
  604. procedure addhelpers(st: TSymtable);
  605. procedure removehelpers(st: TSymtable);
  606. public
  607. procedure push(st: TSymtable); override;
  608. procedure pop(st: TSymtable); override;
  609. end;
  610. var
  611. current_structdef: tabstractrecorddef; { used for private functions check !! }
  612. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  613. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  614. { default types }
  615. generrordef, { error in definition }
  616. voidpointertype, { pointer for Void-pointeddef }
  617. charpointertype, { pointer for Char-pointeddef }
  618. widecharpointertype, { pointer for WideChar-pointeddef }
  619. voidfarpointertype,
  620. cundefinedtype,
  621. cformaltype, { unique formal definition }
  622. ctypedformaltype, { unique typed formal definition }
  623. voidtype, { Void (procedure) }
  624. cchartype, { Char }
  625. cwidechartype, { WideChar }
  626. booltype, { boolean type }
  627. bool8type,
  628. bool16type,
  629. bool32type,
  630. bool64type, { implement me }
  631. u8inttype, { 8-Bit unsigned integer }
  632. s8inttype, { 8-Bit signed integer }
  633. u16inttype, { 16-Bit unsigned integer }
  634. s16inttype, { 16-Bit signed integer }
  635. u32inttype, { 32-Bit unsigned integer }
  636. s32inttype, { 32-Bit signed integer }
  637. u64inttype, { 64-bit unsigned integer }
  638. s64inttype, { 64-bit signed integer }
  639. s32floattype, { 32 bit floating point number }
  640. s64floattype, { 64 bit floating point number }
  641. s80floattype, { 80 bit floating point number }
  642. sc80floattype, { 80 bit floating point number but stored like in C }
  643. s64currencytype, { pointer to a currency type }
  644. cshortstringtype, { pointer to type of short string const }
  645. clongstringtype, { pointer to type of long string const }
  646. cansistringtype, { pointer to type of ansi string const }
  647. cwidestringtype, { pointer to type of wide string const }
  648. cunicodestringtype,
  649. openshortstringtype, { pointer to type of an open shortstring,
  650. needed for readln() }
  651. openchararraytype, { pointer to type of an open array of char,
  652. needed for readln() }
  653. cfiletype, { get the same definition for all file }
  654. { used for stabs }
  655. methodpointertype, { typecasting of methodpointers to extract self }
  656. hresultdef,
  657. { we use only one variant def for every variant class }
  658. cvarianttype,
  659. colevarianttype,
  660. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  661. sinttype,
  662. uinttype,
  663. { unsigned and signed ord type with the same size as a pointer }
  664. ptruinttype,
  665. ptrsinttype,
  666. { several types to simulate more or less C++ objects for GDB }
  667. vmttype,
  668. vmtarraytype,
  669. pvmttype : tdef; { type of classrefs, used for stabs }
  670. { pointer to the anchestor of all classes }
  671. class_tobject : tobjectdef;
  672. { pointer to the ancestor of all COM interfaces }
  673. interface_iunknown : tobjectdef;
  674. { pointer to the ancestor of all dispinterfaces }
  675. interface_idispatch : tobjectdef;
  676. { pointer to the TGUID type
  677. of all interfaces }
  678. rec_tguid : trecorddef;
  679. { pointer to jump buffer }
  680. rec_jmp_buf : trecorddef;
  681. { Objective-C base types }
  682. objc_metaclasstype,
  683. objc_superclasstype,
  684. objc_idtype,
  685. objc_seltype : tpointerdef;
  686. objc_objecttype : trecorddef;
  687. { base type of @protocol(protocolname) Objective-C statements }
  688. objc_protocoltype : tobjectdef;
  689. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  690. objc_fastenumeration : tobjectdef;
  691. objc_fastenumerationstate : trecorddef;
  692. const
  693. {$ifdef i386}
  694. pbestrealtype : ^tdef = @s80floattype;
  695. {$endif}
  696. {$ifdef x86_64}
  697. pbestrealtype : ^tdef = @s80floattype;
  698. {$endif}
  699. {$ifdef m68k}
  700. pbestrealtype : ^tdef = @s64floattype;
  701. {$endif}
  702. {$ifdef alpha}
  703. pbestrealtype : ^tdef = @s64floattype;
  704. {$endif}
  705. {$ifdef powerpc}
  706. pbestrealtype : ^tdef = @s64floattype;
  707. {$endif}
  708. {$ifdef POWERPC64}
  709. pbestrealtype : ^tdef = @s64floattype;
  710. {$endif}
  711. {$ifdef ia64}
  712. pbestrealtype : ^tdef = @s64floattype;
  713. {$endif}
  714. {$ifdef SPARC}
  715. pbestrealtype : ^tdef = @s64floattype;
  716. {$endif SPARC}
  717. {$ifdef vis}
  718. pbestrealtype : ^tdef = @s64floattype;
  719. {$endif vis}
  720. {$ifdef ARM}
  721. pbestrealtype : ^tdef = @s64floattype;
  722. {$endif ARM}
  723. {$ifdef MIPS}
  724. pbestrealtype : ^tdef = @s64floattype;
  725. {$endif MIPS}
  726. {$ifdef AVR}
  727. pbestrealtype : ^tdef = @s64floattype;
  728. {$endif AVR}
  729. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  730. { should be in the types unit, but the types unit uses the node stuff :( }
  731. function is_interfacecom(def: tdef): boolean;
  732. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  733. function is_interfacecorba(def: tdef): boolean;
  734. function is_interface(def: tdef): boolean;
  735. function is_dispinterface(def: tdef): boolean;
  736. function is_object(def: tdef): boolean;
  737. function is_class(def: tdef): boolean;
  738. function is_cppclass(def: tdef): boolean;
  739. function is_objectpascal_helper(def: tdef): boolean;
  740. function is_objcclass(def: tdef): boolean;
  741. function is_objcclassref(def: tdef): boolean;
  742. function is_objcprotocol(def: tdef): boolean;
  743. function is_objccategory(def: tdef): boolean;
  744. function is_objc_class_or_protocol(def: tdef): boolean;
  745. function is_objc_protocol_or_category(def: tdef): boolean;
  746. function is_classhelper(def: tdef): boolean;
  747. function is_class_or_interface(def: tdef): boolean;
  748. function is_class_or_interface_or_objc(def: tdef): boolean;
  749. function is_class_or_interface_or_object(def: tdef): boolean;
  750. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  751. function is_implicit_pointer_object_type(def: tdef): boolean;
  752. function is_class_or_object(def: tdef): boolean;
  753. function is_record(def: tdef): boolean;
  754. procedure loadobjctypes;
  755. procedure maybeloadcocoatypes;
  756. function use_vectorfpu(def : tdef) : boolean;
  757. implementation
  758. uses
  759. SysUtils,
  760. cutils,
  761. { global }
  762. verbose,
  763. { target }
  764. systems,aasmcpu,paramgr,
  765. { symtable }
  766. symsym,symtable,symutil,defutil,objcdef,
  767. { module }
  768. fmodule,
  769. { other }
  770. gendef,
  771. fpccrc
  772. ;
  773. {****************************************************************************
  774. Helpers
  775. ****************************************************************************}
  776. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  777. var
  778. s,hs,
  779. prefix : string;
  780. oldlen,
  781. newlen,
  782. i : longint;
  783. crc : dword;
  784. hp : tparavarsym;
  785. begin
  786. prefix:='';
  787. if not assigned(st) then
  788. internalerror(200204212);
  789. { sub procedures }
  790. while (st.symtabletype=localsymtable) do
  791. begin
  792. if st.defowner.typ<>procdef then
  793. internalerror(200204173);
  794. { Add the full mangledname of procedure to prevent
  795. conflicts with 2 overloads having both a nested procedure
  796. with the same name, see tb0314 (PFV) }
  797. s:=tprocdef(st.defowner).procsym.name;
  798. oldlen:=length(s);
  799. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  800. begin
  801. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  802. if not(vo_is_hidden_para in hp.varoptions) then
  803. s:=s+'$'+hp.vardef.mangledparaname;
  804. end;
  805. if not is_void(tprocdef(st.defowner).returndef) then
  806. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  807. newlen:=length(s);
  808. { Replace with CRC if the parameter line is very long }
  809. if (newlen-oldlen>12) and
  810. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  811. begin
  812. crc:=0;
  813. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  814. begin
  815. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  816. if not(vo_is_hidden_para in hp.varoptions) then
  817. begin
  818. hs:=hp.vardef.mangledparaname;
  819. crc:=UpdateCrc32(crc,hs[1],length(hs));
  820. end;
  821. end;
  822. hs:=hp.vardef.mangledparaname;
  823. crc:=UpdateCrc32(crc,hs[1],length(hs));
  824. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  825. end;
  826. if prefix<>'' then
  827. prefix:=s+'_'+prefix
  828. else
  829. prefix:=s;
  830. if length(prefix)>100 then
  831. begin
  832. crc:=0;
  833. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  834. prefix:='$CRC'+hexstr(crc,8);
  835. end;
  836. st:=st.defowner.owner;
  837. end;
  838. { object/classes symtable, nested type definitions in classes require the while loop }
  839. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  840. begin
  841. if not (st.defowner.typ in [objectdef,recorddef]) then
  842. internalerror(200204174);
  843. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  844. st:=st.defowner.owner;
  845. end;
  846. { symtable must now be static or global }
  847. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  848. internalerror(200204175);
  849. result:='';
  850. if typeprefix<>'' then
  851. result:=result+typeprefix+'_';
  852. { Add P$ for program, which can have the same name as
  853. a unit }
  854. if (TSymtable(main_module.localsymtable)=st) and
  855. (not main_module.is_unit) then
  856. result:=result+'P$'+st.name^
  857. else
  858. result:=result+st.name^;
  859. if prefix<>'' then
  860. result:=result+'_'+prefix;
  861. if suffix<>'' then
  862. result:=result+'_'+suffix;
  863. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  864. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  865. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  866. { those in the debug map, leading to troubles with dsymutil). So always }
  867. { add an underscore on darwin. }
  868. if (target_info.system in systems_darwin) then
  869. result := '_' + result;
  870. end;
  871. {****************************************************************************
  872. TDEFAWARESYMTABLESTACK
  873. (symtablestack descendant that does some special actions on
  874. the pushed/popped symtables)
  875. ****************************************************************************}
  876. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  877. var
  878. i: integer;
  879. s: string;
  880. list: TFPObjectList;
  881. def: tdef;
  882. begin
  883. { search the symtable from first to last; the helper to use will be the
  884. last one in the list }
  885. for i:=0 to st.symlist.count-1 do
  886. begin
  887. if not (st.symlist[i] is ttypesym) then
  888. continue;
  889. def:=ttypesym(st.SymList[i]).typedef;
  890. if is_objectpascal_helper(def) then
  891. begin
  892. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  893. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  894. if not assigned(list) then
  895. begin
  896. list:=TFPObjectList.Create(false);
  897. current_module.extendeddefs.Add(s,list);
  898. end;
  899. list.Add(def);
  900. end
  901. else
  902. { add nested helpers as well }
  903. if def.typ in [recorddef,objectdef] then
  904. addhelpers(tabstractrecorddef(def).symtable);
  905. end;
  906. end;
  907. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  908. var
  909. i, j: integer;
  910. tmpst: TSymtable;
  911. list: TFPObjectList;
  912. begin
  913. for i:=current_module.extendeddefs.count-1 downto 0 do
  914. begin
  915. list:=TFPObjectList(current_module.extendeddefs[i]);
  916. for j:=list.count-1 downto 0 do
  917. begin
  918. if not (list[j] is tobjectdef) then
  919. Internalerror(2011031501);
  920. tmpst:=tobjectdef(list[j]).owner;
  921. repeat
  922. if tmpst=st then
  923. begin
  924. list.delete(j);
  925. break;
  926. end
  927. else
  928. begin
  929. if assigned(tmpst.defowner) then
  930. tmpst:=tmpst.defowner.owner
  931. else
  932. tmpst:=nil;
  933. end;
  934. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  935. end;
  936. if list.count=0 then
  937. current_module.extendeddefs.delete(i);
  938. end;
  939. end;
  940. procedure tdefawaresymtablestack.push(st: TSymtable);
  941. begin
  942. { nested helpers will be added as well }
  943. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  944. (sto_has_helper in st.tableoptions) then
  945. addhelpers(st);
  946. inherited push(st);
  947. end;
  948. procedure tdefawaresymtablestack.pop(st: TSymtable);
  949. begin
  950. inherited pop(st);
  951. { nested helpers will be removed as well }
  952. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  953. (sto_has_helper in st.tableoptions) then
  954. removehelpers(st);
  955. end;
  956. {****************************************************************************
  957. TDEF (base class for definitions)
  958. ****************************************************************************}
  959. constructor tstoreddef.create(dt:tdeftyp);
  960. var
  961. insertstack : psymtablestackitem;
  962. begin
  963. inherited create(dt);
  964. savesize := 0;
  965. {$ifdef EXTDEBUG}
  966. fileinfo := current_filepos;
  967. {$endif}
  968. generictokenbuf:=nil;
  969. genericdef:=nil;
  970. { Don't register forwarddefs, they are disposed at the
  971. end of an type block }
  972. if (dt=forwarddef) then
  973. exit;
  974. { Register in current_module }
  975. if assigned(current_module) then
  976. begin
  977. current_module.deflist.Add(self);
  978. DefId:=current_module.deflist.Count-1;
  979. end;
  980. { Register in symtable stack }
  981. if assigned(symtablestack) then
  982. begin
  983. insertstack:=symtablestack.stack;
  984. while assigned(insertstack) and
  985. (insertstack^.symtable.symtabletype=withsymtable) do
  986. insertstack:=insertstack^.next;
  987. if not assigned(insertstack) then
  988. internalerror(200602044);
  989. insertstack^.symtable.insertdef(self);
  990. end;
  991. end;
  992. destructor tstoreddef.destroy;
  993. begin
  994. { Direct calls are not allowed, use symtable.deletedef() }
  995. if assigned(owner) then
  996. internalerror(200612311);
  997. if assigned(generictokenbuf) then
  998. begin
  999. generictokenbuf.free;
  1000. generictokenbuf:=nil;
  1001. end;
  1002. inherited destroy;
  1003. end;
  1004. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1005. var
  1006. sizeleft,i : longint;
  1007. buf : array[0..255] of byte;
  1008. begin
  1009. inherited create(dt);
  1010. DefId:=ppufile.getlongint;
  1011. current_module.deflist[DefId]:=self;
  1012. {$ifdef EXTDEBUG}
  1013. fillchar(fileinfo,sizeof(fileinfo),0);
  1014. {$endif}
  1015. { load }
  1016. ppufile.getderef(typesymderef);
  1017. ppufile.getsmallset(defoptions);
  1018. ppufile.getsmallset(defstates);
  1019. if df_generic in defoptions then
  1020. begin
  1021. sizeleft:=ppufile.getlongint;
  1022. initgeneric;
  1023. while sizeleft>0 do
  1024. begin
  1025. if sizeleft>sizeof(buf) then
  1026. i:=sizeof(buf)
  1027. else
  1028. i:=sizeleft;
  1029. ppufile.getdata(buf,i);
  1030. generictokenbuf.write(buf,i);
  1031. dec(sizeleft,i);
  1032. end;
  1033. end;
  1034. if df_specialization in defoptions then
  1035. ppufile.getderef(genericdefderef);
  1036. end;
  1037. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1038. var
  1039. prefix : string[4];
  1040. begin
  1041. if rt=fullrtti then
  1042. begin
  1043. prefix:='RTTI';
  1044. include(defstates,ds_rtti_table_used);
  1045. end
  1046. else
  1047. begin
  1048. prefix:='INIT';
  1049. include(defstates,ds_init_table_used);
  1050. end;
  1051. if assigned(typesym) and
  1052. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1053. result:=make_mangledname(prefix,owner,typesym.name)
  1054. else
  1055. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1056. end;
  1057. function tstoreddef.OwnerHierarchyName: string;
  1058. var
  1059. tmp: tdef;
  1060. begin
  1061. tmp:=self;
  1062. result:='';
  1063. repeat
  1064. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1065. tmp:=tdef(tmp.owner.defowner)
  1066. else
  1067. break;
  1068. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1069. until tmp=nil;
  1070. end;
  1071. function tstoreddef.in_currentunit: boolean;
  1072. var
  1073. st: tsymtable;
  1074. begin
  1075. st:=owner;
  1076. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1077. st:=st.defowner.owner;
  1078. result:=st.iscurrentunit;
  1079. end;
  1080. function tstoreddef.getcopy : tstoreddef;
  1081. begin
  1082. Message(sym_e_cant_create_unique_type);
  1083. getcopy:=terrordef.create;
  1084. end;
  1085. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1086. var
  1087. sizeleft,i : longint;
  1088. buf : array[0..255] of byte;
  1089. oldintfcrc : boolean;
  1090. begin
  1091. ppufile.putlongint(DefId);
  1092. ppufile.putderef(typesymderef);
  1093. ppufile.putsmallset(defoptions);
  1094. oldintfcrc:=ppufile.do_crc;
  1095. ppufile.do_crc:=false;
  1096. ppufile.putsmallset(defstates);
  1097. if df_generic in defoptions then
  1098. begin
  1099. if assigned(generictokenbuf) then
  1100. begin
  1101. sizeleft:=generictokenbuf.size;
  1102. generictokenbuf.seek(0);
  1103. end
  1104. else
  1105. sizeleft:=0;
  1106. ppufile.putlongint(sizeleft);
  1107. while sizeleft>0 do
  1108. begin
  1109. if sizeleft>sizeof(buf) then
  1110. i:=sizeof(buf)
  1111. else
  1112. i:=sizeleft;
  1113. generictokenbuf.read(buf,i);
  1114. ppufile.putdata(buf,i);
  1115. dec(sizeleft,i);
  1116. end;
  1117. end;
  1118. ppufile.do_crc:=oldintfcrc;
  1119. if df_specialization in defoptions then
  1120. ppufile.putderef(genericdefderef);
  1121. end;
  1122. procedure tstoreddef.buildderef;
  1123. begin
  1124. typesymderef.build(typesym);
  1125. genericdefderef.build(genericdef);
  1126. end;
  1127. procedure tstoreddef.buildderefimpl;
  1128. begin
  1129. end;
  1130. procedure tstoreddef.deref;
  1131. begin
  1132. typesym:=ttypesym(typesymderef.resolve);
  1133. if df_specialization in defoptions then
  1134. genericdef:=tstoreddef(genericdefderef.resolve);
  1135. end;
  1136. procedure tstoreddef.derefimpl;
  1137. begin
  1138. end;
  1139. function tstoreddef.size : asizeint;
  1140. begin
  1141. size:=savesize;
  1142. end;
  1143. function tstoreddef.getvardef:longint;
  1144. begin
  1145. result:=varUndefined;
  1146. end;
  1147. function tstoreddef.alignment : shortint;
  1148. begin
  1149. { natural alignment by default }
  1150. alignment:=size_2_align(savesize);
  1151. { can happen if savesize = 0, e.g. for voiddef or
  1152. an empty record
  1153. }
  1154. if (alignment=0) then
  1155. alignment:=1;
  1156. end;
  1157. { returns true, if the definition can be published }
  1158. function tstoreddef.is_publishable : boolean;
  1159. begin
  1160. is_publishable:=false;
  1161. end;
  1162. { needs an init table }
  1163. function tstoreddef.needs_inittable : boolean;
  1164. begin
  1165. needs_inittable:=false;
  1166. end;
  1167. function tstoreddef.is_intregable : boolean;
  1168. var
  1169. recsize,temp: longint;
  1170. begin
  1171. is_intregable:=false;
  1172. case typ of
  1173. orddef,
  1174. pointerdef,
  1175. enumdef,
  1176. classrefdef:
  1177. is_intregable:=true;
  1178. procvardef :
  1179. is_intregable:=tprocvardef(self).is_addressonly;
  1180. objectdef:
  1181. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1182. setdef:
  1183. is_intregable:=is_smallset(self);
  1184. recorddef:
  1185. begin
  1186. recsize:=size;
  1187. is_intregable:=
  1188. ispowerof2(recsize,temp) and
  1189. (recsize <= sizeof(asizeint));
  1190. end;
  1191. end;
  1192. end;
  1193. function tstoreddef.is_fpuregable : boolean;
  1194. begin
  1195. {$ifdef x86}
  1196. result:=use_vectorfpu(self);
  1197. {$else x86}
  1198. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1199. {$endif x86}
  1200. end;
  1201. procedure tstoreddef.initgeneric;
  1202. begin
  1203. if assigned(generictokenbuf) then
  1204. internalerror(200512131);
  1205. generictokenbuf:=tdynamicarray.create(256);
  1206. end;
  1207. {****************************************************************************
  1208. Tstringdef
  1209. ****************************************************************************}
  1210. constructor tstringdef.createshort(l : byte);
  1211. begin
  1212. inherited create(stringdef);
  1213. stringtype:=st_shortstring;
  1214. len:=l;
  1215. savesize:=len+1;
  1216. end;
  1217. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1218. begin
  1219. inherited ppuload(stringdef,ppufile);
  1220. stringtype:=st_shortstring;
  1221. len:=ppufile.getbyte;
  1222. savesize:=len+1;
  1223. end;
  1224. constructor tstringdef.createlong(l : asizeint);
  1225. begin
  1226. inherited create(stringdef);
  1227. stringtype:=st_longstring;
  1228. len:=l;
  1229. savesize:=sizeof(pint);
  1230. end;
  1231. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1232. begin
  1233. inherited ppuload(stringdef,ppufile);
  1234. stringtype:=st_longstring;
  1235. len:=ppufile.getasizeint;
  1236. savesize:=sizeof(pint);
  1237. end;
  1238. constructor tstringdef.createansi;
  1239. begin
  1240. inherited create(stringdef);
  1241. stringtype:=st_ansistring;
  1242. len:=-1;
  1243. savesize:=sizeof(pint);
  1244. end;
  1245. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1246. begin
  1247. inherited ppuload(stringdef,ppufile);
  1248. stringtype:=st_ansistring;
  1249. len:=ppufile.getaint;
  1250. savesize:=sizeof(pint);
  1251. end;
  1252. constructor tstringdef.createwide;
  1253. begin
  1254. inherited create(stringdef);
  1255. stringtype:=st_widestring;
  1256. len:=-1;
  1257. savesize:=sizeof(pint);
  1258. end;
  1259. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1260. begin
  1261. inherited ppuload(stringdef,ppufile);
  1262. stringtype:=st_widestring;
  1263. len:=ppufile.getaint;
  1264. savesize:=sizeof(pint);
  1265. end;
  1266. constructor tstringdef.createunicode;
  1267. begin
  1268. inherited create(stringdef);
  1269. stringtype:=st_unicodestring;
  1270. len:=-1;
  1271. savesize:=sizeof(pint);
  1272. end;
  1273. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1274. begin
  1275. inherited ppuload(stringdef,ppufile);
  1276. stringtype:=st_unicodestring;
  1277. len:=ppufile.getaint;
  1278. savesize:=sizeof(pint);
  1279. end;
  1280. function tstringdef.getcopy : tstoreddef;
  1281. begin
  1282. result:=tstringdef.create(typ);
  1283. result.typ:=stringdef;
  1284. tstringdef(result).stringtype:=stringtype;
  1285. tstringdef(result).len:=len;
  1286. tstringdef(result).savesize:=savesize;
  1287. end;
  1288. function tstringdef.stringtypname:string;
  1289. const
  1290. typname:array[tstringtype] of string[10]=(
  1291. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1292. );
  1293. begin
  1294. stringtypname:=typname[stringtype];
  1295. end;
  1296. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1297. begin
  1298. inherited ppuwrite(ppufile);
  1299. if stringtype=st_shortstring then
  1300. begin
  1301. {$ifdef extdebug}
  1302. if len > 255 then internalerror(12122002);
  1303. {$endif}
  1304. ppufile.putbyte(byte(len))
  1305. end
  1306. else
  1307. ppufile.putaint(len);
  1308. case stringtype of
  1309. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1310. st_longstring : ppufile.writeentry(iblongstringdef);
  1311. st_ansistring : ppufile.writeentry(ibansistringdef);
  1312. st_widestring : ppufile.writeentry(ibwidestringdef);
  1313. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1314. end;
  1315. end;
  1316. function tstringdef.needs_inittable : boolean;
  1317. begin
  1318. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1319. end;
  1320. function tstringdef.GetTypeName : string;
  1321. const
  1322. names : array[tstringtype] of string[15] = (
  1323. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1324. begin
  1325. GetTypeName:=names[stringtype];
  1326. end;
  1327. function tstringdef.getvardef : longint;
  1328. const
  1329. vardef : array[tstringtype] of longint = (
  1330. varUndefined,varUndefined,varString,varOleStr,varUString);
  1331. begin
  1332. result:=vardef[stringtype];
  1333. end;
  1334. function tstringdef.alignment : shortint;
  1335. begin
  1336. case stringtype of
  1337. st_unicodestring,
  1338. st_widestring,
  1339. st_ansistring:
  1340. alignment:=size_2_align(savesize);
  1341. st_longstring,
  1342. st_shortstring:
  1343. { char to string accesses byte 0 and 1 with one word access }
  1344. if (tf_requires_proper_alignment in target_info.flags) or
  1345. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1346. (m_mac in current_settings.modeswitches) then
  1347. alignment:=size_2_align(2)
  1348. else
  1349. alignment:=size_2_align(1);
  1350. else
  1351. internalerror(200412301);
  1352. end;
  1353. end;
  1354. function tstringdef.getmangledparaname : string;
  1355. begin
  1356. getmangledparaname:='STRING';
  1357. end;
  1358. function tstringdef.is_publishable : boolean;
  1359. begin
  1360. is_publishable:=true;
  1361. end;
  1362. {****************************************************************************
  1363. TENUMDEF
  1364. ****************************************************************************}
  1365. constructor tenumdef.create;
  1366. begin
  1367. inherited create(enumdef);
  1368. minval:=0;
  1369. maxval:=0;
  1370. calcsavesize;
  1371. has_jumps:=false;
  1372. basedef:=nil;
  1373. symtable:=tenumsymtable.create(self);
  1374. end;
  1375. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1376. begin
  1377. inherited create(enumdef);
  1378. minval:=_min;
  1379. maxval:=_max;
  1380. basedef:=_basedef;
  1381. calcsavesize;
  1382. has_jumps:=false;
  1383. symtable:=basedef.symtable.getcopy;
  1384. include(defoptions, df_copied_def);
  1385. end;
  1386. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1387. begin
  1388. inherited ppuload(enumdef,ppufile);
  1389. minval:=ppufile.getaint;
  1390. maxval:=ppufile.getaint;
  1391. savesize:=ppufile.getaint;
  1392. has_jumps:=false;
  1393. if df_copied_def in defoptions then
  1394. begin
  1395. symtable:=nil;
  1396. ppufile.getderef(basedefderef);
  1397. end
  1398. else
  1399. begin
  1400. // create with nil defowner first to prevent values changes on insert
  1401. symtable:=tenumsymtable.create(nil);
  1402. tenumsymtable(symtable).ppuload(ppufile);
  1403. symtable.defowner:=self;
  1404. end;
  1405. end;
  1406. destructor tenumdef.destroy;
  1407. begin
  1408. symtable.free;
  1409. symtable:=nil;
  1410. inherited destroy;
  1411. end;
  1412. function tenumdef.getcopy : tstoreddef;
  1413. begin
  1414. if assigned(basedef) then
  1415. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1416. else
  1417. begin
  1418. result:=tenumdef.create;
  1419. tenumdef(result).minval:=minval;
  1420. tenumdef(result).maxval:=maxval;
  1421. tenumdef(result).symtable.free;
  1422. tenumdef(result).symtable:=symtable.getcopy;
  1423. tenumdef(result).basedef:=self;
  1424. end;
  1425. tenumdef(result).has_jumps:=has_jumps;
  1426. tenumdef(result).basedefderef:=basedefderef;
  1427. include(tenumdef(result).defoptions,df_copied_def);
  1428. end;
  1429. procedure tenumdef.calcsavesize;
  1430. begin
  1431. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1432. savesize:=8
  1433. else
  1434. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1435. savesize:=4
  1436. else
  1437. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1438. savesize:=2
  1439. else
  1440. savesize:=1;
  1441. end;
  1442. function tenumdef.packedbitsize: asizeint;
  1443. var
  1444. sizeval: tconstexprint;
  1445. power: longint;
  1446. begin
  1447. result := 0;
  1448. if (minval >= 0) and
  1449. (maxval <= 1) then
  1450. result := 1
  1451. else
  1452. begin
  1453. if (minval>=0) then
  1454. sizeval:=maxval
  1455. else
  1456. { don't count 0 twice }
  1457. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1458. { 256 must become 512 etc. }
  1459. nextpowerof2(sizeval+1,power);
  1460. result := power;
  1461. end;
  1462. end;
  1463. procedure tenumdef.setmax(_max:asizeint);
  1464. begin
  1465. maxval:=_max;
  1466. calcsavesize;
  1467. end;
  1468. procedure tenumdef.setmin(_min:asizeint);
  1469. begin
  1470. minval:=_min;
  1471. calcsavesize;
  1472. end;
  1473. function tenumdef.min:asizeint;
  1474. begin
  1475. min:=minval;
  1476. end;
  1477. function tenumdef.max:asizeint;
  1478. begin
  1479. max:=maxval;
  1480. end;
  1481. function tenumdef.getfirstsym: tsym;
  1482. var
  1483. i:integer;
  1484. begin
  1485. for i := 0 to symtable.SymList.Count - 1 do
  1486. begin
  1487. result:=tsym(symtable.SymList[i]);
  1488. if tenumsym(result).value=minval then
  1489. exit;
  1490. end;
  1491. result:=nil;
  1492. end;
  1493. procedure tenumdef.buildderef;
  1494. begin
  1495. inherited buildderef;
  1496. if df_copied_def in defoptions then
  1497. basedefderef.build(basedef)
  1498. else
  1499. tenumsymtable(symtable).buildderef;
  1500. end;
  1501. procedure tenumdef.deref;
  1502. begin
  1503. inherited deref;
  1504. if df_copied_def in defoptions then
  1505. begin
  1506. basedef:=tenumdef(basedefderef.resolve);
  1507. symtable:=basedef.symtable.getcopy;
  1508. end
  1509. else
  1510. tenumsymtable(symtable).deref;
  1511. end;
  1512. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1513. begin
  1514. inherited ppuwrite(ppufile);
  1515. ppufile.putaint(min);
  1516. ppufile.putaint(max);
  1517. ppufile.putaint(savesize);
  1518. if df_copied_def in defoptions then
  1519. ppufile.putderef(basedefderef);
  1520. ppufile.writeentry(ibenumdef);
  1521. if not (df_copied_def in defoptions) then
  1522. tenumsymtable(symtable).ppuwrite(ppufile);
  1523. end;
  1524. function tenumdef.is_publishable : boolean;
  1525. begin
  1526. is_publishable:=true;
  1527. end;
  1528. function tenumdef.GetTypeName : string;
  1529. begin
  1530. GetTypeName:='<enumeration type>';
  1531. end;
  1532. {****************************************************************************
  1533. TORDDEF
  1534. ****************************************************************************}
  1535. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1536. begin
  1537. inherited create(orddef);
  1538. low:=v;
  1539. high:=b;
  1540. ordtype:=t;
  1541. setsize;
  1542. end;
  1543. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1544. begin
  1545. inherited ppuload(orddef,ppufile);
  1546. ordtype:=tordtype(ppufile.getbyte);
  1547. low:=ppufile.getexprint;
  1548. high:=ppufile.getexprint;
  1549. setsize;
  1550. end;
  1551. function torddef.getcopy : tstoreddef;
  1552. begin
  1553. result:=torddef.create(ordtype,low,high);
  1554. result.typ:=orddef;
  1555. torddef(result).low:=low;
  1556. torddef(result).high:=high;
  1557. torddef(result).ordtype:=ordtype;
  1558. torddef(result).savesize:=savesize;
  1559. end;
  1560. function torddef.alignment:shortint;
  1561. begin
  1562. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1563. (ordtype in [s64bit,u64bit]) then
  1564. result := 4
  1565. else
  1566. result := inherited alignment;
  1567. end;
  1568. procedure torddef.setsize;
  1569. const
  1570. sizetbl : array[tordtype] of longint = (
  1571. 0,
  1572. 1,2,4,8,
  1573. 1,2,4,8,
  1574. 1,1,2,4,8,
  1575. 1,2,8
  1576. );
  1577. begin
  1578. savesize:=sizetbl[ordtype];
  1579. end;
  1580. function torddef.packedbitsize: asizeint;
  1581. var
  1582. sizeval: tconstexprint;
  1583. power: longint;
  1584. begin
  1585. result := 0;
  1586. if ordtype = uvoid then
  1587. exit;
  1588. {$ifndef cpu64bitalu}
  1589. if (ordtype in [s64bit,u64bit]) then
  1590. {$else not cpu64bitalu}
  1591. if (ordtype = u64bit) or
  1592. ((ordtype = s64bit) and
  1593. ((low <= (system.low(int64) div 2)) or
  1594. (high > (system.high(int64) div 2)))) then
  1595. {$endif cpu64bitalu}
  1596. result := 64
  1597. else if (low >= 0) and
  1598. (high <= 1) then
  1599. result := 1
  1600. else
  1601. begin
  1602. if (low>=0) then
  1603. sizeval:=high
  1604. else
  1605. { don't count 0 twice }
  1606. sizeval:=(cutils.max(-low,high)*2)-1;
  1607. { 256 must become 512 etc. }
  1608. nextpowerof2(sizeval+1,power);
  1609. result := power;
  1610. end;
  1611. end;
  1612. function torddef.getvardef : longint;
  1613. const
  1614. basetype2vardef : array[tordtype] of longint = (
  1615. varUndefined,
  1616. varbyte,varword,varlongword,varqword,
  1617. varshortint,varsmallint,varinteger,varint64,
  1618. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1619. varUndefined,varUndefined,varCurrency);
  1620. begin
  1621. result:=basetype2vardef[ordtype];
  1622. end;
  1623. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1624. begin
  1625. inherited ppuwrite(ppufile);
  1626. ppufile.putbyte(byte(ordtype));
  1627. ppufile.putexprint(low);
  1628. ppufile.putexprint(high);
  1629. ppufile.writeentry(iborddef);
  1630. end;
  1631. function torddef.is_publishable : boolean;
  1632. begin
  1633. is_publishable:=(ordtype<>uvoid);
  1634. end;
  1635. function torddef.GetTypeName : string;
  1636. const
  1637. names : array[tordtype] of string[20] = (
  1638. 'untyped',
  1639. 'Byte','Word','DWord','QWord',
  1640. 'ShortInt','SmallInt','LongInt','Int64',
  1641. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1642. 'Char','WideChar','Currency');
  1643. begin
  1644. GetTypeName:=names[ordtype];
  1645. end;
  1646. {****************************************************************************
  1647. TFLOATDEF
  1648. ****************************************************************************}
  1649. constructor tfloatdef.create(t : tfloattype);
  1650. begin
  1651. inherited create(floatdef);
  1652. floattype:=t;
  1653. setsize;
  1654. end;
  1655. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1656. begin
  1657. inherited ppuload(floatdef,ppufile);
  1658. floattype:=tfloattype(ppufile.getbyte);
  1659. setsize;
  1660. end;
  1661. function tfloatdef.getcopy : tstoreddef;
  1662. begin
  1663. result:=tfloatdef.create(floattype);
  1664. result.typ:=floatdef;
  1665. tfloatdef(result).savesize:=savesize;
  1666. end;
  1667. function tfloatdef.alignment:shortint;
  1668. begin
  1669. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1670. case floattype of
  1671. s80real: result:=16;
  1672. s64real,
  1673. s64currency,
  1674. s64comp : result:=4;
  1675. else
  1676. result := inherited alignment;
  1677. end
  1678. else
  1679. result := inherited alignment;
  1680. end;
  1681. procedure tfloatdef.setsize;
  1682. begin
  1683. case floattype of
  1684. s32real : savesize:=4;
  1685. s80real : savesize:=10;
  1686. sc80real:
  1687. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1688. system_x86_64_linux,system_x86_64_freebsd,
  1689. system_x86_64_solaris,system_x86_64_embedded] then
  1690. savesize:=16
  1691. else
  1692. savesize:=12;
  1693. s64real,
  1694. s64currency,
  1695. s64comp : savesize:=8;
  1696. else
  1697. savesize:=0;
  1698. end;
  1699. end;
  1700. function tfloatdef.getvardef : longint;
  1701. const
  1702. floattype2vardef : array[tfloattype] of longint = (
  1703. varSingle,varDouble,varUndefined,varUndefined,
  1704. varUndefined,varCurrency,varUndefined);
  1705. begin
  1706. if (upper(typename)='TDATETIME') and
  1707. assigned(owner) and
  1708. assigned(owner.name) and
  1709. (owner.name^='SYSTEM') then
  1710. result:=varDate
  1711. else
  1712. result:=floattype2vardef[floattype];
  1713. end;
  1714. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1715. begin
  1716. inherited ppuwrite(ppufile);
  1717. ppufile.putbyte(byte(floattype));
  1718. ppufile.writeentry(ibfloatdef);
  1719. end;
  1720. function tfloatdef.is_publishable : boolean;
  1721. begin
  1722. is_publishable:=true;
  1723. end;
  1724. function tfloatdef.GetTypeName : string;
  1725. const
  1726. names : array[tfloattype] of string[20] = (
  1727. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1728. begin
  1729. GetTypeName:=names[floattype];
  1730. end;
  1731. {****************************************************************************
  1732. TFILEDEF
  1733. ****************************************************************************}
  1734. constructor tfiledef.createtext;
  1735. begin
  1736. inherited create(filedef);
  1737. filetyp:=ft_text;
  1738. typedfiledef:=nil;
  1739. setsize;
  1740. end;
  1741. constructor tfiledef.createuntyped;
  1742. begin
  1743. inherited create(filedef);
  1744. filetyp:=ft_untyped;
  1745. typedfiledef:=nil;
  1746. setsize;
  1747. end;
  1748. constructor tfiledef.createtyped(def:tdef);
  1749. begin
  1750. inherited create(filedef);
  1751. filetyp:=ft_typed;
  1752. typedfiledef:=def;
  1753. setsize;
  1754. end;
  1755. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1756. begin
  1757. inherited ppuload(filedef,ppufile);
  1758. filetyp:=tfiletyp(ppufile.getbyte);
  1759. if filetyp=ft_typed then
  1760. ppufile.getderef(typedfiledefderef)
  1761. else
  1762. typedfiledef:=nil;
  1763. setsize;
  1764. end;
  1765. function tfiledef.getcopy : tstoreddef;
  1766. begin
  1767. case filetyp of
  1768. ft_typed:
  1769. result:=tfiledef.createtyped(typedfiledef);
  1770. ft_untyped:
  1771. result:=tfiledef.createuntyped;
  1772. ft_text:
  1773. result:=tfiledef.createtext;
  1774. else
  1775. internalerror(2004121201);
  1776. end;
  1777. end;
  1778. procedure tfiledef.buildderef;
  1779. begin
  1780. inherited buildderef;
  1781. if filetyp=ft_typed then
  1782. typedfiledefderef.build(typedfiledef);
  1783. end;
  1784. procedure tfiledef.deref;
  1785. begin
  1786. inherited deref;
  1787. if filetyp=ft_typed then
  1788. typedfiledef:=tdef(typedfiledefderef.resolve);
  1789. end;
  1790. procedure tfiledef.setsize;
  1791. begin
  1792. {$ifdef cpu64bitaddr}
  1793. case filetyp of
  1794. ft_text :
  1795. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1796. savesize:=632{+8}
  1797. else
  1798. savesize:=628{+8};
  1799. ft_typed,
  1800. ft_untyped :
  1801. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1802. savesize:=372
  1803. else
  1804. savesize:=368;
  1805. end;
  1806. {$endif cpu64bitaddr}
  1807. {$ifdef cpu32bitaddr}
  1808. case filetyp of
  1809. ft_text :
  1810. savesize:=592{+4};
  1811. ft_typed,
  1812. ft_untyped :
  1813. savesize:=332;
  1814. end;
  1815. {$endif cpu32bitaddr}
  1816. {$ifdef cpu8bitaddr}
  1817. case filetyp of
  1818. ft_text :
  1819. savesize:=127;
  1820. ft_typed,
  1821. ft_untyped :
  1822. savesize:=127;
  1823. end;
  1824. {$endif cpu8bitaddr}
  1825. end;
  1826. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1827. begin
  1828. inherited ppuwrite(ppufile);
  1829. ppufile.putbyte(byte(filetyp));
  1830. if filetyp=ft_typed then
  1831. ppufile.putderef(typedfiledefderef);
  1832. ppufile.writeentry(ibfiledef);
  1833. end;
  1834. function tfiledef.GetTypeName : string;
  1835. begin
  1836. case filetyp of
  1837. ft_untyped:
  1838. GetTypeName:='File';
  1839. ft_typed:
  1840. GetTypeName:='File Of '+typedfiledef.typename;
  1841. ft_text:
  1842. GetTypeName:='Text'
  1843. end;
  1844. end;
  1845. function tfiledef.getmangledparaname : string;
  1846. begin
  1847. case filetyp of
  1848. ft_untyped:
  1849. getmangledparaname:='FILE';
  1850. ft_typed:
  1851. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1852. ft_text:
  1853. getmangledparaname:='TEXT'
  1854. end;
  1855. end;
  1856. {****************************************************************************
  1857. TVARIANTDEF
  1858. ****************************************************************************}
  1859. constructor tvariantdef.create(v : tvarianttype);
  1860. begin
  1861. inherited create(variantdef);
  1862. varianttype:=v;
  1863. setsize;
  1864. end;
  1865. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1866. begin
  1867. inherited ppuload(variantdef,ppufile);
  1868. varianttype:=tvarianttype(ppufile.getbyte);
  1869. setsize;
  1870. end;
  1871. function tvariantdef.getcopy : tstoreddef;
  1872. begin
  1873. result:=tvariantdef.create(varianttype);
  1874. end;
  1875. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1876. begin
  1877. inherited ppuwrite(ppufile);
  1878. ppufile.putbyte(byte(varianttype));
  1879. ppufile.writeentry(ibvariantdef);
  1880. end;
  1881. function tvariantdef.getvardef : longint;
  1882. begin
  1883. Result:=varVariant;
  1884. end;
  1885. procedure tvariantdef.setsize;
  1886. begin
  1887. {$ifdef cpu64bitaddr}
  1888. savesize:=24;
  1889. {$else cpu64bitaddr}
  1890. savesize:=16;
  1891. {$endif cpu64bitaddr}
  1892. end;
  1893. function tvariantdef.GetTypeName : string;
  1894. begin
  1895. case varianttype of
  1896. vt_normalvariant:
  1897. GetTypeName:='Variant';
  1898. vt_olevariant:
  1899. GetTypeName:='OleVariant';
  1900. end;
  1901. end;
  1902. function tvariantdef.needs_inittable : boolean;
  1903. begin
  1904. needs_inittable:=true;
  1905. end;
  1906. function tvariantdef.is_publishable : boolean;
  1907. begin
  1908. is_publishable:=true;
  1909. end;
  1910. {****************************************************************************
  1911. TABSTRACtpointerdef
  1912. ****************************************************************************}
  1913. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1914. begin
  1915. inherited create(dt);
  1916. pointeddef:=def;
  1917. savesize:=sizeof(pint);
  1918. end;
  1919. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1920. begin
  1921. inherited ppuload(dt,ppufile);
  1922. ppufile.getderef(pointeddefderef);
  1923. savesize:=sizeof(pint);
  1924. end;
  1925. procedure tabstractpointerdef.buildderef;
  1926. begin
  1927. inherited buildderef;
  1928. pointeddefderef.build(pointeddef);
  1929. end;
  1930. procedure tabstractpointerdef.deref;
  1931. begin
  1932. inherited deref;
  1933. pointeddef:=tdef(pointeddefderef.resolve);
  1934. end;
  1935. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1936. begin
  1937. inherited ppuwrite(ppufile);
  1938. ppufile.putderef(pointeddefderef);
  1939. end;
  1940. {****************************************************************************
  1941. tpointerdef
  1942. ****************************************************************************}
  1943. constructor tpointerdef.create(def:tdef);
  1944. begin
  1945. inherited create(pointerdef,def);
  1946. is_far:=false;
  1947. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1948. end;
  1949. constructor tpointerdef.createfar(def:tdef);
  1950. begin
  1951. inherited create(pointerdef,def);
  1952. is_far:=true;
  1953. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1954. end;
  1955. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1956. begin
  1957. inherited ppuload(pointerdef,ppufile);
  1958. is_far:=(ppufile.getbyte<>0);
  1959. has_pointer_math:=(ppufile.getbyte<>0);
  1960. end;
  1961. function tpointerdef.getcopy : tstoreddef;
  1962. begin
  1963. { don't use direct pointeddef if it is a forwarddef because in other case
  1964. one of them will be destroyed on forward type resolve and the second will
  1965. point to garbage }
  1966. if pointeddef.typ=forwarddef then
  1967. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  1968. else
  1969. result:=tpointerdef.create(pointeddef);
  1970. tpointerdef(result).is_far:=is_far;
  1971. tpointerdef(result).has_pointer_math:=has_pointer_math;
  1972. tpointerdef(result).savesize:=savesize;
  1973. end;
  1974. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1975. begin
  1976. inherited ppuwrite(ppufile);
  1977. ppufile.putbyte(byte(is_far));
  1978. ppufile.putbyte(byte(has_pointer_math));
  1979. ppufile.writeentry(ibpointerdef);
  1980. end;
  1981. function tpointerdef.GetTypeName : string;
  1982. begin
  1983. if is_far then
  1984. GetTypeName:='^'+pointeddef.typename+';far'
  1985. else
  1986. GetTypeName:='^'+pointeddef.typename;
  1987. end;
  1988. {****************************************************************************
  1989. TCLASSREFDEF
  1990. ****************************************************************************}
  1991. constructor tclassrefdef.create(def:tdef);
  1992. begin
  1993. inherited create(classrefdef,def);
  1994. end;
  1995. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1996. begin
  1997. inherited ppuload(classrefdef,ppufile);
  1998. end;
  1999. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2000. begin
  2001. inherited ppuwrite(ppufile);
  2002. ppufile.writeentry(ibclassrefdef);
  2003. end;
  2004. function tclassrefdef.getcopy:tstoreddef;
  2005. begin
  2006. if pointeddef.typ=forwarddef then
  2007. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2008. else
  2009. result:=tclassrefdef.create(pointeddef);
  2010. tclassrefdef(result).savesize:=savesize;
  2011. end;
  2012. function tclassrefdef.GetTypeName : string;
  2013. begin
  2014. GetTypeName:='Class Of '+pointeddef.typename;
  2015. end;
  2016. function tclassrefdef.is_publishable : boolean;
  2017. begin
  2018. result:=true;
  2019. end;
  2020. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2021. begin
  2022. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2023. result:=inherited rtti_mangledname(rt)
  2024. else
  2025. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2026. end;
  2027. procedure tclassrefdef.register_created_object_type;
  2028. begin
  2029. tobjectdef(pointeddef).register_created_classref_type;
  2030. end;
  2031. {***************************************************************************
  2032. TSETDEF
  2033. ***************************************************************************}
  2034. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2035. var
  2036. setallocbits: aint;
  2037. packedsavesize: aint;
  2038. begin
  2039. inherited create(setdef);
  2040. elementdef:=def;
  2041. setmax:=high;
  2042. if (current_settings.setalloc=0) then
  2043. begin
  2044. setbase:=0;
  2045. if (high<32) then
  2046. savesize:=Sizeof(longint)
  2047. else if (high<256) then
  2048. savesize:=32
  2049. else
  2050. savesize:=(high+7) div 8
  2051. end
  2052. else
  2053. begin
  2054. setallocbits:=current_settings.setalloc*8;
  2055. setbase:=low and not(setallocbits-1);
  2056. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2057. savesize:=packedsavesize;
  2058. if savesize=3 then
  2059. savesize:=4;
  2060. end;
  2061. end;
  2062. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2063. begin
  2064. inherited ppuload(setdef,ppufile);
  2065. ppufile.getderef(elementdefderef);
  2066. savesize:=ppufile.getaint;
  2067. setbase:=ppufile.getaint;
  2068. setmax:=ppufile.getaint;
  2069. end;
  2070. function tsetdef.getcopy : tstoreddef;
  2071. begin
  2072. result:=tsetdef.create(elementdef,setbase,setmax);
  2073. { the copy might have been created with a different setalloc setting }
  2074. tsetdef(result).savesize:=savesize;
  2075. end;
  2076. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2077. begin
  2078. inherited ppuwrite(ppufile);
  2079. ppufile.putderef(elementdefderef);
  2080. ppufile.putaint(savesize);
  2081. ppufile.putaint(setbase);
  2082. ppufile.putaint(setmax);
  2083. ppufile.writeentry(ibsetdef);
  2084. end;
  2085. procedure tsetdef.buildderef;
  2086. begin
  2087. inherited buildderef;
  2088. elementdefderef.build(elementdef);
  2089. end;
  2090. procedure tsetdef.deref;
  2091. begin
  2092. inherited deref;
  2093. elementdef:=tdef(elementdefderef.resolve);
  2094. end;
  2095. function tsetdef.is_publishable : boolean;
  2096. begin
  2097. is_publishable:=savesize in [1,2,4];
  2098. end;
  2099. function tsetdef.GetTypeName : string;
  2100. begin
  2101. if assigned(elementdef) then
  2102. GetTypeName:='Set Of '+elementdef.typename
  2103. else
  2104. GetTypeName:='Empty Set';
  2105. end;
  2106. {***************************************************************************
  2107. TFORMALDEF
  2108. ***************************************************************************}
  2109. constructor tformaldef.create(Atyped:boolean);
  2110. begin
  2111. inherited create(formaldef);
  2112. typed:=Atyped;
  2113. savesize:=0;
  2114. end;
  2115. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2116. begin
  2117. inherited ppuload(formaldef,ppufile);
  2118. typed:=boolean(ppufile.getbyte);
  2119. savesize:=0;
  2120. end;
  2121. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2122. begin
  2123. inherited ppuwrite(ppufile);
  2124. ppufile.putbyte(byte(typed));
  2125. ppufile.writeentry(ibformaldef);
  2126. end;
  2127. function tformaldef.GetTypeName : string;
  2128. begin
  2129. if typed then
  2130. GetTypeName:='<Typed formal type>'
  2131. else
  2132. GetTypeName:='<Formal type>';
  2133. end;
  2134. {***************************************************************************
  2135. TARRAYDEF
  2136. ***************************************************************************}
  2137. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2138. begin
  2139. inherited create(arraydef);
  2140. lowrange:=l;
  2141. highrange:=h;
  2142. rangedef:=def;
  2143. _elementdef:=nil;
  2144. arrayoptions:=[];
  2145. symtable:=tarraysymtable.create(self);
  2146. end;
  2147. destructor tarraydef.destroy;
  2148. begin
  2149. symtable.free;
  2150. symtable:=nil;
  2151. inherited;
  2152. end;
  2153. constructor tarraydef.create_from_pointer(def:tdef);
  2154. begin
  2155. { use -1 so that the elecount will not overflow }
  2156. self.create(0,high(aint)-1,s32inttype);
  2157. arrayoptions:=[ado_IsConvertedPointer];
  2158. setelementdef(def);
  2159. end;
  2160. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2161. begin
  2162. inherited ppuload(arraydef,ppufile);
  2163. { the addresses are calculated later }
  2164. ppufile.getderef(_elementdefderef);
  2165. ppufile.getderef(rangedefderef);
  2166. lowrange:=ppufile.getaint;
  2167. highrange:=ppufile.getaint;
  2168. ppufile.getsmallset(arrayoptions);
  2169. symtable:=tarraysymtable.create(self);
  2170. tarraysymtable(symtable).ppuload(ppufile)
  2171. end;
  2172. function tarraydef.getcopy : tstoreddef;
  2173. begin
  2174. result:=tarraydef.create(lowrange,highrange,rangedef);
  2175. tarraydef(result).arrayoptions:=arrayoptions;
  2176. tarraydef(result)._elementdef:=_elementdef;
  2177. end;
  2178. procedure tarraydef.buildderef;
  2179. begin
  2180. inherited buildderef;
  2181. tarraysymtable(symtable).buildderef;
  2182. _elementdefderef.build(_elementdef);
  2183. rangedefderef.build(rangedef);
  2184. end;
  2185. procedure tarraydef.deref;
  2186. begin
  2187. inherited deref;
  2188. tarraysymtable(symtable).deref;
  2189. _elementdef:=tdef(_elementdefderef.resolve);
  2190. rangedef:=tdef(rangedefderef.resolve);
  2191. end;
  2192. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2193. begin
  2194. inherited ppuwrite(ppufile);
  2195. ppufile.putderef(_elementdefderef);
  2196. ppufile.putderef(rangedefderef);
  2197. ppufile.putaint(lowrange);
  2198. ppufile.putaint(highrange);
  2199. ppufile.putsmallset(arrayoptions);
  2200. ppufile.writeentry(ibarraydef);
  2201. tarraysymtable(symtable).ppuwrite(ppufile);
  2202. end;
  2203. function tarraydef.elesize : asizeint;
  2204. begin
  2205. if (ado_IsBitPacked in arrayoptions) then
  2206. internalerror(2006080101);
  2207. if assigned(_elementdef) then
  2208. result:=_elementdef.size
  2209. else
  2210. result:=0;
  2211. end;
  2212. function tarraydef.elepackedbitsize : asizeint;
  2213. begin
  2214. if not(ado_IsBitPacked in arrayoptions) then
  2215. internalerror(2006080102);
  2216. if assigned(_elementdef) then
  2217. result:=_elementdef.packedbitsize
  2218. else
  2219. result:=0;
  2220. end;
  2221. function tarraydef.elecount : asizeuint;
  2222. var
  2223. qhigh,qlow : qword;
  2224. begin
  2225. if ado_IsDynamicArray in arrayoptions then
  2226. begin
  2227. result:=0;
  2228. exit;
  2229. end;
  2230. if (highrange>0) and (lowrange<0) then
  2231. begin
  2232. qhigh:=highrange;
  2233. qlow:=qword(-lowrange);
  2234. { prevent overflow, return 0 to indicate overflow }
  2235. if qhigh+qlow>qword(high(asizeint)-1) then
  2236. result:=0
  2237. else
  2238. result:=qhigh+qlow+1;
  2239. end
  2240. else
  2241. result:=int64(highrange)-lowrange+1;
  2242. end;
  2243. function tarraydef.size : asizeint;
  2244. var
  2245. cachedelecount : asizeuint;
  2246. cachedelesize : asizeint;
  2247. begin
  2248. if ado_IsDynamicArray in arrayoptions then
  2249. begin
  2250. size:=sizeof(pint);
  2251. exit;
  2252. end;
  2253. { Tarraydef.size may never be called for an open array! }
  2254. if highrange<lowrange then
  2255. internalerror(99080501);
  2256. if not (ado_IsBitPacked in arrayoptions) then
  2257. cachedelesize:=elesize
  2258. else
  2259. cachedelesize := elepackedbitsize;
  2260. cachedelecount:=elecount;
  2261. if (cachedelesize = 0) then
  2262. begin
  2263. size := 0;
  2264. exit;
  2265. end;
  2266. if (cachedelecount = 0) then
  2267. begin
  2268. size := -1;
  2269. exit;
  2270. end;
  2271. { prevent overflow, return -1 to indicate overflow }
  2272. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2273. if (cachedelecount > asizeuint(high(asizeint))) or
  2274. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2275. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2276. accessing the array, see ncgmem (PFV) }
  2277. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2278. begin
  2279. result:=-1;
  2280. exit;
  2281. end;
  2282. result:=cachedelesize*asizeint(cachedelecount);
  2283. if (ado_IsBitPacked in arrayoptions) then
  2284. { can't just add 7 and divide by 8, because that may overflow }
  2285. result:=result div 8 + ord((result mod 8)<>0);
  2286. end;
  2287. procedure tarraydef.setelementdef(def:tdef);
  2288. begin
  2289. _elementdef:=def;
  2290. if not(
  2291. (ado_IsDynamicArray in arrayoptions) or
  2292. (ado_IsConvertedPointer in arrayoptions) or
  2293. (highrange<lowrange)
  2294. ) and
  2295. (size=-1) then
  2296. Message(sym_e_segment_too_large);
  2297. end;
  2298. function tarraydef.alignment : shortint;
  2299. begin
  2300. { alignment of dyn. arrays doesn't depend on the element size }
  2301. if (ado_IsDynamicArray in arrayoptions) then
  2302. alignment:=size_2_align(sizeof(pint))
  2303. { alignment is the alignment of the elements }
  2304. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2305. ((elementdef.typ=objectdef) and
  2306. is_object(elementdef)) then
  2307. alignment:=elementdef.alignment
  2308. { alignment is the size of the elements }
  2309. else if not (ado_IsBitPacked in arrayoptions) then
  2310. alignment:=size_2_align(elesize)
  2311. else
  2312. alignment:=packedbitsloadsize(elepackedbitsize);
  2313. end;
  2314. function tarraydef.needs_inittable : boolean;
  2315. begin
  2316. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2317. end;
  2318. function tarraydef.GetTypeName : string;
  2319. begin
  2320. if (ado_IsConstString in arrayoptions) then
  2321. result:='Constant String'
  2322. else if (ado_isarrayofconst in arrayoptions) or
  2323. (ado_isConstructor in arrayoptions) then
  2324. begin
  2325. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2326. GetTypeName:='Array Of Const'
  2327. else
  2328. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2329. end
  2330. else if (ado_IsDynamicArray in arrayoptions) then
  2331. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2332. else if ((highrange=-1) and (lowrange=0)) then
  2333. GetTypeName:='Open Array Of '+elementdef.typename
  2334. else
  2335. begin
  2336. result := '';
  2337. if (ado_IsBitPacked in arrayoptions) then
  2338. result:='Packed ';
  2339. if rangedef.typ=enumdef then
  2340. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2341. else
  2342. result:=result+'Array['+tostr(lowrange)+'..'+
  2343. tostr(highrange)+'] Of '+elementdef.typename
  2344. end;
  2345. end;
  2346. function tarraydef.getmangledparaname : string;
  2347. begin
  2348. if ado_isarrayofconst in arrayoptions then
  2349. getmangledparaname:='array_of_const'
  2350. else
  2351. if ((highrange=-1) and (lowrange=0)) then
  2352. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2353. else
  2354. internalerror(200204176);
  2355. end;
  2356. function tarraydef.is_publishable : boolean;
  2357. begin
  2358. Result:=ado_IsDynamicArray in arrayoptions;
  2359. end;
  2360. {***************************************************************************
  2361. tabstractrecorddef
  2362. ***************************************************************************}
  2363. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2364. begin
  2365. inherited create(dt);
  2366. objname:=stringdup(upper(n));
  2367. objrealname:=stringdup(n);
  2368. objectoptions:=[];
  2369. end;
  2370. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2371. begin
  2372. inherited ppuload(dt,ppufile);
  2373. objrealname:=stringdup(ppufile.getstring);
  2374. objname:=stringdup(upper(objrealname^));
  2375. ppufile.getsmallset(objectoptions);
  2376. end;
  2377. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2378. begin
  2379. inherited ppuwrite(ppufile);
  2380. ppufile.putstring(objrealname^);
  2381. ppufile.putsmallset(objectoptions);
  2382. end;
  2383. destructor tabstractrecorddef.destroy;
  2384. begin
  2385. stringdispose(objname);
  2386. stringdispose(objrealname);
  2387. inherited destroy;
  2388. end;
  2389. procedure tabstractrecorddef.check_forwards;
  2390. begin
  2391. tstoredsymtable(symtable).check_forwards;
  2392. end;
  2393. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2394. var
  2395. i: longint;
  2396. sym: tsym;
  2397. begin
  2398. for i:=0 to symtable.SymList.Count-1 do
  2399. begin
  2400. sym:=tsym(symtable.SymList[i]);
  2401. if sym.typ=procsym then
  2402. begin
  2403. result:=tprocsym(sym).find_procdef_bytype(pt);
  2404. if assigned(result) then
  2405. exit;
  2406. end;
  2407. end;
  2408. result:=nil;
  2409. end;
  2410. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2411. begin
  2412. if t=gs_record then
  2413. GetSymtable:=symtable
  2414. else
  2415. GetSymtable:=nil;
  2416. end;
  2417. function tabstractrecorddef.is_packed:boolean;
  2418. begin
  2419. result:=tabstractrecordsymtable(symtable).is_packed;
  2420. end;
  2421. function tabstractrecorddef.RttiName: string;
  2422. var
  2423. tmp: tabstractrecorddef;
  2424. begin
  2425. Result:=OwnerHierarchyName+objrealname^;
  2426. end;
  2427. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2428. var
  2429. sym : tsym;
  2430. i : integer;
  2431. pd : tprocdef;
  2432. hashedid : THashedIDString;
  2433. begin
  2434. result:=nil;
  2435. hashedid.id:='GETENUMERATOR';
  2436. sym:=tsym(symtable.FindWithHash(hashedid));
  2437. if assigned(sym) and (sym.typ=procsym) then
  2438. begin
  2439. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2440. begin
  2441. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2442. if (pd.proctypeoption = potype_function) and
  2443. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2444. (pd.visibility >= vis_public) then
  2445. begin
  2446. result:=pd;
  2447. exit;
  2448. end;
  2449. end;
  2450. end;
  2451. end;
  2452. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2453. var
  2454. sym : tsym;
  2455. i : integer;
  2456. pd : tprocdef;
  2457. hashedid : THashedIDString;
  2458. begin
  2459. result:=nil;
  2460. // first search for po_enumerator_movenext method modifier
  2461. // then search for public function MoveNext: Boolean
  2462. for i:=0 to symtable.SymList.Count-1 do
  2463. begin
  2464. sym:=TSym(symtable.SymList[i]);
  2465. if (sym.typ=procsym) then
  2466. begin
  2467. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2468. if assigned(pd) then
  2469. begin
  2470. result:=pd;
  2471. exit;
  2472. end;
  2473. end;
  2474. end;
  2475. hashedid.id:='MOVENEXT';
  2476. sym:=tsym(symtable.FindWithHash(hashedid));
  2477. if assigned(sym) and (sym.typ=procsym) then
  2478. begin
  2479. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2480. begin
  2481. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2482. if (pd.proctypeoption = potype_function) and
  2483. is_boolean(pd.returndef) and
  2484. (pd.minparacount = 0) and
  2485. (pd.visibility >= vis_public) then
  2486. begin
  2487. result:=pd;
  2488. exit;
  2489. end;
  2490. end;
  2491. end;
  2492. end;
  2493. function tabstractrecorddef.search_enumerator_current: tsym;
  2494. var
  2495. sym: tsym;
  2496. i: integer;
  2497. hashedid : THashedIDString;
  2498. begin
  2499. result:=nil;
  2500. // first search for ppo_enumerator_current property modifier
  2501. // then search for public property Current
  2502. for i:=0 to symtable.SymList.Count-1 do
  2503. begin
  2504. sym:=TSym(symtable.SymList[i]);
  2505. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2506. begin
  2507. result:=sym;
  2508. exit;
  2509. end;
  2510. end;
  2511. hashedid.id:='CURRENT';
  2512. sym:=tsym(symtable.FindWithHash(hashedid));
  2513. if assigned(sym) and (sym.typ=propertysym) and
  2514. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2515. begin
  2516. result:=sym;
  2517. exit;
  2518. end;
  2519. end;
  2520. {***************************************************************************
  2521. trecorddef
  2522. ***************************************************************************}
  2523. constructor trecorddef.create(const n:string; p:TSymtable);
  2524. begin
  2525. inherited create(n,recorddef);
  2526. symtable:=p;
  2527. { we can own the symtable only if nobody else owns a copy so far }
  2528. if symtable.refcount=1 then
  2529. symtable.defowner:=self;
  2530. isunion:=false;
  2531. end;
  2532. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2533. begin
  2534. inherited ppuload(recorddef,ppufile);
  2535. if df_copied_def in defoptions then
  2536. ppufile.getderef(cloneddefderef)
  2537. else
  2538. begin
  2539. symtable:=trecordsymtable.create(objrealname^,0);
  2540. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2541. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2542. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2543. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2544. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2545. trecordsymtable(symtable).ppuload(ppufile);
  2546. { requires usefieldalignment to be set }
  2547. symtable.defowner:=self;
  2548. end;
  2549. isunion:=false;
  2550. end;
  2551. destructor trecorddef.destroy;
  2552. begin
  2553. if assigned(symtable) then
  2554. begin
  2555. symtable.free;
  2556. symtable:=nil;
  2557. end;
  2558. inherited destroy;
  2559. end;
  2560. function trecorddef.getcopy : tstoreddef;
  2561. begin
  2562. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2563. trecorddef(result).isunion:=isunion;
  2564. include(trecorddef(result).defoptions,df_copied_def);
  2565. end;
  2566. function trecorddef.needs_inittable : boolean;
  2567. begin
  2568. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2569. end;
  2570. procedure trecorddef.buildderef;
  2571. begin
  2572. inherited buildderef;
  2573. if df_copied_def in defoptions then
  2574. cloneddefderef.build(symtable.defowner)
  2575. else
  2576. tstoredsymtable(symtable).buildderef;
  2577. end;
  2578. procedure trecorddef.deref;
  2579. begin
  2580. inherited deref;
  2581. { now dereference the definitions }
  2582. if df_copied_def in defoptions then
  2583. begin
  2584. cloneddef:=trecorddef(cloneddefderef.resolve);
  2585. symtable:=cloneddef.symtable.getcopy;
  2586. end
  2587. else
  2588. tstoredsymtable(symtable).deref;
  2589. { assign TGUID? load only from system unit }
  2590. if not(assigned(rec_tguid)) and
  2591. (upper(typename)='TGUID') and
  2592. assigned(owner) and
  2593. assigned(owner.name) and
  2594. (owner.name^='SYSTEM') then
  2595. rec_tguid:=self;
  2596. { assign JMP_BUF? load only from system unit }
  2597. if not(assigned(rec_jmp_buf)) and
  2598. (upper(typename)='JMP_BUF') and
  2599. assigned(owner) and
  2600. assigned(owner.name) and
  2601. (owner.name^='SYSTEM') then
  2602. rec_jmp_buf:=self;
  2603. end;
  2604. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2605. begin
  2606. inherited ppuwrite(ppufile);
  2607. if df_copied_def in defoptions then
  2608. ppufile.putderef(cloneddefderef)
  2609. else
  2610. begin
  2611. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2612. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2613. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2614. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2615. ppufile.putaint(trecordsymtable(symtable).datasize);
  2616. end;
  2617. ppufile.writeentry(ibrecorddef);
  2618. if not(df_copied_def in defoptions) then
  2619. trecordsymtable(symtable).ppuwrite(ppufile);
  2620. end;
  2621. function trecorddef.size:asizeint;
  2622. begin
  2623. result:=trecordsymtable(symtable).datasize;
  2624. end;
  2625. function trecorddef.alignment:shortint;
  2626. begin
  2627. alignment:=trecordsymtable(symtable).recordalignment;
  2628. end;
  2629. function trecorddef.padalignment:shortint;
  2630. begin
  2631. padalignment := trecordsymtable(symtable).padalignment;
  2632. end;
  2633. function trecorddef.GetTypeName : string;
  2634. begin
  2635. GetTypeName:='<record type>'
  2636. end;
  2637. {***************************************************************************
  2638. TABSTRACTPROCDEF
  2639. ***************************************************************************}
  2640. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2641. begin
  2642. inherited create(dt);
  2643. parast:=tparasymtable.create(self,level);
  2644. paras:=nil;
  2645. minparacount:=0;
  2646. maxparacount:=0;
  2647. proctypeoption:=potype_none;
  2648. proccalloption:=pocall_none;
  2649. procoptions:=[];
  2650. returndef:=voidtype;
  2651. savesize:=sizeof(pint);
  2652. callerargareasize:=0;
  2653. calleeargareasize:=0;
  2654. has_paraloc_info:=callnoside;
  2655. funcretloc[callerside].init;
  2656. funcretloc[calleeside].init;
  2657. check_mark_as_nested;
  2658. end;
  2659. destructor tabstractprocdef.destroy;
  2660. begin
  2661. if assigned(paras) then
  2662. begin
  2663. {$ifdef MEMDEBUG}
  2664. memprocpara.start;
  2665. {$endif MEMDEBUG}
  2666. paras.free;
  2667. paras:=nil;
  2668. {$ifdef MEMDEBUG}
  2669. memprocpara.stop;
  2670. {$endif MEMDEBUG}
  2671. end;
  2672. if assigned(parast) then
  2673. begin
  2674. {$ifdef MEMDEBUG}
  2675. memprocparast.start;
  2676. {$endif MEMDEBUG}
  2677. parast.free;
  2678. parast:=nil;
  2679. {$ifdef MEMDEBUG}
  2680. memprocparast.stop;
  2681. {$endif MEMDEBUG}
  2682. end;
  2683. funcretloc[callerside].done;
  2684. funcretloc[calleeside].done;
  2685. inherited destroy;
  2686. end;
  2687. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2688. begin
  2689. if (tsym(p).typ<>paravarsym) then
  2690. exit;
  2691. inc(plongint(arg)^);
  2692. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2693. begin
  2694. if not assigned(tparavarsym(p).defaultconstsym) then
  2695. inc(minparacount);
  2696. inc(maxparacount);
  2697. end;
  2698. end;
  2699. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2700. begin
  2701. if (tsym(p).typ<>paravarsym) then
  2702. exit;
  2703. paras.add(p);
  2704. end;
  2705. procedure tabstractprocdef.calcparas;
  2706. var
  2707. paracount : longint;
  2708. begin
  2709. { This can already be assigned when
  2710. we need to reresolve this unit (PFV) }
  2711. if assigned(paras) then
  2712. paras.free;
  2713. paras:=tparalist.create(false);
  2714. paracount:=0;
  2715. minparacount:=0;
  2716. maxparacount:=0;
  2717. parast.SymList.ForEachCall(@count_para,@paracount);
  2718. paras.capacity:=paracount;
  2719. { Insert parameters in table }
  2720. parast.SymList.ForEachCall(@insert_para,nil);
  2721. { Order parameters }
  2722. paras.sortparas;
  2723. end;
  2724. procedure tabstractprocdef.buildderef;
  2725. begin
  2726. { released procdef? }
  2727. if not assigned(parast) then
  2728. exit;
  2729. inherited buildderef;
  2730. returndefderef.build(returndef);
  2731. { parast }
  2732. tparasymtable(parast).buildderef;
  2733. end;
  2734. procedure tabstractprocdef.deref;
  2735. begin
  2736. inherited deref;
  2737. returndef:=tdef(returndefderef.resolve);
  2738. { parast }
  2739. tparasymtable(parast).deref;
  2740. { recalculated parameters }
  2741. calcparas;
  2742. end;
  2743. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2744. begin
  2745. inherited ppuload(dt,ppufile);
  2746. parast:=nil;
  2747. Paras:=nil;
  2748. minparacount:=0;
  2749. maxparacount:=0;
  2750. ppufile.getderef(returndefderef);
  2751. { TODO: remove fpu_used loading}
  2752. ppufile.getbyte;
  2753. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2754. proccalloption:=tproccalloption(ppufile.getbyte);
  2755. ppufile.getnormalset(procoptions);
  2756. funcretloc[callerside].init;
  2757. if po_explicitparaloc in procoptions then
  2758. funcretloc[callerside].ppuload(ppufile);
  2759. savesize:=sizeof(pint);
  2760. if (po_explicitparaloc in procoptions) then
  2761. has_paraloc_info:=callerside;
  2762. end;
  2763. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2764. var
  2765. oldintfcrc : boolean;
  2766. begin
  2767. { released procdef? }
  2768. if not assigned(parast) then
  2769. exit;
  2770. inherited ppuwrite(ppufile);
  2771. ppufile.putderef(returndefderef);
  2772. oldintfcrc:=ppufile.do_interface_crc;
  2773. ppufile.do_interface_crc:=false;
  2774. ppufile.putbyte(0);
  2775. ppufile.putbyte(ord(proctypeoption));
  2776. ppufile.putbyte(ord(proccalloption));
  2777. ppufile.putnormalset(procoptions);
  2778. ppufile.do_interface_crc:=oldintfcrc;
  2779. if (po_explicitparaloc in procoptions) then
  2780. funcretloc[callerside].ppuwrite(ppufile);
  2781. end;
  2782. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2783. var
  2784. hs,s : string;
  2785. hp : TParavarsym;
  2786. hpc : tconstsym;
  2787. first : boolean;
  2788. i : integer;
  2789. begin
  2790. s:='';
  2791. first:=true;
  2792. for i:=0 to paras.count-1 do
  2793. begin
  2794. hp:=tparavarsym(paras[i]);
  2795. if not(vo_is_hidden_para in hp.varoptions) or
  2796. (showhidden) then
  2797. begin
  2798. if first then
  2799. begin
  2800. s:=s+'(';
  2801. first:=false;
  2802. end
  2803. else
  2804. s:=s+',';
  2805. if vo_is_hidden_para in hp.varoptions then
  2806. s:=s+'<';
  2807. case hp.varspez of
  2808. vs_var :
  2809. s:=s+'var ';
  2810. vs_const :
  2811. s:=s+'const ';
  2812. vs_out :
  2813. s:=s+'out ';
  2814. vs_constref :
  2815. s:=s+'constref ';
  2816. end;
  2817. if hp.univpara then
  2818. s:=s+'univ ';
  2819. if assigned(hp.vardef.typesym) then
  2820. begin
  2821. hs:=hp.vardef.typesym.realname;
  2822. if hs[1]<>'$' then
  2823. s:=s+hs
  2824. else
  2825. s:=s+hp.vardef.GetTypeName;
  2826. end
  2827. else
  2828. s:=s+hp.vardef.GetTypeName;
  2829. { default value }
  2830. if assigned(hp.defaultconstsym) then
  2831. begin
  2832. hpc:=tconstsym(hp.defaultconstsym);
  2833. hs:='';
  2834. case hpc.consttyp of
  2835. conststring,
  2836. constresourcestring :
  2837. begin
  2838. If hpc.value.len>0 then
  2839. begin
  2840. setLength(hs,hpc.value.len);
  2841. { don't write past the end of hs if the constant
  2842. is > 255 chars }
  2843. move(hpc.value.valueptr^,hs[1],length(hs));
  2844. { make sure that constant strings with newline chars
  2845. don't create a linebreak in the assembler code,
  2846. since comments are line-based. Also remove nulls
  2847. because the comments are written as a pchar. }
  2848. ReplaceCase(hs,#0,'.');
  2849. ReplaceCase(hs,#10,'.');
  2850. ReplaceCase(hs,#13,'.');
  2851. end;
  2852. end;
  2853. constreal :
  2854. str(pbestreal(hpc.value.valueptr)^,hs);
  2855. constpointer :
  2856. hs:=tostr(hpc.value.valueordptr);
  2857. constord :
  2858. begin
  2859. if is_boolean(hpc.constdef) then
  2860. begin
  2861. if hpc.value.valueord<>0 then
  2862. hs:='TRUE'
  2863. else
  2864. hs:='FALSE';
  2865. end
  2866. else
  2867. hs:=tostr(hpc.value.valueord);
  2868. end;
  2869. constnil :
  2870. hs:='nil';
  2871. constset :
  2872. hs:='<set>';
  2873. end;
  2874. if hs<>'' then
  2875. s:=s+'="'+hs+'"';
  2876. end;
  2877. if vo_is_hidden_para in hp.varoptions then
  2878. s:=s+'>';
  2879. end;
  2880. end;
  2881. if not first then
  2882. s:=s+')';
  2883. if (po_varargs in procoptions) then
  2884. s:=s+';VarArgs';
  2885. typename_paras:=s;
  2886. end;
  2887. function tabstractprocdef.is_methodpointer:boolean;
  2888. begin
  2889. result:=false;
  2890. end;
  2891. function tabstractprocdef.is_addressonly:boolean;
  2892. begin
  2893. result:=true;
  2894. end;
  2895. function tabstractprocdef.no_self_node: boolean;
  2896. begin
  2897. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2898. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  2899. end;
  2900. procedure tabstractprocdef.check_mark_as_nested;
  2901. begin
  2902. { nested procvars require that nested functions use the Delphi-style
  2903. nested procedure calling convention }
  2904. if (parast.symtablelevel>normal_function_level) and
  2905. (m_nested_procvars in current_settings.modeswitches) then
  2906. include(procoptions,po_delphi_nested_cc);
  2907. end;
  2908. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2909. begin
  2910. if (side in [callerside,callbothsides]) and
  2911. not(has_paraloc_info in [callerside,callbothsides]) then
  2912. begin
  2913. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2914. if has_paraloc_info in [calleeside,callbothsides] then
  2915. has_paraloc_info:=callbothsides
  2916. else
  2917. has_paraloc_info:=callerside;
  2918. end;
  2919. if (side in [calleeside,callbothsides]) and
  2920. not(has_paraloc_info in [calleeside,callbothsides]) then
  2921. begin
  2922. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2923. if has_paraloc_info in [callerside,callbothsides] then
  2924. has_paraloc_info:=callbothsides
  2925. else
  2926. has_paraloc_info:=calleeside;
  2927. end;
  2928. end;
  2929. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2930. var
  2931. p: tparavarsym;
  2932. ploc: PCGParalocation;
  2933. i: longint;
  2934. begin
  2935. result:=false;
  2936. init_paraloc_info(side);
  2937. for i:=0 to parast.SymList.Count-1 do
  2938. if tsym(parast.SymList[i]).typ=paravarsym then
  2939. begin
  2940. p:=tparavarsym(parast.SymList[i]);
  2941. { check if no parameter is located on the stack }
  2942. if is_open_array(p.vardef) or
  2943. is_array_of_const(p.vardef) then
  2944. begin
  2945. result:=true;
  2946. exit;
  2947. end;
  2948. ploc:=p.paraloc[side].location;
  2949. while assigned(ploc) do
  2950. begin
  2951. if (ploc^.loc=LOC_REFERENCE) then
  2952. begin
  2953. result:=true;
  2954. exit
  2955. end;
  2956. ploc:=ploc^.next;
  2957. end;
  2958. end;
  2959. end;
  2960. {***************************************************************************
  2961. TPROCDEF
  2962. ***************************************************************************}
  2963. constructor tprocdef.create(level:byte);
  2964. begin
  2965. inherited create(procdef,level);
  2966. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2967. _mangledname:=nil;
  2968. fileinfo:=current_filepos;
  2969. extnumber:=$ffff;
  2970. aliasnames:=TCmdStrList.create;
  2971. funcretsym:=nil;
  2972. forwarddef:=true;
  2973. interfacedef:=false;
  2974. hasforward:=false;
  2975. struct := nil;
  2976. import_dll:=nil;
  2977. import_name:=nil;
  2978. import_nr:=0;
  2979. inlininginfo:=nil;
  2980. deprecatedmsg:=nil;
  2981. {$ifdef i386}
  2982. fpu_used:=maxfpuregs;
  2983. {$endif i386}
  2984. end;
  2985. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2986. var
  2987. i,aliasnamescount : longint;
  2988. level : byte;
  2989. begin
  2990. inherited ppuload(procdef,ppufile);
  2991. if po_has_mangledname in procoptions then
  2992. _mangledname:=stringdup(ppufile.getstring)
  2993. else
  2994. _mangledname:=nil;
  2995. extnumber:=ppufile.getword;
  2996. level:=ppufile.getbyte;
  2997. ppufile.getderef(structderef);
  2998. ppufile.getderef(procsymderef);
  2999. ppufile.getposinfo(fileinfo);
  3000. visibility:=tvisibility(ppufile.getbyte);
  3001. ppufile.getsmallset(symoptions);
  3002. if sp_has_deprecated_msg in symoptions then
  3003. deprecatedmsg:=stringdup(ppufile.getstring)
  3004. else
  3005. deprecatedmsg:=nil;
  3006. {$ifdef powerpc}
  3007. { library symbol for AmigaOS/MorphOS }
  3008. ppufile.getderef(libsymderef);
  3009. {$endif powerpc}
  3010. { import stuff }
  3011. if po_has_importdll in procoptions then
  3012. import_dll:=stringdup(ppufile.getstring)
  3013. else
  3014. import_dll:=nil;
  3015. if po_has_importname in procoptions then
  3016. import_name:=stringdup(ppufile.getstring)
  3017. else
  3018. import_name:=nil;
  3019. import_nr:=ppufile.getword;
  3020. if (po_msgint in procoptions) then
  3021. messageinf.i:=ppufile.getlongint;
  3022. if (po_msgstr in procoptions) then
  3023. messageinf.str:=stringdup(ppufile.getstring);
  3024. if (po_dispid in procoptions) then
  3025. dispid:=ppufile.getlongint;
  3026. { inline stuff }
  3027. if (po_has_inlininginfo in procoptions) then
  3028. begin
  3029. ppufile.getderef(funcretsymderef);
  3030. new(inlininginfo);
  3031. ppufile.getsmallset(inlininginfo^.flags);
  3032. end
  3033. else
  3034. begin
  3035. inlininginfo:=nil;
  3036. funcretsym:=nil;
  3037. end;
  3038. aliasnames:=TCmdStrList.create;
  3039. { count alias names }
  3040. aliasnamescount:=ppufile.getbyte;
  3041. for i:=1 to aliasnamescount do
  3042. aliasnames.insert(ppufile.getstring);
  3043. { load para symtable }
  3044. parast:=tparasymtable.create(self,level);
  3045. tparasymtable(parast).ppuload(ppufile);
  3046. { load local symtable }
  3047. if (po_has_inlininginfo in procoptions) then
  3048. begin
  3049. localst:=tlocalsymtable.create(self,level);
  3050. tlocalsymtable(localst).ppuload(ppufile);
  3051. end
  3052. else
  3053. localst:=nil;
  3054. { inline stuff }
  3055. if (po_has_inlininginfo in procoptions) then
  3056. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3057. { default values for no persistent data }
  3058. if (cs_link_deffile in current_settings.globalswitches) and
  3059. (tf_need_export in target_info.flags) and
  3060. (po_exports in procoptions) then
  3061. deffile.AddExport(mangledname);
  3062. forwarddef:=false;
  3063. interfacedef:=false;
  3064. hasforward:=false;
  3065. { Disable po_has_inlining until the derefimpl is done }
  3066. exclude(procoptions,po_has_inlininginfo);
  3067. {$ifdef i386}
  3068. fpu_used:=maxfpuregs;
  3069. {$endif i386}
  3070. end;
  3071. destructor tprocdef.destroy;
  3072. begin
  3073. aliasnames.free;
  3074. aliasnames:=nil;
  3075. if assigned(localst) and
  3076. (localst.symtabletype<>staticsymtable) then
  3077. begin
  3078. {$ifdef MEMDEBUG}
  3079. memproclocalst.start;
  3080. {$endif MEMDEBUG}
  3081. localst.free;
  3082. localst:=nil;
  3083. {$ifdef MEMDEBUG}
  3084. memproclocalst.start;
  3085. {$endif MEMDEBUG}
  3086. end;
  3087. if assigned(inlininginfo) then
  3088. begin
  3089. {$ifdef MEMDEBUG}
  3090. memprocnodetree.start;
  3091. {$endif MEMDEBUG}
  3092. tnode(inlininginfo^.code).free;
  3093. {$ifdef MEMDEBUG}
  3094. memprocnodetree.start;
  3095. {$endif MEMDEBUG}
  3096. dispose(inlininginfo);
  3097. inlininginfo:=nil;
  3098. end;
  3099. stringdispose(resultname);
  3100. stringdispose(import_dll);
  3101. stringdispose(import_name);
  3102. stringdispose(deprecatedmsg);
  3103. if (po_msgstr in procoptions) then
  3104. stringdispose(messageinf.str);
  3105. if assigned(_mangledname) then
  3106. begin
  3107. {$ifdef MEMDEBUG}
  3108. memmanglednames.start;
  3109. {$endif MEMDEBUG}
  3110. stringdispose(_mangledname);
  3111. {$ifdef MEMDEBUG}
  3112. memmanglednames.stop;
  3113. {$endif MEMDEBUG}
  3114. end;
  3115. inherited destroy;
  3116. end;
  3117. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3118. var
  3119. oldintfcrc : boolean;
  3120. aliasnamescount : longint;
  3121. item : TCmdStrListItem;
  3122. begin
  3123. { released procdef? }
  3124. if not assigned(parast) then
  3125. exit;
  3126. inherited ppuwrite(ppufile);
  3127. if po_has_mangledname in procoptions then
  3128. ppufile.putstring(_mangledname^);
  3129. ppufile.putword(extnumber);
  3130. ppufile.putbyte(parast.symtablelevel);
  3131. ppufile.putderef(structderef);
  3132. ppufile.putderef(procsymderef);
  3133. ppufile.putposinfo(fileinfo);
  3134. ppufile.putbyte(byte(visibility));
  3135. ppufile.putsmallset(symoptions);
  3136. if sp_has_deprecated_msg in symoptions then
  3137. ppufile.putstring(deprecatedmsg^);
  3138. {$ifdef powerpc}
  3139. { library symbol for AmigaOS/MorphOS }
  3140. ppufile.putderef(libsymderef);
  3141. {$endif powerpc}
  3142. { import }
  3143. if po_has_importdll in procoptions then
  3144. ppufile.putstring(import_dll^);
  3145. if po_has_importname in procoptions then
  3146. ppufile.putstring(import_name^);
  3147. ppufile.putword(import_nr);
  3148. if (po_msgint in procoptions) then
  3149. ppufile.putlongint(messageinf.i);
  3150. if (po_msgstr in procoptions) then
  3151. ppufile.putstring(messageinf.str^);
  3152. if (po_dispid in procoptions) then
  3153. ppufile.putlongint(dispid);
  3154. { inline stuff }
  3155. oldintfcrc:=ppufile.do_crc;
  3156. ppufile.do_crc:=false;
  3157. if (po_has_inlininginfo in procoptions) then
  3158. begin
  3159. ppufile.putderef(funcretsymderef);
  3160. ppufile.putsmallset(inlininginfo^.flags);
  3161. end;
  3162. { count alias names }
  3163. aliasnamescount:=0;
  3164. item:=TCmdStrListItem(aliasnames.first);
  3165. while assigned(item) do
  3166. begin
  3167. inc(aliasnamescount);
  3168. item:=TCmdStrListItem(item.next);
  3169. end;
  3170. if aliasnamescount>255 then
  3171. internalerror(200711021);
  3172. ppufile.putbyte(aliasnamescount);
  3173. item:=TCmdStrListItem(aliasnames.first);
  3174. while assigned(item) do
  3175. begin
  3176. ppufile.putstring(item.str);
  3177. item:=TCmdStrListItem(item.next);
  3178. end;
  3179. ppufile.do_crc:=oldintfcrc;
  3180. { write this entry }
  3181. ppufile.writeentry(ibprocdef);
  3182. { Save the para symtable, this is taken from the interface }
  3183. tparasymtable(parast).ppuwrite(ppufile);
  3184. { save localsymtable for inline procedures or when local
  3185. browser info is requested, this has no influence on the crc }
  3186. if (po_has_inlininginfo in procoptions) then
  3187. begin
  3188. oldintfcrc:=ppufile.do_crc;
  3189. ppufile.do_crc:=false;
  3190. tlocalsymtable(localst).ppuwrite(ppufile);
  3191. ppufile.do_crc:=oldintfcrc;
  3192. end;
  3193. { node tree for inlining }
  3194. oldintfcrc:=ppufile.do_crc;
  3195. ppufile.do_crc:=false;
  3196. if (po_has_inlininginfo in procoptions) then
  3197. ppuwritenodetree(ppufile,inlininginfo^.code);
  3198. ppufile.do_crc:=oldintfcrc;
  3199. end;
  3200. function tprocdef.fullprocname(showhidden:boolean):string;
  3201. var
  3202. s : string;
  3203. t : ttoken;
  3204. begin
  3205. {$ifdef EXTDEBUG}
  3206. showhidden:=true;
  3207. {$endif EXTDEBUG}
  3208. s:='';
  3209. if assigned(struct) then
  3210. begin
  3211. s:=struct.RttiName+'.';
  3212. if (po_classmethod in procoptions) and
  3213. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3214. s:='class ' + s;
  3215. end;
  3216. if proctypeoption=potype_operator then
  3217. begin
  3218. for t:=NOTOKEN to last_overloaded do
  3219. if procsym.realname='$'+overloaded_names[t] then
  3220. begin
  3221. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  3222. break;
  3223. end;
  3224. end
  3225. else
  3226. s:=s+procsym.realname+typename_paras(showhidden);
  3227. case proctypeoption of
  3228. potype_constructor:
  3229. s:='constructor '+s;
  3230. potype_destructor:
  3231. s:='destructor '+s;
  3232. potype_class_constructor:
  3233. s:='class constructor '+s;
  3234. potype_class_destructor:
  3235. s:='class destructor '+s;
  3236. else
  3237. if assigned(returndef) and
  3238. not(is_void(returndef)) then
  3239. s:=s+':'+returndef.GetTypeName;
  3240. end;
  3241. if owner.symtabletype=localsymtable then
  3242. s:=s+' is nested';
  3243. s:=s+';';
  3244. { forced calling convention? }
  3245. if (po_hascallingconvention in procoptions) then
  3246. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3247. if (po_staticmethod in procoptions) and
  3248. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3249. s:=s+' Static;';
  3250. fullprocname:=s;
  3251. end;
  3252. function tprocdef.is_methodpointer:boolean;
  3253. begin
  3254. { don't check assigned(_class), that's also the case for nested
  3255. procedures inside methods }
  3256. result:=owner.symtabletype=ObjectSymtable;
  3257. end;
  3258. function tprocdef.is_addressonly:boolean;
  3259. begin
  3260. result:=assigned(owner) and
  3261. (owner.symtabletype<>ObjectSymtable) and
  3262. (not(m_nested_procvars in current_settings.modeswitches) or
  3263. not is_nested_pd(self));
  3264. end;
  3265. procedure tprocdef.make_external;
  3266. begin
  3267. include(procoptions,po_external);
  3268. forwarddef:=false;
  3269. end;
  3270. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3271. begin
  3272. case t of
  3273. gs_local :
  3274. GetSymtable:=localst;
  3275. gs_para :
  3276. GetSymtable:=parast;
  3277. else
  3278. GetSymtable:=nil;
  3279. end;
  3280. end;
  3281. procedure tprocdef.buildderef;
  3282. begin
  3283. inherited buildderef;
  3284. structderef.build(struct);
  3285. { procsym that originaly defined this definition, should be in the
  3286. same symtable }
  3287. procsymderef.build(procsym);
  3288. {$ifdef powerpc}
  3289. { library symbol for AmigaOS/MorphOS }
  3290. libsymderef.build(libsym);
  3291. {$endif powerpc}
  3292. end;
  3293. procedure tprocdef.buildderefimpl;
  3294. begin
  3295. inherited buildderefimpl;
  3296. { Localst is not available for main/unit init }
  3297. if assigned(localst) then
  3298. begin
  3299. tlocalsymtable(localst).buildderef;
  3300. tlocalsymtable(localst).buildderefimpl;
  3301. end;
  3302. { inline tree }
  3303. if (po_has_inlininginfo in procoptions) then
  3304. begin
  3305. funcretsymderef.build(funcretsym);
  3306. inlininginfo^.code.buildderefimpl;
  3307. end;
  3308. end;
  3309. procedure tprocdef.deref;
  3310. begin
  3311. inherited deref;
  3312. struct:=tabstractrecorddef(structderef.resolve);
  3313. { procsym that originaly defined this definition, should be in the
  3314. same symtable }
  3315. procsym:=tprocsym(procsymderef.resolve);
  3316. {$ifdef powerpc}
  3317. { library symbol for AmigaOS/MorphOS }
  3318. libsym:=tsym(libsymderef.resolve);
  3319. {$endif powerpc}
  3320. end;
  3321. procedure tprocdef.derefimpl;
  3322. begin
  3323. { Enable has_inlininginfo when the inlininginfo
  3324. structure is available. The has_inlininginfo was disabled
  3325. after the load, since the data was invalid }
  3326. if assigned(inlininginfo) then
  3327. include(procoptions,po_has_inlininginfo);
  3328. { Locals }
  3329. if assigned(localst) then
  3330. begin
  3331. tlocalsymtable(localst).deref;
  3332. tlocalsymtable(localst).derefimpl;
  3333. end;
  3334. { Inline }
  3335. if (po_has_inlininginfo in procoptions) then
  3336. begin
  3337. inlininginfo^.code.derefimpl;
  3338. { funcretsym, this is always located in the localst }
  3339. funcretsym:=tsym(funcretsymderef.resolve);
  3340. end
  3341. else
  3342. begin
  3343. { safety }
  3344. { Not safe! A unit may be reresolved after its interface has been
  3345. parsed but before its implementation has been parsed, and in that
  3346. case the funcretsym is still required!
  3347. funcretsym:=nil; }
  3348. end;
  3349. end;
  3350. function tprocdef.GetTypeName : string;
  3351. begin
  3352. GetTypeName := FullProcName(false);
  3353. end;
  3354. function tprocdef.mangledname : string;
  3355. var
  3356. hp : TParavarsym;
  3357. hs : string;
  3358. crc : dword;
  3359. newlen,
  3360. oldlen,
  3361. i : integer;
  3362. begin
  3363. if assigned(_mangledname) then
  3364. begin
  3365. {$ifdef compress}
  3366. mangledname:=minilzw_decode(_mangledname^);
  3367. {$else}
  3368. mangledname:=_mangledname^;
  3369. {$endif}
  3370. exit;
  3371. end;
  3372. { we need to use the symtable where the procsym is inserted,
  3373. because that is visible to the world }
  3374. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3375. oldlen:=length(mangledname);
  3376. { add parameter types }
  3377. for i:=0 to paras.count-1 do
  3378. begin
  3379. hp:=tparavarsym(paras[i]);
  3380. if not(vo_is_hidden_para in hp.varoptions) then
  3381. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3382. end;
  3383. { add resultdef, add $$ as separator to make it unique from a
  3384. parameter separator }
  3385. if not is_void(returndef) then
  3386. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3387. newlen:=length(mangledname);
  3388. { Replace with CRC if the parameter line is very long }
  3389. if (newlen-oldlen>12) and
  3390. ((newlen>100) or (newlen-oldlen>64)) then
  3391. begin
  3392. crc:=0;
  3393. for i:=0 to paras.count-1 do
  3394. begin
  3395. hp:=tparavarsym(paras[i]);
  3396. if not(vo_is_hidden_para in hp.varoptions) then
  3397. begin
  3398. hs:=hp.vardef.mangledparaname;
  3399. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3400. end;
  3401. end;
  3402. hs:=hp.vardef.mangledparaname;
  3403. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3404. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3405. end;
  3406. {$ifdef compress}
  3407. _mangledname:=stringdup(minilzw_encode(mangledname));
  3408. {$else}
  3409. _mangledname:=stringdup(mangledname);
  3410. {$endif}
  3411. end;
  3412. function tprocdef.cplusplusmangledname : string;
  3413. function getcppparaname(p : tdef) : string;
  3414. const
  3415. {$ifdef NAMEMANGLING_GCC2}
  3416. ordtype2str : array[tordtype] of string[2] = (
  3417. '',
  3418. 'Uc','Us','Ui','Us',
  3419. 'Sc','s','i','x',
  3420. 'b','b','b','b','b',
  3421. 'c','w','x');
  3422. {$else NAMEMANGLING_GCC2}
  3423. ordtype2str : array[tordtype] of string[1] = (
  3424. 'v',
  3425. 'h','t','j','y',
  3426. 'a','s','i','x',
  3427. 'b','b','b','b','b',
  3428. 'c','w','x');
  3429. floattype2str : array[tfloattype] of string[1] = (
  3430. 'f','d','e','e',
  3431. 'd','d','g');
  3432. {$endif NAMEMANGLING_GCC2}
  3433. var
  3434. s : string;
  3435. begin
  3436. case p.typ of
  3437. orddef:
  3438. s:=ordtype2str[torddef(p).ordtype];
  3439. pointerdef:
  3440. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3441. {$ifndef NAMEMANGLING_GCC2}
  3442. floatdef:
  3443. s:=floattype2str[tfloatdef(p).floattype];
  3444. {$endif NAMEMANGLING_GCC2}
  3445. else
  3446. internalerror(2103001);
  3447. end;
  3448. getcppparaname:=s;
  3449. end;
  3450. var
  3451. s,s2 : string;
  3452. hp : TParavarsym;
  3453. i : integer;
  3454. begin
  3455. {$ifdef NAMEMANGLING_GCC2}
  3456. { outdated gcc 2.x name mangling scheme }
  3457. s := procsym.realname;
  3458. if procsym.owner.symtabletype=ObjectSymtable then
  3459. begin
  3460. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3461. case proctypeoption of
  3462. potype_destructor:
  3463. s:='_$_'+tostr(length(s2))+s2;
  3464. potype_constructor:
  3465. s:='___'+tostr(length(s2))+s2;
  3466. else
  3467. s:='_'+s+'__'+tostr(length(s2))+s2;
  3468. end;
  3469. end
  3470. else s:=s+'__';
  3471. s:=s+'F';
  3472. { concat modifiers }
  3473. { !!!!! }
  3474. { now we handle the parameters }
  3475. if maxparacount>0 then
  3476. begin
  3477. for i:=0 to paras.count-1 do
  3478. begin
  3479. hp:=tparavarsym(paras[i]);
  3480. { no hidden parameters form part of a C++ mangled name:
  3481. a) self is not included
  3482. b) there are no "high" or other hidden parameters
  3483. }
  3484. if vo_is_hidden_para in hp.varoptions then
  3485. continue;
  3486. s2:=getcppparaname(hp.vardef);
  3487. if hp.varspez in [vs_var,vs_out] then
  3488. s2:='R'+s2;
  3489. s:=s+s2;
  3490. end;
  3491. end
  3492. else
  3493. s:=s+'v';
  3494. cplusplusmangledname:=s;
  3495. {$else NAMEMANGLING_GCC2}
  3496. { gcc 3.x and 4.x name mangling scheme }
  3497. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3498. if procsym.owner.symtabletype=ObjectSymtable then
  3499. begin
  3500. s:='_ZN';
  3501. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3502. s:=s+tostr(length(s2))+s2;
  3503. case proctypeoption of
  3504. potype_constructor:
  3505. s:=s+'C1';
  3506. potype_destructor:
  3507. s:=s+'D1';
  3508. else
  3509. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3510. end;
  3511. s:=s+'E';
  3512. end
  3513. else
  3514. s:=procsym.realname;
  3515. { now we handle the parameters }
  3516. if maxparacount>0 then
  3517. begin
  3518. for i:=0 to paras.count-1 do
  3519. begin
  3520. hp:=tparavarsym(paras[i]);
  3521. { no hidden parameters form part of a C++ mangled name:
  3522. a) self is not included
  3523. b) there are no "high" or other hidden parameters
  3524. }
  3525. if vo_is_hidden_para in hp.varoptions then
  3526. continue;
  3527. s2:=getcppparaname(hp.vardef);
  3528. if hp.varspez in [vs_var,vs_out] then
  3529. s2:='R'+s2;
  3530. s:=s+s2;
  3531. end;
  3532. end
  3533. else
  3534. s:=s+'v';
  3535. cplusplusmangledname:=s;
  3536. {$endif NAMEMANGLING_GCC2}
  3537. end;
  3538. function tprocdef.objcmangledname : string;
  3539. var
  3540. manglednamelen: longint;
  3541. iscatmethod : boolean;
  3542. begin
  3543. if not (po_msgstr in procoptions) then
  3544. internalerror(2009030901);
  3545. { we may very well need longer strings to handle these... }
  3546. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3547. length('+"[ ]"')+length(messageinf.str^);
  3548. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3549. if (iscatmethod) then
  3550. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3551. if manglednamelen>255 then
  3552. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3553. if not(po_classmethod in procoptions) then
  3554. result:='"-['
  3555. else
  3556. result:='"+[';
  3557. { quotes are necessary because the +/- otherwise confuse the assembler
  3558. into expecting a number
  3559. }
  3560. if iscatmethod then
  3561. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3562. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3563. if iscatmethod then
  3564. result:=result+')';
  3565. result:=result+' '+messageinf.str^+']"';
  3566. end;
  3567. procedure tprocdef.setmangledname(const s : string);
  3568. begin
  3569. { This is not allowed anymore, the forward declaration
  3570. already needs to create the correct mangledname, no changes
  3571. afterwards are allowed (PFV) }
  3572. { Exception: interface definitions in mode macpas, since in that }
  3573. { case no reference to the old name can exist yet (JM) }
  3574. if assigned(_mangledname) then
  3575. if ((m_mac in current_settings.modeswitches) and
  3576. (interfacedef)) then
  3577. stringdispose(_mangledname)
  3578. else
  3579. internalerror(200411171);
  3580. {$ifdef compress}
  3581. _mangledname:=stringdup(minilzw_encode(s));
  3582. {$else}
  3583. _mangledname:=stringdup(s);
  3584. {$endif}
  3585. include(procoptions,po_has_mangledname);
  3586. end;
  3587. {***************************************************************************
  3588. TPROCVARDEF
  3589. ***************************************************************************}
  3590. constructor tprocvardef.create(level:byte);
  3591. begin
  3592. inherited create(procvardef,level);
  3593. end;
  3594. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3595. begin
  3596. inherited ppuload(procvardef,ppufile);
  3597. { load para symtable }
  3598. parast:=tparasymtable.create(self,ppufile.getbyte);
  3599. tparasymtable(parast).ppuload(ppufile);
  3600. end;
  3601. function tprocvardef.getcopy : tstoreddef;
  3602. var
  3603. i : tcallercallee;
  3604. j : longint;
  3605. begin
  3606. result:=tprocvardef.create(parast.symtablelevel);
  3607. tprocvardef(result).returndef:=returndef;
  3608. tprocvardef(result).returndefderef:=returndefderef;
  3609. tprocvardef(result).parast:=parast.getcopy;
  3610. tprocvardef(result).savesize:=savesize;
  3611. { create paralist copy }
  3612. tprocvardef(result).paras:=tparalist.create(false);
  3613. tprocvardef(result).paras.count:=paras.count;
  3614. for j:=0 to paras.count-1 do
  3615. tprocvardef(result).paras[j]:=paras[j];
  3616. tprocvardef(result).proctypeoption:=proctypeoption;
  3617. tprocvardef(result).proccalloption:=proccalloption;
  3618. tprocvardef(result).procoptions:=procoptions;
  3619. tprocvardef(result).callerargareasize:=callerargareasize;
  3620. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3621. tprocvardef(result).maxparacount:=maxparacount;
  3622. tprocvardef(result).minparacount:=minparacount;
  3623. for i:=low(tcallercallee) to high(tcallercallee) do
  3624. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3625. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3626. {$ifdef m68k}
  3627. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3628. {$endif}
  3629. end;
  3630. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3631. begin
  3632. inherited ppuwrite(ppufile);
  3633. { Save the para symtable level (necessary to distinguish nested
  3634. procvars) }
  3635. ppufile.putbyte(parast.symtablelevel);
  3636. { Write this entry }
  3637. ppufile.writeentry(ibprocvardef);
  3638. { Save the para symtable, this is taken from the interface }
  3639. tparasymtable(parast).ppuwrite(ppufile);
  3640. end;
  3641. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3642. begin
  3643. case t of
  3644. gs_para :
  3645. GetSymtable:=parast;
  3646. else
  3647. GetSymtable:=nil;
  3648. end;
  3649. end;
  3650. function tprocvardef.size : asizeint;
  3651. begin
  3652. if ((po_methodpointer in procoptions) or
  3653. is_nested_pd(self)) and
  3654. not(po_addressonly in procoptions) then
  3655. size:=2*sizeof(pint)
  3656. else
  3657. size:=sizeof(pint);
  3658. end;
  3659. function tprocvardef.is_methodpointer:boolean;
  3660. begin
  3661. result:=(po_methodpointer in procoptions);
  3662. end;
  3663. function tprocvardef.is_addressonly:boolean;
  3664. begin
  3665. result:=(not(po_methodpointer in procoptions) and
  3666. not is_nested_pd(self)) or
  3667. (po_addressonly in procoptions);
  3668. end;
  3669. function tprocvardef.getmangledparaname:string;
  3670. begin
  3671. if not(po_methodpointer in procoptions) then
  3672. if not is_nested_pd(self) then
  3673. result:='procvar'
  3674. else
  3675. result:='nestedprovar'
  3676. else
  3677. result:='procvarofobj'
  3678. end;
  3679. function tprocvardef.is_publishable : boolean;
  3680. begin
  3681. is_publishable:=(po_methodpointer in procoptions);
  3682. end;
  3683. function tprocvardef.GetTypeName : string;
  3684. var
  3685. s: string;
  3686. showhidden : boolean;
  3687. begin
  3688. {$ifdef EXTDEBUG}
  3689. showhidden:=true;
  3690. {$else EXTDEBUG}
  3691. showhidden:=false;
  3692. {$endif EXTDEBUG}
  3693. s:='<';
  3694. if po_classmethod in procoptions then
  3695. s := s+'class method type of'
  3696. else
  3697. if po_addressonly in procoptions then
  3698. s := s+'address of'
  3699. else
  3700. s := s+'procedure variable type of';
  3701. if assigned(returndef) and
  3702. (returndef<>voidtype) then
  3703. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3704. else
  3705. s:=s+' procedure'+typename_paras(showhidden);
  3706. if po_methodpointer in procoptions then
  3707. s := s+' of object';
  3708. if is_nested_pd(self) then
  3709. s := s+' is nested';
  3710. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3711. end;
  3712. {***************************************************************************
  3713. TOBJECTDEF
  3714. ***************************************************************************}
  3715. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3716. begin
  3717. inherited create(n,objectdef);
  3718. fcurrent_dispid:=0;
  3719. objecttype:=ot;
  3720. childof:=nil;
  3721. if objecttype=odt_helper then
  3722. owner.includeoption(sto_has_helper);
  3723. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3724. { create space for vmt !! }
  3725. vmtentries:=TFPList.Create;
  3726. vmt_offset:=0;
  3727. set_parent(c);
  3728. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3729. prepareguid;
  3730. { setup implemented interfaces }
  3731. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3732. ImplementedInterfaces:=TFPObjectList.Create(true)
  3733. else
  3734. ImplementedInterfaces:=nil;
  3735. writing_class_record_dbginfo:=false;
  3736. end;
  3737. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3738. var
  3739. i,
  3740. implintfcount : longint;
  3741. d : tderef;
  3742. ImplIntf : TImplementedInterface;
  3743. vmtentry : pvmtentry;
  3744. begin
  3745. inherited ppuload(objectdef,ppufile);
  3746. objecttype:=tobjecttyp(ppufile.getbyte);
  3747. objextname:=stringdup(ppufile.getstring);
  3748. { only used for external Objective-C classes/protocols }
  3749. if (objextname^='') then
  3750. stringdispose(objextname);
  3751. import_lib:=stringdup(ppufile.getstring);
  3752. { only used for external C++ classes }
  3753. if (import_lib^='') then
  3754. stringdispose(import_lib);
  3755. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3756. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3757. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3758. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3759. vmt_offset:=ppufile.getlongint;
  3760. ppufile.getderef(childofderef);
  3761. { load guid }
  3762. iidstr:=nil;
  3763. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3764. begin
  3765. new(iidguid);
  3766. ppufile.getguid(iidguid^);
  3767. iidstr:=stringdup(ppufile.getstring);
  3768. end;
  3769. if objecttype=odt_helper then
  3770. ppufile.getderef(extendeddefderef);
  3771. vmtentries:=TFPList.Create;
  3772. vmtentries.count:=ppufile.getlongint;
  3773. for i:=0 to vmtentries.count-1 do
  3774. begin
  3775. ppufile.getderef(d);
  3776. new(vmtentry);
  3777. vmtentry^.procdef:=nil;
  3778. vmtentry^.procdefderef:=d;
  3779. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3780. vmtentries[i]:=vmtentry;
  3781. end;
  3782. { load implemented interfaces }
  3783. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3784. begin
  3785. ImplementedInterfaces:=TFPObjectList.Create(true);
  3786. implintfcount:=ppufile.getlongint;
  3787. for i:=0 to implintfcount-1 do
  3788. begin
  3789. ppufile.getderef(d);
  3790. ImplIntf:=TImplementedInterface.Create_deref(d);
  3791. ImplIntf.IOffset:=ppufile.getlongint;
  3792. ImplementedInterfaces.Add(ImplIntf);
  3793. end;
  3794. end
  3795. else
  3796. ImplementedInterfaces:=nil;
  3797. if df_copied_def in defoptions then
  3798. ppufile.getderef(cloneddefderef)
  3799. else
  3800. tObjectSymtable(symtable).ppuload(ppufile);
  3801. { handles the predefined class tobject }
  3802. { the last TOBJECT which is loaded gets }
  3803. { it ! }
  3804. if (childof=nil) and
  3805. (objecttype=odt_class) and
  3806. (objname^='TOBJECT') then
  3807. class_tobject:=self;
  3808. if (childof=nil) and
  3809. (objecttype=odt_interfacecom) then
  3810. if (objname^='IUNKNOWN') then
  3811. interface_iunknown:=self
  3812. else
  3813. if (objname^='IDISPATCH') then
  3814. interface_idispatch:=self;
  3815. if (childof=nil) and
  3816. (objecttype=odt_objcclass) and
  3817. (objname^='PROTOCOL') then
  3818. objc_protocoltype:=self;
  3819. writing_class_record_dbginfo:=false;
  3820. end;
  3821. destructor tobjectdef.destroy;
  3822. begin
  3823. if assigned(symtable) then
  3824. begin
  3825. symtable.free;
  3826. symtable:=nil;
  3827. end;
  3828. stringdispose(objextname);
  3829. stringdispose(import_lib);
  3830. stringdispose(iidstr);
  3831. if assigned(ImplementedInterfaces) then
  3832. begin
  3833. ImplementedInterfaces.free;
  3834. ImplementedInterfaces:=nil;
  3835. end;
  3836. if assigned(iidguid) then
  3837. begin
  3838. dispose(iidguid);
  3839. iidguid:=nil;
  3840. end;
  3841. if assigned(vmtentries) then
  3842. begin
  3843. resetvmtentries;
  3844. vmtentries.free;
  3845. vmtentries:=nil;
  3846. end;
  3847. if assigned(vmcallstaticinfo) then
  3848. begin
  3849. freemem(vmcallstaticinfo);
  3850. vmcallstaticinfo:=nil;
  3851. end;
  3852. inherited destroy;
  3853. end;
  3854. function tobjectdef.getcopy : tstoreddef;
  3855. var
  3856. i : longint;
  3857. begin
  3858. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3859. { the constructor allocates a symtable which we release to avoid memory leaks }
  3860. tobjectdef(result).symtable.free;
  3861. tobjectdef(result).symtable:=symtable.getcopy;
  3862. if assigned(objextname) then
  3863. tobjectdef(result).objextname:=stringdup(objextname^);
  3864. if assigned(import_lib) then
  3865. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3866. tobjectdef(result).objectoptions:=objectoptions;
  3867. include(tobjectdef(result).defoptions,df_copied_def);
  3868. tobjectdef(result).vmt_offset:=vmt_offset;
  3869. if assigned(iidguid) then
  3870. begin
  3871. new(tobjectdef(result).iidguid);
  3872. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3873. end;
  3874. if assigned(iidstr) then
  3875. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3876. if assigned(ImplementedInterfaces) then
  3877. begin
  3878. for i:=0 to ImplementedInterfaces.count-1 do
  3879. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3880. end;
  3881. if assigned(vmtentries) then
  3882. begin
  3883. tobjectdef(result).vmtentries:=TFPList.Create;
  3884. tobjectdef(result).copyvmtentries(self);
  3885. end;
  3886. end;
  3887. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3888. var
  3889. i : longint;
  3890. vmtentry : pvmtentry;
  3891. ImplIntf : TImplementedInterface;
  3892. old_do_indirect_crc: boolean;
  3893. begin
  3894. { if class1 in unit A changes, and class2 in unit B inherits from it
  3895. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3896. However, if there is also a class3 in unit C that only depends on
  3897. unit B, then unit C will not be recompiled because nothing changed
  3898. to the interface of unit B. Nevertheless, unit C can indirectly
  3899. depend on unit A via derefs, and these must be updated -> the
  3900. indirect crc keeps track of such changes. }
  3901. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3902. ppufile.do_indirect_crc:=true;
  3903. inherited ppuwrite(ppufile);
  3904. ppufile.putbyte(byte(objecttype));
  3905. if assigned(objextname) then
  3906. ppufile.putstring(objextname^)
  3907. else
  3908. ppufile.putstring('');
  3909. if assigned(import_lib) then
  3910. ppufile.putstring(import_lib^)
  3911. else
  3912. ppufile.putstring('');
  3913. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3914. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3915. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3916. ppufile.putlongint(vmt_offset);
  3917. ppufile.putderef(childofderef);
  3918. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3919. begin
  3920. ppufile.putguid(iidguid^);
  3921. ppufile.putstring(iidstr^);
  3922. end;
  3923. if objecttype=odt_helper then
  3924. ppufile.putderef(extendeddefderef);
  3925. ppufile.putlongint(vmtentries.count);
  3926. for i:=0 to vmtentries.count-1 do
  3927. begin
  3928. vmtentry:=pvmtentry(vmtentries[i]);
  3929. ppufile.putderef(vmtentry^.procdefderef);
  3930. ppufile.putbyte(byte(vmtentry^.visibility));
  3931. end;
  3932. if assigned(ImplementedInterfaces) then
  3933. begin
  3934. ppufile.putlongint(ImplementedInterfaces.Count);
  3935. for i:=0 to ImplementedInterfaces.Count-1 do
  3936. begin
  3937. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3938. ppufile.putderef(ImplIntf.intfdefderef);
  3939. ppufile.putlongint(ImplIntf.Ioffset);
  3940. end;
  3941. end;
  3942. if df_copied_def in defoptions then
  3943. ppufile.putderef(cloneddefderef);
  3944. ppufile.writeentry(ibobjectdef);
  3945. if not(df_copied_def in defoptions) then
  3946. tObjectSymtable(symtable).ppuwrite(ppufile);
  3947. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3948. end;
  3949. function tobjectdef.GetTypeName:string;
  3950. begin
  3951. { in this case we will go in endless recursion, because then }
  3952. { there is no tsym associated yet with the def. It can occur }
  3953. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3954. { instead of the actual type name }
  3955. if not assigned(typesym) then
  3956. result:='<Currently Parsed Class>'
  3957. else
  3958. result:=typename;
  3959. end;
  3960. procedure tobjectdef.buildderef;
  3961. var
  3962. i : longint;
  3963. vmtentry : pvmtentry;
  3964. begin
  3965. inherited buildderef;
  3966. childofderef.build(childof);
  3967. if df_copied_def in defoptions then
  3968. cloneddefderef.build(symtable.defowner)
  3969. else
  3970. tstoredsymtable(symtable).buildderef;
  3971. if objecttype=odt_helper then
  3972. extendeddefderef.build(extendeddef);
  3973. for i:=0 to vmtentries.count-1 do
  3974. begin
  3975. vmtentry:=pvmtentry(vmtentries[i]);
  3976. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3977. end;
  3978. if assigned(ImplementedInterfaces) then
  3979. begin
  3980. for i:=0 to ImplementedInterfaces.count-1 do
  3981. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3982. end;
  3983. end;
  3984. procedure tobjectdef.deref;
  3985. var
  3986. i : longint;
  3987. vmtentry : pvmtentry;
  3988. begin
  3989. inherited deref;
  3990. childof:=tobjectdef(childofderef.resolve);
  3991. if df_copied_def in defoptions then
  3992. begin
  3993. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3994. symtable:=cloneddef.symtable.getcopy;
  3995. end
  3996. else
  3997. tstoredsymtable(symtable).deref;
  3998. if objecttype=odt_helper then
  3999. extendeddef:=tobjectdef(extendeddefderef.resolve);
  4000. for i:=0 to vmtentries.count-1 do
  4001. begin
  4002. vmtentry:=pvmtentry(vmtentries[i]);
  4003. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4004. end;
  4005. if assigned(ImplementedInterfaces) then
  4006. begin
  4007. for i:=0 to ImplementedInterfaces.count-1 do
  4008. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4009. end;
  4010. end;
  4011. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4012. var
  4013. pd: tprocdef absolute def;
  4014. st: tsymtable;
  4015. psym: tsym;
  4016. nname: TIDString;
  4017. begin
  4018. if (tdef(def).typ<>procdef) then
  4019. exit;
  4020. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4021. owner = symtable in which objcclassdef is defined
  4022. }
  4023. st:=pd.owner.defowner.owner;
  4024. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4025. { check for an existing procsym with our special name }
  4026. psym:=tsym(st.find(nname));
  4027. if not assigned(psym) then
  4028. begin
  4029. psym:=tprocsym.create(nname);
  4030. { avoid warning about this symbol being unused }
  4031. psym.IncRefCount;
  4032. { don't check for duplicates:
  4033. a) we checked above
  4034. b) in case we are in the implementation section of a unit, this
  4035. will also check for this symbol in the interface section
  4036. (since you normally cannot have symbols with the same name
  4037. both interface and implementation), and it's possible to
  4038. have class helpers for the same class in the interface and
  4039. in the implementation, and they cannot be merged since only
  4040. the once in the interface must be saved to the ppu/visible
  4041. from other units }
  4042. st.insert(psym,false);
  4043. end
  4044. else if (psym.typ<>procsym) then
  4045. internalerror(2009111501);
  4046. { add ourselves to this special procsym }
  4047. tprocsym(psym).procdeflist.add(def);
  4048. end;
  4049. procedure tobjectdef.buildderefimpl;
  4050. begin
  4051. inherited buildderefimpl;
  4052. if not (df_copied_def in defoptions) then
  4053. tstoredsymtable(symtable).buildderefimpl;
  4054. end;
  4055. procedure tobjectdef.derefimpl;
  4056. begin
  4057. inherited derefimpl;
  4058. if not (df_copied_def in defoptions) then
  4059. tstoredsymtable(symtable).derefimpl;
  4060. { the procdefs are not owned by the class helper procsyms, so they
  4061. are not stored/restored either -> re-add them here }
  4062. if (objecttype=odt_objcclass) or
  4063. (oo_is_classhelper in objectoptions) then
  4064. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4065. end;
  4066. procedure tobjectdef.resetvmtentries;
  4067. var
  4068. i : longint;
  4069. begin
  4070. for i:=0 to vmtentries.Count-1 do
  4071. Dispose(pvmtentry(vmtentries[i]));
  4072. vmtentries.clear;
  4073. end;
  4074. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4075. var
  4076. i : longint;
  4077. vmtentry : pvmtentry;
  4078. begin
  4079. resetvmtentries;
  4080. vmtentries.count:=objdef.vmtentries.count;
  4081. for i:=0 to objdef.vmtentries.count-1 do
  4082. begin
  4083. new(vmtentry);
  4084. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4085. vmtentries[i]:=vmtentry;
  4086. end;
  4087. end;
  4088. function tobjectdef.getparentdef:tdef;
  4089. begin
  4090. { TODO: Remove getparentdef hack}
  4091. { With 2 forward declared classes with the child class before the
  4092. parent class the child class is written earlier to the ppu. Leaving it
  4093. possible to have a reference to the parent class for property overriding,
  4094. but the parent class still has the childof not resolved yet (PFV) }
  4095. if childof=nil then
  4096. childof:=tobjectdef(childofderef.resolve);
  4097. result:=childof;
  4098. end;
  4099. procedure tobjectdef.prepareguid;
  4100. begin
  4101. { set up guid }
  4102. if not assigned(iidguid) then
  4103. begin
  4104. new(iidguid);
  4105. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  4106. end;
  4107. { setup iidstring }
  4108. if not assigned(iidstr) then
  4109. iidstr:=stringdup(''); { default is empty string }
  4110. end;
  4111. procedure tobjectdef.set_parent( c : tobjectdef);
  4112. begin
  4113. if assigned(childof) then
  4114. exit;
  4115. childof:=c;
  4116. if not assigned(c) then
  4117. exit;
  4118. { inherit options and status }
  4119. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  4120. { add the data of the anchestor class/object }
  4121. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  4122. begin
  4123. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  4124. { inherit recordalignment }
  4125. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  4126. if (oo_has_vmt in objectoptions) and
  4127. (oo_has_vmt in c.objectoptions) then
  4128. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  4129. { if parent has a vmt field then the offset is the same for the child PM }
  4130. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4131. begin
  4132. vmt_offset:=c.vmt_offset;
  4133. include(objectoptions,oo_has_vmt);
  4134. end;
  4135. end;
  4136. end;
  4137. procedure tobjectdef.insertvmt;
  4138. var
  4139. vs: tfieldvarsym;
  4140. begin
  4141. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  4142. exit;
  4143. if (oo_has_vmt in objectoptions) then
  4144. internalerror(12345)
  4145. else
  4146. begin
  4147. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  4148. tObjectSymtable(symtable).fieldalignment);
  4149. if (tf_requires_proper_alignment in target_info.flags) then
  4150. begin
  4151. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  4152. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  4153. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  4154. end;
  4155. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  4156. hidesym(vs);
  4157. tObjectSymtable(symtable).insert(vs);
  4158. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  4159. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  4160. vmt_offset:=vs.fieldoffset
  4161. else
  4162. vmt_offset:=vs.fieldoffset div 8;
  4163. include(objectoptions,oo_has_vmt);
  4164. end;
  4165. end;
  4166. procedure tobjectdef.check_forwards;
  4167. begin
  4168. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  4169. inherited;
  4170. if (oo_is_forward in objectoptions) then
  4171. begin
  4172. { ok, in future, the forward can be resolved }
  4173. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4174. exclude(objectoptions,oo_is_forward);
  4175. end;
  4176. end;
  4177. { true if prot implements d (or if they are equal) }
  4178. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  4179. var
  4180. i : longint;
  4181. begin
  4182. { objcprotocols have multiple inheritance, all protocols from which
  4183. the current protocol inherits are stored in implementedinterfaces }
  4184. result:=prot=d;
  4185. if result then
  4186. exit;
  4187. for i:=0 to prot.ImplementedInterfaces.count-1 do
  4188. begin
  4189. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  4190. if result then
  4191. exit;
  4192. end;
  4193. end;
  4194. { true, if self inherits from d (or if they are equal) }
  4195. function tobjectdef.is_related(d : tdef) : boolean;
  4196. var
  4197. hp : tobjectdef;
  4198. begin
  4199. if self=d then
  4200. begin
  4201. is_related:=true;
  4202. exit;
  4203. end;
  4204. if (d.typ<>objectdef) then
  4205. begin
  4206. is_related:=false;
  4207. exit;
  4208. end;
  4209. { Objective-C protocols can use multiple inheritance }
  4210. if (objecttype=odt_objcprotocol) then
  4211. begin
  4212. is_related:=is_related_protocol(self,d);
  4213. exit
  4214. end;
  4215. { formally declared Objective-C classes match Objective-C classes with
  4216. the same name }
  4217. if (objecttype=odt_objcclass) and
  4218. (tobjectdef(d).objecttype=odt_objcclass) and
  4219. ((oo_is_formal in objectoptions) or
  4220. (oo_is_formal in tobjectdef(d).objectoptions)) and
  4221. (objrealname^=tobjectdef(d).objrealname^) then
  4222. begin
  4223. is_related:=true;
  4224. exit;
  4225. end;
  4226. hp:=childof;
  4227. while assigned(hp) do
  4228. begin
  4229. if hp=d then
  4230. begin
  4231. is_related:=true;
  4232. exit;
  4233. end;
  4234. hp:=hp.childof;
  4235. end;
  4236. is_related:=false;
  4237. end;
  4238. function tobjectdef.find_destructor: tprocdef;
  4239. var
  4240. objdef: tobjectdef;
  4241. begin
  4242. objdef:=self;
  4243. while assigned(objdef) do
  4244. begin
  4245. result:=objdef.find_procdef_bytype(potype_destructor);
  4246. if assigned(result) then
  4247. exit;
  4248. objdef:=objdef.childof;
  4249. end;
  4250. result:=nil;
  4251. end;
  4252. function tobjectdef.implements_any_interfaces: boolean;
  4253. begin
  4254. result := (ImplementedInterfaces.Count > 0) or
  4255. (assigned(childof) and childof.implements_any_interfaces);
  4256. end;
  4257. function tobjectdef.size : asizeint;
  4258. begin
  4259. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4260. result:=sizeof(pint)
  4261. else
  4262. result:=tObjectSymtable(symtable).datasize;
  4263. end;
  4264. function tobjectdef.alignment:shortint;
  4265. begin
  4266. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4267. alignment:=sizeof(pint)
  4268. else
  4269. alignment:=tObjectSymtable(symtable).recordalignment;
  4270. end;
  4271. function tobjectdef.vmtmethodoffset(index:longint):longint;
  4272. begin
  4273. { for offset of methods for classes, see rtl/inc/objpash.inc }
  4274. case objecttype of
  4275. odt_class:
  4276. { the +2*sizeof(pint) is size and -size }
  4277. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  4278. odt_helper,
  4279. odt_objcclass,
  4280. odt_objcprotocol:
  4281. vmtmethodoffset:=0;
  4282. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  4283. vmtmethodoffset:=index*sizeof(pint);
  4284. else
  4285. {$ifdef WITHDMT}
  4286. vmtmethodoffset:=(index+4)*sizeof(pint);
  4287. {$else WITHDMT}
  4288. vmtmethodoffset:=(index+3)*sizeof(pint);
  4289. {$endif WITHDMT}
  4290. end;
  4291. end;
  4292. function tobjectdef.vmt_mangledname : string;
  4293. begin
  4294. if not(oo_has_vmt in objectoptions) then
  4295. Message1(parser_n_object_has_no_vmt,objrealname^);
  4296. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4297. end;
  4298. function tobjectdef.needs_inittable : boolean;
  4299. begin
  4300. case objecttype of
  4301. odt_helper,
  4302. odt_class :
  4303. needs_inittable:=false;
  4304. odt_dispinterface,
  4305. odt_interfacecom:
  4306. needs_inittable:=true;
  4307. odt_interfacecorba:
  4308. needs_inittable:=is_related(interface_iunknown);
  4309. odt_object:
  4310. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  4311. odt_cppclass,
  4312. odt_objcclass,
  4313. odt_objcprotocol:
  4314. needs_inittable:=false;
  4315. else
  4316. internalerror(200108267);
  4317. end;
  4318. end;
  4319. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  4320. begin
  4321. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  4322. result:=inherited rtti_mangledname(rt)
  4323. else
  4324. begin
  4325. { necessary in case of a dynamic array of nsobject, or
  4326. if an nsobject field appears in a record that needs
  4327. init/finalisation }
  4328. if rt=initrtti then
  4329. begin
  4330. result:=voidpointertype.rtti_mangledname(rt);
  4331. exit;
  4332. end;
  4333. if not(target_info.system in systems_objc_nfabi) then
  4334. begin
  4335. result:=target_asm.labelprefix;
  4336. case objecttype of
  4337. odt_objcclass:
  4338. begin
  4339. case rt of
  4340. objcclassrtti:
  4341. if not(oo_is_classhelper in objectoptions) then
  4342. result:=result+'_OBJC_CLASS_'
  4343. else
  4344. result:=result+'_OBJC_CATEGORY_';
  4345. objcmetartti:
  4346. if not(oo_is_classhelper in objectoptions) then
  4347. result:=result+'_OBJC_METACLASS_'
  4348. else
  4349. internalerror(2009111511);
  4350. else
  4351. internalerror(2009092302);
  4352. end;
  4353. end;
  4354. odt_objcprotocol:
  4355. result:=result+'_OBJC_PROTOCOL_';
  4356. end;
  4357. end
  4358. else
  4359. begin
  4360. case objecttype of
  4361. odt_objcclass:
  4362. begin
  4363. if (oo_is_classhelper in objectoptions) and
  4364. (rt<>objcclassrtti) then
  4365. internalerror(2009111512);
  4366. case rt of
  4367. objcclassrtti:
  4368. if not(oo_is_classhelper in objectoptions) then
  4369. result:='_OBJC_CLASS_$_'
  4370. else
  4371. result:='_OBJC_$_CATEGORY_';
  4372. objcmetartti:
  4373. result:='_OBJC_METACLASS_$_';
  4374. objcclassrortti:
  4375. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4376. objcmetarortti:
  4377. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4378. else
  4379. internalerror(2009092303);
  4380. end;
  4381. end;
  4382. odt_objcprotocol:
  4383. begin
  4384. result:=lower(target_asm.labelprefix);
  4385. case rt of
  4386. objcclassrtti:
  4387. result:=result+'_OBJC_PROTOCOL_$_';
  4388. objcmetartti:
  4389. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4390. else
  4391. internalerror(2009092501);
  4392. end;
  4393. end;
  4394. end;
  4395. end;
  4396. result:=result+objextname^;
  4397. end;
  4398. end;
  4399. function tobjectdef.members_need_inittable : boolean;
  4400. begin
  4401. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4402. end;
  4403. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4404. var
  4405. ImplIntf : TImplementedInterface;
  4406. i : longint;
  4407. begin
  4408. result:=nil;
  4409. if not assigned(ImplementedInterfaces) then
  4410. exit;
  4411. for i:=0 to ImplementedInterfaces.Count-1 do
  4412. begin
  4413. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4414. if ImplIntf.intfdef=aintfdef then
  4415. begin
  4416. result:=ImplIntf;
  4417. exit;
  4418. end;
  4419. end;
  4420. end;
  4421. function tobjectdef.is_publishable : boolean;
  4422. begin
  4423. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4424. end;
  4425. function tobjectdef.get_next_dispid: longint;
  4426. begin
  4427. inc(fcurrent_dispid);
  4428. result:=fcurrent_dispid;
  4429. end;
  4430. function tobjectdef.search_enumerator_get: tprocdef;
  4431. begin
  4432. result:=inherited;
  4433. if not assigned(result) and assigned(childof) then
  4434. result:=childof.search_enumerator_get;
  4435. end;
  4436. function tobjectdef.search_enumerator_move: tprocdef;
  4437. begin
  4438. result:=inherited;
  4439. if not assigned(result) and assigned(childof) then
  4440. result:=childof.search_enumerator_move;
  4441. end;
  4442. function tobjectdef.search_enumerator_current: tsym;
  4443. begin
  4444. result:=inherited;
  4445. if not assigned(result) and assigned(childof) then
  4446. result:=childof.search_enumerator_current;
  4447. end;
  4448. procedure tobjectdef.register_created_classref_type;
  4449. begin
  4450. if not classref_created_in_current_module then
  4451. begin
  4452. classref_created_in_current_module:=true;
  4453. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4454. end;
  4455. end;
  4456. procedure tobjectdef.register_created_object_type;
  4457. begin
  4458. if not created_in_current_module then
  4459. begin
  4460. created_in_current_module:=true;
  4461. current_module.wpoinfo.addcreatedobjtype(self);
  4462. end;
  4463. end;
  4464. procedure tobjectdef.register_maybe_created_object_type;
  4465. begin
  4466. { if we know it has been created for sure, no need
  4467. to also record that it maybe can be created in
  4468. this module
  4469. }
  4470. if not (created_in_current_module) and
  4471. not (maybe_created_in_current_module) then
  4472. begin
  4473. maybe_created_in_current_module:=true;
  4474. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4475. end;
  4476. end;
  4477. procedure tobjectdef.register_vmt_call(index: longint);
  4478. begin
  4479. if (is_object(self) or is_class(self)) then
  4480. current_module.wpoinfo.addcalledvmtentry(self,index);
  4481. end;
  4482. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4483. var
  4484. def: tdef absolute data;
  4485. pd: tprocdef absolute data;
  4486. i,
  4487. paracount: longint;
  4488. begin
  4489. if (def.typ=procdef) then
  4490. begin
  4491. { add all messages also under a dummy name to the symtable in
  4492. which the objcclass/protocol/category is declared, so they can
  4493. be called via id.<name>
  4494. }
  4495. create_class_helper_for_procdef(pd,nil);
  4496. { we have to wait until now to set the mangled name because it
  4497. depends on the (possibly external) class name, which is defined
  4498. at the very end. }
  4499. if not(po_msgstr in pd.procoptions) then
  4500. begin
  4501. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4502. { recover to avoid internalerror later on }
  4503. include(pd.procoptions,po_msgstr);
  4504. pd.messageinf.str:=stringdup('MissingDeclaration');
  4505. end;
  4506. { Mangled name is already set in case this is a copy of
  4507. another type. }
  4508. if not(po_has_mangledname in pd.procoptions) then
  4509. begin
  4510. { check whether the number of formal parameters is correct,
  4511. and whether they have valid Objective-C types }
  4512. paracount:=0;
  4513. for i:=1 to length(pd.messageinf.str^) do
  4514. if pd.messageinf.str^[i]=':' then
  4515. inc(paracount);
  4516. for i:=0 to pd.paras.count-1 do
  4517. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4518. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4519. dec(paracount);
  4520. if (paracount<>0) then
  4521. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4522. pd.setmangledname(pd.objcmangledname);
  4523. end
  4524. else
  4525. { all checks already done }
  4526. exit;
  4527. if not(oo_is_external in pd.struct.objectoptions) then
  4528. begin
  4529. if (po_varargs in pd.procoptions) then
  4530. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4531. else
  4532. begin
  4533. { check for "array of const" parameters }
  4534. for i:=0 to pd.parast.symlist.count-1 do
  4535. begin
  4536. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4537. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4538. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4539. end;
  4540. end;
  4541. end;
  4542. end;
  4543. end;
  4544. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4545. var
  4546. sym: tsym absolute data;
  4547. begin
  4548. if (sym.typ=fieldvarsym) and
  4549. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4550. sym.IncRefCount;
  4551. end;
  4552. procedure tobjectdef.finish_objc_data;
  4553. begin
  4554. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4555. if (oo_is_external in objectoptions) then
  4556. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4557. end;
  4558. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4559. var
  4560. sym: tabstractvarsym absolute data;
  4561. res: pboolean absolute arg;
  4562. founderrordef: tdef;
  4563. begin
  4564. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4565. exit;
  4566. if (sym.typ=paravarsym) and
  4567. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4568. is_array_of_const(tparavarsym(sym).vardef)) then
  4569. exit;
  4570. if not objcchecktype(sym.vardef,founderrordef) then
  4571. begin
  4572. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4573. res^:=false;
  4574. end;
  4575. end;
  4576. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4577. var
  4578. def: tdef absolute data;
  4579. res: pboolean absolute arg;
  4580. founderrordef: tdef;
  4581. begin
  4582. if (def.typ<>procdef) then
  4583. exit;
  4584. { check parameter types for validity }
  4585. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4586. { check the result type for validity }
  4587. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4588. begin
  4589. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4590. res^:=false;
  4591. end;
  4592. end;
  4593. function tobjectdef.check_objc_types: boolean;
  4594. begin
  4595. { done in separate step from finish_objc_data, because when
  4596. finish_objc_data is called, not all forwarddefs have been resolved
  4597. yet and we need to know all types here }
  4598. result:=true;
  4599. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4600. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4601. end;
  4602. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4603. var
  4604. def: tdef absolute data;
  4605. pd: tprocdef absolute data;
  4606. begin
  4607. if (def.typ=procdef) then
  4608. begin
  4609. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4610. if (oo_is_external in pd.struct.objectoptions) then
  4611. begin
  4612. { copied from psub.read_proc }
  4613. if assigned(tobjectdef(pd.struct).import_lib) then
  4614. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,0,false,false)
  4615. else
  4616. begin
  4617. { add import name to external list for DLL scanning }
  4618. if tf_has_dllscanner in target_info.flags then
  4619. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4620. end;
  4621. end;
  4622. end;
  4623. end;
  4624. procedure tobjectdef.finish_cpp_data;
  4625. begin
  4626. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4627. end;
  4628. {****************************************************************************
  4629. TImplementedInterface
  4630. ****************************************************************************}
  4631. constructor TImplementedInterface.create(aintf: tobjectdef);
  4632. begin
  4633. inherited create;
  4634. intfdef:=aintf;
  4635. IOffset:=-1;
  4636. IType:=etStandard;
  4637. NameMappings:=nil;
  4638. procdefs:=nil;
  4639. end;
  4640. constructor TImplementedInterface.create_deref(d:tderef);
  4641. begin
  4642. inherited create;
  4643. intfdef:=nil;
  4644. intfdefderef:=d;
  4645. IOffset:=-1;
  4646. IType:=etStandard;
  4647. NameMappings:=nil;
  4648. procdefs:=nil;
  4649. end;
  4650. destructor TImplementedInterface.destroy;
  4651. var
  4652. i : longint;
  4653. mappedname : pshortstring;
  4654. begin
  4655. if assigned(NameMappings) then
  4656. begin
  4657. for i:=0 to NameMappings.Count-1 do
  4658. begin
  4659. mappedname:=pshortstring(NameMappings[i]);
  4660. stringdispose(mappedname);
  4661. end;
  4662. NameMappings.free;
  4663. NameMappings:=nil;
  4664. end;
  4665. if assigned(procdefs) then
  4666. begin
  4667. procdefs.free;
  4668. procdefs:=nil;
  4669. end;
  4670. inherited destroy;
  4671. end;
  4672. procedure TImplementedInterface.buildderef;
  4673. begin
  4674. intfdefderef.build(intfdef);
  4675. end;
  4676. procedure TImplementedInterface.deref;
  4677. begin
  4678. intfdef:=tobjectdef(intfdefderef.resolve);
  4679. end;
  4680. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4681. begin
  4682. if not assigned(NameMappings) then
  4683. NameMappings:=TFPHashList.Create;
  4684. NameMappings.Add(origname,stringdup(newname));
  4685. end;
  4686. function TImplementedInterface.GetMapping(const origname: string):string;
  4687. var
  4688. mappedname : pshortstring;
  4689. begin
  4690. result:='';
  4691. if not assigned(NameMappings) then
  4692. exit;
  4693. mappedname:=PShortstring(NameMappings.Find(origname));
  4694. if assigned(mappedname) then
  4695. result:=mappedname^;
  4696. end;
  4697. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4698. begin
  4699. if not assigned(procdefs) then
  4700. procdefs:=TFPObjectList.Create(false);
  4701. { duplicate entries must be stored, because multiple }
  4702. { interfaces can declare methods with the same name }
  4703. { and all of these get their own VMT entry }
  4704. procdefs.Add(pd);
  4705. end;
  4706. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4707. var
  4708. i : longint;
  4709. begin
  4710. result:=false;
  4711. { interfaces being implemented through delegation are not mergable (FK) }
  4712. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4713. exit;
  4714. weight:=0;
  4715. { empty interface is mergeable }
  4716. if ProcDefs.Count=0 then
  4717. begin
  4718. result:=true;
  4719. exit;
  4720. end;
  4721. { The interface to merge must at least the number of
  4722. procedures of this interface }
  4723. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4724. exit;
  4725. for i:=0 to ProcDefs.Count-1 do
  4726. begin
  4727. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4728. exit;
  4729. end;
  4730. weight:=ProcDefs.Count;
  4731. result:=true;
  4732. end;
  4733. function TImplementedInterface.getcopy:TImplementedInterface;
  4734. begin
  4735. Result:=TImplementedInterface.Create(nil);
  4736. {$warning: this is completely wrong on so many levels...}
  4737. { 1) the procdefs list will be freed once for each copy
  4738. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4739. 3) idem for the name mappings
  4740. }
  4741. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4742. end;
  4743. {****************************************************************************
  4744. TFORWARDDEF
  4745. ****************************************************************************}
  4746. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  4747. begin
  4748. inherited create(forwarddef);
  4749. tosymname:=stringdup(s);
  4750. forwardpos:=pos;
  4751. end;
  4752. function tforwarddef.GetTypeName:string;
  4753. begin
  4754. GetTypeName:='unresolved forward to '+tosymname^;
  4755. end;
  4756. destructor tforwarddef.destroy;
  4757. begin
  4758. stringdispose(tosymname);
  4759. inherited destroy;
  4760. end;
  4761. function tforwarddef.getcopy:tstoreddef;
  4762. begin
  4763. result:=tforwarddef.create(tosymname^, forwardpos);
  4764. end;
  4765. {****************************************************************************
  4766. TUNDEFINEDDEF
  4767. ****************************************************************************}
  4768. constructor tundefineddef.create;
  4769. begin
  4770. inherited create(undefineddef);
  4771. end;
  4772. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4773. begin
  4774. inherited ppuload(undefineddef,ppufile);
  4775. end;
  4776. function tundefineddef.GetTypeName:string;
  4777. begin
  4778. GetTypeName:='<undefined type>';
  4779. end;
  4780. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4781. begin
  4782. inherited ppuwrite(ppufile);
  4783. ppufile.writeentry(ibundefineddef);
  4784. end;
  4785. {****************************************************************************
  4786. TERRORDEF
  4787. ****************************************************************************}
  4788. constructor terrordef.create;
  4789. begin
  4790. inherited create(errordef);
  4791. { prevent consecutive faults }
  4792. savesize:=1;
  4793. end;
  4794. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4795. begin
  4796. { Can't write errordefs to ppu }
  4797. internalerror(200411063);
  4798. end;
  4799. function terrordef.GetTypeName:string;
  4800. begin
  4801. GetTypeName:='<erroneous type>';
  4802. end;
  4803. function terrordef.getmangledparaname:string;
  4804. begin
  4805. getmangledparaname:='error';
  4806. end;
  4807. {****************************************************************************
  4808. Definition Helpers
  4809. ****************************************************************************}
  4810. function is_interfacecom(def: tdef): boolean;
  4811. begin
  4812. is_interfacecom:=
  4813. assigned(def) and
  4814. (def.typ=objectdef) and
  4815. (tobjectdef(def).objecttype=odt_interfacecom);
  4816. end;
  4817. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  4818. begin
  4819. is_interfacecom_or_dispinterface:=
  4820. assigned(def) and
  4821. (def.typ=objectdef) and
  4822. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  4823. end;
  4824. function is_interfacecorba(def: tdef): boolean;
  4825. begin
  4826. is_interfacecorba:=
  4827. assigned(def) and
  4828. (def.typ=objectdef) and
  4829. (tobjectdef(def).objecttype=odt_interfacecorba);
  4830. end;
  4831. function is_interface(def: tdef): boolean;
  4832. begin
  4833. is_interface:=
  4834. assigned(def) and
  4835. (def.typ=objectdef) and
  4836. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4837. end;
  4838. function is_dispinterface(def: tdef): boolean;
  4839. begin
  4840. result:=
  4841. assigned(def) and
  4842. (def.typ=objectdef) and
  4843. (tobjectdef(def).objecttype=odt_dispinterface);
  4844. end;
  4845. function is_class(def: tdef): boolean;
  4846. begin
  4847. is_class:=
  4848. assigned(def) and
  4849. (def.typ=objectdef) and
  4850. (tobjectdef(def).objecttype=odt_class);
  4851. end;
  4852. function is_object(def: tdef): boolean;
  4853. begin
  4854. is_object:=
  4855. assigned(def) and
  4856. (def.typ=objectdef) and
  4857. (tobjectdef(def).objecttype=odt_object);
  4858. end;
  4859. function is_cppclass(def: tdef): boolean;
  4860. begin
  4861. is_cppclass:=
  4862. assigned(def) and
  4863. (def.typ=objectdef) and
  4864. (tobjectdef(def).objecttype=odt_cppclass);
  4865. end;
  4866. function is_objcclass(def: tdef): boolean;
  4867. begin
  4868. is_objcclass:=
  4869. assigned(def) and
  4870. (def.typ=objectdef) and
  4871. (tobjectdef(def).objecttype=odt_objcclass);
  4872. end;
  4873. function is_objectpascal_helper(def: tdef): boolean;
  4874. begin
  4875. result:=
  4876. assigned(def) and
  4877. (def.typ=objectdef) and
  4878. (tobjectdef(def).objecttype=odt_helper);
  4879. end;
  4880. function is_objcclassref(def: tdef): boolean;
  4881. begin
  4882. is_objcclassref:=
  4883. assigned(def) and
  4884. (def.typ=classrefdef) and
  4885. is_objcclass(tclassrefdef(def).pointeddef);
  4886. end;
  4887. function is_objcprotocol(def: tdef): boolean;
  4888. begin
  4889. result:=
  4890. assigned(def) and
  4891. (def.typ=objectdef) and
  4892. (tobjectdef(def).objecttype=odt_objcprotocol);
  4893. end;
  4894. function is_objccategory(def: tdef): boolean;
  4895. begin
  4896. result:=
  4897. assigned(def) and
  4898. (def.typ=objectdef) and
  4899. { if used as a forward type }
  4900. ((tobjectdef(def).objecttype=odt_objccategory) or
  4901. { if used as after it has been resolved }
  4902. ((tobjectdef(def).objecttype=odt_objcclass) and
  4903. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4904. end;
  4905. function is_objc_class_or_protocol(def: tdef): boolean;
  4906. begin
  4907. result:=
  4908. assigned(def) and
  4909. (def.typ=objectdef) and
  4910. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4911. end;
  4912. function is_objc_protocol_or_category(def: tdef): boolean;
  4913. begin
  4914. result:=
  4915. assigned(def) and
  4916. (def.typ=objectdef) and
  4917. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4918. ((tobjectdef(def).objecttype = odt_objcclass) and
  4919. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4920. end;
  4921. function is_classhelper(def: tdef): boolean;
  4922. begin
  4923. result:=
  4924. is_objectpascal_helper(def) or
  4925. is_objccategory(def);
  4926. end;
  4927. function is_class_or_interface(def: tdef): boolean;
  4928. begin
  4929. result:=
  4930. assigned(def) and
  4931. (def.typ=objectdef) and
  4932. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4933. end;
  4934. function is_class_or_interface_or_objc(def: tdef): boolean;
  4935. begin
  4936. result:=
  4937. assigned(def) and
  4938. (def.typ=objectdef) and
  4939. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4940. end;
  4941. function is_class_or_interface_or_object(def: tdef): boolean;
  4942. begin
  4943. result:=
  4944. assigned(def) and
  4945. (def.typ=objectdef) and
  4946. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4947. end;
  4948. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4949. begin
  4950. result:=
  4951. assigned(def) and
  4952. (def.typ=objectdef) and
  4953. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4954. end;
  4955. function is_implicit_pointer_object_type(def: tdef): boolean;
  4956. begin
  4957. result:=
  4958. assigned(def) and
  4959. (def.typ=objectdef) and
  4960. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4961. end;
  4962. function is_class_or_object(def: tdef): boolean;
  4963. begin
  4964. result:=
  4965. assigned(def) and
  4966. (def.typ=objectdef) and
  4967. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4968. end;
  4969. function is_record(def: tdef): boolean;
  4970. begin
  4971. result:=
  4972. assigned(def) and
  4973. (def.typ=recorddef);
  4974. end;
  4975. procedure loadobjctypes;
  4976. begin
  4977. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4978. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4979. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4980. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4981. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4982. end;
  4983. procedure maybeloadcocoatypes;
  4984. var
  4985. tsym: ttypesym;
  4986. begin
  4987. if assigned(objc_fastenumeration) then
  4988. exit;
  4989. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4990. if assigned(tsym) then
  4991. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4992. else
  4993. objc_fastenumeration:=nil;
  4994. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  4995. if assigned(tsym) then
  4996. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  4997. else
  4998. objc_fastenumerationstate:=nil;
  4999. end;
  5000. function use_vectorfpu(def : tdef) : boolean;
  5001. begin
  5002. {$ifdef x86}
  5003. {$define use_vectorfpuimplemented}
  5004. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  5005. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  5006. {$endif x86}
  5007. {$ifdef arm}
  5008. {$define use_vectorfpuimplemented}
  5009. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  5010. {$endif arm}
  5011. {$ifndef use_vectorfpuimplemented}
  5012. use_vectorfpu:=false;
  5013. {$endif}
  5014. end;
  5015. end.