symdef.pas 179 KB

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