symdef.pas 183 KB

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