symdef.pas 184 KB

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