scanner.pas 218 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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 scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. symtype,symdef,symsym,
  24. verbose,comphook,
  25. finput,
  26. widestr;
  27. const
  28. max_include_nesting=32;
  29. max_macro_nesting=16;
  30. preprocbufsize=32*1024;
  31. { when parsing an internally generated macro, if an identifier is
  32. prefixed with this constant then it will always be interpreted as a
  33. unit name (to avoid clashes with user-specified parameter or field
  34. names duplicated in internally generated code) }
  35. internal_macro_escape_unit_namespace_name = #1;
  36. internal_macro_escape_begin = internal_macro_escape_unit_namespace_name;
  37. internal_macro_escape_end = internal_macro_escape_unit_namespace_name;
  38. type
  39. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  40. tscannerfile = class;
  41. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  42. tpreprocstack = class
  43. typ,
  44. { stores the preproctyp of the last (else)if(ndef) directive
  45. so we can check properly for ifend when legacyifend is on }
  46. iftyp : preproctyp;
  47. accept : boolean;
  48. next : tpreprocstack;
  49. name : TIDString;
  50. line_nb : longint;
  51. fileindex : longint;
  52. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  53. end;
  54. tdirectiveproc=procedure;
  55. tdirectiveitem = class(TFPHashObject)
  56. public
  57. is_conditional : boolean;
  58. proc : tdirectiveproc;
  59. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  60. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  61. end;
  62. // stack for replay buffers
  63. treplaystack = class
  64. token : ttoken;
  65. idtoken : ttoken;
  66. orgpattern,
  67. pattern : string;
  68. cstringpattern: ansistring;
  69. patternw : pcompilerwidestring;
  70. settings : tsettings;
  71. tokenbuf : tdynamicarray;
  72. tokenbuf_needs_swapping : boolean;
  73. next : treplaystack;
  74. constructor Create(atoken: ttoken;aidtoken:ttoken;
  75. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  76. apatternw:pcompilerwidestring;asettings:tsettings;
  77. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  78. destructor destroy;override;
  79. end;
  80. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  81. tspecialgenerictoken =
  82. (ST_LOADSETTINGS,
  83. ST_LINE,
  84. ST_COLUMN,
  85. ST_FILEINDEX,
  86. ST_LOADMESSAGES);
  87. { tscannerfile }
  88. tscannerfile = class
  89. private
  90. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  91. procedure cachenexttokenpos;
  92. procedure setnexttoken;
  93. procedure savetokenpos;
  94. procedure restoretokenpos;
  95. procedure writetoken(t: ttoken);
  96. function readtoken : ttoken;
  97. public
  98. inputfile : tinputfile; { current inputfile list }
  99. inputfilecount : longint;
  100. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  101. private
  102. hidden_inputbuffer, { input buffer }
  103. hidden_inputpointer : pchar;
  104. { Gets char at inputpointer with offset,
  105. after checking that it doesn't overflow inputfile.bufsize }
  106. function get_inputpointer_char(offset : longint = 0) : char;
  107. procedure inc_inputpointer(amount : longint = 1);
  108. procedure dec_inputpointer;
  109. public
  110. {$else not CHECK_INPUTPOINTER_LIMITS}
  111. inputbuffer, { input buffer }
  112. inputpointer : pchar;
  113. {$endif}
  114. inputstart : longint;
  115. line_no, { line }
  116. lastlinepos : longint;
  117. lasttokenpos,
  118. nexttokenpos : longint; { token }
  119. lasttoken,
  120. nexttoken : ttoken;
  121. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  122. oldcurrent_filepos,
  123. oldcurrent_tokenpos : tfileposinfo;
  124. replaytokenbuf,
  125. recordtokenbuf : tdynamicarray;
  126. { last settings we stored }
  127. last_settings : tsettings;
  128. last_message : pmessagestaterecord;
  129. { last filepos we stored }
  130. last_filepos,
  131. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  132. next_filepos : tfileposinfo;
  133. { current macro nesting depth }
  134. macro_nesting_depth,
  135. comment_level,
  136. yylexcount : longint;
  137. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  138. preprocstack : tpreprocstack;
  139. replaystack : treplaystack;
  140. preproc_pattern : string;
  141. preproc_token : ttoken;
  142. { true, if we are parsing preprocessor expressions }
  143. in_preproc_comp_expr : boolean;
  144. { true if tokens must be converted to opposite endianess}
  145. change_endian_for_replay : boolean;
  146. constructor Create(const fn:string; is_macro: boolean = false);
  147. destructor Destroy;override;
  148. { File buffer things }
  149. function openinputfile:boolean;
  150. procedure closeinputfile;
  151. function tempopeninputfile:boolean;
  152. procedure tempcloseinputfile;
  153. procedure saveinputfile;
  154. procedure restoreinputfile;
  155. procedure firstfile;
  156. procedure nextfile;
  157. procedure addfile(hp:tinputfile);
  158. procedure reload;
  159. { replaces current token with the text in p }
  160. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  161. { Scanner things }
  162. procedure gettokenpos;
  163. procedure inc_comment_level;
  164. procedure dec_comment_level;
  165. procedure illegal_char(c:char);
  166. procedure end_of_file;
  167. procedure checkpreprocstack;
  168. procedure poppreprocstack;
  169. procedure ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  170. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  171. procedure elsepreprocstack;
  172. procedure popreplaystack;
  173. function replay_stack_depth:longint;
  174. procedure handleconditional(p:tdirectiveitem);
  175. procedure handledirectives;
  176. procedure linebreak;
  177. procedure recordtoken;
  178. procedure startrecordtokens(buf:tdynamicarray);
  179. procedure stoprecordtokens;
  180. function is_recording_tokens:boolean;
  181. procedure replaytoken;
  182. procedure startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  183. { bit length asizeint is target depend }
  184. procedure tokenwritesizeint(val : asizeint);
  185. procedure tokenwritelongint(val : longint);
  186. procedure tokenwritelongword(val : longword);
  187. procedure tokenwriteword(val : word);
  188. procedure tokenwriteshortint(val : shortint);
  189. procedure tokenwriteset(var b;size : longint);
  190. procedure tokenwriteenum(var b;size : longint);
  191. function tokenreadsizeint : asizeint;
  192. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  193. { longword/longint are 32 bits on all targets }
  194. { word/smallint are 16-bits on all targest }
  195. function tokenreadlongword : longword;
  196. function tokenreadword : word;
  197. function tokenreadlongint : longint;
  198. function tokenreadsmallint : smallint;
  199. { short int is one a signed byte }
  200. function tokenreadshortint : shortint;
  201. function tokenreadbyte : byte;
  202. { This one takes the set size as an parameter }
  203. procedure tokenreadset(var b;size : longint);
  204. function tokenreadenum(size : longint) : longword;
  205. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  206. procedure readchar;
  207. procedure readstring;
  208. procedure readnumber;
  209. function readid:string;
  210. function readval:longint;
  211. function readval64:int64;
  212. function readcomment(include_special_char: boolean = false):string;
  213. function readquotedstring:string;
  214. function readstate:char;
  215. function readoptionalstate(fallback:char):char;
  216. function readstatedefault:char;
  217. procedure skipspace;
  218. procedure skipuntildirective;
  219. procedure skipcomment(read_first_char:boolean);
  220. procedure skipdelphicomment;
  221. procedure skipoldtpcomment(read_first_char:boolean);
  222. procedure readtoken(allowrecordtoken:boolean);
  223. function readpreproc:ttoken;
  224. function readpreprocint(var value:int64;const place:string):boolean;
  225. function readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  226. function asmgetchar:char;
  227. {$ifdef EXTDEBUG}
  228. function DumpPointer : string;
  229. {$endif EXTDEBUG}
  230. end;
  231. {$ifdef PREPROCWRITE}
  232. tpreprocfile=class
  233. f : text;
  234. buf : pointer;
  235. spacefound,
  236. eolfound : boolean;
  237. constructor create(const fn:string);
  238. destructor destroy; override;
  239. procedure Add(const s:string);
  240. procedure AddSpace;
  241. end;
  242. {$endif PREPROCWRITE}
  243. var
  244. { read strings }
  245. c : char;
  246. orgpattern,
  247. pattern : string;
  248. cstringpattern : ansistring;
  249. patternw : pcompilerwidestring;
  250. { token }
  251. token, { current token being parsed }
  252. idtoken : ttoken; { holds the token if the pattern is a known word }
  253. current_commentstyle : tcommentstyle; { needed to use read_comment from directives }
  254. {$ifdef PREPROCWRITE}
  255. preprocfile : tpreprocfile; { used with only preprocessing }
  256. {$endif PREPROCWRITE}
  257. type
  258. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  259. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  260. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  261. procedure InitScanner;
  262. procedure DoneScanner;
  263. function current_scanner : tscannerfile; { current scanner in use }
  264. procedure set_current_scanner(avalue : tscannerfile); { current scanner in use }
  265. { To be called when the language mode is finally determined }
  266. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  267. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  268. procedure SetAppType(NewAppType:tapptype);
  269. var
  270. onfreescanner : procedure(s : tscannerfile) = nil;
  271. implementation
  272. uses
  273. SysUtils,
  274. cutils,cfileutl,
  275. systems,
  276. switches,
  277. symbase,symtable,symconst,defutil,defcmp,node,
  278. { This is needed for tcputype }
  279. cpuinfo,
  280. fmodule,fppu,
  281. { this is needed for $I %CURRENTROUTINE%}
  282. procinfo;
  283. var
  284. { dictionaries with the supported directives }
  285. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  286. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  287. {
  288. By default the current_scanner is current_module.scanner.
  289. set_current_scanner sets the _temp_scanner variable.
  290. If _temp_scanner is set, it is returned as the current scanner
  291. }
  292. _temp_scanner : tscannerfile;
  293. function current_scanner : tscannerfile; { current scanner in use }
  294. begin
  295. Result:=_temp_scanner;
  296. if result<>nil then
  297. exit;
  298. if assigned(current_module) then
  299. Result:=Tscannerfile(current_module.scanner)
  300. else
  301. Result:=Nil;
  302. end;
  303. {*****************************************************************************
  304. Helper routines
  305. *****************************************************************************}
  306. const
  307. { use any special name that is an invalid file name to avoid problems }
  308. preprocstring : array [preproctyp] of string[7]
  309. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  310. function is_keyword(const s:string):boolean;
  311. var
  312. low,high,mid : longint;
  313. begin
  314. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  315. not (s[1] in ['a'..'z','A'..'Z']) then
  316. begin
  317. is_keyword:=false;
  318. exit;
  319. end;
  320. low:=ord(tokenidx^[length(s),s[1]].first);
  321. high:=ord(tokenidx^[length(s),s[1]].last);
  322. while low<high do
  323. begin
  324. mid:=(high+low+1) shr 1;
  325. if pattern<tokeninfo^[ttoken(mid)].str then
  326. high:=mid-1
  327. else
  328. low:=mid;
  329. end;
  330. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  331. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  332. end;
  333. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  334. begin
  335. { turn ansi/unicodestrings on by default ? (only change when this
  336. particular setting is changed, so that a random modeswitch won't
  337. change the state of $h+/$h-) }
  338. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  339. begin
  340. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  341. begin
  342. { can't have both ansistring and unicodestring as default }
  343. if switch=m_default_ansistring then
  344. begin
  345. exclude(current_settings.modeswitches,m_default_unicodestring);
  346. if changeinit then
  347. exclude(init_settings.modeswitches,m_default_unicodestring);
  348. end
  349. else if switch=m_default_unicodestring then
  350. begin
  351. exclude(current_settings.modeswitches,m_default_ansistring);
  352. if changeinit then
  353. exclude(init_settings.modeswitches,m_default_ansistring);
  354. end;
  355. { enable $h+ }
  356. include(current_settings.localswitches,cs_refcountedstrings);
  357. if changeinit then
  358. include(init_settings.localswitches,cs_refcountedstrings);
  359. if m_default_unicodestring in current_settings.modeswitches then
  360. begin
  361. def_system_macro('FPC_UNICODESTRINGS');
  362. def_system_macro('UNICODE');
  363. end;
  364. end
  365. else
  366. begin
  367. exclude(current_settings.localswitches,cs_refcountedstrings);
  368. if changeinit then
  369. exclude(init_settings.localswitches,cs_refcountedstrings);
  370. undef_system_macro('FPC_UNICODESTRINGS');
  371. undef_system_macro('UNICODE');
  372. end;
  373. end;
  374. { turn inline on by default ? }
  375. if switch in [m_none,m_default_inline] then
  376. begin
  377. if (m_default_inline in current_settings.modeswitches) then
  378. begin
  379. include(current_settings.localswitches,cs_do_inline);
  380. if changeinit then
  381. include(init_settings.localswitches,cs_do_inline);
  382. end
  383. else
  384. begin
  385. exclude(current_settings.localswitches,cs_do_inline);
  386. if changeinit then
  387. exclude(init_settings.localswitches,cs_do_inline);
  388. end;
  389. end;
  390. { turn on system codepage by default }
  391. if switch in [m_none,m_systemcodepage] then
  392. begin
  393. { both m_systemcodepage and specifying a code page via -FcXXX or
  394. "$codepage XXX" change current_settings.sourcecodepage. If
  395. we used -FcXXX and then have a sourcefile with "$mode objfpc",
  396. this routine will be called to disable m_systemcodepage (to ensure
  397. it's off in case it would have been set on the command line, or
  398. by a previous mode(switch).
  399. In that case, we have to ensure that we don't overwrite
  400. current_settings.sourcecodepage, as that would cancel out the
  401. -FcXXX. This is why we use two separate module switches
  402. (cs_explicit_codepage and cs_system_codepage) for the same setting
  403. (current_settings.sourcecodepage)
  404. }
  405. if m_systemcodepage in current_settings.modeswitches then
  406. begin
  407. { m_systemcodepage gets enabled -> disable any -FcXXX and
  408. "codepage XXX" settings (exclude cs_explicit_codepage), and
  409. overwrite the sourcecode page }
  410. current_settings.sourcecodepage:=DefaultSystemCodePage;
  411. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  412. begin
  413. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  414. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  415. end;
  416. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  417. include(current_settings.moduleswitches,cs_system_codepage);
  418. if changeinit then
  419. begin
  420. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  421. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  422. include(init_settings.moduleswitches,cs_system_codepage);
  423. end;
  424. end
  425. else
  426. begin
  427. { m_systemcodepage gets disabled -> reset sourcecodepage only if
  428. cs_explicit_codepage is not set (it may be set in the scenario
  429. where -FcXXX was passed on the command line and then "$mode
  430. fpc" is used, because then the caller of this routine will
  431. set the "$mode fpc" modeswitches (which don't include
  432. m_systemcodepage) and call this routine with m_none).
  433. Or it can happen if -FcXXX was passed, and the sourcefile
  434. contains "$modeswitch systemcodepage-" statement.
  435. Since we unset cs_system_codepage if m_systemcodepage gets
  436. activated, we will revert to the default code page if you
  437. set a source file code page, then enable the systemcode page
  438. and finally disable it again. We don't keep a stack of
  439. settings, by design. The only thing we have to ensure is that
  440. disabling m_systemcodepage if it wasn't on in the first place
  441. doesn't overwrite the sourcecodepage }
  442. exclude(current_settings.moduleswitches,cs_system_codepage);
  443. if not(cs_explicit_codepage in current_settings.moduleswitches) then
  444. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  445. if changeinit then
  446. begin
  447. exclude(init_settings.moduleswitches,cs_system_codepage);
  448. if not(cs_explicit_codepage in init_settings.moduleswitches) then
  449. init_settings.sourcecodepage:=default_settings.sourcecodepage;
  450. end;
  451. end;
  452. end;
  453. {$ifdef i8086}
  454. { enable cs_force_far_calls when m_nested_procvars is enabled }
  455. if switch=m_nested_procvars then
  456. begin
  457. include(current_settings.localswitches,cs_force_far_calls);
  458. if changeinit then
  459. include(init_settings.localswitches,cs_force_far_calls);
  460. end;
  461. {$endif i8086}
  462. end;
  463. procedure set_current_scanner(avalue: tscannerfile);
  464. begin
  465. _temp_scanner:=avalue;
  466. end;
  467. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  468. var
  469. b : boolean;
  470. oldmodeswitches : tmodeswitches;
  471. begin
  472. oldmodeswitches:=current_settings.modeswitches;
  473. b:=true;
  474. if s='DEFAULT' then
  475. current_settings.modeswitches:=fpcmodeswitches
  476. else
  477. if s='DELPHI' then
  478. current_settings.modeswitches:=delphimodeswitches
  479. else
  480. if s='DELPHIUNICODE' then
  481. current_settings.modeswitches:=delphiunicodemodeswitches
  482. else
  483. if s='TP' then
  484. current_settings.modeswitches:=tpmodeswitches
  485. else
  486. if s='FPC' then begin
  487. current_settings.modeswitches:=fpcmodeswitches;
  488. { TODO: enable this for 2.3/2.9 }
  489. // include(current_settings.localswitches, cs_typed_addresses);
  490. end else
  491. if s='OBJFPC' then begin
  492. current_settings.modeswitches:=objfpcmodeswitches;
  493. { TODO: enable this for 2.3/2.9 }
  494. // include(current_settings.localswitches, cs_typed_addresses);
  495. end
  496. {$ifdef gpc_mode}
  497. else if s='GPC' then
  498. current_settings.modeswitches:=gpcmodeswitches
  499. {$endif}
  500. else
  501. if s='MACPAS' then
  502. current_settings.modeswitches:=macmodeswitches
  503. else
  504. if s='ISO' then
  505. current_settings.modeswitches:=isomodeswitches
  506. else
  507. if s='EXTENDEDPASCAL' then
  508. current_settings.modeswitches:=extpasmodeswitches
  509. else
  510. b:=false;
  511. {$ifdef jvm}
  512. { enable final fields by default for the JVM targets }
  513. include(current_settings.modeswitches,m_final_fields);
  514. {$endif jvm}
  515. if b and changeInit then
  516. init_settings.modeswitches := current_settings.modeswitches;
  517. if b then
  518. begin
  519. { resolve all postponed switch changes }
  520. flushpendingswitchesstate;
  521. HandleModeSwitches(m_none,changeinit);
  522. { turn on bitpacking and case checking for mode macpas and iso pascal,
  523. as well as extended pascal }
  524. if ([m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  525. begin
  526. include(current_settings.localswitches,cs_bitpacking);
  527. include(current_settings.localswitches,cs_check_all_case_coverage);
  528. if changeinit then
  529. begin
  530. include(init_settings.localswitches,cs_bitpacking);
  531. include(init_settings.localswitches,cs_check_all_case_coverage);
  532. end;
  533. end;
  534. { support goto/label by default in delphi/tp7/mac/iso/extpas modes }
  535. if ([m_delphi,m_tp7,m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  536. begin
  537. include(current_settings.moduleswitches,cs_support_goto);
  538. if changeinit then
  539. include(init_settings.moduleswitches,cs_support_goto);
  540. end;
  541. { support pointer math by default in fpc/objfpc modes }
  542. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  543. begin
  544. include(current_settings.localswitches,cs_pointermath);
  545. if changeinit then
  546. include(init_settings.localswitches,cs_pointermath);
  547. end
  548. else
  549. begin
  550. exclude(current_settings.localswitches,cs_pointermath);
  551. if changeinit then
  552. exclude(init_settings.localswitches,cs_pointermath);
  553. end;
  554. { Default enum and set packing for delphi/tp7 }
  555. if (m_tp7 in current_settings.modeswitches) or
  556. (m_delphi in current_settings.modeswitches) then
  557. begin
  558. current_settings.packenum:=1;
  559. current_settings.setalloc:=1;
  560. end
  561. else if (m_mac in current_settings.modeswitches) then
  562. begin
  563. { compatible with Metrowerks Pascal }
  564. current_settings.packenum:=2;
  565. current_settings.setalloc:=default_settings.setalloc;
  566. end
  567. else
  568. begin
  569. current_settings.packenum:=default_settings.packenum;
  570. current_settings.setalloc:=default_settings.setalloc;
  571. end;
  572. if changeinit then
  573. begin
  574. init_settings.packenum:=current_settings.packenum;
  575. init_settings.setalloc:=current_settings.setalloc;
  576. end;
  577. {$if defined(i386) or defined(i8086)}
  578. { Default to intel assembler for delphi/tp7 on i386/i8086 }
  579. if (m_delphi in current_settings.modeswitches) or
  580. (m_tp7 in current_settings.modeswitches) then
  581. begin
  582. {$ifdef i8086}
  583. current_settings.asmmode:=asmmode_i8086_intel;
  584. {$else i8086}
  585. current_settings.asmmode:=asmmode_i386_intel;
  586. {$endif i8086}
  587. if changeinit then
  588. init_settings.asmmode:=current_settings.asmmode;
  589. end;
  590. {$endif i386 or i8086}
  591. { Exception support explicitly turned on (mainly for macpas, to }
  592. { compensate for lack of interprocedural goto support) }
  593. if (cs_support_exceptions in current_settings.globalswitches) then
  594. include(current_settings.modeswitches,m_except);
  595. { Default strict string var checking in TP/Delphi modes }
  596. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  597. begin
  598. include(current_settings.localswitches,cs_strict_var_strings);
  599. if changeinit then
  600. include(init_settings.localswitches,cs_strict_var_strings);
  601. end;
  602. { in delphi mode, excess precision and open strings are by default on }
  603. if ([m_delphi] * current_settings.modeswitches <> []) then
  604. begin
  605. include(current_settings.localswitches,cs_excessprecision);
  606. include(current_settings.localswitches,cs_openstring);
  607. if changeinit then
  608. begin
  609. include(init_settings.localswitches,cs_excessprecision);
  610. include(init_settings.localswitches,cs_openstring);
  611. end;
  612. end;
  613. {$ifdef i8086}
  614. { Do not force far calls in the TP mode by default, force it in other modes }
  615. if (m_tp7 in current_settings.modeswitches) then
  616. begin
  617. exclude(current_settings.localswitches,cs_force_far_calls);
  618. if changeinit then
  619. exclude(init_settings.localswitches,cs_force_far_calls);
  620. end
  621. else
  622. begin
  623. include(current_settings.localswitches,cs_force_far_calls);
  624. if changeinit then
  625. include(init_settings.localswitches,cs_force_far_calls);
  626. end;
  627. {$endif i8086}
  628. { Undefine old symbol }
  629. if (m_delphi in oldmodeswitches) then
  630. undef_system_macro('FPC_DELPHI')
  631. else if (m_tp7 in oldmodeswitches) then
  632. undef_system_macro('FPC_TP')
  633. else if (m_objfpc in oldmodeswitches) then
  634. undef_system_macro('FPC_OBJFPC')
  635. {$ifdef gpc_mode}
  636. else if (m_gpc in oldmodeswitches) then
  637. undef_system_macro('FPC_GPC')
  638. {$endif}
  639. else if (m_mac in oldmodeswitches) then
  640. undef_system_macro('FPC_MACPAS')
  641. else if (m_iso in oldmodeswitches) then
  642. undef_system_macro('FPC_ISO')
  643. else if (m_extpas in oldmodeswitches) then
  644. undef_system_macro('FPC_EXTENDEDPASCAL');
  645. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  646. if (m_delphi in current_settings.modeswitches) then
  647. def_system_macro('FPC_DELPHI')
  648. else if (m_tp7 in current_settings.modeswitches) then
  649. def_system_macro('FPC_TP')
  650. else if (m_objfpc in current_settings.modeswitches) then
  651. def_system_macro('FPC_OBJFPC')
  652. {$ifdef gpc_mode}
  653. else if (m_gpc in current_settings.modeswitches) then
  654. def_system_macro('FPC_GPC')
  655. {$endif}
  656. else if (m_mac in current_settings.modeswitches) then
  657. def_system_macro('FPC_MACPAS')
  658. else if (m_iso in current_settings.modeswitches) then
  659. def_system_macro('FPC_ISO')
  660. else if (m_extpas in current_settings.modeswitches) then
  661. def_system_macro('FPC_EXTENDEDPASCAL');
  662. end;
  663. SetCompileMode:=b;
  664. end;
  665. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  666. var
  667. i : tmodeswitch;
  668. doinclude : boolean;
  669. begin
  670. s:=upper(s);
  671. { on/off? }
  672. doinclude:=true;
  673. case s[length(s)] of
  674. '+':
  675. setlength(s,length(s)-1);
  676. '-':
  677. begin
  678. setlength(s,length(s)-1);
  679. doinclude:=false;
  680. end;
  681. end;
  682. Result:=false;
  683. for i:=m_class to high(tmodeswitch) do
  684. if s=modeswitchstr[i] then
  685. begin
  686. { Objective-C is currently only supported for Darwin targets }
  687. if doinclude and
  688. (i in [m_objectivec1,m_objectivec2]) and
  689. not(target_info.system in systems_objc_supported) then
  690. begin
  691. Message1(option_unsupported_target_for_feature,'Objective-C');
  692. break;
  693. end;
  694. { Blocks supported? }
  695. if doinclude and
  696. (i = m_blocks) and
  697. not(target_info.system in systems_blocks_supported) then
  698. begin
  699. Message1(option_unsupported_target_for_feature,'Blocks');
  700. break;
  701. end;
  702. if changeInit then
  703. current_settings.modeswitches:=init_settings.modeswitches;
  704. Result:=true;
  705. if doinclude then
  706. begin
  707. include(current_settings.modeswitches,i);
  708. { Objective-C 2.0 support implies 1.0 support }
  709. if (i=m_objectivec2) then
  710. include(current_settings.modeswitches,m_objectivec1);
  711. if (i in [m_objectivec1,m_objectivec2]) then
  712. include(current_settings.modeswitches,m_class);
  713. end
  714. else
  715. begin
  716. exclude(current_settings.modeswitches,i);
  717. { Objective-C 2.0 support implies 1.0 support }
  718. if (i=m_objectivec2) then
  719. exclude(current_settings.modeswitches,m_objectivec1);
  720. if (i in [m_objectivec1,m_objectivec2]) and
  721. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  722. exclude(current_settings.modeswitches,m_class);
  723. end;
  724. { set other switches depending on changed mode switch }
  725. HandleModeSwitches(i,changeinit);
  726. if changeInit then
  727. init_settings.modeswitches:=current_settings.modeswitches;
  728. break;
  729. end;
  730. end;
  731. procedure SetAppType(NewAppType:tapptype);
  732. begin
  733. {$ifdef i8086}
  734. if (target_info.system in [system_i8086_msdos,system_i8086_embedded]) and (apptype<>NewAppType) then
  735. begin
  736. if NewAppType=app_com then
  737. begin
  738. targetinfos[target_info.system]^.exeext:='.com';
  739. target_info.exeext:='.com';
  740. end
  741. else
  742. begin
  743. targetinfos[target_info.system]^.exeext:='.exe';
  744. target_info.exeext:='.exe';
  745. end;
  746. end;
  747. {$endif i8086}
  748. {$ifdef m68k}
  749. if target_info.system in [system_m68k_atari] then
  750. case NewAppType of
  751. app_cui:
  752. begin
  753. targetinfos[target_info.system]^.exeext:='.ttp';
  754. target_info.exeext:='.ttp';
  755. end;
  756. app_gui:
  757. begin
  758. targetinfos[target_info.system]^.exeext:='.prg';
  759. target_info.exeext:='.prg';
  760. end;
  761. else
  762. ;
  763. end;
  764. {$endif m68k}
  765. if apptype in [app_cui,app_com] then
  766. undef_system_macro('CONSOLE');
  767. apptype:=NewAppType;
  768. if apptype in [app_cui,app_com] then
  769. def_system_macro('CONSOLE');
  770. end;
  771. {*****************************************************************************
  772. Conditional Directives
  773. *****************************************************************************}
  774. procedure dir_else;
  775. begin
  776. current_scanner.elsepreprocstack;
  777. end;
  778. procedure dir_endif;
  779. begin
  780. if (cs_legacyifend in current_settings.localswitches) and
  781. (current_scanner.preprocstack.typ<>pp_ifdef) and (current_scanner.preprocstack.typ<>pp_ifndef) and
  782. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_ifdef,pp_ifndef])) then
  783. Message(scan_e_unexpected_endif);
  784. current_scanner.poppreprocstack;
  785. end;
  786. procedure dir_ifend;
  787. begin
  788. if (cs_legacyifend in current_settings.localswitches) and
  789. (current_scanner.preprocstack.typ<>pp_elseif) and (current_scanner.preprocstack.typ<>pp_if) and
  790. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_if,pp_elseif])) then
  791. Message(scan_e_unexpected_ifend);
  792. current_scanner.poppreprocstack;
  793. end;
  794. function isdef(var valuedescr: String): Boolean;
  795. var
  796. hs : string;
  797. begin
  798. current_scanner.skipspace;
  799. hs:=current_scanner.readid;
  800. valuedescr:= hs;
  801. if hs='' then
  802. Message(scan_e_error_in_preproc_expr);
  803. isdef:=defined_macro(hs);
  804. end;
  805. procedure dir_ifdef;
  806. begin
  807. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  808. end;
  809. function isnotdef(var valuedescr: String): Boolean;
  810. var
  811. hs : string;
  812. begin
  813. current_scanner.skipspace;
  814. hs:=current_scanner.readid;
  815. valuedescr:= hs;
  816. if hs='' then
  817. Message(scan_e_error_in_preproc_expr);
  818. isnotdef:=not defined_macro(hs);
  819. end;
  820. procedure dir_ifndef;
  821. begin
  822. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  823. end;
  824. function opt_check(var valuedescr: String): Boolean;
  825. var
  826. hs : string;
  827. state : char;
  828. begin
  829. opt_check:= false;
  830. current_scanner.skipspace;
  831. hs:=current_scanner.readid;
  832. valuedescr:= hs;
  833. if (length(hs)>1) then
  834. Message1(scan_w_illegal_switch,hs)
  835. else
  836. begin
  837. state:=current_scanner.ReadState;
  838. if state in ['-','+'] then
  839. opt_check:=CheckSwitch(hs[1],state)
  840. else
  841. Message(scan_e_error_in_preproc_expr);
  842. end;
  843. end;
  844. procedure dir_ifopt;
  845. begin
  846. flushpendingswitchesstate;
  847. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  848. end;
  849. procedure dir_libprefix;
  850. var
  851. s : string;
  852. begin
  853. current_scanner.skipspace;
  854. if c <> '''' then
  855. Message2(scan_f_syn_expected, '''', c);
  856. s := current_scanner.readquotedstring;
  857. stringdispose(outputprefix);
  858. outputprefix := stringdup(s);
  859. with current_module do
  860. setfilename(paramfn, paramallowoutput);
  861. end;
  862. procedure dir_libsuffix;
  863. var
  864. s : string;
  865. begin
  866. current_scanner.skipspace;
  867. if c <> '''' then
  868. Message2(scan_f_syn_expected, '''', c);
  869. s := current_scanner.readquotedstring;
  870. stringdispose(outputsuffix);
  871. outputsuffix := stringdup(s);
  872. with current_module do
  873. setfilename(paramfn, paramallowoutput);
  874. end;
  875. procedure dir_extension;
  876. var
  877. s : string;
  878. begin
  879. current_scanner.skipspace;
  880. if c <> '''' then
  881. Message2(scan_f_syn_expected, '''', c);
  882. s := current_scanner.readquotedstring;
  883. if OutputFileName='' then
  884. OutputFileName:=InputFileName;
  885. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  886. with current_module do
  887. setfilename(paramfn, paramallowoutput);
  888. end;
  889. {
  890. Compile time expression type check
  891. ----------------------------------
  892. Each subexpression returns its type to the caller, which then can
  893. do type check. Since data types of compile time expressions is
  894. not well defined, the type system does a best effort. The drawback is
  895. that some errors might not be detected.
  896. Instead of returning a particular data type, a set of possible data types
  897. are returned. This way ambigouos types can be handled. For instance a
  898. value of 1 can be both a boolean and and integer.
  899. Booleans
  900. --------
  901. The following forms of boolean values are supported:
  902. * C coded, that is 0 is false, non-zero is true.
  903. * TRUE/FALSE for mac style compile time variables
  904. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  905. When a compile time expression is evaluated, they are then translated
  906. to C coded booleans (0/1), to simplify for the expression evaluator.
  907. Note that this scheme then also of support mac compile time variables which
  908. are 0/1 but with a boolean meaning.
  909. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  910. means that units which is not recompiled, and thus stores
  911. compile time variables as the old format (0/1), continue to work.
  912. Short circuit evaluation
  913. ------------------------
  914. For this to work, the part of a compile time expression which is short
  915. circuited, should not be evaluated, while it still should be parsed.
  916. Therefor there is a parameter eval, telling whether evaluation is needed.
  917. In case not, the value returned can be arbitrary.
  918. }
  919. type
  920. { texprvalue }
  921. texprvalue = class
  922. private
  923. { we can't use built-in defs since they
  924. may be not created at the moment }
  925. class var
  926. sintdef,uintdef,booldef,strdef,setdef,realdef: tdef;
  927. class constructor createdefs;
  928. class destructor destroydefs;
  929. public
  930. consttyp: tconsttyp;
  931. value: tconstvalue;
  932. def: tdef;
  933. constructor create_const(c:tconstsym);
  934. constructor create_error;
  935. constructor create_ord(v: Tconstexprint);
  936. constructor create_int(v: int64);
  937. constructor create_uint(v: qword);
  938. constructor create_bool(b: boolean);
  939. constructor create_str(const s: string);
  940. constructor create_set(ns: tnormalset);
  941. constructor create_real(r: bestreal);
  942. class function try_parse_number(const s:string):texprvalue; static;
  943. class function try_parse_real(const s:string):texprvalue; static;
  944. function evaluate(v:texprvalue;op:ttoken):texprvalue;
  945. procedure error(expecteddef, place: string);
  946. function isBoolean: Boolean;
  947. function isInt: Boolean;
  948. function asBool: Boolean;
  949. function asInt: Integer;
  950. function asInt64: Int64;
  951. function asStr: String;
  952. function asSet: tnormalset;
  953. destructor destroy; override;
  954. end;
  955. class constructor texprvalue.createdefs;
  956. begin
  957. { do not use corddef etc here: this code is executed before those
  958. variables are initialised. Since these types are only used for
  959. compile-time evaluation of conditional expressions, it doesn't matter
  960. that we use the base types instead of the cpu-specific ones. }
  961. sintdef:=torddef.create(s64bit,low(int64),high(int64),false);
  962. uintdef:=torddef.create(u64bit,low(qword),high(qword),false);
  963. booldef:=torddef.create(pasbool1,0,1,false);
  964. strdef:=tstringdef.createansi(0,false);
  965. setdef:=tsetdef.create(sintdef,0,255,false);
  966. realdef:=tfloatdef.create(s80real,false);
  967. end;
  968. class destructor texprvalue.destroydefs;
  969. begin
  970. setdef.free;
  971. sintdef.free;
  972. uintdef.free;
  973. booldef.free;
  974. strdef.free;
  975. realdef.free;
  976. end;
  977. constructor texprvalue.create_const(c: tconstsym);
  978. begin
  979. consttyp:=c.consttyp;
  980. def:=c.constdef;
  981. case consttyp of
  982. conststring,
  983. constresourcestring:
  984. begin
  985. value.len:=c.value.len;
  986. getmem(value.valueptr,value.len+1);
  987. move(c.value.valueptr^,value.valueptr^,value.len+1);
  988. end;
  989. constwstring,
  990. constwresourcestring:
  991. begin
  992. initwidestring(value.valueptr);
  993. copywidestring(c.value.valueptr,value.valueptr);
  994. end;
  995. constreal:
  996. begin
  997. new(pbestreal(value.valueptr));
  998. pbestreal(value.valueptr)^:=pbestreal(c.value.valueptr)^;
  999. end;
  1000. constset:
  1001. begin
  1002. new(pnormalset(value.valueptr));
  1003. pnormalset(value.valueptr)^:=pnormalset(c.value.valueptr)^;
  1004. end;
  1005. constguid:
  1006. begin
  1007. new(pguid(value.valueptr));
  1008. pguid(value.valueptr)^:=pguid(c.value.valueptr)^;
  1009. end;
  1010. else
  1011. value:=c.value;
  1012. end;
  1013. end;
  1014. constructor texprvalue.create_error;
  1015. begin
  1016. fillchar(value,sizeof(value),#0);
  1017. consttyp:=constnone;
  1018. def:=generrordef;
  1019. end;
  1020. constructor texprvalue.create_ord(v: Tconstexprint);
  1021. begin
  1022. fillchar(value,sizeof(value),#0);
  1023. consttyp:=constord;
  1024. value.valueord:=v;
  1025. if v.signed then
  1026. def:=sintdef
  1027. else
  1028. def:=uintdef;
  1029. end;
  1030. constructor texprvalue.create_int(v: int64);
  1031. begin
  1032. fillchar(value,sizeof(value),#0);
  1033. consttyp:=constord;
  1034. value.valueord:=v;
  1035. def:=sintdef;
  1036. end;
  1037. constructor texprvalue.create_uint(v: qword);
  1038. begin
  1039. fillchar(value,sizeof(value),#0);
  1040. consttyp:=constord;
  1041. value.valueord:=v;
  1042. def:=uintdef;
  1043. end;
  1044. constructor texprvalue.create_bool(b: boolean);
  1045. begin
  1046. fillchar(value,sizeof(value),#0);
  1047. consttyp:=constord;
  1048. value.valueord:=ord(b);
  1049. def:=booldef;
  1050. end;
  1051. constructor texprvalue.create_str(const s: string);
  1052. var
  1053. sp: pansichar;
  1054. len: integer;
  1055. begin
  1056. fillchar(value,sizeof(value),#0);
  1057. consttyp:=conststring;
  1058. len:=length(s);
  1059. getmem(sp,len+1);
  1060. move(s[1],sp^,len+1);
  1061. value.valueptr:=sp;
  1062. value.len:=len;
  1063. def:=strdef;
  1064. end;
  1065. constructor texprvalue.create_set(ns: tnormalset);
  1066. begin
  1067. fillchar(value,sizeof(value),#0);
  1068. consttyp:=constset;
  1069. new(pnormalset(value.valueptr));
  1070. pnormalset(value.valueptr)^:=ns;
  1071. def:=setdef;
  1072. end;
  1073. constructor texprvalue.create_real(r: bestreal);
  1074. begin
  1075. fillchar(value,sizeof(value),#0);
  1076. consttyp:=constreal;
  1077. new(pbestreal(value.valueptr));
  1078. pbestreal(value.valueptr)^:=r;
  1079. def:=realdef;
  1080. end;
  1081. class function texprvalue.try_parse_number(const s:string):texprvalue;
  1082. var
  1083. ic: int64;
  1084. qc: qword;
  1085. code: integer;
  1086. begin
  1087. { try int64 }
  1088. val(s,ic,code);
  1089. if code=0 then
  1090. result:=texprvalue.create_int(ic)
  1091. else
  1092. begin
  1093. { try qword }
  1094. val(s,qc,code);
  1095. if code=0 then
  1096. result:=texprvalue.create_uint(qc)
  1097. else
  1098. result:=try_parse_real(s);
  1099. end;
  1100. end;
  1101. class function texprvalue.try_parse_real(const s:string):texprvalue;
  1102. var
  1103. d: bestreal;
  1104. code: integer;
  1105. begin
  1106. val(s,d,code);
  1107. if code=0 then
  1108. result:=texprvalue.create_real(d)
  1109. else
  1110. result:=nil;
  1111. end;
  1112. function texprvalue.evaluate(v:texprvalue;op:ttoken):texprvalue;
  1113. function check_compatible: boolean;
  1114. begin
  1115. result:=(
  1116. (is_ordinal(v.def) or is_fpu(v.def)) and
  1117. (is_ordinal(def) or is_fpu(def))
  1118. ) or
  1119. (is_stringlike(v.def) and is_stringlike(def));
  1120. if not result then
  1121. Message2(type_e_incompatible_types,def.typename,v.def.typename);
  1122. end;
  1123. var
  1124. lv,rv: tconstexprint;
  1125. lvd,rvd: bestreal;
  1126. lvs,rvs: string;
  1127. begin
  1128. case op of
  1129. _OP_IN:
  1130. begin
  1131. if not is_set(v.def) then
  1132. begin
  1133. v.error('Set', 'IN');
  1134. result:=texprvalue.create_error;
  1135. end
  1136. else
  1137. if not is_ordinal(def) then
  1138. begin
  1139. error('Ordinal', 'IN');
  1140. result:=texprvalue.create_error;
  1141. end
  1142. else
  1143. if value.valueord.signed then
  1144. result:=texprvalue.create_bool(value.valueord.svalue in pnormalset(v.value.valueptr)^)
  1145. else
  1146. result:=texprvalue.create_bool(value.valueord.uvalue in pnormalset(v.value.valueptr)^);
  1147. end;
  1148. _OP_NOT:
  1149. begin
  1150. if isBoolean then
  1151. result:=texprvalue.create_bool(not asBool)
  1152. else if is_ordinal(def) then
  1153. begin
  1154. result:=texprvalue.create_ord(value.valueord);
  1155. result.def:=def;
  1156. calc_not_ordvalue(result.value.valueord,result.def);
  1157. end
  1158. else
  1159. begin
  1160. error('Boolean', 'NOT');
  1161. result:=texprvalue.create_error;
  1162. end;
  1163. end;
  1164. _OP_OR:
  1165. begin
  1166. if isBoolean then
  1167. if v.isBoolean then
  1168. result:=texprvalue.create_bool(asBool or v.asBool)
  1169. else
  1170. begin
  1171. v.error('Boolean','OR');
  1172. result:=texprvalue.create_error;
  1173. end
  1174. else if is_ordinal(def) then
  1175. if is_ordinal(v.def) then
  1176. result:=texprvalue.create_ord(value.valueord or v.value.valueord)
  1177. else
  1178. begin
  1179. v.error('Ordinal','OR');
  1180. result:=texprvalue.create_error;
  1181. end
  1182. else
  1183. begin
  1184. error('Boolean','OR');
  1185. result:=texprvalue.create_error;
  1186. end;
  1187. end;
  1188. _OP_XOR:
  1189. begin
  1190. if isBoolean then
  1191. if v.isBoolean then
  1192. result:=texprvalue.create_bool(asBool xor v.asBool)
  1193. else
  1194. begin
  1195. v.error('Boolean','XOR');
  1196. result:=texprvalue.create_error;
  1197. end
  1198. else if is_ordinal(def) then
  1199. if is_ordinal(v.def) then
  1200. result:=texprvalue.create_ord(value.valueord xor v.value.valueord)
  1201. else
  1202. begin
  1203. v.error('Ordinal','XOR');
  1204. result:=texprvalue.create_error;
  1205. end
  1206. else
  1207. begin
  1208. error('Boolean','XOR');
  1209. result:=texprvalue.create_error;
  1210. end;
  1211. end;
  1212. _OP_AND:
  1213. begin
  1214. if isBoolean then
  1215. if v.isBoolean then
  1216. result:=texprvalue.create_bool(asBool and v.asBool)
  1217. else
  1218. begin
  1219. v.error('Boolean','AND');
  1220. result:=texprvalue.create_error;
  1221. end
  1222. else if is_ordinal(def) then
  1223. if is_ordinal(v.def) then
  1224. result:=texprvalue.create_ord(value.valueord and v.value.valueord)
  1225. else
  1226. begin
  1227. v.error('Ordinal','AND');
  1228. result:=texprvalue.create_error;
  1229. end
  1230. else
  1231. begin
  1232. error('Boolean','AND');
  1233. result:=texprvalue.create_error;
  1234. end;
  1235. end;
  1236. _EQ,_NE,_LT,_GT,_GTE,_LTE,_PLUS,_MINUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR:
  1237. if (op=_MINUS) and not(assigned(v)) then
  1238. begin
  1239. if is_ordinal(def) then
  1240. result:=texprvalue.create_ord(-value.valueord)
  1241. else if is_fpu(def) then
  1242. result:=texprvalue.create_real(-pbestreal(value.valueptr)^)
  1243. else
  1244. begin
  1245. { actually we should never get here but this avoids a warning }
  1246. Message(parser_e_illegal_expression);
  1247. result:=texprvalue.create_error;
  1248. end;
  1249. end
  1250. else if check_compatible then
  1251. begin
  1252. if (is_ordinal(def) and is_ordinal(v.def)) then
  1253. begin
  1254. lv:=value.valueord;
  1255. rv:=v.value.valueord;
  1256. case op of
  1257. _EQ:
  1258. result:=texprvalue.create_bool(lv=rv);
  1259. _NE:
  1260. result:=texprvalue.create_bool(lv<>rv);
  1261. _LT:
  1262. result:=texprvalue.create_bool(lv<rv);
  1263. _GT:
  1264. result:=texprvalue.create_bool(lv>rv);
  1265. _GTE:
  1266. result:=texprvalue.create_bool(lv>=rv);
  1267. _LTE:
  1268. result:=texprvalue.create_bool(lv<=rv);
  1269. _PLUS:
  1270. result:=texprvalue.create_ord(lv+rv);
  1271. _MINUS:
  1272. result:=texprvalue.create_ord(lv-rv);
  1273. _STAR:
  1274. result:=texprvalue.create_ord(lv*rv);
  1275. _SLASH:
  1276. result:=texprvalue.create_real(lv/rv);
  1277. _OP_DIV:
  1278. result:=texprvalue.create_ord(lv div rv);
  1279. _OP_MOD:
  1280. result:=texprvalue.create_ord(lv mod rv);
  1281. _OP_SHL:
  1282. result:=texprvalue.create_ord(lv shl rv);
  1283. _OP_SHR:
  1284. result:=texprvalue.create_ord(lv shr rv);
  1285. else
  1286. begin
  1287. { actually we should never get here but this avoids a warning }
  1288. Message(parser_e_illegal_expression);
  1289. result:=texprvalue.create_error;
  1290. end;
  1291. end;
  1292. end
  1293. else
  1294. if (is_fpu(def) or is_ordinal(def)) and
  1295. (is_fpu(v.def) or is_ordinal(v.def)) then
  1296. begin
  1297. if is_fpu(def) then
  1298. lvd:=pbestreal(value.valueptr)^
  1299. else
  1300. lvd:=value.valueord;
  1301. if is_fpu(v.def) then
  1302. rvd:=pbestreal(v.value.valueptr)^
  1303. else
  1304. rvd:=v.value.valueord;
  1305. case op of
  1306. _EQ:
  1307. result:=texprvalue.create_bool(lvd=rvd);
  1308. _NE:
  1309. result:=texprvalue.create_bool(lvd<>rvd);
  1310. _LT:
  1311. result:=texprvalue.create_bool(lvd<rvd);
  1312. _GT:
  1313. result:=texprvalue.create_bool(lvd>rvd);
  1314. _GTE:
  1315. result:=texprvalue.create_bool(lvd>=rvd);
  1316. _LTE:
  1317. result:=texprvalue.create_bool(lvd<=rvd);
  1318. _PLUS:
  1319. result:=texprvalue.create_real(lvd+rvd);
  1320. _MINUS:
  1321. result:=texprvalue.create_real(lvd-rvd);
  1322. _STAR:
  1323. result:=texprvalue.create_real(lvd*rvd);
  1324. _SLASH:
  1325. result:=texprvalue.create_real(lvd/rvd);
  1326. else
  1327. begin
  1328. Message(parser_e_illegal_expression);
  1329. result:=texprvalue.create_error;
  1330. end;
  1331. end;
  1332. end
  1333. else
  1334. begin
  1335. lvs:=asStr;
  1336. rvs:=v.asStr;
  1337. case op of
  1338. _EQ:
  1339. result:=texprvalue.create_bool(lvs=rvs);
  1340. _NE:
  1341. result:=texprvalue.create_bool(lvs<>rvs);
  1342. _LT:
  1343. result:=texprvalue.create_bool(lvs<rvs);
  1344. _GT:
  1345. result:=texprvalue.create_bool(lvs>rvs);
  1346. _GTE:
  1347. result:=texprvalue.create_bool(lvs>=rvs);
  1348. _LTE:
  1349. result:=texprvalue.create_bool(lvs<=rvs);
  1350. _PLUS:
  1351. result:=texprvalue.create_str(lvs+rvs);
  1352. else
  1353. begin
  1354. Message(parser_e_illegal_expression);
  1355. result:=texprvalue.create_error;
  1356. end;
  1357. end;
  1358. end;
  1359. end
  1360. else
  1361. result:=texprvalue.create_error;
  1362. else
  1363. result:=texprvalue.create_error;
  1364. end;
  1365. end;
  1366. procedure texprvalue.error(expecteddef, place: string);
  1367. begin
  1368. Message3(scan_e_compile_time_typeerror,
  1369. expecteddef,
  1370. def.typename,
  1371. place
  1372. );
  1373. end;
  1374. function texprvalue.isBoolean: Boolean;
  1375. var
  1376. i: int64;
  1377. begin
  1378. result:=is_boolean(def);
  1379. if not result and is_integer(def) then
  1380. begin
  1381. i:=asInt64;
  1382. result:=(i=0)or(i=1);
  1383. end;
  1384. end;
  1385. function texprvalue.isInt: Boolean;
  1386. begin
  1387. result:=is_integer(def);
  1388. end;
  1389. function texprvalue.asBool: Boolean;
  1390. begin
  1391. result:=value.valueord<>0;
  1392. end;
  1393. function texprvalue.asInt: Integer;
  1394. begin
  1395. result:=value.valueord.svalue;
  1396. end;
  1397. function texprvalue.asInt64: Int64;
  1398. begin
  1399. result:=value.valueord.svalue;
  1400. end;
  1401. function texprvalue.asSet: tnormalset;
  1402. begin
  1403. result:=pnormalset(value.valueptr)^;
  1404. end;
  1405. function texprvalue.asStr: String;
  1406. var
  1407. b:byte;
  1408. begin
  1409. case consttyp of
  1410. constord:
  1411. result:=tostr(value.valueord);
  1412. conststring,
  1413. constresourcestring:
  1414. SetString(result,pchar(value.valueptr),value.len);
  1415. constreal:
  1416. str(pbestreal(value.valueptr)^,result);
  1417. constset:
  1418. begin
  1419. result:=',';
  1420. for b:=0 to 255 do
  1421. if b in pconstset(value.valueptr)^ then
  1422. result:=result+tostr(b)+',';
  1423. end;
  1424. { error values }
  1425. constnone:
  1426. result:='';
  1427. else
  1428. internalerror(2013112801);
  1429. end;
  1430. end;
  1431. destructor texprvalue.destroy;
  1432. begin
  1433. case consttyp of
  1434. conststring,
  1435. constresourcestring :
  1436. freemem(value.valueptr,value.len+1);
  1437. constwstring,
  1438. constwresourcestring:
  1439. donewidestring(pcompilerwidestring(value.valueptr));
  1440. constreal :
  1441. dispose(pbestreal(value.valueptr));
  1442. constset :
  1443. dispose(pnormalset(value.valueptr));
  1444. constguid :
  1445. dispose(pguid(value.valueptr));
  1446. constord,
  1447. { error values }
  1448. constnone:
  1449. ;
  1450. else
  1451. internalerror(2013112802);
  1452. end;
  1453. inherited destroy;
  1454. end;
  1455. const
  1456. preproc_operators=[_EQ,_NE,_LT,_GT,_LTE,_GTE,_MINUS,_PLUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR,_OP_IN,_OP_AND,_OP_OR,_OP_XOR];
  1457. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1458. var
  1459. found : boolean;
  1460. hpath : TCmdStr;
  1461. begin
  1462. (* look for the include file
  1463. If path was absolute and specified as part of {$I } then
  1464. 1. specified path
  1465. else
  1466. 1. path of current inputfile,current dir
  1467. 2. local includepath
  1468. 3. global includepath
  1469. -- Check mantis #13461 before changing this *)
  1470. found:=false;
  1471. foundfile:='';
  1472. hpath:='';
  1473. if path_absolute(path) then
  1474. begin
  1475. found:=FindFile(name,path,true,foundfile);
  1476. end
  1477. else
  1478. begin
  1479. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  1480. found:=FindFile(path+name, hpath,true,foundfile);
  1481. if not found then
  1482. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  1483. if not found then
  1484. found:=includesearchpath.FindFile(path+name,true,foundfile);
  1485. end;
  1486. result:=found;
  1487. end;
  1488. function preproc_comp_expr(conform_to:tdef):texprvalue;
  1489. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1490. procedure preproc_consume(t:ttoken);
  1491. begin
  1492. if t<>current_scanner.preproc_token then
  1493. Message(scan_e_preproc_syntax_error);
  1494. current_scanner.preproc_token:=current_scanner.readpreproc;
  1495. end;
  1496. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1497. var
  1498. hmodule: tmodule;
  1499. ns:ansistring;
  1500. nssym:tsym;
  1501. begin
  1502. result:=false;
  1503. tokentoconsume:=_ID;
  1504. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1505. begin
  1506. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1507. internalerror(200501154);
  1508. { only allow unit.symbol access if the name was
  1509. found in the current module
  1510. we can use iscurrentunit because generic specializations does not
  1511. change current_unit variable }
  1512. hmodule:=find_module_from_symtable(srsym.Owner);
  1513. if not Assigned(hmodule) then
  1514. internalerror(201001120);
  1515. if hmodule.unit_index=current_filepos.moduleindex then
  1516. begin
  1517. preproc_consume(_POINT);
  1518. current_scanner.skipspace;
  1519. if srsym.typ=namespacesym then
  1520. begin
  1521. ns:=srsym.name;
  1522. nssym:=srsym;
  1523. while assigned(srsym) and (srsym.typ=namespacesym) do
  1524. begin
  1525. { we have a namespace. the next identifier should be either a namespace or a unit }
  1526. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1527. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1528. begin
  1529. ns:=ns+'.'+current_scanner.preproc_pattern;
  1530. nssym:=srsym;
  1531. preproc_consume(_ID);
  1532. current_scanner.skipspace;
  1533. preproc_consume(_POINT);
  1534. current_scanner.skipspace;
  1535. end;
  1536. end;
  1537. { check if there is a hidden unit with this pattern in the namespace }
  1538. if not assigned(srsym) and
  1539. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1540. srsym:=tnamespacesym(nssym).unitsym;
  1541. if assigned(srsym) and (srsym.typ<>unitsym) then
  1542. internalerror(201108260);
  1543. if not assigned(srsym) then
  1544. begin
  1545. result:=true;
  1546. srsymtable:=nil;
  1547. exit;
  1548. end;
  1549. end;
  1550. case current_scanner.preproc_token of
  1551. _ID:
  1552. { system.char? (char=widechar comes from the implicit
  1553. uachar/uuchar unit -> override) }
  1554. if (current_scanner.preproc_pattern='CHAR') and
  1555. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1556. begin
  1557. if m_default_unicodestring in current_settings.modeswitches then
  1558. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1559. else
  1560. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1561. end
  1562. else
  1563. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1564. _STRING:
  1565. begin
  1566. { system.string? }
  1567. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1568. begin
  1569. if cs_refcountedstrings in current_settings.localswitches then
  1570. begin
  1571. if m_default_unicodestring in current_settings.modeswitches then
  1572. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1573. else
  1574. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1575. end
  1576. else
  1577. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1578. tokentoconsume:=_STRING;
  1579. end;
  1580. end
  1581. else
  1582. ;
  1583. end;
  1584. end
  1585. else
  1586. begin
  1587. srsym:=nil;
  1588. srsymtable:=nil;
  1589. end;
  1590. result:=true;
  1591. end;
  1592. end;
  1593. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1594. var
  1595. def:tdef;
  1596. tokentoconsume:ttoken;
  1597. found:boolean;
  1598. begin
  1599. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1600. if found then
  1601. begin
  1602. preproc_consume(tokentoconsume);
  1603. current_scanner.skipspace;
  1604. end;
  1605. while (current_scanner.preproc_token=_POINT) do
  1606. begin
  1607. if assigned(srsym)and(srsym.typ=typesym) then
  1608. begin
  1609. def:=ttypesym(srsym).typedef;
  1610. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1611. begin
  1612. preproc_consume(_POINT);
  1613. current_scanner.skipspace;
  1614. if def.typ=objectdef then
  1615. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1616. else
  1617. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1618. if not found then
  1619. begin
  1620. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1621. exit;
  1622. end;
  1623. preproc_consume(_ID);
  1624. current_scanner.skipspace;
  1625. end
  1626. else
  1627. begin
  1628. Message(sym_e_type_must_be_rec_or_object_or_class);
  1629. exit;
  1630. end;
  1631. end
  1632. else
  1633. begin
  1634. Message(type_e_type_id_expected);
  1635. exit;
  1636. end;
  1637. end;
  1638. end;
  1639. function preproc_substitutedtoken(const basesearchstr:string;eval:Boolean):texprvalue;
  1640. { Currently this parses identifiers as well as numbers.
  1641. The result from this procedure can either be that the token
  1642. itself is a value, or that it is a compile time variable/macro,
  1643. which then is substituted for another value (for macros
  1644. recursivelly substituted).}
  1645. var
  1646. hs: string;
  1647. mac: tmacro;
  1648. macrocount,
  1649. len: integer;
  1650. foundmacro: boolean;
  1651. searchstr: pshortstring;
  1652. searchstr2store: string;
  1653. begin
  1654. if not eval then
  1655. begin
  1656. result:=texprvalue.create_str(basesearchstr);
  1657. exit;
  1658. end;
  1659. searchstr := @basesearchstr;
  1660. mac:=nil;
  1661. foundmacro:=false;
  1662. { Substitue macros and compiler variables with their content/value.
  1663. For real macros also do recursive substitution. }
  1664. macrocount:=0;
  1665. repeat
  1666. mac:=tmacro(search_macro(searchstr^));
  1667. inc(macrocount);
  1668. if macrocount>max_macro_nesting then
  1669. begin
  1670. Message(scan_w_macro_too_deep);
  1671. break;
  1672. end;
  1673. if assigned(mac) and mac.defined then
  1674. if assigned(mac.buftext) then
  1675. begin
  1676. if mac.buflen>255 then
  1677. begin
  1678. len:=255;
  1679. Message(scan_w_macro_cut_after_255_chars);
  1680. end
  1681. else
  1682. len:=mac.buflen;
  1683. hs[0]:=char(len);
  1684. move(mac.buftext^,hs[1],len);
  1685. searchstr2store:=upcase(hs);
  1686. searchstr:=@searchstr2store;
  1687. mac.is_used:=true;
  1688. foundmacro:=true;
  1689. end
  1690. else
  1691. begin
  1692. Message1(scan_e_error_macro_lacks_value,searchstr^);
  1693. break;
  1694. end
  1695. else
  1696. break;
  1697. if mac.is_compiler_var then
  1698. break;
  1699. until false;
  1700. { At this point, result do contain the value. Do some decoding and
  1701. determine the type.}
  1702. result:=texprvalue.try_parse_number(searchstr^);
  1703. if not assigned(result) then
  1704. begin
  1705. if foundmacro and (searchstr^='FALSE') then
  1706. result:=texprvalue.create_bool(false)
  1707. else if foundmacro and (searchstr^='TRUE') then
  1708. result:=texprvalue.create_bool(true)
  1709. else if (m_mac in current_settings.modeswitches) and
  1710. (not assigned(mac) or not mac.defined) and
  1711. (macrocount = 1) then
  1712. begin
  1713. {Errors in mode mac is issued here. For non macpas modes there is
  1714. more liberty, but the error will eventually be caught at a later stage.}
  1715. Message1(scan_e_error_macro_undefined,searchstr^);
  1716. result:=texprvalue.create_str(searchstr^); { just to have something }
  1717. end
  1718. else
  1719. result:=texprvalue.create_str(searchstr^);
  1720. end;
  1721. end;
  1722. procedure MarkSymbolAsUsed(sym: tsym);
  1723. begin
  1724. sym.IncRefCount;
  1725. { do we know an owner? }
  1726. if Assigned(current_module) and Assigned(current_module.unitmap) and Assigned(sym.owner) then
  1727. inc(current_module.unitmap[sym.owner.moduleid].refs);
  1728. end;
  1729. function preproc_factor(eval: Boolean):texprvalue;
  1730. var
  1731. hs,countstr,storedpattern: string;
  1732. mac: tmacro;
  1733. srsym : tsym;
  1734. srsymtable : TSymtable;
  1735. hdef : TDef;
  1736. l : longint;
  1737. hasKlammer,
  1738. read_next: Boolean;
  1739. exprvalue:texprvalue;
  1740. ns:tnormalset;
  1741. fs,path,name: tpathstr;
  1742. foundfile: TCmdStr;
  1743. found: boolean;
  1744. begin
  1745. result:=nil;
  1746. hasKlammer:=false;
  1747. if current_scanner.preproc_token=_ID then
  1748. begin
  1749. if current_scanner.preproc_pattern='FILEEXISTS' then
  1750. begin
  1751. preproc_consume(_ID);
  1752. preproc_consume(_LKLAMMER);
  1753. hs:=current_scanner.preproc_pattern;
  1754. preproc_consume(_CSTRING);
  1755. fs:=GetToken(hs,' ');
  1756. fs:=FixFileName(fs);
  1757. path:=ExtractFilePath(fs);
  1758. name:=ExtractFileName(fs);
  1759. { this like 'include' }
  1760. if (length(name)>=1) and
  1761. (name[1]='*') then
  1762. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1763. { try to find the file, this like 'include' }
  1764. found:=findincludefile(path,name,foundfile);
  1765. if (not found) and (ExtractFileExt(name)='') then
  1766. begin
  1767. { try default extensions .inc , .pp and .pas }
  1768. if (not found) then
  1769. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1770. if (not found) then
  1771. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1772. if (not found) then
  1773. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1774. end;
  1775. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  1776. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  1777. result:=texprvalue.create_bool(found);
  1778. current_scanner.skipspace;
  1779. preproc_consume(_RKLAMMER);
  1780. end
  1781. else
  1782. if current_scanner.preproc_pattern='DEFINED' then
  1783. begin
  1784. preproc_consume(_ID);
  1785. current_scanner.skipspace;
  1786. if current_scanner.preproc_token =_LKLAMMER then
  1787. begin
  1788. preproc_consume(_LKLAMMER);
  1789. current_scanner.skipspace;
  1790. hasKlammer:= true;
  1791. end
  1792. else if (m_mac in current_settings.modeswitches) then
  1793. hasKlammer:= false
  1794. else
  1795. Message(scan_e_error_in_preproc_expr);
  1796. if current_scanner.preproc_token =_ID then
  1797. begin
  1798. hs := current_scanner.preproc_pattern;
  1799. mac := tmacro(search_macro(hs));
  1800. if assigned(mac) and mac.defined then
  1801. begin
  1802. result:=texprvalue.create_bool(true);
  1803. mac.is_used:=true;
  1804. end
  1805. else
  1806. result:=texprvalue.create_bool(false);
  1807. preproc_consume(_ID);
  1808. current_scanner.skipspace;
  1809. end
  1810. else
  1811. Message(scan_e_error_in_preproc_expr);
  1812. if hasKlammer then
  1813. if current_scanner.preproc_token =_RKLAMMER then
  1814. preproc_consume(_RKLAMMER)
  1815. else
  1816. Message(scan_e_error_in_preproc_expr);
  1817. end
  1818. else
  1819. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1820. begin
  1821. preproc_consume(_ID);
  1822. current_scanner.skipspace;
  1823. if current_scanner.preproc_token =_ID then
  1824. begin
  1825. hs := current_scanner.preproc_pattern;
  1826. mac := tmacro(search_macro(hs));
  1827. if assigned(mac) then
  1828. begin
  1829. result:=texprvalue.create_bool(false);
  1830. mac.is_used:=true;
  1831. end
  1832. else
  1833. result:=texprvalue.create_bool(true);
  1834. preproc_consume(_ID);
  1835. current_scanner.skipspace;
  1836. end
  1837. else
  1838. Message(scan_e_error_in_preproc_expr);
  1839. end
  1840. else
  1841. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1842. begin
  1843. preproc_consume(_ID);
  1844. current_scanner.skipspace;
  1845. if current_scanner.preproc_token =_LKLAMMER then
  1846. begin
  1847. preproc_consume(_LKLAMMER);
  1848. current_scanner.skipspace;
  1849. end
  1850. else
  1851. Message(scan_e_error_in_preproc_expr);
  1852. if not (current_scanner.preproc_token = _ID) then
  1853. Message(scan_e_error_in_preproc_expr);
  1854. hs:=current_scanner.preproc_pattern;
  1855. if (length(hs) > 1) then
  1856. {This is allowed in Metrowerks Pascal}
  1857. Message(scan_e_error_in_preproc_expr)
  1858. else
  1859. begin
  1860. if CheckSwitch(hs[1],'+') then
  1861. result:=texprvalue.create_bool(true)
  1862. else
  1863. result:=texprvalue.create_bool(false);
  1864. end;
  1865. preproc_consume(_ID);
  1866. current_scanner.skipspace;
  1867. if current_scanner.preproc_token =_RKLAMMER then
  1868. preproc_consume(_RKLAMMER)
  1869. else
  1870. Message(scan_e_error_in_preproc_expr);
  1871. end
  1872. else
  1873. if current_scanner.preproc_pattern='SIZEOF' then
  1874. begin
  1875. preproc_consume(_ID);
  1876. current_scanner.skipspace;
  1877. if current_scanner.preproc_token =_LKLAMMER then
  1878. begin
  1879. preproc_consume(_LKLAMMER);
  1880. current_scanner.skipspace;
  1881. end
  1882. else
  1883. Message(scan_e_preproc_syntax_error);
  1884. storedpattern:=current_scanner.preproc_pattern;
  1885. preproc_consume(_ID);
  1886. current_scanner.skipspace;
  1887. if eval then
  1888. if searchsym(storedpattern,srsym,srsymtable) then
  1889. begin
  1890. try_consume_nestedsym(srsym,srsymtable);
  1891. l:=0;
  1892. if assigned(srsym) then
  1893. case srsym.typ of
  1894. staticvarsym,
  1895. localvarsym,
  1896. paravarsym :
  1897. begin
  1898. l:=tabstractvarsym(srsym).getsize;
  1899. MarkSymbolAsUsed(srsym);
  1900. end;
  1901. typesym:
  1902. begin
  1903. l:=ttypesym(srsym).typedef.size;
  1904. MarkSymbolAsUsed(srsym);
  1905. end;
  1906. else
  1907. Message(scan_e_error_in_preproc_expr);
  1908. end;
  1909. result:=texprvalue.create_int(l);
  1910. end
  1911. else
  1912. Message1(sym_e_id_not_found,storedpattern);
  1913. if current_scanner.preproc_token =_RKLAMMER then
  1914. preproc_consume(_RKLAMMER)
  1915. else
  1916. Message(scan_e_preproc_syntax_error);
  1917. end
  1918. else
  1919. if current_scanner.preproc_pattern='HIGH' then
  1920. begin
  1921. preproc_consume(_ID);
  1922. current_scanner.skipspace;
  1923. if current_scanner.preproc_token =_LKLAMMER then
  1924. begin
  1925. preproc_consume(_LKLAMMER);
  1926. current_scanner.skipspace;
  1927. end
  1928. else
  1929. Message(scan_e_preproc_syntax_error);
  1930. storedpattern:=current_scanner.preproc_pattern;
  1931. preproc_consume(_ID);
  1932. current_scanner.skipspace;
  1933. if eval then
  1934. if searchsym(storedpattern,srsym,srsymtable) then
  1935. begin
  1936. try_consume_nestedsym(srsym,srsymtable);
  1937. hdef:=nil;
  1938. hs:='';
  1939. l:=0;
  1940. if assigned(srsym) then
  1941. case srsym.typ of
  1942. staticvarsym,
  1943. localvarsym,
  1944. paravarsym :
  1945. begin
  1946. hdef:=tabstractvarsym(srsym).vardef;
  1947. MarkSymbolAsUsed(srsym);
  1948. end;
  1949. typesym:
  1950. begin
  1951. hdef:=ttypesym(srsym).typedef;
  1952. MarkSymbolAsUsed(srsym);
  1953. end;
  1954. else
  1955. Message(scan_e_error_in_preproc_expr);
  1956. end;
  1957. if assigned(hdef) then
  1958. begin
  1959. if hdef.typ=setdef then
  1960. hdef:=tsetdef(hdef).elementdef;
  1961. case hdef.typ of
  1962. orddef:
  1963. with torddef(hdef).high do
  1964. if signed then
  1965. result:=texprvalue.create_int(svalue)
  1966. else
  1967. result:=texprvalue.create_uint(uvalue);
  1968. enumdef:
  1969. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1970. arraydef:
  1971. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1972. Message(type_e_mismatch)
  1973. else
  1974. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1975. stringdef:
  1976. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1977. Message(type_e_mismatch)
  1978. else
  1979. result:=texprvalue.create_int(tstringdef(hdef).len);
  1980. else
  1981. Message(type_e_mismatch);
  1982. end;
  1983. end;
  1984. end
  1985. else
  1986. Message1(sym_e_id_not_found,storedpattern);
  1987. if current_scanner.preproc_token =_RKLAMMER then
  1988. preproc_consume(_RKLAMMER)
  1989. else
  1990. Message(scan_e_preproc_syntax_error);
  1991. end
  1992. else
  1993. if current_scanner.preproc_pattern='DECLARED' then
  1994. begin
  1995. preproc_consume(_ID);
  1996. current_scanner.skipspace;
  1997. if current_scanner.preproc_token =_LKLAMMER then
  1998. begin
  1999. preproc_consume(_LKLAMMER);
  2000. current_scanner.skipspace;
  2001. end
  2002. else
  2003. Message(scan_e_error_in_preproc_expr);
  2004. if current_scanner.preproc_token =_ID then
  2005. begin
  2006. hs := upper(current_scanner.preproc_pattern);
  2007. preproc_consume(_ID);
  2008. current_scanner.skipspace;
  2009. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  2010. begin
  2011. l:=1;
  2012. preproc_consume(current_scanner.preproc_token);
  2013. current_scanner.skipspace;
  2014. while current_scanner.preproc_token=_COMMA do
  2015. begin
  2016. inc(l);
  2017. preproc_consume(_COMMA);
  2018. current_scanner.skipspace;
  2019. end;
  2020. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  2021. Message(scan_e_error_in_preproc_expr)
  2022. else
  2023. preproc_consume(current_scanner.preproc_token);
  2024. str(l,countstr);
  2025. hs:=hs+'$'+countstr;
  2026. end
  2027. else
  2028. { special case: <> }
  2029. if current_scanner.preproc_token=_NE then
  2030. begin
  2031. hs:=hs+'$1';
  2032. preproc_consume(_NE);
  2033. end;
  2034. current_scanner.skipspace;
  2035. if searchsym(hs,srsym,srsymtable) then
  2036. begin
  2037. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  2038. if (sp_generic_dummy in srsym.symoptions) and
  2039. (srsym.typ=typesym) and
  2040. (
  2041. { mode delphi}
  2042. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  2043. { non-delphi modes }
  2044. (df_generic in ttypesym(srsym).typedef.defoptions)
  2045. ) then
  2046. result:=texprvalue.create_bool(false)
  2047. else
  2048. result:=texprvalue.create_bool(true);
  2049. MarkSymbolAsUsed(srsym);
  2050. end
  2051. else
  2052. result:=texprvalue.create_bool(false);
  2053. end
  2054. else
  2055. Message(scan_e_error_in_preproc_expr);
  2056. if current_scanner.preproc_token =_RKLAMMER then
  2057. preproc_consume(_RKLAMMER)
  2058. else
  2059. Message(scan_e_error_in_preproc_expr);
  2060. end
  2061. else
  2062. if current_scanner.preproc_pattern='ORD' then
  2063. begin
  2064. preproc_consume(_ID);
  2065. current_scanner.skipspace;
  2066. if current_scanner.preproc_token =_LKLAMMER then
  2067. begin
  2068. preproc_consume(_LKLAMMER);
  2069. current_scanner.skipspace;
  2070. end
  2071. else
  2072. Message(scan_e_preproc_syntax_error);
  2073. exprvalue:=preproc_factor(eval);
  2074. if eval then
  2075. begin
  2076. if is_ordinal(exprvalue.def) then
  2077. result:=texprvalue.create_int(exprvalue.asInt)
  2078. else
  2079. begin
  2080. exprvalue.error('Ordinal','ORD');
  2081. result:=texprvalue.create_int(0);
  2082. end;
  2083. end
  2084. else
  2085. result:=texprvalue.create_int(0);
  2086. exprvalue.free;
  2087. if current_scanner.preproc_token =_RKLAMMER then
  2088. preproc_consume(_RKLAMMER)
  2089. else
  2090. Message(scan_e_error_in_preproc_expr);
  2091. end
  2092. else
  2093. if current_scanner.preproc_pattern='NOT' then
  2094. begin
  2095. preproc_consume(_ID);
  2096. exprvalue:=preproc_factor(eval);
  2097. if eval then
  2098. result:=exprvalue.evaluate(nil,_OP_NOT)
  2099. else
  2100. result:=texprvalue.create_bool(false); {Just to have something}
  2101. exprvalue.free;
  2102. end
  2103. else
  2104. if (current_scanner.preproc_pattern='TRUE') then
  2105. begin
  2106. result:=texprvalue.create_bool(true);
  2107. preproc_consume(_ID);
  2108. end
  2109. else
  2110. if (current_scanner.preproc_pattern='FALSE') then
  2111. begin
  2112. result:=texprvalue.create_bool(false);
  2113. preproc_consume(_ID);
  2114. end
  2115. else
  2116. begin
  2117. storedpattern:=current_scanner.preproc_pattern;
  2118. preproc_consume(_ID);
  2119. current_scanner.skipspace;
  2120. { first look for a macros/int/float }
  2121. result:=preproc_substitutedtoken(storedpattern,eval);
  2122. if eval and (result.consttyp=conststring) then
  2123. begin
  2124. if searchsym(storedpattern,srsym,srsymtable) then
  2125. begin
  2126. try_consume_nestedsym(srsym,srsymtable);
  2127. if assigned(srsym) then
  2128. begin
  2129. MarkSymbolAsUsed(srsym);
  2130. case srsym.typ of
  2131. constsym:
  2132. begin
  2133. { const def must conform to the set type }
  2134. if (conform_to<>nil) and
  2135. (conform_to.typ=setdef) and
  2136. (tconstsym(srsym).constdef.typ=setdef) and
  2137. (compare_defs(tsetdef(tconstsym(srsym).constdef).elementdef,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2138. begin
  2139. result.free;
  2140. result:=nil;
  2141. // TODO(ryan): better error?
  2142. Message(scan_e_error_in_preproc_expr);
  2143. end;
  2144. if result<>nil then
  2145. begin
  2146. result.free;
  2147. result:=texprvalue.create_const(tconstsym(srsym));
  2148. end;
  2149. end;
  2150. enumsym:
  2151. begin
  2152. { enum definition must conform to the set type }
  2153. if (conform_to<>nil) and
  2154. (conform_to.typ=setdef) and
  2155. (compare_defs(tenumsym(srsym).definition,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2156. begin
  2157. result.free;
  2158. result:=nil;
  2159. // TODO(ryan): better error?
  2160. Message(scan_e_error_in_preproc_expr);
  2161. end;
  2162. if result<>nil then
  2163. begin
  2164. result.free;
  2165. result:=texprvalue.create_int(tenumsym(srsym).value);
  2166. end;
  2167. end;
  2168. else
  2169. ;
  2170. end;
  2171. end;
  2172. end
  2173. { the id must be belong to the set type }
  2174. else if (conform_to<>nil) and (conform_to.typ=setdef) then
  2175. begin
  2176. result.free;
  2177. result:=nil;
  2178. // TODO(ryan): better error?
  2179. Message(scan_e_error_in_preproc_expr);
  2180. end;
  2181. end
  2182. { skip id(<expr>) if expression must not be evaluated }
  2183. else if not(eval) and (result.consttyp=conststring) then
  2184. begin
  2185. if current_scanner.preproc_token =_LKLAMMER then
  2186. begin
  2187. preproc_consume(_LKLAMMER);
  2188. current_scanner.skipspace;
  2189. result:=preproc_factor(false);
  2190. if current_scanner.preproc_token =_RKLAMMER then
  2191. preproc_consume(_RKLAMMER)
  2192. else
  2193. Message(scan_e_error_in_preproc_expr);
  2194. end;
  2195. end;
  2196. end
  2197. end
  2198. else if current_scanner.preproc_token =_LKLAMMER then
  2199. begin
  2200. preproc_consume(_LKLAMMER);
  2201. result:=preproc_sub_expr(opcompare,eval);
  2202. preproc_consume(_RKLAMMER);
  2203. end
  2204. else if current_scanner.preproc_token = _LECKKLAMMER then
  2205. begin
  2206. preproc_consume(_LECKKLAMMER);
  2207. ns:=[];
  2208. read_next:=false;
  2209. while (current_scanner.preproc_token in [_ID,_INTCONST]) or read_next do
  2210. begin
  2211. read_next:=false;
  2212. exprvalue:=preproc_factor(eval);
  2213. { the const set does not conform to the set def }
  2214. if (conform_to<>nil) and
  2215. (conform_to.typ=setdef) and
  2216. (exprvalue.consttyp=constnone) then
  2217. begin
  2218. result:=texprvalue.create_error;
  2219. break;
  2220. end;
  2221. { reject duplicate enums in the set }
  2222. if exprvalue.asInt in ns then
  2223. begin
  2224. Message1(sym_e_duplicate_id,current_scanner.preproc_pattern);
  2225. result:=texprvalue.create_error;
  2226. break;
  2227. end;
  2228. include(ns,exprvalue.asInt);
  2229. if current_scanner.preproc_token = _COMMA then
  2230. begin
  2231. preproc_consume(_COMMA);
  2232. read_next:=true;
  2233. end
  2234. end;
  2235. preproc_consume(_RECKKLAMMER);
  2236. if result=nil then
  2237. result:=texprvalue.create_set(ns);
  2238. end
  2239. else if current_scanner.preproc_token = _INTCONST then
  2240. begin
  2241. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  2242. if not assigned(result) then
  2243. begin
  2244. Message(parser_e_invalid_integer);
  2245. result:=texprvalue.create_int(1);
  2246. end;
  2247. preproc_consume(_INTCONST);
  2248. end
  2249. else if current_scanner.preproc_token = _CSTRING then
  2250. begin
  2251. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  2252. preproc_consume(_CSTRING);
  2253. end
  2254. else if current_scanner.preproc_token = _REALNUMBER then
  2255. begin
  2256. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  2257. if not assigned(result) then
  2258. begin
  2259. Message(parser_e_error_in_real);
  2260. result:=texprvalue.create_real(1.0);
  2261. end;
  2262. preproc_consume(_REALNUMBER);
  2263. end
  2264. else if current_scanner.preproc_token = _MINUS then
  2265. begin
  2266. preproc_consume(_MINUS);
  2267. exprvalue:=preproc_factor(eval);
  2268. if eval then
  2269. result:=exprvalue.evaluate(nil,_MINUS)
  2270. else if exprvalue.isInt then
  2271. result:=texprvalue.create_int(-exprvalue.asInt)
  2272. else if is_fpu(exprvalue.def) then
  2273. result:=texprvalue.create_real(-pbestreal(exprvalue.value.valueptr)^)
  2274. else
  2275. Message(scan_e_error_in_preproc_expr);
  2276. exprvalue.free;
  2277. end
  2278. else
  2279. Message(scan_e_error_in_preproc_expr);
  2280. if not assigned(result) then
  2281. result:=texprvalue.create_error;
  2282. end;
  2283. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  2284. var
  2285. hs1,hs2: texprvalue;
  2286. op: ttoken;
  2287. begin
  2288. if pred_level=highest_precedence then
  2289. result:=preproc_factor(eval)
  2290. else
  2291. result:=preproc_sub_expr(succ(pred_level),eval);
  2292. repeat
  2293. op:=current_scanner.preproc_token;
  2294. if (op in preproc_operators) and
  2295. (op in operator_levels[pred_level]) then
  2296. begin
  2297. hs1:=result;
  2298. preproc_consume(op);
  2299. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  2300. begin
  2301. { stop evaluation the rest of expression }
  2302. result:=texprvalue.create_bool(true);
  2303. if pred_level=highest_precedence then
  2304. hs2:=preproc_factor(false)
  2305. else
  2306. hs2:=preproc_sub_expr(succ(pred_level),false);
  2307. end
  2308. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  2309. begin
  2310. { stop evaluation the rest of expression }
  2311. result:=texprvalue.create_bool(false);
  2312. if pred_level=highest_precedence then
  2313. hs2:=preproc_factor(false)
  2314. else
  2315. hs2:=preproc_sub_expr(succ(pred_level),false);
  2316. end
  2317. else
  2318. begin
  2319. if pred_level=highest_precedence then
  2320. hs2:=preproc_factor(eval)
  2321. else
  2322. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2323. if eval then
  2324. result:=hs1.evaluate(hs2,op)
  2325. else
  2326. result:=texprvalue.create_bool(false); {Just to have something}
  2327. end;
  2328. hs1.free;
  2329. hs2.free;
  2330. end
  2331. else
  2332. break;
  2333. until false;
  2334. end;
  2335. begin
  2336. current_scanner.in_preproc_comp_expr:=true;
  2337. current_scanner.skipspace;
  2338. { start preproc expression scanner }
  2339. current_scanner.preproc_token:=current_scanner.readpreproc;
  2340. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2341. current_scanner.in_preproc_comp_expr:=false;
  2342. end;
  2343. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2344. var
  2345. hs: texprvalue;
  2346. begin
  2347. hs:=preproc_comp_expr(nil);
  2348. if hs.isBoolean then
  2349. result:=hs.asBool
  2350. else
  2351. begin
  2352. hs.error('Boolean', 'IF or ELSEIF');
  2353. result:=false;
  2354. end;
  2355. valuedescr:=hs.asStr;
  2356. hs.free;
  2357. end;
  2358. procedure dir_if;
  2359. begin
  2360. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2361. end;
  2362. procedure dir_elseif;
  2363. begin
  2364. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2365. end;
  2366. procedure dir_define_impl(macstyle: boolean);
  2367. var
  2368. hs : string;
  2369. bracketcount : longint;
  2370. mac : tmacro;
  2371. macropos : SizeInt;
  2372. macrobuffer : array[0..maxmacrolen-1] of char;
  2373. begin
  2374. current_scanner.skipspace;
  2375. hs:=current_scanner.readid;
  2376. if hs='' then
  2377. begin
  2378. Message(scan_e_emptymacroname);
  2379. exit;
  2380. end;
  2381. mac:=tmacro(search_macro(hs));
  2382. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2383. begin
  2384. mac:=tmacro.create(hs);
  2385. mac.defined:=true;
  2386. current_module.localmacrosymtable.insertsym(mac);
  2387. end
  2388. else
  2389. begin
  2390. mac.defined:=true;
  2391. mac.is_compiler_var:=false;
  2392. { delete old definition }
  2393. mac.free_buftext;
  2394. end;
  2395. Message1(parser_c_macro_defined,mac.name);
  2396. mac.is_used:=true;
  2397. if (cs_support_macro in current_settings.moduleswitches) then
  2398. begin
  2399. current_scanner.skipspace;
  2400. if not macstyle then
  2401. begin
  2402. { may be a macro? }
  2403. if c <> ':' then
  2404. exit;
  2405. current_scanner.readchar;
  2406. if c <> '=' then
  2407. exit;
  2408. current_scanner.readchar;
  2409. current_scanner.skipspace;
  2410. end;
  2411. { key words are never substituted }
  2412. if is_keyword(hs) then
  2413. Message(scan_e_keyword_cant_be_a_macro);
  2414. current_scanner.gettokenpos;
  2415. mac.fileinfo:=current_tokenpos;
  2416. macropos:=0;
  2417. { parse macro, brackets are counted so it's possible
  2418. to have a $ifdef etc. in the macro }
  2419. bracketcount:=0;
  2420. repeat
  2421. case c of
  2422. '}' :
  2423. if (bracketcount=0) then
  2424. break
  2425. else
  2426. dec(bracketcount);
  2427. '{' :
  2428. inc(bracketcount);
  2429. #10,#13 :
  2430. current_scanner.linebreak;
  2431. #26 :
  2432. current_scanner.end_of_file;
  2433. end;
  2434. if macropos>=maxmacrolen then
  2435. Message(scan_f_macro_buffer_overflow);
  2436. macrobuffer[macropos]:=c;
  2437. inc(macropos);
  2438. current_scanner.readchar;
  2439. until false;
  2440. { copy the text }
  2441. move(pchar(@macrobuffer[0])^,mac.allocate_buftext(macropos)^,macropos);
  2442. end
  2443. else
  2444. begin
  2445. { check if there is an assignment, then we need to give a
  2446. warning }
  2447. current_scanner.skipspace;
  2448. if c=':' then
  2449. begin
  2450. current_scanner.readchar;
  2451. if c='=' then
  2452. Message(scan_w_macro_support_turned_off);
  2453. end;
  2454. end;
  2455. end;
  2456. procedure dir_define;
  2457. begin
  2458. dir_define_impl(false);
  2459. end;
  2460. procedure dir_definec;
  2461. begin
  2462. dir_define_impl(true);
  2463. end;
  2464. procedure dir_setc;
  2465. var
  2466. hs : string;
  2467. mac : tmacro;
  2468. exprvalue: texprvalue;
  2469. begin
  2470. current_scanner.skipspace;
  2471. hs:=current_scanner.readid;
  2472. mac:=tmacro(search_macro(hs));
  2473. if not assigned(mac) or
  2474. (mac.owner <> current_module.localmacrosymtable) then
  2475. begin
  2476. mac:=tmacro.create(hs);
  2477. mac.defined:=true;
  2478. mac.is_compiler_var:=true;
  2479. current_module.localmacrosymtable.insertsym(mac);
  2480. end
  2481. else
  2482. begin
  2483. mac.defined:=true;
  2484. mac.is_compiler_var:=true;
  2485. { delete old definition }
  2486. mac.free_buftext;
  2487. end;
  2488. Message1(parser_c_macro_defined,mac.name);
  2489. mac.is_used:=true;
  2490. { key words are never substituted }
  2491. if is_keyword(hs) then
  2492. Message(scan_e_keyword_cant_be_a_macro);
  2493. { macro assignment can be both := and = }
  2494. current_scanner.skipspace;
  2495. if c=':' then
  2496. current_scanner.readchar;
  2497. if c='=' then
  2498. begin
  2499. current_scanner.readchar;
  2500. exprvalue:=preproc_comp_expr(nil);
  2501. if not is_boolean(exprvalue.def) and
  2502. not is_integer(exprvalue.def) then
  2503. exprvalue.error('Boolean, Integer', 'SETC');
  2504. hs:=exprvalue.asStr;
  2505. if length(hs) <> 0 then
  2506. begin
  2507. {If we are absolutely shure it is boolean, translate
  2508. to TRUE/FALSE to increase possibility to do future type check}
  2509. if exprvalue.isBoolean then
  2510. begin
  2511. if exprvalue.asBool then
  2512. hs:='TRUE'
  2513. else
  2514. hs:='FALSE';
  2515. end;
  2516. Message2(parser_c_macro_set_to,mac.name,hs);
  2517. { copy the text }
  2518. move(hs[1],mac.allocate_buftext(length(hs))^,length(hs));
  2519. end
  2520. else
  2521. Message(scan_e_preproc_syntax_error);
  2522. exprvalue.free;
  2523. end
  2524. else
  2525. Message(scan_e_preproc_syntax_error);
  2526. end;
  2527. procedure dir_undef;
  2528. var
  2529. hs : string;
  2530. mac : tmacro;
  2531. begin
  2532. current_scanner.skipspace;
  2533. hs:=current_scanner.readid;
  2534. mac:=tmacro(search_macro(hs));
  2535. if not assigned(mac) or
  2536. (mac.owner <> current_module.localmacrosymtable) then
  2537. begin
  2538. mac:=tmacro.create(hs);
  2539. mac.defined:=false;
  2540. current_module.localmacrosymtable.insertsym(mac);
  2541. end
  2542. else
  2543. begin
  2544. mac.defined:=false;
  2545. mac.is_compiler_var:=false;
  2546. { delete old definition }
  2547. mac.free_buftext;
  2548. end;
  2549. Message1(parser_c_macro_undefined,mac.name);
  2550. mac.is_used:=true;
  2551. end;
  2552. procedure dir_include;
  2553. var
  2554. foundfile : TCmdStr;
  2555. path,
  2556. name,
  2557. hs : tpathstr;
  2558. args : string;
  2559. hp : tinputfile;
  2560. found : boolean;
  2561. macroIsString : boolean;
  2562. begin
  2563. current_scanner.skipspace;
  2564. args:=current_scanner.readcomment;
  2565. hs:=GetToken(args,' ');
  2566. if hs='' then
  2567. exit;
  2568. if (hs[1]='%') then
  2569. begin
  2570. { case insensitive }
  2571. hs:=upper(hs);
  2572. { remove %'s }
  2573. Delete(hs,1,1);
  2574. if hs[length(hs)]='%' then
  2575. Delete(hs,length(hs),1);
  2576. { save old }
  2577. path:=hs;
  2578. { first check for internal macros }
  2579. macroIsString:=true;
  2580. case hs of
  2581. 'TIME':
  2582. if timestr<>'' then
  2583. hs:=timestr
  2584. else
  2585. hs:=gettimestr;
  2586. 'DATE':
  2587. if datestr<>'' then
  2588. hs:=datestr
  2589. else
  2590. hs:=getdatestr;
  2591. 'DATEYEAR':
  2592. begin
  2593. hs:=tostr(startsystime.Year);
  2594. macroIsString:=false;
  2595. end;
  2596. 'DATEMONTH':
  2597. begin
  2598. hs:=tostr(startsystime.Month);
  2599. macroIsString:=false;
  2600. end;
  2601. 'DATEDAY':
  2602. begin
  2603. hs:=tostr(startsystime.Day);
  2604. macroIsString:=false;
  2605. end;
  2606. 'TIMEHOUR':
  2607. begin
  2608. hs:=tostr(startsystime.Hour);
  2609. macroIsString:=false;
  2610. end;
  2611. 'TIMEMINUTE':
  2612. begin
  2613. hs:=tostr(startsystime.Minute);
  2614. macroIsString:=false;
  2615. end;
  2616. 'TIMESECOND':
  2617. begin
  2618. hs:=tostr(startsystime.Second);
  2619. macroIsString:=false;
  2620. end;
  2621. 'FILE':
  2622. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2623. 'LINE':
  2624. hs:=tostr(current_filepos.line);
  2625. 'LINENUM':
  2626. begin
  2627. hs:=tostr(current_filepos.line);
  2628. macroIsString:=false;
  2629. end;
  2630. 'FPCVERSION':
  2631. hs:=version_string;
  2632. 'FPCDATE':
  2633. hs:=date_string;
  2634. 'FPCTARGET':
  2635. hs:=target_cpu_string;
  2636. 'FPCTARGETCPU':
  2637. hs:=target_cpu_string;
  2638. 'FPCTARGETOS':
  2639. hs:=target_info.shortname;
  2640. 'CURRENTROUTINE':
  2641. hs:=current_procinfo.procdef.procsym.RealName;
  2642. else
  2643. hs:=GetEnvironmentVariable(hs);
  2644. end;
  2645. if hs='' then
  2646. Message1(scan_w_include_env_not_found,path);
  2647. { make it a stringconst }
  2648. if macroIsString then
  2649. hs:=''''+hs+'''';
  2650. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2651. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2652. end
  2653. else
  2654. begin
  2655. hs:=FixFileName(hs);
  2656. path:=ExtractFilePath(hs);
  2657. name:=ExtractFileName(hs);
  2658. { Special case for Delphi compatibility: '*' has to be replaced
  2659. by the file name of the current source file. }
  2660. if (length(name)>=1) and
  2661. (name[1]='*') then
  2662. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2663. { try to find the file }
  2664. found:=findincludefile(path,name,foundfile);
  2665. if (not found) and (ExtractFileExt(name)='') then
  2666. begin
  2667. { try default extensions .inc , .pp and .pas }
  2668. if (not found) then
  2669. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2670. if (not found) then
  2671. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2672. if (not found) then
  2673. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2674. end;
  2675. { if the name ends in dot, try without the dot }
  2676. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2677. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2678. if current_scanner.inputfilecount<max_include_nesting then
  2679. begin
  2680. inc(current_scanner.inputfilecount);
  2681. { we need to reread the current char }
  2682. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2683. current_scanner.dec_inputpointer;
  2684. {$else not CHECK_INPUTPOINTER_LIMITS}
  2685. dec(current_scanner.inputpointer);
  2686. {$endif CHECK_INPUTPOINTER_LIMITS}
  2687. { reset c }
  2688. c:=#0;
  2689. { shutdown current file }
  2690. current_scanner.tempcloseinputfile;
  2691. { load new file }
  2692. hp:=do_openinputfile(foundfile);
  2693. hp.inc_path:=path;
  2694. current_scanner.addfile(hp);
  2695. current_module.sourcefiles.register_file(hp);
  2696. if (not found) then
  2697. Message1(scan_f_cannot_open_includefile,hs);
  2698. if (not current_scanner.openinputfile) then
  2699. Message1(scan_f_cannot_open_includefile,hs);
  2700. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2701. current_scanner.reload;
  2702. end
  2703. else
  2704. Message(scan_f_include_deep_ten);
  2705. end;
  2706. end;
  2707. {*****************************************************************************
  2708. Preprocessor writing
  2709. *****************************************************************************}
  2710. {$ifdef PREPROCWRITE}
  2711. constructor tpreprocfile.create(const fn:string);
  2712. begin
  2713. inherited create;
  2714. { open outputfile }
  2715. assign(f,fn);
  2716. {$push}{$I-}
  2717. rewrite(f);
  2718. {$pop}
  2719. if ioresult<>0 then
  2720. Comment(V_Fatal,'can''t create file '+fn);
  2721. getmem(buf,preprocbufsize);
  2722. settextbuf(f,buf^,preprocbufsize);
  2723. { reset }
  2724. eolfound:=false;
  2725. spacefound:=false;
  2726. end;
  2727. destructor tpreprocfile.destroy;
  2728. begin
  2729. close(f);
  2730. freemem(buf,preprocbufsize);
  2731. end;
  2732. procedure tpreprocfile.add(const s:string);
  2733. begin
  2734. write(f,s);
  2735. end;
  2736. procedure tpreprocfile.addspace;
  2737. begin
  2738. if eolfound then
  2739. begin
  2740. writeln(f,'');
  2741. eolfound:=false;
  2742. spacefound:=false;
  2743. end
  2744. else
  2745. if spacefound then
  2746. begin
  2747. write(f,' ');
  2748. spacefound:=false;
  2749. end;
  2750. end;
  2751. {$endif PREPROCWRITE}
  2752. {*****************************************************************************
  2753. TPreProcStack
  2754. *****************************************************************************}
  2755. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2756. begin
  2757. accept:=a;
  2758. typ:=atyp;
  2759. next:=n;
  2760. end;
  2761. {*****************************************************************************
  2762. TReplayStack
  2763. *****************************************************************************}
  2764. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2765. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2766. apatternw:pcompilerwidestring;asettings:tsettings;
  2767. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2768. begin
  2769. token:=atoken;
  2770. idtoken:=aidtoken;
  2771. orgpattern:=aorgpattern;
  2772. pattern:=apattern;
  2773. cstringpattern:=acstringpattern;
  2774. initwidestring(patternw);
  2775. if assigned(apatternw) then
  2776. begin
  2777. setlengthwidestring(patternw,apatternw^.len);
  2778. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2779. end;
  2780. settings:=asettings;
  2781. tokenbuf:=atokenbuf;
  2782. tokenbuf_needs_swapping:=change_endian;
  2783. next:=anext;
  2784. end;
  2785. destructor treplaystack.destroy;
  2786. begin
  2787. donewidestring(patternw);
  2788. end;
  2789. {*****************************************************************************
  2790. TDirectiveItem
  2791. *****************************************************************************}
  2792. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2793. begin
  2794. inherited Create(AList,n);
  2795. is_conditional:=false;
  2796. proc:=p;
  2797. end;
  2798. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2799. begin
  2800. inherited Create(AList,n);
  2801. is_conditional:=true;
  2802. proc:=p;
  2803. end;
  2804. {****************************************************************************
  2805. TSCANNERFILE
  2806. ****************************************************************************}
  2807. constructor tscannerfile.Create(const fn: string; is_macro: boolean);
  2808. begin
  2809. inputfile:=do_openinputfile(fn);
  2810. if is_macro then
  2811. inputfile.is_macro:=true;
  2812. if assigned(current_module) then
  2813. current_module.sourcefiles.register_file(inputfile);
  2814. { reset localinput }
  2815. c:=#0;
  2816. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2817. hidden_inputbuffer:=nil;
  2818. hidden_inputpointer:=nil;
  2819. {$else not CHECK_INPUTPOINTER_LIMITS}
  2820. inputbuffer:=nil;
  2821. inputpointer:=nil;
  2822. {$endif CHECK_INPUTPOINTER_LIMITS}
  2823. inputstart:=0;
  2824. { reset scanner }
  2825. preprocstack:=nil;
  2826. replaystack:=nil;
  2827. comment_level:=0;
  2828. yylexcount:=0;
  2829. block_type:=bt_general;
  2830. line_no:=0;
  2831. lastlinepos:=0;
  2832. lasttokenpos:=0;
  2833. nexttokenpos:=0;
  2834. lasttoken:=NOTOKEN;
  2835. nexttoken:=NOTOKEN;
  2836. ignoredirectives:=TFPHashList.Create;
  2837. change_endian_for_replay:=false;
  2838. end;
  2839. procedure tscannerfile.firstfile;
  2840. begin
  2841. { load block }
  2842. if not openinputfile then
  2843. Message1(scan_f_cannot_open_input,inputfile.name);
  2844. reload;
  2845. end;
  2846. destructor tscannerfile.Destroy;
  2847. begin
  2848. if assigned(onfreescanner) then
  2849. onfreescanner(self);
  2850. if assigned(current_module) and
  2851. (current_module.state in [ms_processed,ms_compiled]) and
  2852. (status.errorcount=0) then
  2853. checkpreprocstack
  2854. else
  2855. begin
  2856. while assigned(preprocstack) do
  2857. poppreprocstack;
  2858. end;
  2859. while assigned(replaystack) do
  2860. popreplaystack;
  2861. if not inputfile.closed then
  2862. closeinputfile;
  2863. if inputfile.is_macro then
  2864. inputfile.free;
  2865. ignoredirectives.free;
  2866. end;
  2867. function tscannerfile.openinputfile:boolean;
  2868. begin
  2869. openinputfile:=inputfile.open;
  2870. { load buffer }
  2871. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2872. hidden_inputbuffer:=inputfile.buf;
  2873. hidden_inputpointer:=inputfile.buf;
  2874. {$else not CHECK_INPUTPOINTER_LIMITS}
  2875. inputbuffer:=inputfile.buf;
  2876. inputpointer:=inputfile.buf;
  2877. {$endif CHECK_INPUTPOINTER_LIMITS}
  2878. inputstart:=inputfile.bufstart;
  2879. { line }
  2880. line_no:=0;
  2881. lastlinepos:=0;
  2882. lasttokenpos:=0;
  2883. nexttokenpos:=0;
  2884. end;
  2885. procedure tscannerfile.closeinputfile;
  2886. begin
  2887. inputfile.close;
  2888. { reset buffer }
  2889. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2890. hidden_inputbuffer:=nil;
  2891. hidden_inputpointer:=nil;
  2892. {$else not CHECK_INPUTPOINTER_LIMITS}
  2893. inputbuffer:=nil;
  2894. inputpointer:=nil;
  2895. {$endif CHECK_INPUTPOINTER_LIMITS}
  2896. inputstart:=0;
  2897. { reset line }
  2898. line_no:=0;
  2899. lastlinepos:=0;
  2900. lasttokenpos:=0;
  2901. nexttokenpos:=0;
  2902. end;
  2903. function tscannerfile.tempopeninputfile:boolean;
  2904. begin
  2905. tempopeninputfile:=false;
  2906. if inputfile.is_macro then
  2907. exit;
  2908. tempopeninputfile:=inputfile.tempopen;
  2909. { reload buffer }
  2910. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2911. hidden_inputbuffer:=inputfile.buf;
  2912. hidden_inputpointer:=inputfile.buf;
  2913. {$else not CHECK_INPUTPOINTER_LIMITS}
  2914. inputbuffer:=inputfile.buf;
  2915. inputpointer:=inputfile.buf;
  2916. {$endif CHECK_INPUTPOINTER_LIMITS}
  2917. inputstart:=inputfile.bufstart;
  2918. end;
  2919. procedure tscannerfile.tempcloseinputfile;
  2920. begin
  2921. if inputfile.closed or inputfile.is_macro then
  2922. exit;
  2923. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2924. inputfile.setpos(inputstart+(hidden_inputpointer-hidden_inputbuffer));
  2925. {$else not CHECK_INPUTPOINTER_LIMITS}
  2926. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2927. {$endif CHECK_INPUTPOINTER_LIMITS}
  2928. inputfile.tempclose;
  2929. { reset buffer }
  2930. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2931. hidden_inputbuffer:=nil;
  2932. hidden_inputpointer:=nil;
  2933. {$else not CHECK_INPUTPOINTER_LIMITS}
  2934. inputbuffer:=nil;
  2935. inputpointer:=nil;
  2936. {$endif CHECK_INPUTPOINTER_LIMITS}
  2937. inputstart:=0;
  2938. end;
  2939. procedure tscannerfile.saveinputfile;
  2940. begin
  2941. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2942. inputfile.saveinputpointer:=hidden_inputpointer;
  2943. {$else not CHECK_INPUTPOINTER_LIMITS}
  2944. inputfile.saveinputpointer:=inputpointer;
  2945. {$endif CHECK_INPUTPOINTER_LIMITS}
  2946. inputfile.savelastlinepos:=lastlinepos;
  2947. inputfile.saveline_no:=line_no;
  2948. end;
  2949. procedure tscannerfile.restoreinputfile;
  2950. begin
  2951. {$ifdef check_inputpointer_limits}
  2952. hidden_inputbuffer:=inputfile.buf;
  2953. hidden_inputpointer:=inputfile.saveinputpointer;
  2954. {$else not check_inputpointer_limits}
  2955. inputbuffer:=inputfile.buf;
  2956. inputpointer:=inputfile.saveinputpointer;
  2957. {$endif check_inputpointer_limits}
  2958. lastlinepos:=inputfile.savelastlinepos;
  2959. line_no:=inputfile.saveline_no;
  2960. if not inputfile.is_macro then
  2961. parser_current_file:=inputfile.name;
  2962. end;
  2963. procedure tscannerfile.nextfile;
  2964. var
  2965. to_dispose : tinputfile;
  2966. begin
  2967. if assigned(inputfile.next) then
  2968. begin
  2969. if inputfile.is_macro then
  2970. begin
  2971. to_dispose:=inputfile;
  2972. dec(macro_nesting_depth);
  2973. end
  2974. else
  2975. begin
  2976. to_dispose:=nil;
  2977. dec(inputfilecount);
  2978. end;
  2979. { we can allways close the file, no ? }
  2980. inputfile.close;
  2981. inputfile:=inputfile.next;
  2982. if assigned(to_dispose) then
  2983. to_dispose.free;
  2984. restoreinputfile;
  2985. end;
  2986. end;
  2987. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2988. function tscannerfile.get_inputpointer_char(offset : longint = 0) : char;
  2989. begin
  2990. assert(hidden_inputpointer-hidden_inputbuffer+offset<=inputfile.bufsize);
  2991. get_inputpointer_char:=(hidden_inputpointer+offset)^;
  2992. end;
  2993. procedure tscannerfile.inc_inputpointer(amount : longint = 1);
  2994. begin
  2995. assert(hidden_inputpointer-hidden_inputbuffer+amount<=inputfile.bufsize);
  2996. inc(hidden_inputpointer,amount);
  2997. end;
  2998. procedure tscannerfile.dec_inputpointer;
  2999. begin
  3000. assert(hidden_inputpointer>hidden_inputbuffer);
  3001. dec(hidden_inputpointer);
  3002. end;
  3003. {$endif}
  3004. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  3005. begin
  3006. if not assigned(buf) then
  3007. internalerror(200511172);
  3008. if assigned(recordtokenbuf) then
  3009. internalerror(200511173);
  3010. recordtokenbuf:=buf;
  3011. fillchar(last_settings,sizeof(last_settings),0);
  3012. last_message:=nil;
  3013. fillchar(last_filepos,sizeof(last_filepos),0);
  3014. end;
  3015. procedure tscannerfile.stoprecordtokens;
  3016. begin
  3017. if not assigned(recordtokenbuf) then
  3018. internalerror(200511174);
  3019. recordtokenbuf:=nil;
  3020. end;
  3021. function tscannerfile.is_recording_tokens: boolean;
  3022. begin
  3023. result:=assigned(recordtokenbuf);
  3024. end;
  3025. procedure tscannerfile.writetoken(t : ttoken);
  3026. var
  3027. b : byte;
  3028. begin
  3029. if ord(t)>$7f then
  3030. begin
  3031. b:=(ord(t) shr 8) or $80;
  3032. recordtokenbuf.write(b,1);
  3033. end;
  3034. b:=ord(t) and $ff;
  3035. recordtokenbuf.write(b,1);
  3036. end;
  3037. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  3038. begin
  3039. recordtokenbuf.write(val,sizeof(asizeint));
  3040. end;
  3041. procedure tscannerfile.tokenwritelongint(val : longint);
  3042. begin
  3043. recordtokenbuf.write(val,sizeof(longint));
  3044. end;
  3045. procedure tscannerfile.tokenwriteshortint(val : shortint);
  3046. begin
  3047. recordtokenbuf.write(val,sizeof(shortint));
  3048. end;
  3049. procedure tscannerfile.tokenwriteword(val : word);
  3050. begin
  3051. recordtokenbuf.write(val,sizeof(word));
  3052. end;
  3053. procedure tscannerfile.tokenwritelongword(val : longword);
  3054. begin
  3055. recordtokenbuf.write(val,sizeof(longword));
  3056. end;
  3057. function tscannerfile.tokenreadsizeint : asizeint;
  3058. var
  3059. val : asizeint;
  3060. begin
  3061. replaytokenbuf.read(val,sizeof(asizeint));
  3062. if change_endian_for_replay then
  3063. val:=swapendian(val);
  3064. result:=val;
  3065. end;
  3066. function tscannerfile.tokenreadlongword : longword;
  3067. var
  3068. val : longword;
  3069. begin
  3070. replaytokenbuf.read(val,sizeof(longword));
  3071. if change_endian_for_replay then
  3072. val:=swapendian(val);
  3073. result:=val;
  3074. end;
  3075. function tscannerfile.tokenreadlongint : longint;
  3076. var
  3077. val : longint;
  3078. begin
  3079. replaytokenbuf.read(val,sizeof(longint));
  3080. if change_endian_for_replay then
  3081. val:=swapendian(val);
  3082. result:=val;
  3083. end;
  3084. function tscannerfile.tokenreadshortint : shortint;
  3085. var
  3086. val : shortint;
  3087. begin
  3088. replaytokenbuf.read(val,sizeof(shortint));
  3089. result:=val;
  3090. end;
  3091. function tscannerfile.tokenreadbyte : byte;
  3092. var
  3093. val : byte;
  3094. begin
  3095. replaytokenbuf.read(val,sizeof(byte));
  3096. result:=val;
  3097. end;
  3098. function tscannerfile.tokenreadsmallint : smallint;
  3099. var
  3100. val : smallint;
  3101. begin
  3102. replaytokenbuf.read(val,sizeof(smallint));
  3103. if change_endian_for_replay then
  3104. val:=swapendian(val);
  3105. result:=val;
  3106. end;
  3107. function tscannerfile.tokenreadword : word;
  3108. var
  3109. val : word;
  3110. begin
  3111. replaytokenbuf.read(val,sizeof(word));
  3112. if change_endian_for_replay then
  3113. val:=swapendian(val);
  3114. result:=val;
  3115. end;
  3116. function tscannerfile.tokenreadenum(size : longint) : longword;
  3117. begin
  3118. if size=1 then
  3119. result:=tokenreadbyte
  3120. else if size=2 then
  3121. result:=tokenreadword
  3122. else if size=4 then
  3123. result:=tokenreadlongword
  3124. else
  3125. internalerror(2013112901);
  3126. end;
  3127. procedure tscannerfile.tokenreadset(var b;size : longint);
  3128. var
  3129. i : longint;
  3130. begin
  3131. replaytokenbuf.read(b,size);
  3132. if change_endian_for_replay then
  3133. for i:=0 to size-1 do
  3134. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  3135. end;
  3136. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  3137. begin
  3138. recordtokenbuf.write(b,size);
  3139. end;
  3140. procedure tscannerfile.tokenwriteset(var b;size : longint);
  3141. begin
  3142. recordtokenbuf.write(b,size);
  3143. end;
  3144. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  3145. { This procedure
  3146. needs to be changed whenever
  3147. globals.tsettings type is changed,
  3148. the problem is that no error will appear
  3149. before tests with generics are tested. PM }
  3150. var
  3151. startpos, endpos : longword;
  3152. begin
  3153. { WARNING all those fields need to be in the correct
  3154. order otherwise cross_endian PPU reading will fail }
  3155. startpos:=replaytokenbuf.pos;
  3156. with asettings do
  3157. begin
  3158. alignment.procalign:=tokenreadlongint;
  3159. alignment.loopalign:=tokenreadlongint;
  3160. alignment.jumpalign:=tokenreadlongint;
  3161. alignment.jumpalignskipmax:=tokenreadlongint;
  3162. alignment.coalescealign:=tokenreadlongint;
  3163. alignment.coalescealignskipmax:=tokenreadlongint;
  3164. alignment.constalignmin:=tokenreadlongint;
  3165. alignment.constalignmax:=tokenreadlongint;
  3166. alignment.varalignmin:=tokenreadlongint;
  3167. alignment.varalignmax:=tokenreadlongint;
  3168. alignment.localalignmin:=tokenreadlongint;
  3169. alignment.localalignmax:=tokenreadlongint;
  3170. alignment.recordalignmin:=tokenreadlongint;
  3171. alignment.recordalignmax:=tokenreadlongint;
  3172. alignment.maxCrecordalign:=tokenreadlongint;
  3173. tokenreadset(globalswitches,sizeof(globalswitches));
  3174. tokenreadset(targetswitches,sizeof(targetswitches));
  3175. tokenreadset(moduleswitches,sizeof(moduleswitches));
  3176. tokenreadset(localswitches,sizeof(localswitches));
  3177. tokenreadset(modeswitches,sizeof(modeswitches));
  3178. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  3179. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3180. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3181. tokenreadset(debugswitches,sizeof(debugswitches));
  3182. { 0: old behaviour for sets <=256 elements
  3183. >0: round to this size }
  3184. setalloc:=tokenreadshortint;
  3185. packenum:=tokenreadshortint;
  3186. packrecords:=tokenreadshortint;
  3187. maxfpuregisters:=tokenreadshortint;
  3188. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3189. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3190. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  3191. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  3192. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  3193. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  3194. { tstringencoding is word type,
  3195. thus this should be OK here }
  3196. sourcecodepage:=tstringEncoding(tokenreadword);
  3197. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  3198. disabledircache:=boolean(tokenreadbyte);
  3199. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  3200. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  3201. { While adding ControllerSupport constant, I decided not to store ct_none }
  3202. { on targets not supporting controllers, but this might be changed here and }
  3203. { in tokenwritesettings in the future to unify the PPU structure and handling }
  3204. { of this field in the compiler. }
  3205. {$PUSH}
  3206. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3207. if ControllerSupport then
  3208. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  3209. else
  3210. ControllerType:=ct_none;
  3211. {$POP}
  3212. endpos:=replaytokenbuf.pos;
  3213. if endpos-startpos<>expected_size then
  3214. Comment(V_Error,'Wrong size of Settings read-in');
  3215. end;
  3216. end;
  3217. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  3218. { This procedure
  3219. needs to be changed whenever
  3220. globals.tsettings type is changed,
  3221. the problem is that no error will appear
  3222. before tests with generics are tested. PM }
  3223. var
  3224. sizepos, startpos, endpos : longword;
  3225. begin
  3226. { WARNING all those fields need to be in the correct
  3227. order otherwise cross_endian PPU reading will fail }
  3228. sizepos:=recordtokenbuf.pos;
  3229. size:=0;
  3230. tokenwritesizeint(size);
  3231. startpos:=recordtokenbuf.pos;
  3232. with asettings do
  3233. begin
  3234. tokenwritelongint(alignment.procalign);
  3235. tokenwritelongint(alignment.loopalign);
  3236. tokenwritelongint(alignment.jumpalign);
  3237. tokenwritelongint(alignment.jumpalignskipmax);
  3238. tokenwritelongint(alignment.coalescealign);
  3239. tokenwritelongint(alignment.coalescealignskipmax);
  3240. tokenwritelongint(alignment.constalignmin);
  3241. tokenwritelongint(alignment.constalignmax);
  3242. tokenwritelongint(alignment.varalignmin);
  3243. tokenwritelongint(alignment.varalignmax);
  3244. tokenwritelongint(alignment.localalignmin);
  3245. tokenwritelongint(alignment.localalignmax);
  3246. tokenwritelongint(alignment.recordalignmin);
  3247. tokenwritelongint(alignment.recordalignmax);
  3248. tokenwritelongint(alignment.maxCrecordalign);
  3249. tokenwriteset(globalswitches,sizeof(globalswitches));
  3250. tokenwriteset(targetswitches,sizeof(targetswitches));
  3251. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3252. tokenwriteset(localswitches,sizeof(localswitches));
  3253. tokenwriteset(modeswitches,sizeof(modeswitches));
  3254. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3255. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3256. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3257. tokenwriteset(debugswitches,sizeof(debugswitches));
  3258. { 0: old behaviour for sets <=256 elements
  3259. >0: round to this size }
  3260. tokenwriteshortint(setalloc);
  3261. tokenwriteshortint(packenum);
  3262. tokenwriteshortint(packrecords);
  3263. tokenwriteshortint(maxfpuregisters);
  3264. tokenwriteenum(cputype,sizeof(tcputype));
  3265. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3266. tokenwriteenum(fputype,sizeof(tfputype));
  3267. tokenwriteenum(asmmode,sizeof(tasmmode));
  3268. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3269. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3270. { tstringencoding is word type,
  3271. thus this should be OK here }
  3272. tokenwriteword(sourcecodepage);
  3273. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3274. recordtokenbuf.write(byte(disabledircache),1);
  3275. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3276. { TH: See note about controllertype field in tokenreadsettings. }
  3277. {$PUSH}
  3278. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3279. if ControllerSupport then
  3280. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3281. {$POP}
  3282. endpos:=recordtokenbuf.pos;
  3283. size:=endpos-startpos;
  3284. recordtokenbuf.seek(sizepos);
  3285. tokenwritesizeint(size);
  3286. recordtokenbuf.seek(endpos);
  3287. end;
  3288. end;
  3289. procedure tscannerfile.recordtoken;
  3290. var
  3291. t : ttoken;
  3292. s : tspecialgenerictoken;
  3293. len,msgnb,copy_size : asizeint;
  3294. val : longint;
  3295. b : byte;
  3296. pmsg : pmessagestaterecord;
  3297. begin
  3298. if not assigned(recordtokenbuf) then
  3299. internalerror(200511176);
  3300. t:=_GENERICSPECIALTOKEN;
  3301. { settings changed? }
  3302. { last field pmessage is handled separately below in
  3303. ST_LOADMESSAGES }
  3304. if CompareByte(current_settings,last_settings,
  3305. sizeof(current_settings)-sizeof(pointer))<>0 then
  3306. begin
  3307. { use a special token to record it }
  3308. s:=ST_LOADSETTINGS;
  3309. writetoken(t);
  3310. recordtokenbuf.write(s,1);
  3311. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3312. tokenwritesettings(current_settings,copy_size);
  3313. last_settings:=current_settings;
  3314. end;
  3315. if current_settings.pmessage<>last_message then
  3316. begin
  3317. { use a special token to record it }
  3318. s:=ST_LOADMESSAGES;
  3319. writetoken(t);
  3320. recordtokenbuf.write(s,1);
  3321. msgnb:=0;
  3322. pmsg:=current_settings.pmessage;
  3323. while assigned(pmsg) do
  3324. begin
  3325. if msgnb=high(asizeint) then
  3326. { Too many messages }
  3327. internalerror(2011090401);
  3328. inc(msgnb);
  3329. pmsg:=pmsg^.next;
  3330. end;
  3331. tokenwritesizeint(msgnb);
  3332. pmsg:=current_settings.pmessage;
  3333. while assigned(pmsg) do
  3334. begin
  3335. { What about endianess here?}
  3336. { SB: this is handled by tokenreadlongint }
  3337. val:=pmsg^.value;
  3338. tokenwritelongint(val);
  3339. val:=ord(pmsg^.state);
  3340. tokenwritelongint(val);
  3341. pmsg:=pmsg^.next;
  3342. end;
  3343. last_message:=current_settings.pmessage;
  3344. end;
  3345. { file pos changes? }
  3346. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3347. begin
  3348. s:=ST_FILEINDEX;
  3349. writetoken(t);
  3350. recordtokenbuf.write(s,1);
  3351. tokenwriteword(current_tokenpos.fileindex);
  3352. last_filepos.fileindex:=current_tokenpos.fileindex;
  3353. last_filepos.line:=0;
  3354. end;
  3355. if current_tokenpos.line<>last_filepos.line then
  3356. begin
  3357. s:=ST_LINE;
  3358. writetoken(t);
  3359. recordtokenbuf.write(s,1);
  3360. tokenwritelongint(current_tokenpos.line);
  3361. last_filepos.line:=current_tokenpos.line;
  3362. last_filepos.column:=0;
  3363. end;
  3364. if current_tokenpos.column<>last_filepos.column then
  3365. begin
  3366. s:=ST_COLUMN;
  3367. writetoken(t);
  3368. { can the column be written packed? }
  3369. if current_tokenpos.column<$80 then
  3370. begin
  3371. b:=$80 or current_tokenpos.column;
  3372. recordtokenbuf.write(b,1);
  3373. end
  3374. else
  3375. begin
  3376. recordtokenbuf.write(s,1);
  3377. tokenwriteword(current_tokenpos.column);
  3378. end;
  3379. last_filepos.column:=current_tokenpos.column;
  3380. end;
  3381. writetoken(token);
  3382. if token<>_GENERICSPECIALTOKEN then
  3383. writetoken(idtoken);
  3384. case token of
  3385. _CWCHAR,
  3386. _CWSTRING :
  3387. begin
  3388. tokenwritesizeint(patternw^.len);
  3389. if patternw^.len>0 then
  3390. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3391. end;
  3392. _CSTRING:
  3393. begin
  3394. len:=length(cstringpattern);
  3395. tokenwritesizeint(len);
  3396. if len>0 then
  3397. recordtokenbuf.write(cstringpattern[1],len);
  3398. end;
  3399. _CCHAR,
  3400. _INTCONST,
  3401. _REALNUMBER :
  3402. begin
  3403. { pexpr.pas messes with pattern in case of negative integer consts,
  3404. see around line 2562 the comment of JM; remove the - before recording it
  3405. (FK)
  3406. }
  3407. if (token=_INTCONST) and (pattern[1]='-') then
  3408. delete(pattern,1,1);
  3409. recordtokenbuf.write(pattern[0],1);
  3410. recordtokenbuf.write(pattern[1],length(pattern));
  3411. end;
  3412. _ID :
  3413. begin
  3414. recordtokenbuf.write(orgpattern[0],1);
  3415. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3416. end;
  3417. else
  3418. ;
  3419. end;
  3420. end;
  3421. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3422. begin
  3423. if not assigned(buf) then
  3424. internalerror(200511175);
  3425. { save current scanner state }
  3426. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3427. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3428. {$ifdef check_inputpointer_limits}
  3429. if assigned(hidden_inputpointer) then
  3430. dec_inputpointer;
  3431. {$else not check_inputpointer_limits}
  3432. if assigned(inputpointer) then
  3433. dec(inputpointer);
  3434. {$endif check_inputpointer_limits}
  3435. { install buffer }
  3436. replaytokenbuf:=buf;
  3437. { Initialize value of change_endian_for_replay variable }
  3438. change_endian_for_replay:=change_endian;
  3439. { reload next token }
  3440. replaytokenbuf.seek(0);
  3441. replaytoken;
  3442. end;
  3443. function tscannerfile.readtoken: ttoken;
  3444. var
  3445. b,b2 : byte;
  3446. begin
  3447. replaytokenbuf.read(b,1);
  3448. if (b and $80)<>0 then
  3449. begin
  3450. replaytokenbuf.read(b2,1);
  3451. result:=ttoken(((b and $7f) shl 8) or b2);
  3452. end
  3453. else
  3454. result:=ttoken(b);
  3455. end;
  3456. procedure tscannerfile.replaytoken;
  3457. var
  3458. wlen,mesgnb,copy_size : asizeint;
  3459. specialtoken : tspecialgenerictoken;
  3460. i : byte;
  3461. pmsg,prevmsg : pmessagestaterecord;
  3462. begin
  3463. if not assigned(replaytokenbuf) then
  3464. internalerror(200511177);
  3465. { End of replay buffer? Then load the next char from the file again }
  3466. if replaytokenbuf.pos>=replaytokenbuf.size then
  3467. begin
  3468. token:=replaystack.token;
  3469. idtoken:=replaystack.idtoken;
  3470. pattern:=replaystack.pattern;
  3471. orgpattern:=replaystack.orgpattern;
  3472. setlengthwidestring(patternw,replaystack.patternw^.len);
  3473. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3474. cstringpattern:=replaystack.cstringpattern;
  3475. replaytokenbuf:=replaystack.tokenbuf;
  3476. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3477. { restore compiler settings }
  3478. current_settings:=replaystack.settings;
  3479. popreplaystack;
  3480. {$ifdef check_inputpointer_limits}
  3481. if assigned(hidden_inputpointer) then
  3482. begin
  3483. c:=get_inputpointer_char;
  3484. inc_inputpointer;
  3485. end;
  3486. {$else not check_inputpointer_limits}
  3487. if assigned(inputpointer) then
  3488. begin
  3489. c:=inputpointer^;
  3490. inc(inputpointer);
  3491. end;
  3492. {$endif check_inputpointer_limits}
  3493. exit;
  3494. end;
  3495. repeat
  3496. { load token from the buffer }
  3497. token:=readtoken;
  3498. if token<>_GENERICSPECIALTOKEN then
  3499. idtoken:=readtoken
  3500. else
  3501. idtoken:=_NOID;
  3502. case token of
  3503. _CWCHAR,
  3504. _CWSTRING :
  3505. begin
  3506. wlen:=tokenreadsizeint;
  3507. setlengthwidestring(patternw,wlen);
  3508. if wlen>0 then
  3509. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3510. orgpattern:='';
  3511. pattern:='';
  3512. cstringpattern:='';
  3513. end;
  3514. _CSTRING:
  3515. begin
  3516. wlen:=tokenreadsizeint;
  3517. if wlen>0 then
  3518. begin
  3519. setlength(cstringpattern,wlen);
  3520. replaytokenbuf.read(cstringpattern[1],wlen);
  3521. end
  3522. else
  3523. cstringpattern:='';
  3524. orgpattern:='';
  3525. pattern:='';
  3526. end;
  3527. _CCHAR,
  3528. _INTCONST,
  3529. _REALNUMBER :
  3530. begin
  3531. replaytokenbuf.read(pattern[0],1);
  3532. replaytokenbuf.read(pattern[1],length(pattern));
  3533. orgpattern:='';
  3534. end;
  3535. _ID :
  3536. begin
  3537. replaytokenbuf.read(orgpattern[0],1);
  3538. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3539. pattern:=upper(orgpattern);
  3540. end;
  3541. _GENERICSPECIALTOKEN:
  3542. begin
  3543. replaytokenbuf.read(specialtoken,1);
  3544. { packed column? }
  3545. if (ord(specialtoken) and $80)<>0 then
  3546. begin
  3547. current_tokenpos.column:=ord(specialtoken) and $7f;
  3548. current_filepos:=current_tokenpos;
  3549. end
  3550. else
  3551. case specialtoken of
  3552. ST_LOADSETTINGS:
  3553. begin
  3554. copy_size:=tokenreadsizeint;
  3555. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3556. // internalerror(2011090501);
  3557. {
  3558. replaytokenbuf.read(current_settings,copy_size);
  3559. }
  3560. tokenreadsettings(current_settings,copy_size);
  3561. end;
  3562. ST_LOADMESSAGES:
  3563. begin
  3564. current_settings.pmessage:=nil;
  3565. mesgnb:=tokenreadsizeint;
  3566. prevmsg:=nil;
  3567. for i:=1 to mesgnb do
  3568. begin
  3569. new(pmsg);
  3570. if i=1 then
  3571. current_settings.pmessage:=pmsg
  3572. else
  3573. prevmsg^.next:=pmsg;
  3574. pmsg^.value:=tokenreadlongint;
  3575. pmsg^.state:=tmsgstate(tokenreadlongint);
  3576. pmsg^.next:=nil;
  3577. prevmsg:=pmsg;
  3578. end;
  3579. end;
  3580. ST_LINE:
  3581. begin
  3582. current_tokenpos.line:=tokenreadlongint;
  3583. current_filepos:=current_tokenpos;
  3584. end;
  3585. ST_COLUMN:
  3586. begin
  3587. current_tokenpos.column:=tokenreadword;
  3588. current_filepos:=current_tokenpos;
  3589. end;
  3590. ST_FILEINDEX:
  3591. begin
  3592. current_tokenpos.fileindex:=tokenreadword;
  3593. current_filepos:=current_tokenpos;
  3594. end;
  3595. end;
  3596. continue;
  3597. end;
  3598. else
  3599. ;
  3600. end;
  3601. break;
  3602. until false;
  3603. end;
  3604. procedure tscannerfile.addfile(hp:tinputfile);
  3605. begin
  3606. saveinputfile;
  3607. { add to list }
  3608. hp.next:=inputfile;
  3609. inputfile:=hp;
  3610. { load new inputfile }
  3611. restoreinputfile;
  3612. end;
  3613. procedure tscannerfile.reload;
  3614. var
  3615. wasmacro: Boolean;
  3616. begin
  3617. with inputfile do
  3618. begin
  3619. { when nothing more to read then leave immediatly, so we
  3620. don't change the current_filepos and leave it point to the last
  3621. char }
  3622. if (c=#26) and (not assigned(next)) then
  3623. exit;
  3624. repeat
  3625. { still more to read?, then change the #0 to a space so its seen
  3626. as a separator, this can't be used for macro's which can change
  3627. the place of the #0 in the buffer with tempopen }
  3628. if (c=#0) and (bufsize>0) and
  3629. not(inputfile.is_macro) and
  3630. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3631. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3632. {$else not CHECK_INPUTPOINTER_LIMITS}
  3633. (inputpointer-inputbuffer<bufsize) then
  3634. {$endif CHECK_INPUTPOINTER_LIMITS}
  3635. begin
  3636. c:=' ';
  3637. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3638. inc_inputpointer;
  3639. {$else not CHECK_INPUTPOINTER_LIMITS}
  3640. inc(inputpointer);
  3641. {$endif CHECK_INPUTPOINTER_LIMITS}
  3642. exit;
  3643. end;
  3644. { can we read more from this file ? }
  3645. if (c<>#26) and (not endoffile) then
  3646. begin
  3647. readbuf;
  3648. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3649. hidden_inputpointer:=buf;
  3650. hidden_inputbuffer:=buf;
  3651. {$else not CHECK_INPUTPOINTER_LIMITS}
  3652. inputpointer:=buf;
  3653. inputbuffer:=buf;
  3654. {$endif CHECK_INPUTPOINTER_LIMITS}
  3655. inputstart:=bufstart;
  3656. { first line? }
  3657. if line_no=0 then
  3658. begin
  3659. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3660. c:=get_inputpointer_char;
  3661. { eat utf-8 signature? }
  3662. if (bufsize>2) and
  3663. (ord(get_inputpointer_char)=$ef) and
  3664. (ord(get_inputpointer_char(1))=$bb) and
  3665. (ord(get_inputpointer_char(2))=$bf) then
  3666. begin
  3667. {$else not CHECK_INPUTPOINTER_LIMITS}
  3668. c:=inputpointer^;
  3669. { eat utf-8 signature? }
  3670. if (bufsize>2) and
  3671. (ord(inputpointer^)=$ef) and
  3672. (ord((inputpointer+1)^)=$bb) and
  3673. (ord((inputpointer+2)^)=$bf) then
  3674. begin
  3675. {$endif CHECK_INPUTPOINTER_LIMITS}
  3676. (* we don't support including files with an UTF-8 bom
  3677. inside another file that wasn't encoded as UTF-8
  3678. already (we don't support {$codepage xxx} switches in
  3679. the middle of a file either) *)
  3680. if (current_settings.sourcecodepage<>CP_UTF8) and
  3681. not current_module.in_global then
  3682. Message(scanner_f_illegal_utf8_bom);
  3683. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3684. inc_inputpointer(3);
  3685. {$else not CHECK_INPUTPOINTER_LIMITS}
  3686. inc(inputpointer,3);
  3687. {$endif CHECK_INPUTPOINTER_LIMITS}
  3688. message(scan_c_switching_to_utf8);
  3689. current_settings.sourcecodepage:=CP_UTF8;
  3690. exclude(current_settings.moduleswitches,cs_system_codepage);
  3691. include(current_settings.moduleswitches,cs_explicit_codepage);
  3692. end;
  3693. line_no:=1;
  3694. if cs_asm_source in current_settings.globalswitches then
  3695. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3696. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3697. {$else not CHECK_INPUTPOINTER_LIMITS}
  3698. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3699. {$endif CHECK_INPUTPOINTER_LIMITS}
  3700. end;
  3701. end
  3702. else
  3703. begin
  3704. wasmacro:=inputfile.is_macro;
  3705. { load eof position in tokenpos/current_filepos }
  3706. gettokenpos;
  3707. { close file }
  3708. closeinputfile;
  3709. { no next module, than EOF }
  3710. if not assigned(inputfile.next) then
  3711. begin
  3712. c:=#26;
  3713. exit;
  3714. end;
  3715. { load next file and reopen it }
  3716. nextfile;
  3717. tempopeninputfile;
  3718. { status }
  3719. Message1(scan_t_back_in,inputfile.name);
  3720. { end of include file is like a line break which ends e.g. also // style comments }
  3721. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3722. begin
  3723. c:=#10;
  3724. { ... but we have to decrease the line number first because it is increased due to this
  3725. inserted line break later on }
  3726. dec(line_no);
  3727. exit;
  3728. end;
  3729. end;
  3730. { load next char }
  3731. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3732. c:=get_inputpointer_char;
  3733. inc_inputpointer;
  3734. {$else not CHECK_INPUTPOINTER_LIMITS}
  3735. c:=inputpointer^;
  3736. inc(inputpointer);
  3737. {$endif CHECK_INPUTPOINTER_LIMITS}
  3738. until c<>#0; { if also end, then reload again }
  3739. end;
  3740. end;
  3741. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3742. var
  3743. hp : tinputfile;
  3744. begin
  3745. { save old postion }
  3746. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3747. dec_inputpointer;
  3748. {$else not CHECK_INPUTPOINTER_LIMITS}
  3749. dec(inputpointer);
  3750. {$endif CHECK_INPUTPOINTER_LIMITS}
  3751. tempcloseinputfile;
  3752. { create macro 'file' }
  3753. { use special name to dispose after !! }
  3754. hp:=do_openinputfile('_Macro_.'+macname);
  3755. addfile(hp);
  3756. with inputfile do
  3757. begin
  3758. inc(macro_nesting_depth);
  3759. setmacro(p,len);
  3760. { local buffer }
  3761. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3762. hidden_inputbuffer:=buf;
  3763. hidden_inputpointer:=buf;
  3764. {$else not CHECK_INPUTPOINTER_LIMITS}
  3765. inputbuffer:=buf;
  3766. inputpointer:=buf;
  3767. {$endif CHECK_INPUTPOINTER_LIMITS}
  3768. inputstart:=bufstart;
  3769. ref_index:=fileindex;
  3770. internally_generated_macro:=internally_generated;
  3771. end;
  3772. { reset line }
  3773. line_no:=line;
  3774. lastlinepos:=0;
  3775. lasttokenpos:=0;
  3776. nexttokenpos:=0;
  3777. { load new c }
  3778. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3779. c:=get_inputpointer_char;
  3780. inc_inputpointer;
  3781. {$else not CHECK_INPUTPOINTER_LIMITS}
  3782. c:=inputpointer^;
  3783. inc(inputpointer);
  3784. {$endif CHECK_INPUTPOINTER_LIMITS}
  3785. end;
  3786. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3787. begin
  3788. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3789. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3790. {$else not CHECK_INPUTPOINTER_LIMITS}
  3791. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3792. {$endif CHECK_INPUTPOINTER_LIMITS}
  3793. filepos.line:=line_no;
  3794. filepos.column:=tokenpos-lastlinepos;
  3795. filepos.fileindex:=inputfile.ref_index;
  3796. filepos.moduleindex:=current_module.unit_index;
  3797. end;
  3798. procedure tscannerfile.gettokenpos;
  3799. { load the values of tokenpos and lasttokenpos }
  3800. begin
  3801. do_gettokenpos(lasttokenpos,current_tokenpos);
  3802. current_filepos:=current_tokenpos;
  3803. end;
  3804. procedure tscannerfile.cachenexttokenpos;
  3805. begin
  3806. do_gettokenpos(nexttokenpos,next_filepos);
  3807. end;
  3808. procedure tscannerfile.setnexttoken;
  3809. begin
  3810. token:=nexttoken;
  3811. nexttoken:=NOTOKEN;
  3812. lasttokenpos:=nexttokenpos;
  3813. current_tokenpos:=next_filepos;
  3814. current_filepos:=current_tokenpos;
  3815. nexttokenpos:=0;
  3816. end;
  3817. procedure tscannerfile.savetokenpos;
  3818. begin
  3819. oldlasttokenpos:=lasttokenpos;
  3820. oldcurrent_filepos:=current_filepos;
  3821. oldcurrent_tokenpos:=current_tokenpos;
  3822. end;
  3823. procedure tscannerfile.restoretokenpos;
  3824. begin
  3825. lasttokenpos:=oldlasttokenpos;
  3826. current_filepos:=oldcurrent_filepos;
  3827. current_tokenpos:=oldcurrent_tokenpos;
  3828. end;
  3829. procedure tscannerfile.inc_comment_level;
  3830. begin
  3831. if (m_nested_comment in current_settings.modeswitches) then
  3832. inc(comment_level)
  3833. else
  3834. comment_level:=1;
  3835. if (comment_level>1) then
  3836. begin
  3837. savetokenpos;
  3838. gettokenpos; { update for warning }
  3839. Message1(scan_w_comment_level,tostr(comment_level));
  3840. restoretokenpos;
  3841. end;
  3842. end;
  3843. procedure tscannerfile.dec_comment_level;
  3844. begin
  3845. if (m_nested_comment in current_settings.modeswitches) then
  3846. dec(comment_level)
  3847. else
  3848. comment_level:=0;
  3849. end;
  3850. procedure tscannerfile.linebreak;
  3851. var
  3852. cur : char;
  3853. begin
  3854. with inputfile do
  3855. begin
  3856. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3857. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3858. {$else not CHECK_INPUTPOINTER_LIMITS}
  3859. if (byte(inputpointer^)=0) and not(endoffile) then
  3860. {$endif CHECK_INPUTPOINTER_LIMITS}
  3861. begin
  3862. cur:=c;
  3863. reload;
  3864. if byte(cur)+byte(c)<>23 then
  3865. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3866. dec_inputpointer;
  3867. {$else not CHECK_INPUTPOINTER_LIMITS}
  3868. dec(inputpointer);
  3869. {$endif CHECK_INPUTPOINTER_LIMITS}
  3870. end
  3871. else
  3872. begin
  3873. { Support all combination of #10 and #13 as line break }
  3874. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3875. if (byte(get_inputpointer_char)+byte(c)=23) then
  3876. inc_inputpointer;
  3877. {$else not CHECK_INPUTPOINTER_LIMITS}
  3878. if (byte(inputpointer^)+byte(c)=23) then
  3879. inc(inputpointer);
  3880. {$endif CHECK_INPUTPOINTER_LIMITS}
  3881. end;
  3882. { Always return #10 as line break }
  3883. c:=#10;
  3884. { increase line counters }
  3885. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3886. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3887. {$else not CHECK_INPUTPOINTER_LIMITS}
  3888. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3889. {$endif CHECK_INPUTPOINTER_LIMITS}
  3890. inc(line_no);
  3891. { update linebuffer }
  3892. if cs_asm_source in current_settings.globalswitches then
  3893. inputfile.setline(line_no,lastlinepos);
  3894. { update for status and call the show status routine,
  3895. but don't touch current_filepos ! }
  3896. savetokenpos;
  3897. gettokenpos; { update for v_status }
  3898. inc(status.compiledlines);
  3899. ShowStatus;
  3900. restoretokenpos;
  3901. end;
  3902. end;
  3903. procedure tscannerfile.illegal_char(c:char);
  3904. var
  3905. s : string;
  3906. begin
  3907. if c in [#32..#255] then
  3908. s:=''''+c+''''
  3909. else
  3910. s:='#'+tostr(ord(c));
  3911. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3912. end;
  3913. procedure tscannerfile.end_of_file;
  3914. begin
  3915. checkpreprocstack;
  3916. Message(scan_f_end_of_file);
  3917. end;
  3918. {-------------------------------------------
  3919. IF Conditional Handling
  3920. -------------------------------------------}
  3921. procedure tscannerfile.checkpreprocstack;
  3922. begin
  3923. { check for missing ifdefs }
  3924. while assigned(preprocstack) do
  3925. begin
  3926. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3927. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3928. tostr(preprocstack.line_nb));
  3929. poppreprocstack;
  3930. end;
  3931. end;
  3932. procedure tscannerfile.poppreprocstack;
  3933. var
  3934. hp : tpreprocstack;
  3935. begin
  3936. if assigned(preprocstack) then
  3937. begin
  3938. Message1(scan_c_endif_found,preprocstack.name);
  3939. hp:=preprocstack.next;
  3940. preprocstack.free;
  3941. preprocstack:=hp;
  3942. end
  3943. else
  3944. Message(scan_e_endif_without_if);
  3945. end;
  3946. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3947. var
  3948. condition: Boolean;
  3949. valuedescr: String;
  3950. begin
  3951. if (preprocstack=nil) or preprocstack.accept then
  3952. condition:=compile_time_predicate(valuedescr)
  3953. else
  3954. begin
  3955. condition:= false;
  3956. valuedescr:= '';
  3957. end;
  3958. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3959. preprocstack.name:=valuedescr;
  3960. preprocstack.line_nb:=line_no;
  3961. preprocstack.fileindex:=current_filepos.fileindex;
  3962. if preprocstack.accept then
  3963. Message2(messid,preprocstack.name,'accepted')
  3964. else
  3965. Message2(messid,preprocstack.name,'rejected');
  3966. end;
  3967. procedure tscannerfile.elsepreprocstack;
  3968. begin
  3969. if assigned(preprocstack) and
  3970. (preprocstack.typ<>pp_else) then
  3971. begin
  3972. if (preprocstack.typ=pp_elseif) then
  3973. preprocstack.accept:=false
  3974. else
  3975. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3976. preprocstack.accept:=not preprocstack.accept;
  3977. preprocstack.iftyp:=preprocstack.typ;
  3978. preprocstack.typ:=pp_else;
  3979. preprocstack.line_nb:=line_no;
  3980. preprocstack.fileindex:=current_filepos.fileindex;
  3981. if preprocstack.accept then
  3982. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3983. else
  3984. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3985. end
  3986. else
  3987. Message(scan_e_endif_without_if);
  3988. end;
  3989. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3990. var
  3991. valuedescr: String;
  3992. begin
  3993. if assigned(preprocstack) and
  3994. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  3995. begin
  3996. { when the branch is accepted we use pp_elseif so we know that
  3997. all the next branches need to be rejected. when this branch is still
  3998. not accepted then leave it at pp_if }
  3999. if (preprocstack.typ=pp_elseif) then
  4000. preprocstack.accept:=false
  4001. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4002. begin
  4003. preprocstack.accept:=false;
  4004. preprocstack.typ:=pp_elseif;
  4005. end
  4006. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4007. and compile_time_predicate(valuedescr) then
  4008. begin
  4009. preprocstack.name:=valuedescr;
  4010. preprocstack.accept:=true;
  4011. preprocstack.typ:=pp_elseif;
  4012. end;
  4013. preprocstack.line_nb:=line_no;
  4014. preprocstack.fileindex:=current_filepos.fileindex;
  4015. if preprocstack.accept then
  4016. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4017. else
  4018. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4019. end
  4020. else
  4021. Message(scan_e_endif_without_if);
  4022. end;
  4023. procedure tscannerfile.popreplaystack;
  4024. var
  4025. hp : treplaystack;
  4026. begin
  4027. if assigned(replaystack) then
  4028. begin
  4029. hp:=replaystack.next;
  4030. replaystack.free;
  4031. replaystack:=hp;
  4032. end;
  4033. end;
  4034. function tscannerfile.replay_stack_depth:longint;
  4035. var
  4036. tmp: treplaystack;
  4037. begin
  4038. result:=0;
  4039. tmp:=replaystack;
  4040. while assigned(tmp) do
  4041. begin
  4042. inc(result);
  4043. tmp:=tmp.next;
  4044. end;
  4045. end;
  4046. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4047. begin
  4048. savetokenpos;
  4049. repeat
  4050. current_scanner.gettokenpos;
  4051. Message1(scan_d_handling_switch,'$'+p.name);
  4052. p.proc();
  4053. { accept the text ? }
  4054. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4055. break
  4056. else
  4057. begin
  4058. current_scanner.gettokenpos;
  4059. Message(scan_c_skipping_until);
  4060. repeat
  4061. current_scanner.skipuntildirective;
  4062. if not (m_mac in current_settings.modeswitches) then
  4063. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4064. else
  4065. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4066. until assigned(p) and (p.is_conditional);
  4067. current_scanner.gettokenpos;
  4068. end;
  4069. until false;
  4070. restoretokenpos;
  4071. end;
  4072. procedure tscannerfile.handledirectives;
  4073. var
  4074. t : tdirectiveitem;
  4075. hs : string;
  4076. begin
  4077. gettokenpos;
  4078. readchar; {Remove the $}
  4079. hs:=readid;
  4080. { handle empty directive }
  4081. if hs='' then
  4082. begin
  4083. Message1(scan_w_illegal_switch,'$');
  4084. exit;
  4085. end;
  4086. {$ifdef PREPROCWRITE}
  4087. if parapreprocess then
  4088. begin
  4089. if not (m_mac in current_settings.modeswitches) then
  4090. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4091. else
  4092. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4093. if assigned(t) and not(t.is_conditional) then
  4094. begin
  4095. preprocfile.AddSpace;
  4096. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4097. exit;
  4098. end;
  4099. end;
  4100. {$endif PREPROCWRITE}
  4101. { skip this directive? }
  4102. if (ignoredirectives.find(hs)<>nil) then
  4103. begin
  4104. if (comment_level>0) then
  4105. readcomment;
  4106. { we've read the whole comment }
  4107. current_commentstyle:=comment_none;
  4108. exit;
  4109. end;
  4110. { Check for compiler switches }
  4111. while (length(hs)=1) and (c in ['-','+']) do
  4112. begin
  4113. Message1(scan_d_handling_switch,'$'+hs+c);
  4114. HandleSwitch(hs[1],c);
  4115. current_scanner.readchar; {Remove + or -}
  4116. if c=',' then
  4117. begin
  4118. current_scanner.readchar; {Remove , }
  4119. { read next switch, support $v+,$+}
  4120. hs:=current_scanner.readid;
  4121. if (hs='') then
  4122. begin
  4123. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4124. begin
  4125. current_scanner.readchar; { skip $ }
  4126. hs:=current_scanner.readid;
  4127. end;
  4128. if (hs='') then
  4129. Message1(scan_w_illegal_directive,'$'+c);
  4130. end;
  4131. end
  4132. else
  4133. hs:='';
  4134. end;
  4135. { directives may follow switches after a , }
  4136. if hs<>'' then
  4137. begin
  4138. if not (m_mac in current_settings.modeswitches) then
  4139. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4140. else
  4141. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4142. if assigned(t) then
  4143. begin
  4144. if t.is_conditional then
  4145. handleconditional(t)
  4146. else
  4147. begin
  4148. Message1(scan_d_handling_switch,'$'+hs);
  4149. t.proc();
  4150. end;
  4151. end
  4152. else
  4153. begin
  4154. current_scanner.ignoredirectives.Add(hs,nil);
  4155. Message1(scan_w_illegal_directive,'$'+hs);
  4156. end;
  4157. { conditionals already read the comment }
  4158. if (current_scanner.comment_level>0) then
  4159. current_scanner.readcomment;
  4160. { we've read the whole comment }
  4161. current_commentstyle:=comment_none;
  4162. end;
  4163. end;
  4164. procedure tscannerfile.readchar;
  4165. begin
  4166. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4167. c:=get_inputpointer_char;
  4168. {$else not CHECK_INPUTPOINTER_LIMITS}
  4169. c:=inputpointer^;
  4170. {$endif CHECK_INPUTPOINTER_LIMITS}
  4171. if c=#0 then
  4172. reload
  4173. else
  4174. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4175. inc_inputpointer;
  4176. {$else not CHECK_INPUTPOINTER_LIMITS}
  4177. inc(inputpointer);
  4178. {$endif CHECK_INPUTPOINTER_LIMITS}
  4179. end;
  4180. procedure tscannerfile.readstring;
  4181. var
  4182. i : longint;
  4183. err : boolean;
  4184. begin
  4185. err:=false;
  4186. i:=0;
  4187. repeat
  4188. case c of
  4189. '_',
  4190. '0'..'9',
  4191. 'A'..'Z',
  4192. 'a'..'z' :
  4193. begin
  4194. if i<255 then
  4195. begin
  4196. inc(i);
  4197. orgpattern[i]:=c;
  4198. if c in ['a'..'z'] then
  4199. pattern[i]:=chr(ord(c)-32)
  4200. else
  4201. pattern[i]:=c;
  4202. end
  4203. else
  4204. begin
  4205. if not err then
  4206. begin
  4207. Message(scan_e_string_exceeds_255_chars);
  4208. err:=true;
  4209. end;
  4210. end;
  4211. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4212. c:=get_inputpointer_char;
  4213. inc_inputpointer;
  4214. {$else not CHECK_INPUTPOINTER_LIMITS}
  4215. c:=inputpointer^;
  4216. inc(inputpointer);
  4217. {$endif CHECK_INPUTPOINTER_LIMITS}
  4218. end;
  4219. #0 :
  4220. reload;
  4221. else if inputfile.internally_generated_macro and
  4222. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4223. begin
  4224. if i<255 then
  4225. begin
  4226. inc(i);
  4227. orgpattern[i]:=c;
  4228. pattern[i]:=c;
  4229. end
  4230. else
  4231. begin
  4232. if not err then
  4233. begin
  4234. Message(scan_e_string_exceeds_255_chars);
  4235. err:=true;
  4236. end;
  4237. end;
  4238. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4239. c:=get_inputpointer_char;
  4240. inc_inputpointer;
  4241. {$else not CHECK_INPUTPOINTER_LIMITS}
  4242. c:=inputpointer^;
  4243. inc(inputpointer);
  4244. {$endif CHECK_INPUTPOINTER_LIMITS}
  4245. end
  4246. else
  4247. break;
  4248. end;
  4249. until false;
  4250. orgpattern[0]:=chr(i);
  4251. pattern[0]:=chr(i);
  4252. end;
  4253. procedure tscannerfile.readnumber;
  4254. var
  4255. base,
  4256. i : longint;
  4257. firstdigitread: Boolean;
  4258. begin
  4259. case c of
  4260. '%' :
  4261. begin
  4262. readchar;
  4263. base:=2;
  4264. pattern[1]:='%';
  4265. i:=1;
  4266. end;
  4267. '&' :
  4268. begin
  4269. readchar;
  4270. base:=8;
  4271. pattern[1]:='&';
  4272. i:=1;
  4273. end;
  4274. '$' :
  4275. begin
  4276. readchar;
  4277. base:=16;
  4278. pattern[1]:='$';
  4279. i:=1;
  4280. end;
  4281. else
  4282. begin
  4283. base:=10;
  4284. i:=0;
  4285. end;
  4286. end;
  4287. firstdigitread:=false;
  4288. while ((base>=10) and (c in ['0'..'9'])) or
  4289. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4290. ((base=8) and (c in ['0'..'7'])) or
  4291. ((base=2) and (c in ['0'..'1'])) or
  4292. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4293. begin
  4294. if (i<255) and (c<>'_') then
  4295. begin
  4296. inc(i);
  4297. pattern[i]:=c;
  4298. end;
  4299. readchar;
  4300. firstdigitread:=true;
  4301. end;
  4302. pattern[0]:=chr(i);
  4303. end;
  4304. function tscannerfile.readid:string;
  4305. begin
  4306. readstring;
  4307. readid:=pattern;
  4308. end;
  4309. function tscannerfile.readval:longint;
  4310. var
  4311. l : longint;
  4312. w : integer;
  4313. begin
  4314. readnumber;
  4315. val(pattern,l,w);
  4316. readval:=l;
  4317. end;
  4318. function tscannerfile.readval64:int64;
  4319. var
  4320. l : int64;
  4321. w : integer;
  4322. begin
  4323. readnumber;
  4324. val(pattern,l,w);
  4325. readval64:=l;
  4326. end;
  4327. function tscannerfile.readcomment(include_special_char: boolean):string;
  4328. var
  4329. i : longint;
  4330. begin
  4331. i:=0;
  4332. repeat
  4333. case c of
  4334. '{' :
  4335. begin
  4336. if (include_special_char) and (i<255) then
  4337. begin
  4338. inc(i);
  4339. readcomment[i]:=c;
  4340. end;
  4341. if current_commentstyle=comment_tp then
  4342. inc_comment_level;
  4343. end;
  4344. '}' :
  4345. begin
  4346. if (include_special_char) and (i<255) then
  4347. begin
  4348. inc(i);
  4349. readcomment[i]:=c;
  4350. end;
  4351. if current_commentstyle=comment_tp then
  4352. begin
  4353. readchar;
  4354. dec_comment_level;
  4355. if comment_level=0 then
  4356. break
  4357. else
  4358. continue;
  4359. end;
  4360. end;
  4361. '*' :
  4362. begin
  4363. if current_commentstyle=comment_oldtp then
  4364. begin
  4365. readchar;
  4366. if c=')' then
  4367. begin
  4368. readchar;
  4369. dec_comment_level;
  4370. break;
  4371. end
  4372. else
  4373. { Add both characters !!}
  4374. if (i<255) then
  4375. begin
  4376. inc(i);
  4377. readcomment[i]:='*';
  4378. if (i<255) then
  4379. begin
  4380. inc(i);
  4381. readcomment[i]:=c;
  4382. end;
  4383. end;
  4384. end
  4385. else
  4386. { Not old TP comment, so add...}
  4387. begin
  4388. if (i<255) then
  4389. begin
  4390. inc(i);
  4391. readcomment[i]:='*';
  4392. end;
  4393. end;
  4394. end;
  4395. #10,#13 :
  4396. linebreak;
  4397. #26 :
  4398. end_of_file;
  4399. else
  4400. begin
  4401. if (i<255) then
  4402. begin
  4403. inc(i);
  4404. readcomment[i]:=c;
  4405. end;
  4406. end;
  4407. end;
  4408. readchar;
  4409. until false;
  4410. readcomment[0]:=chr(i);
  4411. end;
  4412. function tscannerfile.readquotedstring:string;
  4413. var
  4414. i : longint;
  4415. msgwritten : boolean;
  4416. begin
  4417. i:=0;
  4418. msgwritten:=false;
  4419. if (c='''') then
  4420. begin
  4421. repeat
  4422. readchar;
  4423. case c of
  4424. #26 :
  4425. end_of_file;
  4426. #10,#13 :
  4427. Message(scan_f_string_exceeds_line);
  4428. '''' :
  4429. begin
  4430. readchar;
  4431. if c<>'''' then
  4432. break;
  4433. end;
  4434. end;
  4435. if i<255 then
  4436. begin
  4437. inc(i);
  4438. result[i]:=c;
  4439. end
  4440. else
  4441. begin
  4442. if not msgwritten then
  4443. begin
  4444. Message(scan_e_string_exceeds_255_chars);
  4445. msgwritten:=true;
  4446. end;
  4447. end;
  4448. until false;
  4449. end;
  4450. result[0]:=chr(i);
  4451. end;
  4452. function tscannerfile.readstate:char;
  4453. var
  4454. state : char;
  4455. begin
  4456. state:=' ';
  4457. if c=' ' then
  4458. begin
  4459. current_scanner.skipspace;
  4460. current_scanner.readid;
  4461. if pattern='ON' then
  4462. state:='+'
  4463. else
  4464. if pattern='OFF' then
  4465. state:='-';
  4466. end
  4467. else
  4468. state:=c;
  4469. if not (state in ['+','-']) then
  4470. Message(scan_e_wrong_switch_toggle);
  4471. readstate:=state;
  4472. end;
  4473. function tscannerfile.readoptionalstate(fallback:char):char;
  4474. var
  4475. state : char;
  4476. begin
  4477. state:=' ';
  4478. if c=' ' then
  4479. begin
  4480. current_scanner.skipspace;
  4481. if c in ['*','}'] then
  4482. state:=fallback
  4483. else
  4484. begin
  4485. current_scanner.readid;
  4486. if pattern='ON' then
  4487. state:='+'
  4488. else
  4489. if pattern='OFF' then
  4490. state:='-';
  4491. end;
  4492. end
  4493. else
  4494. if c in ['*','}'] then
  4495. state:=fallback
  4496. else
  4497. state:=c;
  4498. if not (state in ['+','-']) then
  4499. Message(scan_e_wrong_switch_toggle);
  4500. readoptionalstate:=state;
  4501. end;
  4502. function tscannerfile.readstatedefault:char;
  4503. var
  4504. state : char;
  4505. begin
  4506. state:=' ';
  4507. if c=' ' then
  4508. begin
  4509. current_scanner.skipspace;
  4510. current_scanner.readid;
  4511. if pattern='ON' then
  4512. state:='+'
  4513. else
  4514. if pattern='OFF' then
  4515. state:='-'
  4516. else
  4517. if pattern='DEFAULT' then
  4518. state:='*';
  4519. end
  4520. else
  4521. state:=c;
  4522. if not (state in ['+','-','*']) then
  4523. Message(scan_e_wrong_switch_toggle_default);
  4524. readstatedefault:=state;
  4525. end;
  4526. procedure tscannerfile.skipspace;
  4527. begin
  4528. repeat
  4529. case c of
  4530. #26 :
  4531. begin
  4532. reload;
  4533. if (c=#26) and not assigned(inputfile.next) then
  4534. break;
  4535. continue;
  4536. end;
  4537. #10,
  4538. #13 :
  4539. linebreak;
  4540. #9,#11,#12,' ' :
  4541. ;
  4542. else
  4543. break;
  4544. end;
  4545. readchar;
  4546. until false;
  4547. end;
  4548. procedure tscannerfile.skipuntildirective;
  4549. var
  4550. found : longint;
  4551. next_char_loaded : boolean;
  4552. begin
  4553. found:=0;
  4554. next_char_loaded:=false;
  4555. repeat
  4556. case c of
  4557. #10,
  4558. #13 :
  4559. linebreak;
  4560. #26 :
  4561. begin
  4562. reload;
  4563. if (c=#26) and not assigned(inputfile.next) then
  4564. end_of_file;
  4565. continue;
  4566. end;
  4567. '{' :
  4568. begin
  4569. if (current_commentstyle in [comment_tp,comment_none]) then
  4570. begin
  4571. current_commentstyle:=comment_tp;
  4572. if (comment_level=0) then
  4573. found:=1;
  4574. inc_comment_level;
  4575. end;
  4576. end;
  4577. '*' :
  4578. begin
  4579. if (current_commentstyle=comment_oldtp) then
  4580. begin
  4581. readchar;
  4582. if c=')' then
  4583. begin
  4584. dec_comment_level;
  4585. found:=0;
  4586. current_commentstyle:=comment_none;
  4587. end
  4588. else
  4589. next_char_loaded:=true;
  4590. end
  4591. else
  4592. found := 0;
  4593. end;
  4594. '}' :
  4595. begin
  4596. if (current_commentstyle=comment_tp) then
  4597. begin
  4598. dec_comment_level;
  4599. if (comment_level=0) then
  4600. current_commentstyle:=comment_none;
  4601. found:=0;
  4602. end;
  4603. end;
  4604. '$' :
  4605. begin
  4606. if found=1 then
  4607. found:=2;
  4608. end;
  4609. '''' :
  4610. if (current_commentstyle=comment_none) then
  4611. begin
  4612. repeat
  4613. readchar;
  4614. case c of
  4615. #26 :
  4616. end_of_file;
  4617. #10,#13 :
  4618. break;
  4619. '''' :
  4620. begin
  4621. readchar;
  4622. if c<>'''' then
  4623. begin
  4624. next_char_loaded:=true;
  4625. break;
  4626. end;
  4627. end;
  4628. end;
  4629. until false;
  4630. end;
  4631. '(' :
  4632. begin
  4633. if (current_commentstyle=comment_none) then
  4634. begin
  4635. readchar;
  4636. if c='*' then
  4637. begin
  4638. readchar;
  4639. if c='$' then
  4640. begin
  4641. found:=2;
  4642. inc_comment_level;
  4643. current_commentstyle:=comment_oldtp;
  4644. end
  4645. else
  4646. begin
  4647. skipoldtpcomment(false);
  4648. next_char_loaded:=true;
  4649. end;
  4650. end
  4651. else
  4652. next_char_loaded:=true;
  4653. end
  4654. else
  4655. found:=0;
  4656. end;
  4657. '/' :
  4658. begin
  4659. if (current_commentstyle=comment_none) then
  4660. begin
  4661. readchar;
  4662. if c='/' then
  4663. skipdelphicomment;
  4664. next_char_loaded:=true;
  4665. end
  4666. else
  4667. found:=0;
  4668. end;
  4669. else
  4670. found:=0;
  4671. end;
  4672. if next_char_loaded then
  4673. next_char_loaded:=false
  4674. else
  4675. readchar;
  4676. until (found=2);
  4677. end;
  4678. {****************************************************************************
  4679. Comment Handling
  4680. ****************************************************************************}
  4681. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4682. begin
  4683. current_commentstyle:=comment_tp;
  4684. if read_first_char then
  4685. readchar;
  4686. inc_comment_level;
  4687. { handle compiler switches }
  4688. if (c='$') then
  4689. handledirectives;
  4690. { handle_switches can dec comment_level, }
  4691. while (comment_level>0) do
  4692. begin
  4693. case c of
  4694. '{' :
  4695. inc_comment_level;
  4696. '}' :
  4697. dec_comment_level;
  4698. '*' :
  4699. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4700. if m_iso in current_settings.modeswitches then
  4701. begin
  4702. readchar;
  4703. if c=')' then
  4704. dec_comment_level
  4705. else
  4706. continue;
  4707. end;
  4708. #10,#13 :
  4709. linebreak;
  4710. #26 :
  4711. begin
  4712. reload;
  4713. if (c=#26) and not assigned(inputfile.next) then
  4714. end_of_file;
  4715. continue;
  4716. end;
  4717. end;
  4718. readchar;
  4719. end;
  4720. current_commentstyle:=comment_none;
  4721. end;
  4722. procedure tscannerfile.skipdelphicomment;
  4723. begin
  4724. current_commentstyle:=comment_delphi;
  4725. inc_comment_level;
  4726. readchar;
  4727. { this is not supported }
  4728. if c='$' then
  4729. Message(scan_w_wrong_styled_switch);
  4730. { skip comment }
  4731. while not (c in [#10,#13,#26]) do
  4732. readchar;
  4733. dec_comment_level;
  4734. current_commentstyle:=comment_none;
  4735. end;
  4736. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4737. var
  4738. found : longint;
  4739. begin
  4740. current_commentstyle:=comment_oldtp;
  4741. inc_comment_level;
  4742. { only load a char if last already processed,
  4743. was cause of bug1634 PM }
  4744. if read_first_char then
  4745. readchar;
  4746. { this is now supported }
  4747. if (c='$') then
  4748. handledirectives;
  4749. { skip comment }
  4750. while (comment_level>0) do
  4751. begin
  4752. found:=0;
  4753. repeat
  4754. case c of
  4755. #26 :
  4756. begin
  4757. reload;
  4758. if (c=#26) and not assigned(inputfile.next) then
  4759. end_of_file;
  4760. continue;
  4761. end;
  4762. #10,#13 :
  4763. begin
  4764. if found=4 then
  4765. inc_comment_level;
  4766. linebreak;
  4767. found:=0;
  4768. end;
  4769. '*' :
  4770. begin
  4771. if found=3 then
  4772. found:=4
  4773. else
  4774. begin
  4775. if found=4 then
  4776. inc_comment_level;
  4777. found:=1;
  4778. end;
  4779. end;
  4780. ')' :
  4781. begin
  4782. if found in [1,4] then
  4783. begin
  4784. dec_comment_level;
  4785. if comment_level=0 then
  4786. found:=2
  4787. else
  4788. found:=0;
  4789. end
  4790. else
  4791. found:=0;
  4792. end;
  4793. '}' :
  4794. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4795. if m_iso in current_settings.modeswitches then
  4796. begin
  4797. dec_comment_level;
  4798. if comment_level=0 then
  4799. found:=2
  4800. else
  4801. found:=0;
  4802. end;
  4803. '(' :
  4804. begin
  4805. if found=4 then
  4806. inc_comment_level;
  4807. found:=3;
  4808. end;
  4809. else
  4810. begin
  4811. if found=4 then
  4812. inc_comment_level;
  4813. found:=0;
  4814. end;
  4815. end;
  4816. readchar;
  4817. until (found=2);
  4818. end;
  4819. current_commentstyle:=comment_none;
  4820. end;
  4821. {****************************************************************************
  4822. Token Scanner
  4823. ****************************************************************************}
  4824. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4825. var
  4826. code : integer;
  4827. d : cardinal;
  4828. len,
  4829. low,high,mid : longint;
  4830. w : word;
  4831. m : longint;
  4832. mac : tmacro;
  4833. asciinr : string[33];
  4834. iswidestring , firstdigitread: boolean;
  4835. label
  4836. exit_label;
  4837. begin
  4838. flushpendingswitchesstate;
  4839. { record tokens? }
  4840. if allowrecordtoken and
  4841. assigned(recordtokenbuf) then
  4842. recordtoken;
  4843. { replay tokens? }
  4844. if assigned(replaytokenbuf) then
  4845. begin
  4846. replaytoken;
  4847. goto exit_label;
  4848. end;
  4849. { was there already a token read, then return that token }
  4850. if nexttoken<>NOTOKEN then
  4851. begin
  4852. setnexttoken;
  4853. goto exit_label;
  4854. end;
  4855. { Skip all spaces and comments }
  4856. repeat
  4857. case c of
  4858. '{' :
  4859. skipcomment(true);
  4860. #26 :
  4861. begin
  4862. reload;
  4863. if (c=#26) and not assigned(inputfile.next) then
  4864. break;
  4865. end;
  4866. ' ',#9..#13 :
  4867. begin
  4868. {$ifdef PREPROCWRITE}
  4869. if parapreprocess then
  4870. begin
  4871. if c=#10 then
  4872. preprocfile.eolfound:=true
  4873. else
  4874. preprocfile.spacefound:=true;
  4875. end;
  4876. {$endif PREPROCWRITE}
  4877. skipspace;
  4878. end
  4879. else
  4880. break;
  4881. end;
  4882. until false;
  4883. { Save current token position, for EOF its already loaded }
  4884. if c<>#26 then
  4885. gettokenpos;
  4886. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4887. if c in ['A'..'Z','a'..'z','_'] then
  4888. begin
  4889. readstring;
  4890. token:=_ID;
  4891. idtoken:=_ID;
  4892. { keyword or any other known token,
  4893. pattern is always uppercased }
  4894. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4895. begin
  4896. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4897. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4898. while low<high do
  4899. begin
  4900. mid:=(high+low+1) shr 1;
  4901. if pattern<tokeninfo^[ttoken(mid)].str then
  4902. high:=mid-1
  4903. else
  4904. low:=mid;
  4905. end;
  4906. with tokeninfo^[ttoken(high)] do
  4907. if pattern=str then
  4908. begin
  4909. if (keyword*current_settings.modeswitches)<>[] then
  4910. if op=NOTOKEN then
  4911. token:=ttoken(high)
  4912. else
  4913. token:=op;
  4914. idtoken:=ttoken(high);
  4915. end;
  4916. end;
  4917. { Only process identifiers and not keywords }
  4918. if token=_ID then
  4919. begin
  4920. { this takes some time ... }
  4921. if (cs_support_macro in current_settings.moduleswitches) then
  4922. begin
  4923. mac:=tmacro(search_macro(pattern));
  4924. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4925. begin
  4926. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4927. begin
  4928. mac.is_used:=true;
  4929. inc(yylexcount);
  4930. substitutemacro(pattern,mac.buftext,mac.buflen,
  4931. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4932. { handle empty macros }
  4933. if c=#0 then
  4934. begin
  4935. reload;
  4936. { avoid macro nesting error in case of
  4937. a sequence of empty macros, see #38802 }
  4938. dec(yylexcount);
  4939. readtoken(false);
  4940. end
  4941. else
  4942. begin
  4943. readtoken(false);
  4944. { that's all folks }
  4945. dec(yylexcount);
  4946. end;
  4947. exit;
  4948. end
  4949. else
  4950. Message(scan_w_macro_too_deep);
  4951. end;
  4952. end;
  4953. end;
  4954. { return token }
  4955. goto exit_label;
  4956. end
  4957. else
  4958. begin
  4959. idtoken:=_NOID;
  4960. case c of
  4961. '$' :
  4962. begin
  4963. readnumber;
  4964. token:=_INTCONST;
  4965. goto exit_label;
  4966. end;
  4967. '%' :
  4968. begin
  4969. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4970. Illegal_Char(c)
  4971. else
  4972. begin
  4973. readnumber;
  4974. token:=_INTCONST;
  4975. goto exit_label;
  4976. end;
  4977. end;
  4978. '&' :
  4979. begin
  4980. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4981. begin
  4982. readnumber;
  4983. if length(pattern)=1 then
  4984. begin
  4985. { does really an identifier follow? }
  4986. if not (c in ['_','A'..'Z','a'..'z']) then
  4987. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4988. readstring;
  4989. token:=_ID;
  4990. idtoken:=_ID;
  4991. end
  4992. else
  4993. token:=_INTCONST;
  4994. goto exit_label;
  4995. end
  4996. else if m_mac in current_settings.modeswitches then
  4997. begin
  4998. readchar;
  4999. token:=_AMPERSAND;
  5000. goto exit_label;
  5001. end
  5002. else
  5003. Illegal_Char(c);
  5004. end;
  5005. '0'..'9' :
  5006. begin
  5007. readnumber;
  5008. if (c in ['.','e','E']) then
  5009. begin
  5010. { first check for a . }
  5011. if c='.' then
  5012. begin
  5013. cachenexttokenpos;
  5014. readchar;
  5015. { is it a .. from a range? }
  5016. case c of
  5017. '.' :
  5018. begin
  5019. readchar;
  5020. token:=_INTCONST;
  5021. nexttoken:=_POINTPOINT;
  5022. goto exit_label;
  5023. end;
  5024. ')' :
  5025. begin
  5026. readchar;
  5027. token:=_INTCONST;
  5028. nexttoken:=_RECKKLAMMER;
  5029. goto exit_label;
  5030. end;
  5031. '0'..'9' :
  5032. begin
  5033. { insert the number after the . }
  5034. pattern:=pattern+'.';
  5035. firstdigitread:=false;
  5036. while (c in ['0'..'9']) or
  5037. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5038. begin
  5039. if c<>'_' then
  5040. pattern:=pattern+c;
  5041. readchar;
  5042. firstdigitread:=true;
  5043. end;
  5044. end;
  5045. else
  5046. begin
  5047. token:=_INTCONST;
  5048. nexttoken:=_POINT;
  5049. goto exit_label;
  5050. end;
  5051. end;
  5052. end;
  5053. { E can also follow after a point is scanned }
  5054. if c in ['e','E'] then
  5055. begin
  5056. pattern:=pattern+'E';
  5057. readchar;
  5058. if c in ['-','+'] then
  5059. begin
  5060. pattern:=pattern+c;
  5061. readchar;
  5062. end;
  5063. if not(c in ['0'..'9']) then
  5064. Illegal_Char(c);
  5065. firstdigitread:=false;
  5066. while (c in ['0'..'9']) or
  5067. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5068. begin
  5069. if c<>'_' then
  5070. pattern:=pattern+c;
  5071. readchar;
  5072. firstdigitread:=true;
  5073. end;
  5074. end;
  5075. token:=_REALNUMBER;
  5076. goto exit_label;
  5077. end;
  5078. token:=_INTCONST;
  5079. goto exit_label;
  5080. end;
  5081. ';' :
  5082. begin
  5083. readchar;
  5084. token:=_SEMICOLON;
  5085. goto exit_label;
  5086. end;
  5087. '[' :
  5088. begin
  5089. readchar;
  5090. token:=_LECKKLAMMER;
  5091. goto exit_label;
  5092. end;
  5093. ']' :
  5094. begin
  5095. readchar;
  5096. token:=_RECKKLAMMER;
  5097. goto exit_label;
  5098. end;
  5099. '(' :
  5100. begin
  5101. readchar;
  5102. case c of
  5103. '*' :
  5104. begin
  5105. skipoldtpcomment(true);
  5106. readtoken(false);
  5107. exit;
  5108. end;
  5109. '.' :
  5110. begin
  5111. readchar;
  5112. token:=_LECKKLAMMER;
  5113. goto exit_label;
  5114. end;
  5115. end;
  5116. token:=_LKLAMMER;
  5117. goto exit_label;
  5118. end;
  5119. ')' :
  5120. begin
  5121. readchar;
  5122. token:=_RKLAMMER;
  5123. goto exit_label;
  5124. end;
  5125. '+' :
  5126. begin
  5127. readchar;
  5128. if c='=' then
  5129. begin
  5130. readchar;
  5131. token:=_PLUSASN;
  5132. goto exit_label;
  5133. end;
  5134. token:=_PLUS;
  5135. goto exit_label;
  5136. end;
  5137. '-' :
  5138. begin
  5139. readchar;
  5140. if c='=' then
  5141. begin
  5142. readchar;
  5143. token:=_MINUSASN;
  5144. goto exit_label;
  5145. end;
  5146. token:=_MINUS;
  5147. goto exit_label;
  5148. end;
  5149. ':' :
  5150. begin
  5151. readchar;
  5152. if c='=' then
  5153. begin
  5154. readchar;
  5155. token:=_ASSIGNMENT;
  5156. goto exit_label;
  5157. end;
  5158. token:=_COLON;
  5159. goto exit_label;
  5160. end;
  5161. '*' :
  5162. begin
  5163. readchar;
  5164. if c='=' then
  5165. begin
  5166. readchar;
  5167. token:=_STARASN;
  5168. end
  5169. else
  5170. if c='*' then
  5171. begin
  5172. readchar;
  5173. token:=_STARSTAR;
  5174. end
  5175. else
  5176. token:=_STAR;
  5177. goto exit_label;
  5178. end;
  5179. '/' :
  5180. begin
  5181. readchar;
  5182. case c of
  5183. '=' :
  5184. begin
  5185. readchar;
  5186. token:=_SLASHASN;
  5187. goto exit_label;
  5188. end;
  5189. '/' :
  5190. begin
  5191. skipdelphicomment;
  5192. readtoken(false);
  5193. exit;
  5194. end;
  5195. end;
  5196. token:=_SLASH;
  5197. goto exit_label;
  5198. end;
  5199. '|' :
  5200. if m_mac in current_settings.modeswitches then
  5201. begin
  5202. readchar;
  5203. token:=_PIPE;
  5204. goto exit_label;
  5205. end
  5206. else
  5207. Illegal_Char(c);
  5208. '=' :
  5209. begin
  5210. readchar;
  5211. token:=_EQ;
  5212. goto exit_label;
  5213. end;
  5214. '.' :
  5215. begin
  5216. readchar;
  5217. case c of
  5218. '.' :
  5219. begin
  5220. readchar;
  5221. case c of
  5222. '.' :
  5223. begin
  5224. readchar;
  5225. token:=_POINTPOINTPOINT;
  5226. goto exit_label;
  5227. end;
  5228. else
  5229. begin
  5230. token:=_POINTPOINT;
  5231. goto exit_label;
  5232. end;
  5233. end;
  5234. end;
  5235. ')' :
  5236. begin
  5237. readchar;
  5238. token:=_RECKKLAMMER;
  5239. goto exit_label;
  5240. end;
  5241. end;
  5242. token:=_POINT;
  5243. goto exit_label;
  5244. end;
  5245. '@' :
  5246. begin
  5247. readchar;
  5248. token:=_KLAMMERAFFE;
  5249. goto exit_label;
  5250. end;
  5251. ',' :
  5252. begin
  5253. readchar;
  5254. token:=_COMMA;
  5255. goto exit_label;
  5256. end;
  5257. '''','#','^' :
  5258. begin
  5259. len:=0;
  5260. cstringpattern:='';
  5261. iswidestring:=false;
  5262. if c='^' then
  5263. begin
  5264. readchar;
  5265. c:=upcase(c);
  5266. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5267. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5268. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5269. begin
  5270. token:=_CARET;
  5271. goto exit_label;
  5272. end
  5273. else
  5274. begin
  5275. inc(len);
  5276. setlength(cstringpattern,256);
  5277. if c<#64 then
  5278. cstringpattern[len]:=chr(ord(c)+64)
  5279. else
  5280. cstringpattern[len]:=chr(ord(c)-64);
  5281. readchar;
  5282. end;
  5283. end;
  5284. repeat
  5285. case c of
  5286. '#' :
  5287. begin
  5288. readchar; { read # }
  5289. case c of
  5290. '$':
  5291. begin
  5292. readchar; { read leading $ }
  5293. asciinr:='$';
  5294. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5295. begin
  5296. asciinr:=asciinr+c;
  5297. readchar;
  5298. end;
  5299. end;
  5300. '&':
  5301. begin
  5302. readchar; { read leading $ }
  5303. asciinr:='&';
  5304. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5305. begin
  5306. asciinr:=asciinr+c;
  5307. readchar;
  5308. end;
  5309. end;
  5310. '%':
  5311. begin
  5312. readchar; { read leading $ }
  5313. asciinr:='%';
  5314. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5315. begin
  5316. asciinr:=asciinr+c;
  5317. readchar;
  5318. end;
  5319. end;
  5320. else
  5321. begin
  5322. asciinr:='';
  5323. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5324. begin
  5325. asciinr:=asciinr+c;
  5326. readchar;
  5327. end;
  5328. end;
  5329. end;
  5330. val(asciinr,m,code);
  5331. if (asciinr='') or (code<>0) then
  5332. Message(scan_e_illegal_char_const)
  5333. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5334. begin
  5335. if (m>=0) and (m<=$10FFFF) then
  5336. begin
  5337. if not iswidestring then
  5338. begin
  5339. if len>0 then
  5340. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5341. else
  5342. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5343. iswidestring:=true;
  5344. len:=0;
  5345. end;
  5346. if m<=$FFFF then
  5347. concatwidestringchar(patternw,tcompilerwidechar(m))
  5348. else
  5349. begin
  5350. { split into surrogate pair }
  5351. dec(m,$10000);
  5352. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5353. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5354. end;
  5355. end
  5356. else
  5357. Message(scan_e_illegal_char_const)
  5358. end
  5359. else if iswidestring then
  5360. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5361. else
  5362. begin
  5363. if len>=length(cstringpattern) then
  5364. setlength(cstringpattern,length(cstringpattern)+256);
  5365. inc(len);
  5366. cstringpattern[len]:=chr(m);
  5367. end;
  5368. end;
  5369. '''' :
  5370. begin
  5371. repeat
  5372. readchar;
  5373. case c of
  5374. #26 :
  5375. end_of_file;
  5376. #10,#13 :
  5377. Message(scan_f_string_exceeds_line);
  5378. '''' :
  5379. begin
  5380. readchar;
  5381. if c<>'''' then
  5382. break;
  5383. end;
  5384. end;
  5385. { interpret as utf-8 string? }
  5386. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5387. begin
  5388. { convert existing string to an utf-8 string }
  5389. if not iswidestring then
  5390. begin
  5391. if len>0 then
  5392. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5393. else
  5394. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5395. iswidestring:=true;
  5396. len:=0;
  5397. end;
  5398. { four chars }
  5399. if (ord(c) and $f0)=$f0 then
  5400. begin
  5401. { this always represents a surrogate pair, so
  5402. read as 32-bit value and then split into
  5403. the corresponding pair of two wchars }
  5404. d:=ord(c) and $f;
  5405. readchar;
  5406. if (ord(c) and $c0)<>$80 then
  5407. message(scan_e_utf8_malformed);
  5408. d:=(d shl 6) or (ord(c) and $3f);
  5409. readchar;
  5410. if (ord(c) and $c0)<>$80 then
  5411. message(scan_e_utf8_malformed);
  5412. d:=(d shl 6) or (ord(c) and $3f);
  5413. readchar;
  5414. if (ord(c) and $c0)<>$80 then
  5415. message(scan_e_utf8_malformed);
  5416. d:=(d shl 6) or (ord(c) and $3f);
  5417. if d<$10000 then
  5418. message(scan_e_utf8_malformed);
  5419. d:=d-$10000;
  5420. { high surrogate }
  5421. w:=$d800+(d shr 10);
  5422. concatwidestringchar(patternw,w);
  5423. { low surrogate }
  5424. w:=$dc00+(d and $3ff);
  5425. concatwidestringchar(patternw,w);
  5426. end
  5427. { three chars }
  5428. else if (ord(c) and $e0)=$e0 then
  5429. begin
  5430. w:=ord(c) and $f;
  5431. readchar;
  5432. if (ord(c) and $c0)<>$80 then
  5433. message(scan_e_utf8_malformed);
  5434. w:=(w shl 6) or (ord(c) and $3f);
  5435. readchar;
  5436. if (ord(c) and $c0)<>$80 then
  5437. message(scan_e_utf8_malformed);
  5438. w:=(w shl 6) or (ord(c) and $3f);
  5439. concatwidestringchar(patternw,w);
  5440. end
  5441. { two chars }
  5442. else if (ord(c) and $c0)<>0 then
  5443. begin
  5444. w:=ord(c) and $1f;
  5445. readchar;
  5446. if (ord(c) and $c0)<>$80 then
  5447. message(scan_e_utf8_malformed);
  5448. w:=(w shl 6) or (ord(c) and $3f);
  5449. concatwidestringchar(patternw,w);
  5450. end
  5451. { illegal }
  5452. else if (ord(c) and $80)<>0 then
  5453. message(scan_e_utf8_malformed)
  5454. else
  5455. concatwidestringchar(patternw,tcompilerwidechar(c))
  5456. end
  5457. else if iswidestring then
  5458. begin
  5459. if current_settings.sourcecodepage=CP_UTF8 then
  5460. concatwidestringchar(patternw,ord(c))
  5461. else
  5462. concatwidestringchar(patternw,asciichar2unicode(c))
  5463. end
  5464. else
  5465. begin
  5466. if len>=length(cstringpattern) then
  5467. setlength(cstringpattern,length(cstringpattern)+256);
  5468. inc(len);
  5469. cstringpattern[len]:=c;
  5470. end;
  5471. until false;
  5472. end;
  5473. '^' :
  5474. begin
  5475. readchar;
  5476. c:=upcase(c);
  5477. if c<#64 then
  5478. c:=chr(ord(c)+64)
  5479. else
  5480. c:=chr(ord(c)-64);
  5481. if iswidestring then
  5482. concatwidestringchar(patternw,asciichar2unicode(c))
  5483. else
  5484. begin
  5485. if len>=length(cstringpattern) then
  5486. setlength(cstringpattern,length(cstringpattern)+256);
  5487. inc(len);
  5488. cstringpattern[len]:=c;
  5489. end;
  5490. readchar;
  5491. end;
  5492. else
  5493. break;
  5494. end;
  5495. until false;
  5496. { strings with length 1 become const chars }
  5497. if iswidestring then
  5498. begin
  5499. if patternw^.len=1 then
  5500. token:=_CWCHAR
  5501. else
  5502. token:=_CWSTRING;
  5503. end
  5504. else
  5505. begin
  5506. setlength(cstringpattern,len);
  5507. if length(cstringpattern)=1 then
  5508. begin
  5509. token:=_CCHAR;
  5510. pattern:=cstringpattern;
  5511. end
  5512. else
  5513. token:=_CSTRING;
  5514. end;
  5515. goto exit_label;
  5516. end;
  5517. '>' :
  5518. begin
  5519. readchar;
  5520. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5521. token:=_RSHARPBRACKET
  5522. else
  5523. begin
  5524. case c of
  5525. '=' :
  5526. begin
  5527. readchar;
  5528. token:=_GTE;
  5529. goto exit_label;
  5530. end;
  5531. '>' :
  5532. begin
  5533. readchar;
  5534. token:=_OP_SHR;
  5535. goto exit_label;
  5536. end;
  5537. '<' :
  5538. begin { >< is for a symetric diff for sets }
  5539. readchar;
  5540. token:=_SYMDIF;
  5541. goto exit_label;
  5542. end;
  5543. end;
  5544. token:=_GT;
  5545. end;
  5546. goto exit_label;
  5547. end;
  5548. '<' :
  5549. begin
  5550. readchar;
  5551. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5552. token:=_LSHARPBRACKET
  5553. else
  5554. begin
  5555. case c of
  5556. '>' :
  5557. begin
  5558. readchar;
  5559. token:=_NE;
  5560. goto exit_label;
  5561. end;
  5562. '=' :
  5563. begin
  5564. readchar;
  5565. token:=_LTE;
  5566. goto exit_label;
  5567. end;
  5568. '<' :
  5569. begin
  5570. readchar;
  5571. token:=_OP_SHL;
  5572. goto exit_label;
  5573. end;
  5574. end;
  5575. token:=_LT;
  5576. end;
  5577. goto exit_label;
  5578. end;
  5579. #26 :
  5580. begin
  5581. token:=_EOF;
  5582. checkpreprocstack;
  5583. goto exit_label;
  5584. end;
  5585. else if inputfile.internally_generated_macro and
  5586. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5587. begin
  5588. token:=_ID;
  5589. readstring;
  5590. end
  5591. else
  5592. Illegal_Char(c);
  5593. end;
  5594. end;
  5595. exit_label:
  5596. lasttoken:=token;
  5597. end;
  5598. function tscannerfile.readpreproc:ttoken;
  5599. var
  5600. low,high,mid: longint;
  5601. optoken: ttoken;
  5602. begin
  5603. skipspace;
  5604. case c of
  5605. '_',
  5606. 'A'..'Z',
  5607. 'a'..'z' :
  5608. begin
  5609. readstring;
  5610. optoken:=_ID;
  5611. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5612. begin
  5613. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5614. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5615. while low<high do
  5616. begin
  5617. mid:=(high+low+1) shr 1;
  5618. if pattern<tokeninfo^[ttoken(mid)].str then
  5619. high:=mid-1
  5620. else
  5621. low:=mid;
  5622. end;
  5623. with tokeninfo^[ttoken(high)] do
  5624. if pattern=str then
  5625. begin
  5626. if (keyword*current_settings.modeswitches)<>[] then
  5627. if op=NOTOKEN then
  5628. optoken:=ttoken(high)
  5629. else
  5630. optoken:=op;
  5631. end;
  5632. if not (optoken in preproc_operators) then
  5633. optoken:=_ID;
  5634. end;
  5635. current_scanner.preproc_pattern:=pattern;
  5636. readpreproc:=optoken;
  5637. end;
  5638. '''' :
  5639. begin
  5640. current_scanner.preproc_pattern:=readquotedstring;
  5641. readpreproc:=_CSTRING;
  5642. end;
  5643. '0'..'9' :
  5644. begin
  5645. readnumber;
  5646. if (c in ['.','e','E']) then
  5647. begin
  5648. { first check for a . }
  5649. if c='.' then
  5650. begin
  5651. readchar;
  5652. if c in ['0'..'9'] then
  5653. begin
  5654. { insert the number after the . }
  5655. pattern:=pattern+'.';
  5656. while c in ['0'..'9'] do
  5657. begin
  5658. pattern:=pattern+c;
  5659. readchar;
  5660. end;
  5661. end
  5662. else
  5663. Illegal_Char(c);
  5664. end;
  5665. { E can also follow after a point is scanned }
  5666. if c in ['e','E'] then
  5667. begin
  5668. pattern:=pattern+'E';
  5669. readchar;
  5670. if c in ['-','+'] then
  5671. begin
  5672. pattern:=pattern+c;
  5673. readchar;
  5674. end;
  5675. if not(c in ['0'..'9']) then
  5676. Illegal_Char(c);
  5677. while c in ['0'..'9'] do
  5678. begin
  5679. pattern:=pattern+c;
  5680. readchar;
  5681. end;
  5682. end;
  5683. readpreproc:=_REALNUMBER;
  5684. end
  5685. else
  5686. readpreproc:=_INTCONST;
  5687. current_scanner.preproc_pattern:=pattern;
  5688. end;
  5689. '$','%':
  5690. begin
  5691. readnumber;
  5692. current_scanner.preproc_pattern:=pattern;
  5693. readpreproc:=_INTCONST;
  5694. end;
  5695. '&' :
  5696. begin
  5697. readnumber;
  5698. if length(pattern)=1 then
  5699. begin
  5700. readstring;
  5701. readpreproc:=_ID;
  5702. end
  5703. else
  5704. readpreproc:=_INTCONST;
  5705. current_scanner.preproc_pattern:=pattern;
  5706. end;
  5707. '.' :
  5708. begin
  5709. readchar;
  5710. readpreproc:=_POINT;
  5711. end;
  5712. ',' :
  5713. begin
  5714. readchar;
  5715. readpreproc:=_COMMA;
  5716. end;
  5717. '}' :
  5718. begin
  5719. readpreproc:=_END;
  5720. end;
  5721. '(' :
  5722. begin
  5723. readchar;
  5724. readpreproc:=_LKLAMMER;
  5725. end;
  5726. ')' :
  5727. begin
  5728. readchar;
  5729. readpreproc:=_RKLAMMER;
  5730. end;
  5731. '[' :
  5732. begin
  5733. readchar;
  5734. readpreproc:=_LECKKLAMMER;
  5735. end;
  5736. ']' :
  5737. begin
  5738. readchar;
  5739. readpreproc:=_RECKKLAMMER;
  5740. end;
  5741. '+' :
  5742. begin
  5743. readchar;
  5744. readpreproc:=_PLUS;
  5745. end;
  5746. '-' :
  5747. begin
  5748. readchar;
  5749. readpreproc:=_MINUS;
  5750. end;
  5751. '*' :
  5752. begin
  5753. readchar;
  5754. readpreproc:=_STAR;
  5755. end;
  5756. '/' :
  5757. begin
  5758. readchar;
  5759. readpreproc:=_SLASH;
  5760. end;
  5761. '=' :
  5762. begin
  5763. readchar;
  5764. readpreproc:=_EQ;
  5765. end;
  5766. '>' :
  5767. begin
  5768. readchar;
  5769. if c='=' then
  5770. begin
  5771. readchar;
  5772. readpreproc:=_GTE;
  5773. end
  5774. else
  5775. readpreproc:=_GT;
  5776. end;
  5777. '<' :
  5778. begin
  5779. readchar;
  5780. case c of
  5781. '>' :
  5782. begin
  5783. readchar;
  5784. readpreproc:=_NE;
  5785. end;
  5786. '=' :
  5787. begin
  5788. readchar;
  5789. readpreproc:=_LTE;
  5790. end;
  5791. else
  5792. readpreproc:=_LT;
  5793. end;
  5794. end;
  5795. #26 :
  5796. begin
  5797. readpreproc:=_EOF;
  5798. checkpreprocstack;
  5799. end;
  5800. else
  5801. begin
  5802. Illegal_Char(c);
  5803. readpreproc:=NOTOKEN;
  5804. end;
  5805. end;
  5806. end;
  5807. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5808. var
  5809. hs : texprvalue;
  5810. begin
  5811. hs:=preproc_comp_expr(nil);
  5812. if hs.isInt then
  5813. begin
  5814. value:=hs.asInt64;
  5815. result:=true;
  5816. end
  5817. else
  5818. begin
  5819. hs.error('Integer',place);
  5820. result:=false;
  5821. end;
  5822. hs.free;
  5823. end;
  5824. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  5825. var
  5826. hs : texprvalue;
  5827. begin
  5828. hs:=preproc_comp_expr(conform_to);
  5829. if hs.def.typ=setdef then
  5830. begin
  5831. value:=hs.asSet;
  5832. result:=true;
  5833. end
  5834. else
  5835. begin
  5836. hs.error('Set',place);
  5837. result:=false;
  5838. end;
  5839. hs.free;
  5840. end;
  5841. function tscannerfile.asmgetchar : char;
  5842. begin
  5843. readchar;
  5844. repeat
  5845. case c of
  5846. #26 :
  5847. begin
  5848. reload;
  5849. if (c=#26) and not assigned(inputfile.next) then
  5850. end_of_file;
  5851. continue;
  5852. end;
  5853. else
  5854. begin
  5855. asmgetchar:=c;
  5856. exit;
  5857. end;
  5858. end;
  5859. until false;
  5860. end;
  5861. {$ifdef EXTDEBUG}
  5862. function tscannerfile.DumpPointer: string;
  5863. var
  5864. i: Integer;
  5865. begin
  5866. Result:='';
  5867. if inputpointer=nil then exit;
  5868. i:=0;
  5869. While (inputpointer[I]<>#0) and (i<200) do
  5870. inc(i);
  5871. Setlength(result,I);
  5872. move(inputpointer^,Result[1],I);
  5873. result:='<'+inttostr(inputstart)+'>'+result;
  5874. end;
  5875. {$endif EXTDEBUG}
  5876. {*****************************************************************************
  5877. Helpers
  5878. *****************************************************************************}
  5879. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5880. begin
  5881. if dm in [directive_all, directive_turbo] then
  5882. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5883. if dm in [directive_all, directive_mac] then
  5884. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5885. end;
  5886. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5887. begin
  5888. if dm in [directive_all, directive_turbo] then
  5889. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5890. if dm in [directive_all, directive_mac] then
  5891. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5892. end;
  5893. {*****************************************************************************
  5894. Initialization
  5895. *****************************************************************************}
  5896. procedure InitScanner;
  5897. begin
  5898. InitWideString(patternw);
  5899. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5900. mac_scannerdirectives:=TFPHashObjectList.Create;
  5901. { Common directives and conditionals }
  5902. AddDirective('I',directive_all, @dir_include);
  5903. AddDirective('DEFINE',directive_all, @dir_define);
  5904. AddDirective('UNDEF',directive_all, @dir_undef);
  5905. AddConditional('IF',directive_all, @dir_if);
  5906. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5907. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5908. AddConditional('ELSE',directive_all, @dir_else);
  5909. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5910. AddConditional('ENDIF',directive_all, @dir_endif);
  5911. { Directives and conditionals for all modes except mode macpas}
  5912. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5913. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5914. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5915. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5916. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5917. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5918. { Directives and conditionals for mode macpas: }
  5919. AddDirective('SETC',directive_mac, @dir_setc);
  5920. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5921. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5922. AddConditional('IFC',directive_mac, @dir_if);
  5923. AddConditional('ELSEC',directive_mac, @dir_else);
  5924. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5925. AddConditional('ENDC',directive_mac, @dir_endif);
  5926. end;
  5927. procedure DoneScanner;
  5928. begin
  5929. turbo_scannerdirectives.Free;
  5930. mac_scannerdirectives.Free;
  5931. DoneWideString(patternw);
  5932. end;
  5933. end.