symdef.pas 184 KB

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