scanner.pas 218 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359
  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. macropos:=0;
  2415. { parse macro, brackets are counted so it's possible
  2416. to have a $ifdef etc. in the macro }
  2417. bracketcount:=0;
  2418. repeat
  2419. case c of
  2420. '}' :
  2421. if (bracketcount=0) then
  2422. break
  2423. else
  2424. dec(bracketcount);
  2425. '{' :
  2426. inc(bracketcount);
  2427. #10,#13 :
  2428. current_scanner.linebreak;
  2429. #26 :
  2430. current_scanner.end_of_file;
  2431. end;
  2432. if macropos>=maxmacrolen then
  2433. Message(scan_f_macro_buffer_overflow);
  2434. macrobuffer[macropos]:=c;
  2435. inc(macropos);
  2436. current_scanner.readchar;
  2437. until false;
  2438. { copy the text }
  2439. move(pchar(@macrobuffer[0])^,mac.allocate_buftext(macropos)^,macropos);
  2440. end
  2441. else
  2442. begin
  2443. { check if there is an assignment, then we need to give a
  2444. warning }
  2445. current_scanner.skipspace;
  2446. if c=':' then
  2447. begin
  2448. current_scanner.readchar;
  2449. if c='=' then
  2450. Message(scan_w_macro_support_turned_off);
  2451. end;
  2452. end;
  2453. end;
  2454. procedure dir_define;
  2455. begin
  2456. dir_define_impl(false);
  2457. end;
  2458. procedure dir_definec;
  2459. begin
  2460. dir_define_impl(true);
  2461. end;
  2462. procedure dir_setc;
  2463. var
  2464. hs : string;
  2465. mac : tmacro;
  2466. exprvalue: texprvalue;
  2467. begin
  2468. current_scanner.skipspace;
  2469. hs:=current_scanner.readid;
  2470. mac:=tmacro(search_macro(hs));
  2471. if not assigned(mac) or
  2472. (mac.owner <> current_module.localmacrosymtable) then
  2473. begin
  2474. mac:=tmacro.create(hs);
  2475. mac.defined:=true;
  2476. mac.is_compiler_var:=true;
  2477. current_module.localmacrosymtable.insertsym(mac);
  2478. end
  2479. else
  2480. begin
  2481. mac.defined:=true;
  2482. mac.is_compiler_var:=true;
  2483. { delete old definition }
  2484. mac.free_buftext;
  2485. end;
  2486. Message1(parser_c_macro_defined,mac.name);
  2487. mac.is_used:=true;
  2488. { key words are never substituted }
  2489. if is_keyword(hs) then
  2490. Message(scan_e_keyword_cant_be_a_macro);
  2491. { macro assignment can be both := and = }
  2492. current_scanner.skipspace;
  2493. if c=':' then
  2494. current_scanner.readchar;
  2495. if c='=' then
  2496. begin
  2497. current_scanner.readchar;
  2498. exprvalue:=preproc_comp_expr(nil);
  2499. if not is_boolean(exprvalue.def) and
  2500. not is_integer(exprvalue.def) then
  2501. exprvalue.error('Boolean, Integer', 'SETC');
  2502. hs:=exprvalue.asStr;
  2503. if length(hs) <> 0 then
  2504. begin
  2505. {If we are absolutely shure it is boolean, translate
  2506. to TRUE/FALSE to increase possibility to do future type check}
  2507. if exprvalue.isBoolean then
  2508. begin
  2509. if exprvalue.asBool then
  2510. hs:='TRUE'
  2511. else
  2512. hs:='FALSE';
  2513. end;
  2514. Message2(parser_c_macro_set_to,mac.name,hs);
  2515. { copy the text }
  2516. move(hs[1],mac.allocate_buftext(length(hs))^,length(hs));
  2517. end
  2518. else
  2519. Message(scan_e_preproc_syntax_error);
  2520. exprvalue.free;
  2521. end
  2522. else
  2523. Message(scan_e_preproc_syntax_error);
  2524. end;
  2525. procedure dir_undef;
  2526. var
  2527. hs : string;
  2528. mac : tmacro;
  2529. begin
  2530. current_scanner.skipspace;
  2531. hs:=current_scanner.readid;
  2532. mac:=tmacro(search_macro(hs));
  2533. if not assigned(mac) or
  2534. (mac.owner <> current_module.localmacrosymtable) then
  2535. begin
  2536. mac:=tmacro.create(hs);
  2537. mac.defined:=false;
  2538. current_module.localmacrosymtable.insertsym(mac);
  2539. end
  2540. else
  2541. begin
  2542. mac.defined:=false;
  2543. mac.is_compiler_var:=false;
  2544. { delete old definition }
  2545. mac.free_buftext;
  2546. end;
  2547. Message1(parser_c_macro_undefined,mac.name);
  2548. mac.is_used:=true;
  2549. end;
  2550. procedure dir_include;
  2551. var
  2552. foundfile : TCmdStr;
  2553. path,
  2554. name,
  2555. hs : tpathstr;
  2556. args : string;
  2557. hp : tinputfile;
  2558. found : boolean;
  2559. macroIsString : boolean;
  2560. begin
  2561. current_scanner.skipspace;
  2562. args:=current_scanner.readcomment;
  2563. hs:=GetToken(args,' ');
  2564. if hs='' then
  2565. exit;
  2566. if (hs[1]='%') then
  2567. begin
  2568. { case insensitive }
  2569. hs:=upper(hs);
  2570. { remove %'s }
  2571. Delete(hs,1,1);
  2572. if hs[length(hs)]='%' then
  2573. Delete(hs,length(hs),1);
  2574. { save old }
  2575. path:=hs;
  2576. { first check for internal macros }
  2577. macroIsString:=true;
  2578. case hs of
  2579. 'TIME':
  2580. if timestr<>'' then
  2581. hs:=timestr
  2582. else
  2583. hs:=gettimestr;
  2584. 'DATE':
  2585. if datestr<>'' then
  2586. hs:=datestr
  2587. else
  2588. hs:=getdatestr;
  2589. 'DATEYEAR':
  2590. begin
  2591. hs:=tostr(startsystime.Year);
  2592. macroIsString:=false;
  2593. end;
  2594. 'DATEMONTH':
  2595. begin
  2596. hs:=tostr(startsystime.Month);
  2597. macroIsString:=false;
  2598. end;
  2599. 'DATEDAY':
  2600. begin
  2601. hs:=tostr(startsystime.Day);
  2602. macroIsString:=false;
  2603. end;
  2604. 'TIMEHOUR':
  2605. begin
  2606. hs:=tostr(startsystime.Hour);
  2607. macroIsString:=false;
  2608. end;
  2609. 'TIMEMINUTE':
  2610. begin
  2611. hs:=tostr(startsystime.Minute);
  2612. macroIsString:=false;
  2613. end;
  2614. 'TIMESECOND':
  2615. begin
  2616. hs:=tostr(startsystime.Second);
  2617. macroIsString:=false;
  2618. end;
  2619. 'FILE':
  2620. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2621. 'LINE':
  2622. hs:=tostr(current_filepos.line);
  2623. 'LINENUM':
  2624. begin
  2625. hs:=tostr(current_filepos.line);
  2626. macroIsString:=false;
  2627. end;
  2628. 'FPCVERSION':
  2629. hs:=version_string;
  2630. 'FPCDATE':
  2631. hs:=date_string;
  2632. 'FPCTARGET':
  2633. hs:=target_cpu_string;
  2634. 'FPCTARGETCPU':
  2635. hs:=target_cpu_string;
  2636. 'FPCTARGETOS':
  2637. hs:=target_info.shortname;
  2638. 'CURRENTROUTINE':
  2639. hs:=current_procinfo.procdef.procsym.RealName;
  2640. else
  2641. hs:=GetEnvironmentVariable(hs);
  2642. end;
  2643. if hs='' then
  2644. Message1(scan_w_include_env_not_found,path);
  2645. { make it a stringconst }
  2646. if macroIsString then
  2647. hs:=''''+hs+'''';
  2648. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2649. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2650. end
  2651. else
  2652. begin
  2653. hs:=FixFileName(hs);
  2654. path:=ExtractFilePath(hs);
  2655. name:=ExtractFileName(hs);
  2656. { Special case for Delphi compatibility: '*' has to be replaced
  2657. by the file name of the current source file. }
  2658. if (length(name)>=1) and
  2659. (name[1]='*') then
  2660. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2661. { try to find the file }
  2662. found:=findincludefile(path,name,foundfile);
  2663. if (not found) and (ExtractFileExt(name)='') then
  2664. begin
  2665. { try default extensions .inc , .pp and .pas }
  2666. if (not found) then
  2667. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2668. if (not found) then
  2669. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2670. if (not found) then
  2671. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2672. end;
  2673. { if the name ends in dot, try without the dot }
  2674. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2675. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2676. if current_scanner.inputfilecount<max_include_nesting then
  2677. begin
  2678. inc(current_scanner.inputfilecount);
  2679. { we need to reread the current char }
  2680. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2681. current_scanner.dec_inputpointer;
  2682. {$else not CHECK_INPUTPOINTER_LIMITS}
  2683. dec(current_scanner.inputpointer);
  2684. {$endif CHECK_INPUTPOINTER_LIMITS}
  2685. { reset c }
  2686. c:=#0;
  2687. { shutdown current file }
  2688. current_scanner.tempcloseinputfile;
  2689. { load new file }
  2690. hp:=do_openinputfile(foundfile);
  2691. hp.inc_path:=path;
  2692. current_scanner.addfile(hp);
  2693. current_module.sourcefiles.register_file(hp);
  2694. if (not found) then
  2695. Message1(scan_f_cannot_open_includefile,hs);
  2696. if (not current_scanner.openinputfile) then
  2697. Message1(scan_f_cannot_open_includefile,hs);
  2698. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2699. current_scanner.reload;
  2700. end
  2701. else
  2702. Message(scan_f_include_deep_ten);
  2703. end;
  2704. end;
  2705. {*****************************************************************************
  2706. Preprocessor writing
  2707. *****************************************************************************}
  2708. {$ifdef PREPROCWRITE}
  2709. constructor tpreprocfile.create(const fn:string);
  2710. begin
  2711. inherited create;
  2712. { open outputfile }
  2713. assign(f,fn);
  2714. {$push}{$I-}
  2715. rewrite(f);
  2716. {$pop}
  2717. if ioresult<>0 then
  2718. Comment(V_Fatal,'can''t create file '+fn);
  2719. getmem(buf,preprocbufsize);
  2720. settextbuf(f,buf^,preprocbufsize);
  2721. { reset }
  2722. eolfound:=false;
  2723. spacefound:=false;
  2724. end;
  2725. destructor tpreprocfile.destroy;
  2726. begin
  2727. close(f);
  2728. freemem(buf,preprocbufsize);
  2729. end;
  2730. procedure tpreprocfile.add(const s:string);
  2731. begin
  2732. write(f,s);
  2733. end;
  2734. procedure tpreprocfile.addspace;
  2735. begin
  2736. if eolfound then
  2737. begin
  2738. writeln(f,'');
  2739. eolfound:=false;
  2740. spacefound:=false;
  2741. end
  2742. else
  2743. if spacefound then
  2744. begin
  2745. write(f,' ');
  2746. spacefound:=false;
  2747. end;
  2748. end;
  2749. {$endif PREPROCWRITE}
  2750. {*****************************************************************************
  2751. TPreProcStack
  2752. *****************************************************************************}
  2753. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2754. begin
  2755. accept:=a;
  2756. typ:=atyp;
  2757. next:=n;
  2758. end;
  2759. {*****************************************************************************
  2760. TReplayStack
  2761. *****************************************************************************}
  2762. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2763. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2764. apatternw:pcompilerwidestring;asettings:tsettings;
  2765. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2766. begin
  2767. token:=atoken;
  2768. idtoken:=aidtoken;
  2769. orgpattern:=aorgpattern;
  2770. pattern:=apattern;
  2771. cstringpattern:=acstringpattern;
  2772. initwidestring(patternw);
  2773. if assigned(apatternw) then
  2774. begin
  2775. setlengthwidestring(patternw,apatternw^.len);
  2776. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2777. end;
  2778. settings:=asettings;
  2779. tokenbuf:=atokenbuf;
  2780. tokenbuf_needs_swapping:=change_endian;
  2781. next:=anext;
  2782. end;
  2783. destructor treplaystack.destroy;
  2784. begin
  2785. donewidestring(patternw);
  2786. end;
  2787. {*****************************************************************************
  2788. TDirectiveItem
  2789. *****************************************************************************}
  2790. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2791. begin
  2792. inherited Create(AList,n);
  2793. is_conditional:=false;
  2794. proc:=p;
  2795. end;
  2796. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2797. begin
  2798. inherited Create(AList,n);
  2799. is_conditional:=true;
  2800. proc:=p;
  2801. end;
  2802. {****************************************************************************
  2803. TSCANNERFILE
  2804. ****************************************************************************}
  2805. constructor tscannerfile.Create(const fn: string; is_macro: boolean);
  2806. begin
  2807. inputfile:=do_openinputfile(fn);
  2808. if is_macro then
  2809. inputfile.is_macro:=true;
  2810. if assigned(current_module) then
  2811. current_module.sourcefiles.register_file(inputfile);
  2812. { reset localinput }
  2813. c:=#0;
  2814. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2815. hidden_inputbuffer:=nil;
  2816. hidden_inputpointer:=nil;
  2817. {$else not CHECK_INPUTPOINTER_LIMITS}
  2818. inputbuffer:=nil;
  2819. inputpointer:=nil;
  2820. {$endif CHECK_INPUTPOINTER_LIMITS}
  2821. inputstart:=0;
  2822. { reset scanner }
  2823. preprocstack:=nil;
  2824. replaystack:=nil;
  2825. comment_level:=0;
  2826. yylexcount:=0;
  2827. block_type:=bt_general;
  2828. line_no:=0;
  2829. lastlinepos:=0;
  2830. lasttokenpos:=0;
  2831. nexttokenpos:=0;
  2832. lasttoken:=NOTOKEN;
  2833. nexttoken:=NOTOKEN;
  2834. ignoredirectives:=TFPHashList.Create;
  2835. change_endian_for_replay:=false;
  2836. end;
  2837. procedure tscannerfile.firstfile;
  2838. begin
  2839. { load block }
  2840. if not openinputfile then
  2841. Message1(scan_f_cannot_open_input,inputfile.name);
  2842. reload;
  2843. end;
  2844. destructor tscannerfile.Destroy;
  2845. begin
  2846. if assigned(onfreescanner) then
  2847. onfreescanner(self);
  2848. if assigned(current_module) and
  2849. (current_module.state in [ms_processed,ms_compiled]) and
  2850. (status.errorcount=0) then
  2851. checkpreprocstack
  2852. else
  2853. begin
  2854. while assigned(preprocstack) do
  2855. poppreprocstack;
  2856. end;
  2857. while assigned(replaystack) do
  2858. popreplaystack;
  2859. if not inputfile.closed then
  2860. closeinputfile;
  2861. if inputfile.is_macro then
  2862. inputfile.free;
  2863. ignoredirectives.free;
  2864. end;
  2865. function tscannerfile.openinputfile:boolean;
  2866. begin
  2867. openinputfile:=inputfile.open;
  2868. { load buffer }
  2869. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2870. hidden_inputbuffer:=inputfile.buf;
  2871. hidden_inputpointer:=inputfile.buf;
  2872. {$else not CHECK_INPUTPOINTER_LIMITS}
  2873. inputbuffer:=inputfile.buf;
  2874. inputpointer:=inputfile.buf;
  2875. {$endif CHECK_INPUTPOINTER_LIMITS}
  2876. inputstart:=inputfile.bufstart;
  2877. { line }
  2878. line_no:=0;
  2879. lastlinepos:=0;
  2880. lasttokenpos:=0;
  2881. nexttokenpos:=0;
  2882. end;
  2883. procedure tscannerfile.closeinputfile;
  2884. begin
  2885. inputfile.close;
  2886. { reset buffer }
  2887. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2888. hidden_inputbuffer:=nil;
  2889. hidden_inputpointer:=nil;
  2890. {$else not CHECK_INPUTPOINTER_LIMITS}
  2891. inputbuffer:=nil;
  2892. inputpointer:=nil;
  2893. {$endif CHECK_INPUTPOINTER_LIMITS}
  2894. inputstart:=0;
  2895. { reset line }
  2896. line_no:=0;
  2897. lastlinepos:=0;
  2898. lasttokenpos:=0;
  2899. nexttokenpos:=0;
  2900. end;
  2901. function tscannerfile.tempopeninputfile:boolean;
  2902. begin
  2903. tempopeninputfile:=false;
  2904. if inputfile.is_macro then
  2905. exit;
  2906. tempopeninputfile:=inputfile.tempopen;
  2907. { reload buffer }
  2908. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2909. hidden_inputbuffer:=inputfile.buf;
  2910. hidden_inputpointer:=inputfile.buf;
  2911. {$else not CHECK_INPUTPOINTER_LIMITS}
  2912. inputbuffer:=inputfile.buf;
  2913. inputpointer:=inputfile.buf;
  2914. {$endif CHECK_INPUTPOINTER_LIMITS}
  2915. inputstart:=inputfile.bufstart;
  2916. end;
  2917. procedure tscannerfile.tempcloseinputfile;
  2918. begin
  2919. if inputfile.closed or inputfile.is_macro then
  2920. exit;
  2921. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2922. inputfile.setpos(inputstart+(hidden_inputpointer-hidden_inputbuffer));
  2923. {$else not CHECK_INPUTPOINTER_LIMITS}
  2924. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2925. {$endif CHECK_INPUTPOINTER_LIMITS}
  2926. inputfile.tempclose;
  2927. { reset buffer }
  2928. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2929. hidden_inputbuffer:=nil;
  2930. hidden_inputpointer:=nil;
  2931. {$else not CHECK_INPUTPOINTER_LIMITS}
  2932. inputbuffer:=nil;
  2933. inputpointer:=nil;
  2934. {$endif CHECK_INPUTPOINTER_LIMITS}
  2935. inputstart:=0;
  2936. end;
  2937. procedure tscannerfile.saveinputfile;
  2938. begin
  2939. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2940. inputfile.saveinputpointer:=hidden_inputpointer;
  2941. {$else not CHECK_INPUTPOINTER_LIMITS}
  2942. inputfile.saveinputpointer:=inputpointer;
  2943. {$endif CHECK_INPUTPOINTER_LIMITS}
  2944. inputfile.savelastlinepos:=lastlinepos;
  2945. inputfile.saveline_no:=line_no;
  2946. end;
  2947. procedure tscannerfile.restoreinputfile;
  2948. begin
  2949. {$ifdef check_inputpointer_limits}
  2950. hidden_inputbuffer:=inputfile.buf;
  2951. hidden_inputpointer:=inputfile.saveinputpointer;
  2952. {$else not check_inputpointer_limits}
  2953. inputbuffer:=inputfile.buf;
  2954. inputpointer:=inputfile.saveinputpointer;
  2955. {$endif check_inputpointer_limits}
  2956. lastlinepos:=inputfile.savelastlinepos;
  2957. line_no:=inputfile.saveline_no;
  2958. if not inputfile.is_macro then
  2959. parser_current_file:=inputfile.name;
  2960. end;
  2961. procedure tscannerfile.nextfile;
  2962. var
  2963. to_dispose : tinputfile;
  2964. begin
  2965. if assigned(inputfile.next) then
  2966. begin
  2967. if inputfile.is_macro then
  2968. begin
  2969. to_dispose:=inputfile;
  2970. dec(macro_nesting_depth);
  2971. end
  2972. else
  2973. begin
  2974. to_dispose:=nil;
  2975. dec(inputfilecount);
  2976. end;
  2977. { we can allways close the file, no ? }
  2978. inputfile.close;
  2979. inputfile:=inputfile.next;
  2980. if assigned(to_dispose) then
  2981. to_dispose.free;
  2982. restoreinputfile;
  2983. end;
  2984. end;
  2985. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2986. function tscannerfile.get_inputpointer_char(offset : longint = 0) : char;
  2987. begin
  2988. assert(hidden_inputpointer-hidden_inputbuffer+offset<=inputfile.bufsize);
  2989. get_inputpointer_char:=(hidden_inputpointer+offset)^;
  2990. end;
  2991. procedure tscannerfile.inc_inputpointer(amount : longint = 1);
  2992. begin
  2993. assert(hidden_inputpointer-hidden_inputbuffer+amount<=inputfile.bufsize);
  2994. inc(hidden_inputpointer,amount);
  2995. end;
  2996. procedure tscannerfile.dec_inputpointer;
  2997. begin
  2998. assert(hidden_inputpointer>hidden_inputbuffer);
  2999. dec(hidden_inputpointer);
  3000. end;
  3001. {$endif}
  3002. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  3003. begin
  3004. if not assigned(buf) then
  3005. internalerror(200511172);
  3006. if assigned(recordtokenbuf) then
  3007. internalerror(200511173);
  3008. recordtokenbuf:=buf;
  3009. fillchar(last_settings,sizeof(last_settings),0);
  3010. last_message:=nil;
  3011. fillchar(last_filepos,sizeof(last_filepos),0);
  3012. end;
  3013. procedure tscannerfile.stoprecordtokens;
  3014. begin
  3015. if not assigned(recordtokenbuf) then
  3016. internalerror(200511174);
  3017. recordtokenbuf:=nil;
  3018. end;
  3019. function tscannerfile.is_recording_tokens: boolean;
  3020. begin
  3021. result:=assigned(recordtokenbuf);
  3022. end;
  3023. procedure tscannerfile.writetoken(t : ttoken);
  3024. var
  3025. b : byte;
  3026. begin
  3027. if ord(t)>$7f then
  3028. begin
  3029. b:=(ord(t) shr 8) or $80;
  3030. recordtokenbuf.write(b,1);
  3031. end;
  3032. b:=ord(t) and $ff;
  3033. recordtokenbuf.write(b,1);
  3034. end;
  3035. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  3036. begin
  3037. recordtokenbuf.write(val,sizeof(asizeint));
  3038. end;
  3039. procedure tscannerfile.tokenwritelongint(val : longint);
  3040. begin
  3041. recordtokenbuf.write(val,sizeof(longint));
  3042. end;
  3043. procedure tscannerfile.tokenwriteshortint(val : shortint);
  3044. begin
  3045. recordtokenbuf.write(val,sizeof(shortint));
  3046. end;
  3047. procedure tscannerfile.tokenwriteword(val : word);
  3048. begin
  3049. recordtokenbuf.write(val,sizeof(word));
  3050. end;
  3051. procedure tscannerfile.tokenwritelongword(val : longword);
  3052. begin
  3053. recordtokenbuf.write(val,sizeof(longword));
  3054. end;
  3055. function tscannerfile.tokenreadsizeint : asizeint;
  3056. var
  3057. val : asizeint;
  3058. begin
  3059. replaytokenbuf.read(val,sizeof(asizeint));
  3060. if change_endian_for_replay then
  3061. val:=swapendian(val);
  3062. result:=val;
  3063. end;
  3064. function tscannerfile.tokenreadlongword : longword;
  3065. var
  3066. val : longword;
  3067. begin
  3068. replaytokenbuf.read(val,sizeof(longword));
  3069. if change_endian_for_replay then
  3070. val:=swapendian(val);
  3071. result:=val;
  3072. end;
  3073. function tscannerfile.tokenreadlongint : longint;
  3074. var
  3075. val : longint;
  3076. begin
  3077. replaytokenbuf.read(val,sizeof(longint));
  3078. if change_endian_for_replay then
  3079. val:=swapendian(val);
  3080. result:=val;
  3081. end;
  3082. function tscannerfile.tokenreadshortint : shortint;
  3083. var
  3084. val : shortint;
  3085. begin
  3086. replaytokenbuf.read(val,sizeof(shortint));
  3087. result:=val;
  3088. end;
  3089. function tscannerfile.tokenreadbyte : byte;
  3090. var
  3091. val : byte;
  3092. begin
  3093. replaytokenbuf.read(val,sizeof(byte));
  3094. result:=val;
  3095. end;
  3096. function tscannerfile.tokenreadsmallint : smallint;
  3097. var
  3098. val : smallint;
  3099. begin
  3100. replaytokenbuf.read(val,sizeof(smallint));
  3101. if change_endian_for_replay then
  3102. val:=swapendian(val);
  3103. result:=val;
  3104. end;
  3105. function tscannerfile.tokenreadword : word;
  3106. var
  3107. val : word;
  3108. begin
  3109. replaytokenbuf.read(val,sizeof(word));
  3110. if change_endian_for_replay then
  3111. val:=swapendian(val);
  3112. result:=val;
  3113. end;
  3114. function tscannerfile.tokenreadenum(size : longint) : longword;
  3115. begin
  3116. if size=1 then
  3117. result:=tokenreadbyte
  3118. else if size=2 then
  3119. result:=tokenreadword
  3120. else if size=4 then
  3121. result:=tokenreadlongword
  3122. else
  3123. internalerror(2013112901);
  3124. end;
  3125. procedure tscannerfile.tokenreadset(var b;size : longint);
  3126. var
  3127. i : longint;
  3128. begin
  3129. replaytokenbuf.read(b,size);
  3130. if change_endian_for_replay then
  3131. for i:=0 to size-1 do
  3132. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  3133. end;
  3134. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  3135. begin
  3136. recordtokenbuf.write(b,size);
  3137. end;
  3138. procedure tscannerfile.tokenwriteset(var b;size : longint);
  3139. begin
  3140. recordtokenbuf.write(b,size);
  3141. end;
  3142. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  3143. { This procedure
  3144. needs to be changed whenever
  3145. globals.tsettings type is changed,
  3146. the problem is that no error will appear
  3147. before tests with generics are tested. PM }
  3148. var
  3149. startpos, endpos : longword;
  3150. begin
  3151. { WARNING all those fields need to be in the correct
  3152. order otherwise cross_endian PPU reading will fail }
  3153. startpos:=replaytokenbuf.pos;
  3154. with asettings do
  3155. begin
  3156. alignment.procalign:=tokenreadlongint;
  3157. alignment.loopalign:=tokenreadlongint;
  3158. alignment.jumpalign:=tokenreadlongint;
  3159. alignment.jumpalignskipmax:=tokenreadlongint;
  3160. alignment.coalescealign:=tokenreadlongint;
  3161. alignment.coalescealignskipmax:=tokenreadlongint;
  3162. alignment.constalignmin:=tokenreadlongint;
  3163. alignment.constalignmax:=tokenreadlongint;
  3164. alignment.varalignmin:=tokenreadlongint;
  3165. alignment.varalignmax:=tokenreadlongint;
  3166. alignment.localalignmin:=tokenreadlongint;
  3167. alignment.localalignmax:=tokenreadlongint;
  3168. alignment.recordalignmin:=tokenreadlongint;
  3169. alignment.recordalignmax:=tokenreadlongint;
  3170. alignment.maxCrecordalign:=tokenreadlongint;
  3171. tokenreadset(globalswitches,sizeof(globalswitches));
  3172. tokenreadset(targetswitches,sizeof(targetswitches));
  3173. tokenreadset(moduleswitches,sizeof(moduleswitches));
  3174. tokenreadset(localswitches,sizeof(localswitches));
  3175. tokenreadset(modeswitches,sizeof(modeswitches));
  3176. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  3177. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3178. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3179. tokenreadset(debugswitches,sizeof(debugswitches));
  3180. { 0: old behaviour for sets <=256 elements
  3181. >0: round to this size }
  3182. setalloc:=tokenreadshortint;
  3183. packenum:=tokenreadshortint;
  3184. packrecords:=tokenreadshortint;
  3185. maxfpuregisters:=tokenreadshortint;
  3186. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3187. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3188. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  3189. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  3190. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  3191. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  3192. { tstringencoding is word type,
  3193. thus this should be OK here }
  3194. sourcecodepage:=tstringEncoding(tokenreadword);
  3195. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  3196. disabledircache:=boolean(tokenreadbyte);
  3197. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  3198. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  3199. { While adding ControllerSupport constant, I decided not to store ct_none }
  3200. { on targets not supporting controllers, but this might be changed here and }
  3201. { in tokenwritesettings in the future to unify the PPU structure and handling }
  3202. { of this field in the compiler. }
  3203. {$PUSH}
  3204. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3205. if ControllerSupport then
  3206. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  3207. else
  3208. ControllerType:=ct_none;
  3209. {$POP}
  3210. endpos:=replaytokenbuf.pos;
  3211. if endpos-startpos<>expected_size then
  3212. Comment(V_Error,'Wrong size of Settings read-in');
  3213. end;
  3214. end;
  3215. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  3216. { This procedure
  3217. needs to be changed whenever
  3218. globals.tsettings type is changed,
  3219. the problem is that no error will appear
  3220. before tests with generics are tested. PM }
  3221. var
  3222. sizepos, startpos, endpos : longword;
  3223. begin
  3224. { WARNING all those fields need to be in the correct
  3225. order otherwise cross_endian PPU reading will fail }
  3226. sizepos:=recordtokenbuf.pos;
  3227. size:=0;
  3228. tokenwritesizeint(size);
  3229. startpos:=recordtokenbuf.pos;
  3230. with asettings do
  3231. begin
  3232. tokenwritelongint(alignment.procalign);
  3233. tokenwritelongint(alignment.loopalign);
  3234. tokenwritelongint(alignment.jumpalign);
  3235. tokenwritelongint(alignment.jumpalignskipmax);
  3236. tokenwritelongint(alignment.coalescealign);
  3237. tokenwritelongint(alignment.coalescealignskipmax);
  3238. tokenwritelongint(alignment.constalignmin);
  3239. tokenwritelongint(alignment.constalignmax);
  3240. tokenwritelongint(alignment.varalignmin);
  3241. tokenwritelongint(alignment.varalignmax);
  3242. tokenwritelongint(alignment.localalignmin);
  3243. tokenwritelongint(alignment.localalignmax);
  3244. tokenwritelongint(alignment.recordalignmin);
  3245. tokenwritelongint(alignment.recordalignmax);
  3246. tokenwritelongint(alignment.maxCrecordalign);
  3247. tokenwriteset(globalswitches,sizeof(globalswitches));
  3248. tokenwriteset(targetswitches,sizeof(targetswitches));
  3249. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3250. tokenwriteset(localswitches,sizeof(localswitches));
  3251. tokenwriteset(modeswitches,sizeof(modeswitches));
  3252. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3253. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3254. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3255. tokenwriteset(debugswitches,sizeof(debugswitches));
  3256. { 0: old behaviour for sets <=256 elements
  3257. >0: round to this size }
  3258. tokenwriteshortint(setalloc);
  3259. tokenwriteshortint(packenum);
  3260. tokenwriteshortint(packrecords);
  3261. tokenwriteshortint(maxfpuregisters);
  3262. tokenwriteenum(cputype,sizeof(tcputype));
  3263. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3264. tokenwriteenum(fputype,sizeof(tfputype));
  3265. tokenwriteenum(asmmode,sizeof(tasmmode));
  3266. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3267. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3268. { tstringencoding is word type,
  3269. thus this should be OK here }
  3270. tokenwriteword(sourcecodepage);
  3271. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3272. recordtokenbuf.write(byte(disabledircache),1);
  3273. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3274. { TH: See note about controllertype field in tokenreadsettings. }
  3275. {$PUSH}
  3276. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3277. if ControllerSupport then
  3278. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3279. {$POP}
  3280. endpos:=recordtokenbuf.pos;
  3281. size:=endpos-startpos;
  3282. recordtokenbuf.seek(sizepos);
  3283. tokenwritesizeint(size);
  3284. recordtokenbuf.seek(endpos);
  3285. end;
  3286. end;
  3287. procedure tscannerfile.recordtoken;
  3288. var
  3289. t : ttoken;
  3290. s : tspecialgenerictoken;
  3291. len,msgnb,copy_size : asizeint;
  3292. val : longint;
  3293. b : byte;
  3294. pmsg : pmessagestaterecord;
  3295. begin
  3296. if not assigned(recordtokenbuf) then
  3297. internalerror(200511176);
  3298. t:=_GENERICSPECIALTOKEN;
  3299. { settings changed? }
  3300. { last field pmessage is handled separately below in
  3301. ST_LOADMESSAGES }
  3302. if CompareByte(current_settings,last_settings,
  3303. sizeof(current_settings)-sizeof(pointer))<>0 then
  3304. begin
  3305. { use a special token to record it }
  3306. s:=ST_LOADSETTINGS;
  3307. writetoken(t);
  3308. recordtokenbuf.write(s,1);
  3309. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3310. tokenwritesettings(current_settings,copy_size);
  3311. last_settings:=current_settings;
  3312. end;
  3313. if current_settings.pmessage<>last_message then
  3314. begin
  3315. { use a special token to record it }
  3316. s:=ST_LOADMESSAGES;
  3317. writetoken(t);
  3318. recordtokenbuf.write(s,1);
  3319. msgnb:=0;
  3320. pmsg:=current_settings.pmessage;
  3321. while assigned(pmsg) do
  3322. begin
  3323. if msgnb=high(asizeint) then
  3324. { Too many messages }
  3325. internalerror(2011090401);
  3326. inc(msgnb);
  3327. pmsg:=pmsg^.next;
  3328. end;
  3329. tokenwritesizeint(msgnb);
  3330. pmsg:=current_settings.pmessage;
  3331. while assigned(pmsg) do
  3332. begin
  3333. { What about endianess here?}
  3334. { SB: this is handled by tokenreadlongint }
  3335. val:=pmsg^.value;
  3336. tokenwritelongint(val);
  3337. val:=ord(pmsg^.state);
  3338. tokenwritelongint(val);
  3339. pmsg:=pmsg^.next;
  3340. end;
  3341. last_message:=current_settings.pmessage;
  3342. end;
  3343. { file pos changes? }
  3344. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3345. begin
  3346. s:=ST_FILEINDEX;
  3347. writetoken(t);
  3348. recordtokenbuf.write(s,1);
  3349. tokenwriteword(current_tokenpos.fileindex);
  3350. last_filepos.fileindex:=current_tokenpos.fileindex;
  3351. last_filepos.line:=0;
  3352. end;
  3353. if current_tokenpos.line<>last_filepos.line then
  3354. begin
  3355. s:=ST_LINE;
  3356. writetoken(t);
  3357. recordtokenbuf.write(s,1);
  3358. tokenwritelongint(current_tokenpos.line);
  3359. last_filepos.line:=current_tokenpos.line;
  3360. last_filepos.column:=0;
  3361. end;
  3362. if current_tokenpos.column<>last_filepos.column then
  3363. begin
  3364. s:=ST_COLUMN;
  3365. writetoken(t);
  3366. { can the column be written packed? }
  3367. if current_tokenpos.column<$80 then
  3368. begin
  3369. b:=$80 or current_tokenpos.column;
  3370. recordtokenbuf.write(b,1);
  3371. end
  3372. else
  3373. begin
  3374. recordtokenbuf.write(s,1);
  3375. tokenwriteword(current_tokenpos.column);
  3376. end;
  3377. last_filepos.column:=current_tokenpos.column;
  3378. end;
  3379. writetoken(token);
  3380. if token<>_GENERICSPECIALTOKEN then
  3381. writetoken(idtoken);
  3382. case token of
  3383. _CWCHAR,
  3384. _CWSTRING :
  3385. begin
  3386. tokenwritesizeint(patternw^.len);
  3387. if patternw^.len>0 then
  3388. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3389. end;
  3390. _CSTRING:
  3391. begin
  3392. len:=length(cstringpattern);
  3393. tokenwritesizeint(len);
  3394. if len>0 then
  3395. recordtokenbuf.write(cstringpattern[1],len);
  3396. end;
  3397. _CCHAR,
  3398. _INTCONST,
  3399. _REALNUMBER :
  3400. begin
  3401. { pexpr.pas messes with pattern in case of negative integer consts,
  3402. see around line 2562 the comment of JM; remove the - before recording it
  3403. (FK)
  3404. }
  3405. if (token=_INTCONST) and (pattern[1]='-') then
  3406. delete(pattern,1,1);
  3407. recordtokenbuf.write(pattern[0],1);
  3408. recordtokenbuf.write(pattern[1],length(pattern));
  3409. end;
  3410. _ID :
  3411. begin
  3412. recordtokenbuf.write(orgpattern[0],1);
  3413. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3414. end;
  3415. else
  3416. ;
  3417. end;
  3418. end;
  3419. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3420. begin
  3421. if not assigned(buf) then
  3422. internalerror(200511175);
  3423. { save current scanner state }
  3424. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3425. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3426. {$ifdef check_inputpointer_limits}
  3427. if assigned(hidden_inputpointer) then
  3428. dec_inputpointer;
  3429. {$else not check_inputpointer_limits}
  3430. if assigned(inputpointer) then
  3431. dec(inputpointer);
  3432. {$endif check_inputpointer_limits}
  3433. { install buffer }
  3434. replaytokenbuf:=buf;
  3435. { Initialize value of change_endian_for_replay variable }
  3436. change_endian_for_replay:=change_endian;
  3437. { reload next token }
  3438. replaytokenbuf.seek(0);
  3439. replaytoken;
  3440. end;
  3441. function tscannerfile.readtoken: ttoken;
  3442. var
  3443. b,b2 : byte;
  3444. begin
  3445. replaytokenbuf.read(b,1);
  3446. if (b and $80)<>0 then
  3447. begin
  3448. replaytokenbuf.read(b2,1);
  3449. result:=ttoken(((b and $7f) shl 8) or b2);
  3450. end
  3451. else
  3452. result:=ttoken(b);
  3453. end;
  3454. procedure tscannerfile.replaytoken;
  3455. var
  3456. wlen,mesgnb,copy_size : asizeint;
  3457. specialtoken : tspecialgenerictoken;
  3458. i : byte;
  3459. pmsg,prevmsg : pmessagestaterecord;
  3460. begin
  3461. if not assigned(replaytokenbuf) then
  3462. internalerror(200511177);
  3463. { End of replay buffer? Then load the next char from the file again }
  3464. if replaytokenbuf.pos>=replaytokenbuf.size then
  3465. begin
  3466. token:=replaystack.token;
  3467. idtoken:=replaystack.idtoken;
  3468. pattern:=replaystack.pattern;
  3469. orgpattern:=replaystack.orgpattern;
  3470. setlengthwidestring(patternw,replaystack.patternw^.len);
  3471. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3472. cstringpattern:=replaystack.cstringpattern;
  3473. replaytokenbuf:=replaystack.tokenbuf;
  3474. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3475. { restore compiler settings }
  3476. current_settings:=replaystack.settings;
  3477. popreplaystack;
  3478. {$ifdef check_inputpointer_limits}
  3479. if assigned(hidden_inputpointer) then
  3480. begin
  3481. c:=get_inputpointer_char;
  3482. inc_inputpointer;
  3483. end;
  3484. {$else not check_inputpointer_limits}
  3485. if assigned(inputpointer) then
  3486. begin
  3487. c:=inputpointer^;
  3488. inc(inputpointer);
  3489. end;
  3490. {$endif check_inputpointer_limits}
  3491. exit;
  3492. end;
  3493. repeat
  3494. { load token from the buffer }
  3495. token:=readtoken;
  3496. if token<>_GENERICSPECIALTOKEN then
  3497. idtoken:=readtoken
  3498. else
  3499. idtoken:=_NOID;
  3500. case token of
  3501. _CWCHAR,
  3502. _CWSTRING :
  3503. begin
  3504. wlen:=tokenreadsizeint;
  3505. setlengthwidestring(patternw,wlen);
  3506. if wlen>0 then
  3507. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3508. orgpattern:='';
  3509. pattern:='';
  3510. cstringpattern:='';
  3511. end;
  3512. _CSTRING:
  3513. begin
  3514. wlen:=tokenreadsizeint;
  3515. if wlen>0 then
  3516. begin
  3517. setlength(cstringpattern,wlen);
  3518. replaytokenbuf.read(cstringpattern[1],wlen);
  3519. end
  3520. else
  3521. cstringpattern:='';
  3522. orgpattern:='';
  3523. pattern:='';
  3524. end;
  3525. _CCHAR,
  3526. _INTCONST,
  3527. _REALNUMBER :
  3528. begin
  3529. replaytokenbuf.read(pattern[0],1);
  3530. replaytokenbuf.read(pattern[1],length(pattern));
  3531. orgpattern:='';
  3532. end;
  3533. _ID :
  3534. begin
  3535. replaytokenbuf.read(orgpattern[0],1);
  3536. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3537. pattern:=upper(orgpattern);
  3538. end;
  3539. _GENERICSPECIALTOKEN:
  3540. begin
  3541. replaytokenbuf.read(specialtoken,1);
  3542. { packed column? }
  3543. if (ord(specialtoken) and $80)<>0 then
  3544. begin
  3545. current_tokenpos.column:=ord(specialtoken) and $7f;
  3546. current_filepos:=current_tokenpos;
  3547. end
  3548. else
  3549. case specialtoken of
  3550. ST_LOADSETTINGS:
  3551. begin
  3552. copy_size:=tokenreadsizeint;
  3553. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3554. // internalerror(2011090501);
  3555. {
  3556. replaytokenbuf.read(current_settings,copy_size);
  3557. }
  3558. tokenreadsettings(current_settings,copy_size);
  3559. end;
  3560. ST_LOADMESSAGES:
  3561. begin
  3562. current_settings.pmessage:=nil;
  3563. mesgnb:=tokenreadsizeint;
  3564. prevmsg:=nil;
  3565. for i:=1 to mesgnb do
  3566. begin
  3567. new(pmsg);
  3568. if i=1 then
  3569. current_settings.pmessage:=pmsg
  3570. else
  3571. prevmsg^.next:=pmsg;
  3572. pmsg^.value:=tokenreadlongint;
  3573. pmsg^.state:=tmsgstate(tokenreadlongint);
  3574. pmsg^.next:=nil;
  3575. prevmsg:=pmsg;
  3576. end;
  3577. end;
  3578. ST_LINE:
  3579. begin
  3580. current_tokenpos.line:=tokenreadlongint;
  3581. current_filepos:=current_tokenpos;
  3582. end;
  3583. ST_COLUMN:
  3584. begin
  3585. current_tokenpos.column:=tokenreadword;
  3586. current_filepos:=current_tokenpos;
  3587. end;
  3588. ST_FILEINDEX:
  3589. begin
  3590. current_tokenpos.fileindex:=tokenreadword;
  3591. current_filepos:=current_tokenpos;
  3592. end;
  3593. end;
  3594. continue;
  3595. end;
  3596. else
  3597. ;
  3598. end;
  3599. break;
  3600. until false;
  3601. end;
  3602. procedure tscannerfile.addfile(hp:tinputfile);
  3603. begin
  3604. saveinputfile;
  3605. { add to list }
  3606. hp.next:=inputfile;
  3607. inputfile:=hp;
  3608. { load new inputfile }
  3609. restoreinputfile;
  3610. end;
  3611. procedure tscannerfile.reload;
  3612. var
  3613. wasmacro: Boolean;
  3614. begin
  3615. with inputfile do
  3616. begin
  3617. { when nothing more to read then leave immediatly, so we
  3618. don't change the current_filepos and leave it point to the last
  3619. char }
  3620. if (c=#26) and (not assigned(next)) then
  3621. exit;
  3622. repeat
  3623. { still more to read?, then change the #0 to a space so its seen
  3624. as a separator, this can't be used for macro's which can change
  3625. the place of the #0 in the buffer with tempopen }
  3626. if (c=#0) and (bufsize>0) and
  3627. not(inputfile.is_macro) and
  3628. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3629. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3630. {$else not CHECK_INPUTPOINTER_LIMITS}
  3631. (inputpointer-inputbuffer<bufsize) then
  3632. {$endif CHECK_INPUTPOINTER_LIMITS}
  3633. begin
  3634. c:=' ';
  3635. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3636. inc_inputpointer;
  3637. {$else not CHECK_INPUTPOINTER_LIMITS}
  3638. inc(inputpointer);
  3639. {$endif CHECK_INPUTPOINTER_LIMITS}
  3640. exit;
  3641. end;
  3642. { can we read more from this file ? }
  3643. if (c<>#26) and (not endoffile) then
  3644. begin
  3645. readbuf;
  3646. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3647. hidden_inputpointer:=buf;
  3648. hidden_inputbuffer:=buf;
  3649. {$else not CHECK_INPUTPOINTER_LIMITS}
  3650. inputpointer:=buf;
  3651. inputbuffer:=buf;
  3652. {$endif CHECK_INPUTPOINTER_LIMITS}
  3653. inputstart:=bufstart;
  3654. { first line? }
  3655. if line_no=0 then
  3656. begin
  3657. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3658. c:=get_inputpointer_char;
  3659. { eat utf-8 signature? }
  3660. if (bufsize>2) and
  3661. (ord(get_inputpointer_char)=$ef) and
  3662. (ord(get_inputpointer_char(1))=$bb) and
  3663. (ord(get_inputpointer_char(2))=$bf) then
  3664. begin
  3665. {$else not CHECK_INPUTPOINTER_LIMITS}
  3666. c:=inputpointer^;
  3667. { eat utf-8 signature? }
  3668. if (bufsize>2) and
  3669. (ord(inputpointer^)=$ef) and
  3670. (ord((inputpointer+1)^)=$bb) and
  3671. (ord((inputpointer+2)^)=$bf) then
  3672. begin
  3673. {$endif CHECK_INPUTPOINTER_LIMITS}
  3674. (* we don't support including files with an UTF-8 bom
  3675. inside another file that wasn't encoded as UTF-8
  3676. already (we don't support {$codepage xxx} switches in
  3677. the middle of a file either) *)
  3678. if (current_settings.sourcecodepage<>CP_UTF8) and
  3679. not current_module.in_global then
  3680. Message(scanner_f_illegal_utf8_bom);
  3681. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3682. inc_inputpointer(3);
  3683. {$else not CHECK_INPUTPOINTER_LIMITS}
  3684. inc(inputpointer,3);
  3685. {$endif CHECK_INPUTPOINTER_LIMITS}
  3686. message(scan_c_switching_to_utf8);
  3687. current_settings.sourcecodepage:=CP_UTF8;
  3688. exclude(current_settings.moduleswitches,cs_system_codepage);
  3689. include(current_settings.moduleswitches,cs_explicit_codepage);
  3690. end;
  3691. line_no:=1;
  3692. if cs_asm_source in current_settings.globalswitches then
  3693. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3694. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3695. {$else not CHECK_INPUTPOINTER_LIMITS}
  3696. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3697. {$endif CHECK_INPUTPOINTER_LIMITS}
  3698. end;
  3699. end
  3700. else
  3701. begin
  3702. wasmacro:=inputfile.is_macro;
  3703. { load eof position in tokenpos/current_filepos }
  3704. gettokenpos;
  3705. { close file }
  3706. closeinputfile;
  3707. { no next module, than EOF }
  3708. if not assigned(inputfile.next) then
  3709. begin
  3710. c:=#26;
  3711. exit;
  3712. end;
  3713. { load next file and reopen it }
  3714. nextfile;
  3715. tempopeninputfile;
  3716. { status }
  3717. Message1(scan_t_back_in,inputfile.name);
  3718. { end of include file is like a line break which ends e.g. also // style comments }
  3719. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3720. begin
  3721. c:=#10;
  3722. { ... but we have to decrease the line number first because it is increased due to this
  3723. inserted line break later on }
  3724. dec(line_no);
  3725. exit;
  3726. end;
  3727. end;
  3728. { load next char }
  3729. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3730. c:=get_inputpointer_char;
  3731. inc_inputpointer;
  3732. {$else not CHECK_INPUTPOINTER_LIMITS}
  3733. c:=inputpointer^;
  3734. inc(inputpointer);
  3735. {$endif CHECK_INPUTPOINTER_LIMITS}
  3736. until c<>#0; { if also end, then reload again }
  3737. end;
  3738. end;
  3739. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3740. var
  3741. hp : tinputfile;
  3742. begin
  3743. { save old postion }
  3744. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3745. dec_inputpointer;
  3746. {$else not CHECK_INPUTPOINTER_LIMITS}
  3747. dec(inputpointer);
  3748. {$endif CHECK_INPUTPOINTER_LIMITS}
  3749. tempcloseinputfile;
  3750. { create macro 'file' }
  3751. { use special name to dispose after !! }
  3752. hp:=do_openinputfile('_Macro_.'+macname);
  3753. addfile(hp);
  3754. with inputfile do
  3755. begin
  3756. inc(macro_nesting_depth);
  3757. setmacro(p,len);
  3758. { local buffer }
  3759. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3760. hidden_inputbuffer:=buf;
  3761. hidden_inputpointer:=buf;
  3762. {$else not CHECK_INPUTPOINTER_LIMITS}
  3763. inputbuffer:=buf;
  3764. inputpointer:=buf;
  3765. {$endif CHECK_INPUTPOINTER_LIMITS}
  3766. inputstart:=bufstart;
  3767. ref_index:=fileindex;
  3768. internally_generated_macro:=internally_generated;
  3769. end;
  3770. { reset line }
  3771. line_no:=line;
  3772. lastlinepos:=0;
  3773. lasttokenpos:=0;
  3774. nexttokenpos:=0;
  3775. { load new c }
  3776. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3777. c:=get_inputpointer_char;
  3778. inc_inputpointer;
  3779. {$else not CHECK_INPUTPOINTER_LIMITS}
  3780. c:=inputpointer^;
  3781. inc(inputpointer);
  3782. {$endif CHECK_INPUTPOINTER_LIMITS}
  3783. end;
  3784. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3785. begin
  3786. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3787. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3788. {$else not CHECK_INPUTPOINTER_LIMITS}
  3789. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3790. {$endif CHECK_INPUTPOINTER_LIMITS}
  3791. filepos.line:=line_no;
  3792. filepos.column:=tokenpos-lastlinepos;
  3793. filepos.fileindex:=inputfile.ref_index;
  3794. filepos.moduleindex:=current_module.unit_index;
  3795. end;
  3796. procedure tscannerfile.gettokenpos;
  3797. { load the values of tokenpos and lasttokenpos }
  3798. begin
  3799. do_gettokenpos(lasttokenpos,current_tokenpos);
  3800. current_filepos:=current_tokenpos;
  3801. end;
  3802. procedure tscannerfile.cachenexttokenpos;
  3803. begin
  3804. do_gettokenpos(nexttokenpos,next_filepos);
  3805. end;
  3806. procedure tscannerfile.setnexttoken;
  3807. begin
  3808. token:=nexttoken;
  3809. nexttoken:=NOTOKEN;
  3810. lasttokenpos:=nexttokenpos;
  3811. current_tokenpos:=next_filepos;
  3812. current_filepos:=current_tokenpos;
  3813. nexttokenpos:=0;
  3814. end;
  3815. procedure tscannerfile.savetokenpos;
  3816. begin
  3817. oldlasttokenpos:=lasttokenpos;
  3818. oldcurrent_filepos:=current_filepos;
  3819. oldcurrent_tokenpos:=current_tokenpos;
  3820. end;
  3821. procedure tscannerfile.restoretokenpos;
  3822. begin
  3823. lasttokenpos:=oldlasttokenpos;
  3824. current_filepos:=oldcurrent_filepos;
  3825. current_tokenpos:=oldcurrent_tokenpos;
  3826. end;
  3827. procedure tscannerfile.inc_comment_level;
  3828. begin
  3829. if (m_nested_comment in current_settings.modeswitches) then
  3830. inc(comment_level)
  3831. else
  3832. comment_level:=1;
  3833. if (comment_level>1) then
  3834. begin
  3835. savetokenpos;
  3836. gettokenpos; { update for warning }
  3837. Message1(scan_w_comment_level,tostr(comment_level));
  3838. restoretokenpos;
  3839. end;
  3840. end;
  3841. procedure tscannerfile.dec_comment_level;
  3842. begin
  3843. if (m_nested_comment in current_settings.modeswitches) then
  3844. dec(comment_level)
  3845. else
  3846. comment_level:=0;
  3847. end;
  3848. procedure tscannerfile.linebreak;
  3849. var
  3850. cur : char;
  3851. begin
  3852. with inputfile do
  3853. begin
  3854. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3855. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3856. {$else not CHECK_INPUTPOINTER_LIMITS}
  3857. if (byte(inputpointer^)=0) and not(endoffile) then
  3858. {$endif CHECK_INPUTPOINTER_LIMITS}
  3859. begin
  3860. cur:=c;
  3861. reload;
  3862. if byte(cur)+byte(c)<>23 then
  3863. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3864. dec_inputpointer;
  3865. {$else not CHECK_INPUTPOINTER_LIMITS}
  3866. dec(inputpointer);
  3867. {$endif CHECK_INPUTPOINTER_LIMITS}
  3868. end
  3869. else
  3870. begin
  3871. { Support all combination of #10 and #13 as line break }
  3872. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3873. if (byte(get_inputpointer_char)+byte(c)=23) then
  3874. inc_inputpointer;
  3875. {$else not CHECK_INPUTPOINTER_LIMITS}
  3876. if (byte(inputpointer^)+byte(c)=23) then
  3877. inc(inputpointer);
  3878. {$endif CHECK_INPUTPOINTER_LIMITS}
  3879. end;
  3880. { Always return #10 as line break }
  3881. c:=#10;
  3882. { increase line counters }
  3883. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3884. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3885. {$else not CHECK_INPUTPOINTER_LIMITS}
  3886. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3887. {$endif CHECK_INPUTPOINTER_LIMITS}
  3888. inc(line_no);
  3889. { update linebuffer }
  3890. if cs_asm_source in current_settings.globalswitches then
  3891. inputfile.setline(line_no,lastlinepos);
  3892. { update for status and call the show status routine,
  3893. but don't touch current_filepos ! }
  3894. savetokenpos;
  3895. gettokenpos; { update for v_status }
  3896. inc(status.compiledlines);
  3897. ShowStatus;
  3898. restoretokenpos;
  3899. end;
  3900. end;
  3901. procedure tscannerfile.illegal_char(c:char);
  3902. var
  3903. s : string;
  3904. begin
  3905. if c in [#32..#255] then
  3906. s:=''''+c+''''
  3907. else
  3908. s:='#'+tostr(ord(c));
  3909. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3910. end;
  3911. procedure tscannerfile.end_of_file;
  3912. begin
  3913. checkpreprocstack;
  3914. Message(scan_f_end_of_file);
  3915. end;
  3916. {-------------------------------------------
  3917. IF Conditional Handling
  3918. -------------------------------------------}
  3919. procedure tscannerfile.checkpreprocstack;
  3920. begin
  3921. { check for missing ifdefs }
  3922. while assigned(preprocstack) do
  3923. begin
  3924. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3925. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3926. tostr(preprocstack.line_nb));
  3927. poppreprocstack;
  3928. end;
  3929. end;
  3930. procedure tscannerfile.poppreprocstack;
  3931. var
  3932. hp : tpreprocstack;
  3933. begin
  3934. if assigned(preprocstack) then
  3935. begin
  3936. Message1(scan_c_endif_found,preprocstack.name);
  3937. hp:=preprocstack.next;
  3938. preprocstack.free;
  3939. preprocstack:=hp;
  3940. end
  3941. else
  3942. Message(scan_e_endif_without_if);
  3943. end;
  3944. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3945. var
  3946. condition: Boolean;
  3947. valuedescr: String;
  3948. begin
  3949. if (preprocstack=nil) or preprocstack.accept then
  3950. condition:=compile_time_predicate(valuedescr)
  3951. else
  3952. begin
  3953. condition:= false;
  3954. valuedescr:= '';
  3955. end;
  3956. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3957. preprocstack.name:=valuedescr;
  3958. preprocstack.line_nb:=line_no;
  3959. preprocstack.fileindex:=current_filepos.fileindex;
  3960. if preprocstack.accept then
  3961. Message2(messid,preprocstack.name,'accepted')
  3962. else
  3963. Message2(messid,preprocstack.name,'rejected');
  3964. end;
  3965. procedure tscannerfile.elsepreprocstack;
  3966. begin
  3967. if assigned(preprocstack) and
  3968. (preprocstack.typ<>pp_else) then
  3969. begin
  3970. if (preprocstack.typ=pp_elseif) then
  3971. preprocstack.accept:=false
  3972. else
  3973. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3974. preprocstack.accept:=not preprocstack.accept;
  3975. preprocstack.iftyp:=preprocstack.typ;
  3976. preprocstack.typ:=pp_else;
  3977. preprocstack.line_nb:=line_no;
  3978. preprocstack.fileindex:=current_filepos.fileindex;
  3979. if preprocstack.accept then
  3980. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3981. else
  3982. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3983. end
  3984. else
  3985. Message(scan_e_endif_without_if);
  3986. end;
  3987. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3988. var
  3989. valuedescr: String;
  3990. begin
  3991. if assigned(preprocstack) and
  3992. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  3993. begin
  3994. { when the branch is accepted we use pp_elseif so we know that
  3995. all the next branches need to be rejected. when this branch is still
  3996. not accepted then leave it at pp_if }
  3997. if (preprocstack.typ=pp_elseif) then
  3998. preprocstack.accept:=false
  3999. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4000. begin
  4001. preprocstack.accept:=false;
  4002. preprocstack.typ:=pp_elseif;
  4003. end
  4004. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4005. and compile_time_predicate(valuedescr) then
  4006. begin
  4007. preprocstack.name:=valuedescr;
  4008. preprocstack.accept:=true;
  4009. preprocstack.typ:=pp_elseif;
  4010. end;
  4011. preprocstack.line_nb:=line_no;
  4012. preprocstack.fileindex:=current_filepos.fileindex;
  4013. if preprocstack.accept then
  4014. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4015. else
  4016. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4017. end
  4018. else
  4019. Message(scan_e_endif_without_if);
  4020. end;
  4021. procedure tscannerfile.popreplaystack;
  4022. var
  4023. hp : treplaystack;
  4024. begin
  4025. if assigned(replaystack) then
  4026. begin
  4027. hp:=replaystack.next;
  4028. replaystack.free;
  4029. replaystack:=hp;
  4030. end;
  4031. end;
  4032. function tscannerfile.replay_stack_depth:longint;
  4033. var
  4034. tmp: treplaystack;
  4035. begin
  4036. result:=0;
  4037. tmp:=replaystack;
  4038. while assigned(tmp) do
  4039. begin
  4040. inc(result);
  4041. tmp:=tmp.next;
  4042. end;
  4043. end;
  4044. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4045. begin
  4046. savetokenpos;
  4047. repeat
  4048. current_scanner.gettokenpos;
  4049. Message1(scan_d_handling_switch,'$'+p.name);
  4050. p.proc();
  4051. { accept the text ? }
  4052. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4053. break
  4054. else
  4055. begin
  4056. current_scanner.gettokenpos;
  4057. Message(scan_c_skipping_until);
  4058. repeat
  4059. current_scanner.skipuntildirective;
  4060. if not (m_mac in current_settings.modeswitches) then
  4061. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4062. else
  4063. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4064. until assigned(p) and (p.is_conditional);
  4065. current_scanner.gettokenpos;
  4066. end;
  4067. until false;
  4068. restoretokenpos;
  4069. end;
  4070. procedure tscannerfile.handledirectives;
  4071. var
  4072. t : tdirectiveitem;
  4073. hs : string;
  4074. begin
  4075. gettokenpos;
  4076. readchar; {Remove the $}
  4077. hs:=readid;
  4078. { handle empty directive }
  4079. if hs='' then
  4080. begin
  4081. Message1(scan_w_illegal_switch,'$');
  4082. exit;
  4083. end;
  4084. {$ifdef PREPROCWRITE}
  4085. if parapreprocess then
  4086. begin
  4087. if not (m_mac in current_settings.modeswitches) then
  4088. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4089. else
  4090. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4091. if assigned(t) and not(t.is_conditional) then
  4092. begin
  4093. preprocfile.AddSpace;
  4094. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4095. exit;
  4096. end;
  4097. end;
  4098. {$endif PREPROCWRITE}
  4099. { skip this directive? }
  4100. if (ignoredirectives.find(hs)<>nil) then
  4101. begin
  4102. if (comment_level>0) then
  4103. readcomment;
  4104. { we've read the whole comment }
  4105. current_commentstyle:=comment_none;
  4106. exit;
  4107. end;
  4108. { Check for compiler switches }
  4109. while (length(hs)=1) and (c in ['-','+']) do
  4110. begin
  4111. Message1(scan_d_handling_switch,'$'+hs+c);
  4112. HandleSwitch(hs[1],c);
  4113. current_scanner.readchar; {Remove + or -}
  4114. if c=',' then
  4115. begin
  4116. current_scanner.readchar; {Remove , }
  4117. { read next switch, support $v+,$+}
  4118. hs:=current_scanner.readid;
  4119. if (hs='') then
  4120. begin
  4121. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4122. begin
  4123. current_scanner.readchar; { skip $ }
  4124. hs:=current_scanner.readid;
  4125. end;
  4126. if (hs='') then
  4127. Message1(scan_w_illegal_directive,'$'+c);
  4128. end;
  4129. end
  4130. else
  4131. hs:='';
  4132. end;
  4133. { directives may follow switches after a , }
  4134. if hs<>'' then
  4135. begin
  4136. if not (m_mac in current_settings.modeswitches) then
  4137. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4138. else
  4139. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4140. if assigned(t) then
  4141. begin
  4142. if t.is_conditional then
  4143. handleconditional(t)
  4144. else
  4145. begin
  4146. Message1(scan_d_handling_switch,'$'+hs);
  4147. t.proc();
  4148. end;
  4149. end
  4150. else
  4151. begin
  4152. current_scanner.ignoredirectives.Add(hs,nil);
  4153. Message1(scan_w_illegal_directive,'$'+hs);
  4154. end;
  4155. { conditionals already read the comment }
  4156. if (current_scanner.comment_level>0) then
  4157. current_scanner.readcomment;
  4158. { we've read the whole comment }
  4159. current_commentstyle:=comment_none;
  4160. end;
  4161. end;
  4162. procedure tscannerfile.readchar;
  4163. begin
  4164. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4165. c:=get_inputpointer_char;
  4166. {$else not CHECK_INPUTPOINTER_LIMITS}
  4167. c:=inputpointer^;
  4168. {$endif CHECK_INPUTPOINTER_LIMITS}
  4169. if c=#0 then
  4170. reload
  4171. else
  4172. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4173. inc_inputpointer;
  4174. {$else not CHECK_INPUTPOINTER_LIMITS}
  4175. inc(inputpointer);
  4176. {$endif CHECK_INPUTPOINTER_LIMITS}
  4177. end;
  4178. procedure tscannerfile.readstring;
  4179. var
  4180. i : longint;
  4181. err : boolean;
  4182. begin
  4183. err:=false;
  4184. i:=0;
  4185. repeat
  4186. case c of
  4187. '_',
  4188. '0'..'9',
  4189. 'A'..'Z',
  4190. 'a'..'z' :
  4191. begin
  4192. if i<255 then
  4193. begin
  4194. inc(i);
  4195. orgpattern[i]:=c;
  4196. if c in ['a'..'z'] then
  4197. pattern[i]:=chr(ord(c)-32)
  4198. else
  4199. pattern[i]:=c;
  4200. end
  4201. else
  4202. begin
  4203. if not err then
  4204. begin
  4205. Message(scan_e_string_exceeds_255_chars);
  4206. err:=true;
  4207. end;
  4208. end;
  4209. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4210. c:=get_inputpointer_char;
  4211. inc_inputpointer;
  4212. {$else not CHECK_INPUTPOINTER_LIMITS}
  4213. c:=inputpointer^;
  4214. inc(inputpointer);
  4215. {$endif CHECK_INPUTPOINTER_LIMITS}
  4216. end;
  4217. #0 :
  4218. reload;
  4219. else if inputfile.internally_generated_macro and
  4220. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4221. begin
  4222. if i<255 then
  4223. begin
  4224. inc(i);
  4225. orgpattern[i]:=c;
  4226. pattern[i]:=c;
  4227. end
  4228. else
  4229. begin
  4230. if not err then
  4231. begin
  4232. Message(scan_e_string_exceeds_255_chars);
  4233. err:=true;
  4234. end;
  4235. end;
  4236. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4237. c:=get_inputpointer_char;
  4238. inc_inputpointer;
  4239. {$else not CHECK_INPUTPOINTER_LIMITS}
  4240. c:=inputpointer^;
  4241. inc(inputpointer);
  4242. {$endif CHECK_INPUTPOINTER_LIMITS}
  4243. end
  4244. else
  4245. break;
  4246. end;
  4247. until false;
  4248. orgpattern[0]:=chr(i);
  4249. pattern[0]:=chr(i);
  4250. end;
  4251. procedure tscannerfile.readnumber;
  4252. var
  4253. base,
  4254. i : longint;
  4255. firstdigitread: Boolean;
  4256. begin
  4257. case c of
  4258. '%' :
  4259. begin
  4260. readchar;
  4261. base:=2;
  4262. pattern[1]:='%';
  4263. i:=1;
  4264. end;
  4265. '&' :
  4266. begin
  4267. readchar;
  4268. base:=8;
  4269. pattern[1]:='&';
  4270. i:=1;
  4271. end;
  4272. '$' :
  4273. begin
  4274. readchar;
  4275. base:=16;
  4276. pattern[1]:='$';
  4277. i:=1;
  4278. end;
  4279. else
  4280. begin
  4281. base:=10;
  4282. i:=0;
  4283. end;
  4284. end;
  4285. firstdigitread:=false;
  4286. while ((base>=10) and (c in ['0'..'9'])) or
  4287. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4288. ((base=8) and (c in ['0'..'7'])) or
  4289. ((base=2) and (c in ['0'..'1'])) or
  4290. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4291. begin
  4292. if (i<255) and (c<>'_') then
  4293. begin
  4294. inc(i);
  4295. pattern[i]:=c;
  4296. end;
  4297. readchar;
  4298. firstdigitread:=true;
  4299. end;
  4300. pattern[0]:=chr(i);
  4301. end;
  4302. function tscannerfile.readid:string;
  4303. begin
  4304. readstring;
  4305. readid:=pattern;
  4306. end;
  4307. function tscannerfile.readval:longint;
  4308. var
  4309. l : longint;
  4310. w : integer;
  4311. begin
  4312. readnumber;
  4313. val(pattern,l,w);
  4314. readval:=l;
  4315. end;
  4316. function tscannerfile.readval64:int64;
  4317. var
  4318. l : int64;
  4319. w : integer;
  4320. begin
  4321. readnumber;
  4322. val(pattern,l,w);
  4323. readval64:=l;
  4324. end;
  4325. function tscannerfile.readcomment(include_special_char: boolean):string;
  4326. var
  4327. i : longint;
  4328. begin
  4329. i:=0;
  4330. repeat
  4331. case c of
  4332. '{' :
  4333. begin
  4334. if (include_special_char) and (i<255) then
  4335. begin
  4336. inc(i);
  4337. readcomment[i]:=c;
  4338. end;
  4339. if current_commentstyle=comment_tp then
  4340. inc_comment_level;
  4341. end;
  4342. '}' :
  4343. begin
  4344. if (include_special_char) and (i<255) then
  4345. begin
  4346. inc(i);
  4347. readcomment[i]:=c;
  4348. end;
  4349. if current_commentstyle=comment_tp then
  4350. begin
  4351. readchar;
  4352. dec_comment_level;
  4353. if comment_level=0 then
  4354. break
  4355. else
  4356. continue;
  4357. end;
  4358. end;
  4359. '*' :
  4360. begin
  4361. if current_commentstyle=comment_oldtp then
  4362. begin
  4363. readchar;
  4364. if c=')' then
  4365. begin
  4366. readchar;
  4367. dec_comment_level;
  4368. break;
  4369. end
  4370. else
  4371. { Add both characters !!}
  4372. if (i<255) then
  4373. begin
  4374. inc(i);
  4375. readcomment[i]:='*';
  4376. if (i<255) then
  4377. begin
  4378. inc(i);
  4379. readcomment[i]:=c;
  4380. end;
  4381. end;
  4382. end
  4383. else
  4384. { Not old TP comment, so add...}
  4385. begin
  4386. if (i<255) then
  4387. begin
  4388. inc(i);
  4389. readcomment[i]:='*';
  4390. end;
  4391. end;
  4392. end;
  4393. #10,#13 :
  4394. linebreak;
  4395. #26 :
  4396. end_of_file;
  4397. else
  4398. begin
  4399. if (i<255) then
  4400. begin
  4401. inc(i);
  4402. readcomment[i]:=c;
  4403. end;
  4404. end;
  4405. end;
  4406. readchar;
  4407. until false;
  4408. readcomment[0]:=chr(i);
  4409. end;
  4410. function tscannerfile.readquotedstring:string;
  4411. var
  4412. i : longint;
  4413. msgwritten : boolean;
  4414. begin
  4415. i:=0;
  4416. msgwritten:=false;
  4417. if (c='''') then
  4418. begin
  4419. repeat
  4420. readchar;
  4421. case c of
  4422. #26 :
  4423. end_of_file;
  4424. #10,#13 :
  4425. Message(scan_f_string_exceeds_line);
  4426. '''' :
  4427. begin
  4428. readchar;
  4429. if c<>'''' then
  4430. break;
  4431. end;
  4432. end;
  4433. if i<255 then
  4434. begin
  4435. inc(i);
  4436. result[i]:=c;
  4437. end
  4438. else
  4439. begin
  4440. if not msgwritten then
  4441. begin
  4442. Message(scan_e_string_exceeds_255_chars);
  4443. msgwritten:=true;
  4444. end;
  4445. end;
  4446. until false;
  4447. end;
  4448. result[0]:=chr(i);
  4449. end;
  4450. function tscannerfile.readstate:char;
  4451. var
  4452. state : char;
  4453. begin
  4454. state:=' ';
  4455. if c=' ' then
  4456. begin
  4457. current_scanner.skipspace;
  4458. current_scanner.readid;
  4459. if pattern='ON' then
  4460. state:='+'
  4461. else
  4462. if pattern='OFF' then
  4463. state:='-';
  4464. end
  4465. else
  4466. state:=c;
  4467. if not (state in ['+','-']) then
  4468. Message(scan_e_wrong_switch_toggle);
  4469. readstate:=state;
  4470. end;
  4471. function tscannerfile.readoptionalstate(fallback:char):char;
  4472. var
  4473. state : char;
  4474. begin
  4475. state:=' ';
  4476. if c=' ' then
  4477. begin
  4478. current_scanner.skipspace;
  4479. if c in ['*','}'] then
  4480. state:=fallback
  4481. else
  4482. begin
  4483. current_scanner.readid;
  4484. if pattern='ON' then
  4485. state:='+'
  4486. else
  4487. if pattern='OFF' then
  4488. state:='-';
  4489. end;
  4490. end
  4491. else
  4492. if c in ['*','}'] then
  4493. state:=fallback
  4494. else
  4495. state:=c;
  4496. if not (state in ['+','-']) then
  4497. Message(scan_e_wrong_switch_toggle);
  4498. readoptionalstate:=state;
  4499. end;
  4500. function tscannerfile.readstatedefault:char;
  4501. var
  4502. state : char;
  4503. begin
  4504. state:=' ';
  4505. if c=' ' then
  4506. begin
  4507. current_scanner.skipspace;
  4508. current_scanner.readid;
  4509. if pattern='ON' then
  4510. state:='+'
  4511. else
  4512. if pattern='OFF' then
  4513. state:='-'
  4514. else
  4515. if pattern='DEFAULT' then
  4516. state:='*';
  4517. end
  4518. else
  4519. state:=c;
  4520. if not (state in ['+','-','*']) then
  4521. Message(scan_e_wrong_switch_toggle_default);
  4522. readstatedefault:=state;
  4523. end;
  4524. procedure tscannerfile.skipspace;
  4525. begin
  4526. repeat
  4527. case c of
  4528. #26 :
  4529. begin
  4530. reload;
  4531. if (c=#26) and not assigned(inputfile.next) then
  4532. break;
  4533. continue;
  4534. end;
  4535. #10,
  4536. #13 :
  4537. linebreak;
  4538. #9,#11,#12,' ' :
  4539. ;
  4540. else
  4541. break;
  4542. end;
  4543. readchar;
  4544. until false;
  4545. end;
  4546. procedure tscannerfile.skipuntildirective;
  4547. var
  4548. found : longint;
  4549. next_char_loaded : boolean;
  4550. begin
  4551. found:=0;
  4552. next_char_loaded:=false;
  4553. repeat
  4554. case c of
  4555. #10,
  4556. #13 :
  4557. linebreak;
  4558. #26 :
  4559. begin
  4560. reload;
  4561. if (c=#26) and not assigned(inputfile.next) then
  4562. end_of_file;
  4563. continue;
  4564. end;
  4565. '{' :
  4566. begin
  4567. if (current_commentstyle in [comment_tp,comment_none]) then
  4568. begin
  4569. current_commentstyle:=comment_tp;
  4570. if (comment_level=0) then
  4571. found:=1;
  4572. inc_comment_level;
  4573. end;
  4574. end;
  4575. '*' :
  4576. begin
  4577. if (current_commentstyle=comment_oldtp) then
  4578. begin
  4579. readchar;
  4580. if c=')' then
  4581. begin
  4582. dec_comment_level;
  4583. found:=0;
  4584. current_commentstyle:=comment_none;
  4585. end
  4586. else
  4587. next_char_loaded:=true;
  4588. end
  4589. else
  4590. found := 0;
  4591. end;
  4592. '}' :
  4593. begin
  4594. if (current_commentstyle=comment_tp) then
  4595. begin
  4596. dec_comment_level;
  4597. if (comment_level=0) then
  4598. current_commentstyle:=comment_none;
  4599. found:=0;
  4600. end;
  4601. end;
  4602. '$' :
  4603. begin
  4604. if found=1 then
  4605. found:=2;
  4606. end;
  4607. '''' :
  4608. if (current_commentstyle=comment_none) then
  4609. begin
  4610. repeat
  4611. readchar;
  4612. case c of
  4613. #26 :
  4614. end_of_file;
  4615. #10,#13 :
  4616. break;
  4617. '''' :
  4618. begin
  4619. readchar;
  4620. if c<>'''' then
  4621. begin
  4622. next_char_loaded:=true;
  4623. break;
  4624. end;
  4625. end;
  4626. end;
  4627. until false;
  4628. end;
  4629. '(' :
  4630. begin
  4631. if (current_commentstyle=comment_none) then
  4632. begin
  4633. readchar;
  4634. if c='*' then
  4635. begin
  4636. readchar;
  4637. if c='$' then
  4638. begin
  4639. found:=2;
  4640. inc_comment_level;
  4641. current_commentstyle:=comment_oldtp;
  4642. end
  4643. else
  4644. begin
  4645. skipoldtpcomment(false);
  4646. next_char_loaded:=true;
  4647. end;
  4648. end
  4649. else
  4650. next_char_loaded:=true;
  4651. end
  4652. else
  4653. found:=0;
  4654. end;
  4655. '/' :
  4656. begin
  4657. if (current_commentstyle=comment_none) then
  4658. begin
  4659. readchar;
  4660. if c='/' then
  4661. skipdelphicomment;
  4662. next_char_loaded:=true;
  4663. end
  4664. else
  4665. found:=0;
  4666. end;
  4667. else
  4668. found:=0;
  4669. end;
  4670. if next_char_loaded then
  4671. next_char_loaded:=false
  4672. else
  4673. readchar;
  4674. until (found=2);
  4675. end;
  4676. {****************************************************************************
  4677. Comment Handling
  4678. ****************************************************************************}
  4679. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4680. begin
  4681. current_commentstyle:=comment_tp;
  4682. if read_first_char then
  4683. readchar;
  4684. inc_comment_level;
  4685. { handle compiler switches }
  4686. if (c='$') then
  4687. handledirectives;
  4688. { handle_switches can dec comment_level, }
  4689. while (comment_level>0) do
  4690. begin
  4691. case c of
  4692. '{' :
  4693. inc_comment_level;
  4694. '}' :
  4695. dec_comment_level;
  4696. '*' :
  4697. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4698. if m_iso in current_settings.modeswitches then
  4699. begin
  4700. readchar;
  4701. if c=')' then
  4702. dec_comment_level
  4703. else
  4704. continue;
  4705. end;
  4706. #10,#13 :
  4707. linebreak;
  4708. #26 :
  4709. begin
  4710. reload;
  4711. if (c=#26) and not assigned(inputfile.next) then
  4712. end_of_file;
  4713. continue;
  4714. end;
  4715. end;
  4716. readchar;
  4717. end;
  4718. current_commentstyle:=comment_none;
  4719. end;
  4720. procedure tscannerfile.skipdelphicomment;
  4721. begin
  4722. current_commentstyle:=comment_delphi;
  4723. inc_comment_level;
  4724. readchar;
  4725. { this is not supported }
  4726. if c='$' then
  4727. Message(scan_w_wrong_styled_switch);
  4728. { skip comment }
  4729. while not (c in [#10,#13,#26]) do
  4730. readchar;
  4731. dec_comment_level;
  4732. current_commentstyle:=comment_none;
  4733. end;
  4734. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4735. var
  4736. found : longint;
  4737. begin
  4738. current_commentstyle:=comment_oldtp;
  4739. inc_comment_level;
  4740. { only load a char if last already processed,
  4741. was cause of bug1634 PM }
  4742. if read_first_char then
  4743. readchar;
  4744. { this is now supported }
  4745. if (c='$') then
  4746. handledirectives;
  4747. { skip comment }
  4748. while (comment_level>0) do
  4749. begin
  4750. found:=0;
  4751. repeat
  4752. case c of
  4753. #26 :
  4754. begin
  4755. reload;
  4756. if (c=#26) and not assigned(inputfile.next) then
  4757. end_of_file;
  4758. continue;
  4759. end;
  4760. #10,#13 :
  4761. begin
  4762. if found=4 then
  4763. inc_comment_level;
  4764. linebreak;
  4765. found:=0;
  4766. end;
  4767. '*' :
  4768. begin
  4769. if found=3 then
  4770. found:=4
  4771. else
  4772. begin
  4773. if found=4 then
  4774. inc_comment_level;
  4775. found:=1;
  4776. end;
  4777. end;
  4778. ')' :
  4779. begin
  4780. if found in [1,4] then
  4781. begin
  4782. dec_comment_level;
  4783. if comment_level=0 then
  4784. found:=2
  4785. else
  4786. found:=0;
  4787. end
  4788. else
  4789. found:=0;
  4790. end;
  4791. '}' :
  4792. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4793. if m_iso in current_settings.modeswitches then
  4794. begin
  4795. dec_comment_level;
  4796. if comment_level=0 then
  4797. found:=2
  4798. else
  4799. found:=0;
  4800. end;
  4801. '(' :
  4802. begin
  4803. if found=4 then
  4804. inc_comment_level;
  4805. found:=3;
  4806. end;
  4807. else
  4808. begin
  4809. if found=4 then
  4810. inc_comment_level;
  4811. found:=0;
  4812. end;
  4813. end;
  4814. readchar;
  4815. until (found=2);
  4816. end;
  4817. current_commentstyle:=comment_none;
  4818. end;
  4819. {****************************************************************************
  4820. Token Scanner
  4821. ****************************************************************************}
  4822. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4823. var
  4824. code : integer;
  4825. d : cardinal;
  4826. len,
  4827. low,high,mid : longint;
  4828. w : word;
  4829. m : longint;
  4830. mac : tmacro;
  4831. asciinr : string[33];
  4832. iswidestring , firstdigitread: boolean;
  4833. label
  4834. exit_label;
  4835. begin
  4836. flushpendingswitchesstate;
  4837. { record tokens? }
  4838. if allowrecordtoken and
  4839. assigned(recordtokenbuf) then
  4840. recordtoken;
  4841. { replay tokens? }
  4842. if assigned(replaytokenbuf) then
  4843. begin
  4844. replaytoken;
  4845. goto exit_label;
  4846. end;
  4847. { was there already a token read, then return that token }
  4848. if nexttoken<>NOTOKEN then
  4849. begin
  4850. setnexttoken;
  4851. goto exit_label;
  4852. end;
  4853. { Skip all spaces and comments }
  4854. repeat
  4855. case c of
  4856. '{' :
  4857. skipcomment(true);
  4858. #26 :
  4859. begin
  4860. reload;
  4861. if (c=#26) and not assigned(inputfile.next) then
  4862. break;
  4863. end;
  4864. ' ',#9..#13 :
  4865. begin
  4866. {$ifdef PREPROCWRITE}
  4867. if parapreprocess then
  4868. begin
  4869. if c=#10 then
  4870. preprocfile.eolfound:=true
  4871. else
  4872. preprocfile.spacefound:=true;
  4873. end;
  4874. {$endif PREPROCWRITE}
  4875. skipspace;
  4876. end
  4877. else
  4878. break;
  4879. end;
  4880. until false;
  4881. { Save current token position, for EOF its already loaded }
  4882. if c<>#26 then
  4883. gettokenpos;
  4884. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4885. if c in ['A'..'Z','a'..'z','_'] then
  4886. begin
  4887. readstring;
  4888. token:=_ID;
  4889. idtoken:=_ID;
  4890. { keyword or any other known token,
  4891. pattern is always uppercased }
  4892. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4893. begin
  4894. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4895. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4896. while low<high do
  4897. begin
  4898. mid:=(high+low+1) shr 1;
  4899. if pattern<tokeninfo^[ttoken(mid)].str then
  4900. high:=mid-1
  4901. else
  4902. low:=mid;
  4903. end;
  4904. with tokeninfo^[ttoken(high)] do
  4905. if pattern=str then
  4906. begin
  4907. if (keyword*current_settings.modeswitches)<>[] then
  4908. if op=NOTOKEN then
  4909. token:=ttoken(high)
  4910. else
  4911. token:=op;
  4912. idtoken:=ttoken(high);
  4913. end;
  4914. end;
  4915. { Only process identifiers and not keywords }
  4916. if token=_ID then
  4917. begin
  4918. { this takes some time ... }
  4919. if (cs_support_macro in current_settings.moduleswitches) then
  4920. begin
  4921. mac:=tmacro(search_macro(pattern));
  4922. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4923. begin
  4924. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4925. begin
  4926. mac.is_used:=true;
  4927. inc(yylexcount);
  4928. substitutemacro(pattern,mac.buftext,mac.buflen,
  4929. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4930. { handle empty macros }
  4931. if c=#0 then
  4932. begin
  4933. reload;
  4934. { avoid macro nesting error in case of
  4935. a sequence of empty macros, see #38802 }
  4936. dec(yylexcount);
  4937. readtoken(false);
  4938. end
  4939. else
  4940. begin
  4941. readtoken(false);
  4942. { that's all folks }
  4943. dec(yylexcount);
  4944. end;
  4945. exit;
  4946. end
  4947. else
  4948. Message(scan_w_macro_too_deep);
  4949. end;
  4950. end;
  4951. end;
  4952. { return token }
  4953. goto exit_label;
  4954. end
  4955. else
  4956. begin
  4957. idtoken:=_NOID;
  4958. case c of
  4959. '$' :
  4960. begin
  4961. readnumber;
  4962. token:=_INTCONST;
  4963. goto exit_label;
  4964. end;
  4965. '%' :
  4966. begin
  4967. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4968. Illegal_Char(c)
  4969. else
  4970. begin
  4971. readnumber;
  4972. token:=_INTCONST;
  4973. goto exit_label;
  4974. end;
  4975. end;
  4976. '&' :
  4977. begin
  4978. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4979. begin
  4980. readnumber;
  4981. if length(pattern)=1 then
  4982. begin
  4983. { does really an identifier follow? }
  4984. if not (c in ['_','A'..'Z','a'..'z']) then
  4985. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4986. readstring;
  4987. token:=_ID;
  4988. idtoken:=_ID;
  4989. end
  4990. else
  4991. token:=_INTCONST;
  4992. goto exit_label;
  4993. end
  4994. else if m_mac in current_settings.modeswitches then
  4995. begin
  4996. readchar;
  4997. token:=_AMPERSAND;
  4998. goto exit_label;
  4999. end
  5000. else
  5001. Illegal_Char(c);
  5002. end;
  5003. '0'..'9' :
  5004. begin
  5005. readnumber;
  5006. if (c in ['.','e','E']) then
  5007. begin
  5008. { first check for a . }
  5009. if c='.' then
  5010. begin
  5011. cachenexttokenpos;
  5012. readchar;
  5013. { is it a .. from a range? }
  5014. case c of
  5015. '.' :
  5016. begin
  5017. readchar;
  5018. token:=_INTCONST;
  5019. nexttoken:=_POINTPOINT;
  5020. goto exit_label;
  5021. end;
  5022. ')' :
  5023. begin
  5024. readchar;
  5025. token:=_INTCONST;
  5026. nexttoken:=_RECKKLAMMER;
  5027. goto exit_label;
  5028. end;
  5029. '0'..'9' :
  5030. begin
  5031. { insert the number after the . }
  5032. pattern:=pattern+'.';
  5033. firstdigitread:=false;
  5034. while (c in ['0'..'9']) or
  5035. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5036. begin
  5037. if c<>'_' then
  5038. pattern:=pattern+c;
  5039. readchar;
  5040. firstdigitread:=true;
  5041. end;
  5042. end;
  5043. else
  5044. begin
  5045. token:=_INTCONST;
  5046. nexttoken:=_POINT;
  5047. goto exit_label;
  5048. end;
  5049. end;
  5050. end;
  5051. { E can also follow after a point is scanned }
  5052. if c in ['e','E'] then
  5053. begin
  5054. pattern:=pattern+'E';
  5055. readchar;
  5056. if c in ['-','+'] then
  5057. begin
  5058. pattern:=pattern+c;
  5059. readchar;
  5060. end;
  5061. if not(c in ['0'..'9']) then
  5062. Illegal_Char(c);
  5063. firstdigitread:=false;
  5064. while (c in ['0'..'9']) or
  5065. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5066. begin
  5067. if c<>'_' then
  5068. pattern:=pattern+c;
  5069. readchar;
  5070. firstdigitread:=true;
  5071. end;
  5072. end;
  5073. token:=_REALNUMBER;
  5074. goto exit_label;
  5075. end;
  5076. token:=_INTCONST;
  5077. goto exit_label;
  5078. end;
  5079. ';' :
  5080. begin
  5081. readchar;
  5082. token:=_SEMICOLON;
  5083. goto exit_label;
  5084. end;
  5085. '[' :
  5086. begin
  5087. readchar;
  5088. token:=_LECKKLAMMER;
  5089. goto exit_label;
  5090. end;
  5091. ']' :
  5092. begin
  5093. readchar;
  5094. token:=_RECKKLAMMER;
  5095. goto exit_label;
  5096. end;
  5097. '(' :
  5098. begin
  5099. readchar;
  5100. case c of
  5101. '*' :
  5102. begin
  5103. skipoldtpcomment(true);
  5104. readtoken(false);
  5105. exit;
  5106. end;
  5107. '.' :
  5108. begin
  5109. readchar;
  5110. token:=_LECKKLAMMER;
  5111. goto exit_label;
  5112. end;
  5113. end;
  5114. token:=_LKLAMMER;
  5115. goto exit_label;
  5116. end;
  5117. ')' :
  5118. begin
  5119. readchar;
  5120. token:=_RKLAMMER;
  5121. goto exit_label;
  5122. end;
  5123. '+' :
  5124. begin
  5125. readchar;
  5126. if c='=' then
  5127. begin
  5128. readchar;
  5129. token:=_PLUSASN;
  5130. goto exit_label;
  5131. end;
  5132. token:=_PLUS;
  5133. goto exit_label;
  5134. end;
  5135. '-' :
  5136. begin
  5137. readchar;
  5138. if c='=' then
  5139. begin
  5140. readchar;
  5141. token:=_MINUSASN;
  5142. goto exit_label;
  5143. end;
  5144. token:=_MINUS;
  5145. goto exit_label;
  5146. end;
  5147. ':' :
  5148. begin
  5149. readchar;
  5150. if c='=' then
  5151. begin
  5152. readchar;
  5153. token:=_ASSIGNMENT;
  5154. goto exit_label;
  5155. end;
  5156. token:=_COLON;
  5157. goto exit_label;
  5158. end;
  5159. '*' :
  5160. begin
  5161. readchar;
  5162. if c='=' then
  5163. begin
  5164. readchar;
  5165. token:=_STARASN;
  5166. end
  5167. else
  5168. if c='*' then
  5169. begin
  5170. readchar;
  5171. token:=_STARSTAR;
  5172. end
  5173. else
  5174. token:=_STAR;
  5175. goto exit_label;
  5176. end;
  5177. '/' :
  5178. begin
  5179. readchar;
  5180. case c of
  5181. '=' :
  5182. begin
  5183. readchar;
  5184. token:=_SLASHASN;
  5185. goto exit_label;
  5186. end;
  5187. '/' :
  5188. begin
  5189. skipdelphicomment;
  5190. readtoken(false);
  5191. exit;
  5192. end;
  5193. end;
  5194. token:=_SLASH;
  5195. goto exit_label;
  5196. end;
  5197. '|' :
  5198. if m_mac in current_settings.modeswitches then
  5199. begin
  5200. readchar;
  5201. token:=_PIPE;
  5202. goto exit_label;
  5203. end
  5204. else
  5205. Illegal_Char(c);
  5206. '=' :
  5207. begin
  5208. readchar;
  5209. token:=_EQ;
  5210. goto exit_label;
  5211. end;
  5212. '.' :
  5213. begin
  5214. readchar;
  5215. case c of
  5216. '.' :
  5217. begin
  5218. readchar;
  5219. case c of
  5220. '.' :
  5221. begin
  5222. readchar;
  5223. token:=_POINTPOINTPOINT;
  5224. goto exit_label;
  5225. end;
  5226. else
  5227. begin
  5228. token:=_POINTPOINT;
  5229. goto exit_label;
  5230. end;
  5231. end;
  5232. end;
  5233. ')' :
  5234. begin
  5235. readchar;
  5236. token:=_RECKKLAMMER;
  5237. goto exit_label;
  5238. end;
  5239. end;
  5240. token:=_POINT;
  5241. goto exit_label;
  5242. end;
  5243. '@' :
  5244. begin
  5245. readchar;
  5246. token:=_KLAMMERAFFE;
  5247. goto exit_label;
  5248. end;
  5249. ',' :
  5250. begin
  5251. readchar;
  5252. token:=_COMMA;
  5253. goto exit_label;
  5254. end;
  5255. '''','#','^' :
  5256. begin
  5257. len:=0;
  5258. cstringpattern:='';
  5259. iswidestring:=false;
  5260. if c='^' then
  5261. begin
  5262. readchar;
  5263. c:=upcase(c);
  5264. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5265. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5266. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5267. begin
  5268. token:=_CARET;
  5269. goto exit_label;
  5270. end
  5271. else
  5272. begin
  5273. inc(len);
  5274. setlength(cstringpattern,256);
  5275. if c<#64 then
  5276. cstringpattern[len]:=chr(ord(c)+64)
  5277. else
  5278. cstringpattern[len]:=chr(ord(c)-64);
  5279. readchar;
  5280. end;
  5281. end;
  5282. repeat
  5283. case c of
  5284. '#' :
  5285. begin
  5286. readchar; { read # }
  5287. case c of
  5288. '$':
  5289. begin
  5290. readchar; { read leading $ }
  5291. asciinr:='$';
  5292. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5293. begin
  5294. asciinr:=asciinr+c;
  5295. readchar;
  5296. end;
  5297. end;
  5298. '&':
  5299. begin
  5300. readchar; { read leading $ }
  5301. asciinr:='&';
  5302. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5303. begin
  5304. asciinr:=asciinr+c;
  5305. readchar;
  5306. end;
  5307. end;
  5308. '%':
  5309. begin
  5310. readchar; { read leading $ }
  5311. asciinr:='%';
  5312. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5313. begin
  5314. asciinr:=asciinr+c;
  5315. readchar;
  5316. end;
  5317. end;
  5318. else
  5319. begin
  5320. asciinr:='';
  5321. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5322. begin
  5323. asciinr:=asciinr+c;
  5324. readchar;
  5325. end;
  5326. end;
  5327. end;
  5328. val(asciinr,m,code);
  5329. if (asciinr='') or (code<>0) then
  5330. Message(scan_e_illegal_char_const)
  5331. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5332. begin
  5333. if (m>=0) and (m<=$10FFFF) then
  5334. begin
  5335. if not iswidestring then
  5336. begin
  5337. if len>0 then
  5338. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5339. else
  5340. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5341. iswidestring:=true;
  5342. len:=0;
  5343. end;
  5344. if m<=$FFFF then
  5345. concatwidestringchar(patternw,tcompilerwidechar(m))
  5346. else
  5347. begin
  5348. { split into surrogate pair }
  5349. dec(m,$10000);
  5350. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5351. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5352. end;
  5353. end
  5354. else
  5355. Message(scan_e_illegal_char_const)
  5356. end
  5357. else if iswidestring then
  5358. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5359. else
  5360. begin
  5361. if len>=length(cstringpattern) then
  5362. setlength(cstringpattern,length(cstringpattern)+256);
  5363. inc(len);
  5364. cstringpattern[len]:=chr(m);
  5365. end;
  5366. end;
  5367. '''' :
  5368. begin
  5369. repeat
  5370. readchar;
  5371. case c of
  5372. #26 :
  5373. end_of_file;
  5374. #10,#13 :
  5375. Message(scan_f_string_exceeds_line);
  5376. '''' :
  5377. begin
  5378. readchar;
  5379. if c<>'''' then
  5380. break;
  5381. end;
  5382. end;
  5383. { interpret as utf-8 string? }
  5384. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5385. begin
  5386. { convert existing string to an utf-8 string }
  5387. if not iswidestring then
  5388. begin
  5389. if len>0 then
  5390. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5391. else
  5392. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5393. iswidestring:=true;
  5394. len:=0;
  5395. end;
  5396. { four chars }
  5397. if (ord(c) and $f0)=$f0 then
  5398. begin
  5399. { this always represents a surrogate pair, so
  5400. read as 32-bit value and then split into
  5401. the corresponding pair of two wchars }
  5402. d:=ord(c) and $f;
  5403. readchar;
  5404. if (ord(c) and $c0)<>$80 then
  5405. message(scan_e_utf8_malformed);
  5406. d:=(d shl 6) or (ord(c) and $3f);
  5407. readchar;
  5408. if (ord(c) and $c0)<>$80 then
  5409. message(scan_e_utf8_malformed);
  5410. d:=(d shl 6) or (ord(c) and $3f);
  5411. readchar;
  5412. if (ord(c) and $c0)<>$80 then
  5413. message(scan_e_utf8_malformed);
  5414. d:=(d shl 6) or (ord(c) and $3f);
  5415. if d<$10000 then
  5416. message(scan_e_utf8_malformed);
  5417. d:=d-$10000;
  5418. { high surrogate }
  5419. w:=$d800+(d shr 10);
  5420. concatwidestringchar(patternw,w);
  5421. { low surrogate }
  5422. w:=$dc00+(d and $3ff);
  5423. concatwidestringchar(patternw,w);
  5424. end
  5425. { three chars }
  5426. else if (ord(c) and $e0)=$e0 then
  5427. begin
  5428. w:=ord(c) and $f;
  5429. readchar;
  5430. if (ord(c) and $c0)<>$80 then
  5431. message(scan_e_utf8_malformed);
  5432. w:=(w shl 6) or (ord(c) and $3f);
  5433. readchar;
  5434. if (ord(c) and $c0)<>$80 then
  5435. message(scan_e_utf8_malformed);
  5436. w:=(w shl 6) or (ord(c) and $3f);
  5437. concatwidestringchar(patternw,w);
  5438. end
  5439. { two chars }
  5440. else if (ord(c) and $c0)<>0 then
  5441. begin
  5442. w:=ord(c) and $1f;
  5443. readchar;
  5444. if (ord(c) and $c0)<>$80 then
  5445. message(scan_e_utf8_malformed);
  5446. w:=(w shl 6) or (ord(c) and $3f);
  5447. concatwidestringchar(patternw,w);
  5448. end
  5449. { illegal }
  5450. else if (ord(c) and $80)<>0 then
  5451. message(scan_e_utf8_malformed)
  5452. else
  5453. concatwidestringchar(patternw,tcompilerwidechar(c))
  5454. end
  5455. else if iswidestring then
  5456. begin
  5457. if current_settings.sourcecodepage=CP_UTF8 then
  5458. concatwidestringchar(patternw,ord(c))
  5459. else
  5460. concatwidestringchar(patternw,asciichar2unicode(c))
  5461. end
  5462. else
  5463. begin
  5464. if len>=length(cstringpattern) then
  5465. setlength(cstringpattern,length(cstringpattern)+256);
  5466. inc(len);
  5467. cstringpattern[len]:=c;
  5468. end;
  5469. until false;
  5470. end;
  5471. '^' :
  5472. begin
  5473. readchar;
  5474. c:=upcase(c);
  5475. if c<#64 then
  5476. c:=chr(ord(c)+64)
  5477. else
  5478. c:=chr(ord(c)-64);
  5479. if iswidestring then
  5480. concatwidestringchar(patternw,asciichar2unicode(c))
  5481. else
  5482. begin
  5483. if len>=length(cstringpattern) then
  5484. setlength(cstringpattern,length(cstringpattern)+256);
  5485. inc(len);
  5486. cstringpattern[len]:=c;
  5487. end;
  5488. readchar;
  5489. end;
  5490. else
  5491. break;
  5492. end;
  5493. until false;
  5494. { strings with length 1 become const chars }
  5495. if iswidestring then
  5496. begin
  5497. if patternw^.len=1 then
  5498. token:=_CWCHAR
  5499. else
  5500. token:=_CWSTRING;
  5501. end
  5502. else
  5503. begin
  5504. setlength(cstringpattern,len);
  5505. if length(cstringpattern)=1 then
  5506. begin
  5507. token:=_CCHAR;
  5508. pattern:=cstringpattern;
  5509. end
  5510. else
  5511. token:=_CSTRING;
  5512. end;
  5513. goto exit_label;
  5514. end;
  5515. '>' :
  5516. begin
  5517. readchar;
  5518. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5519. token:=_RSHARPBRACKET
  5520. else
  5521. begin
  5522. case c of
  5523. '=' :
  5524. begin
  5525. readchar;
  5526. token:=_GTE;
  5527. goto exit_label;
  5528. end;
  5529. '>' :
  5530. begin
  5531. readchar;
  5532. token:=_OP_SHR;
  5533. goto exit_label;
  5534. end;
  5535. '<' :
  5536. begin { >< is for a symetric diff for sets }
  5537. readchar;
  5538. token:=_SYMDIF;
  5539. goto exit_label;
  5540. end;
  5541. end;
  5542. token:=_GT;
  5543. end;
  5544. goto exit_label;
  5545. end;
  5546. '<' :
  5547. begin
  5548. readchar;
  5549. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5550. token:=_LSHARPBRACKET
  5551. else
  5552. begin
  5553. case c of
  5554. '>' :
  5555. begin
  5556. readchar;
  5557. token:=_NE;
  5558. goto exit_label;
  5559. end;
  5560. '=' :
  5561. begin
  5562. readchar;
  5563. token:=_LTE;
  5564. goto exit_label;
  5565. end;
  5566. '<' :
  5567. begin
  5568. readchar;
  5569. token:=_OP_SHL;
  5570. goto exit_label;
  5571. end;
  5572. end;
  5573. token:=_LT;
  5574. end;
  5575. goto exit_label;
  5576. end;
  5577. #26 :
  5578. begin
  5579. token:=_EOF;
  5580. checkpreprocstack;
  5581. goto exit_label;
  5582. end;
  5583. else if inputfile.internally_generated_macro and
  5584. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5585. begin
  5586. token:=_ID;
  5587. readstring;
  5588. end
  5589. else
  5590. Illegal_Char(c);
  5591. end;
  5592. end;
  5593. exit_label:
  5594. lasttoken:=token;
  5595. end;
  5596. function tscannerfile.readpreproc:ttoken;
  5597. var
  5598. low,high,mid: longint;
  5599. optoken: ttoken;
  5600. begin
  5601. skipspace;
  5602. case c of
  5603. '_',
  5604. 'A'..'Z',
  5605. 'a'..'z' :
  5606. begin
  5607. readstring;
  5608. optoken:=_ID;
  5609. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5610. begin
  5611. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5612. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5613. while low<high do
  5614. begin
  5615. mid:=(high+low+1) shr 1;
  5616. if pattern<tokeninfo^[ttoken(mid)].str then
  5617. high:=mid-1
  5618. else
  5619. low:=mid;
  5620. end;
  5621. with tokeninfo^[ttoken(high)] do
  5622. if pattern=str then
  5623. begin
  5624. if (keyword*current_settings.modeswitches)<>[] then
  5625. if op=NOTOKEN then
  5626. optoken:=ttoken(high)
  5627. else
  5628. optoken:=op;
  5629. end;
  5630. if not (optoken in preproc_operators) then
  5631. optoken:=_ID;
  5632. end;
  5633. current_scanner.preproc_pattern:=pattern;
  5634. readpreproc:=optoken;
  5635. end;
  5636. '''' :
  5637. begin
  5638. current_scanner.preproc_pattern:=readquotedstring;
  5639. readpreproc:=_CSTRING;
  5640. end;
  5641. '0'..'9' :
  5642. begin
  5643. readnumber;
  5644. if (c in ['.','e','E']) then
  5645. begin
  5646. { first check for a . }
  5647. if c='.' then
  5648. begin
  5649. readchar;
  5650. if c in ['0'..'9'] then
  5651. begin
  5652. { insert the number after the . }
  5653. pattern:=pattern+'.';
  5654. while c in ['0'..'9'] do
  5655. begin
  5656. pattern:=pattern+c;
  5657. readchar;
  5658. end;
  5659. end
  5660. else
  5661. Illegal_Char(c);
  5662. end;
  5663. { E can also follow after a point is scanned }
  5664. if c in ['e','E'] then
  5665. begin
  5666. pattern:=pattern+'E';
  5667. readchar;
  5668. if c in ['-','+'] then
  5669. begin
  5670. pattern:=pattern+c;
  5671. readchar;
  5672. end;
  5673. if not(c in ['0'..'9']) then
  5674. Illegal_Char(c);
  5675. while c in ['0'..'9'] do
  5676. begin
  5677. pattern:=pattern+c;
  5678. readchar;
  5679. end;
  5680. end;
  5681. readpreproc:=_REALNUMBER;
  5682. end
  5683. else
  5684. readpreproc:=_INTCONST;
  5685. current_scanner.preproc_pattern:=pattern;
  5686. end;
  5687. '$','%':
  5688. begin
  5689. readnumber;
  5690. current_scanner.preproc_pattern:=pattern;
  5691. readpreproc:=_INTCONST;
  5692. end;
  5693. '&' :
  5694. begin
  5695. readnumber;
  5696. if length(pattern)=1 then
  5697. begin
  5698. readstring;
  5699. readpreproc:=_ID;
  5700. end
  5701. else
  5702. readpreproc:=_INTCONST;
  5703. current_scanner.preproc_pattern:=pattern;
  5704. end;
  5705. '.' :
  5706. begin
  5707. readchar;
  5708. readpreproc:=_POINT;
  5709. end;
  5710. ',' :
  5711. begin
  5712. readchar;
  5713. readpreproc:=_COMMA;
  5714. end;
  5715. '}' :
  5716. begin
  5717. readpreproc:=_END;
  5718. end;
  5719. '(' :
  5720. begin
  5721. readchar;
  5722. readpreproc:=_LKLAMMER;
  5723. end;
  5724. ')' :
  5725. begin
  5726. readchar;
  5727. readpreproc:=_RKLAMMER;
  5728. end;
  5729. '[' :
  5730. begin
  5731. readchar;
  5732. readpreproc:=_LECKKLAMMER;
  5733. end;
  5734. ']' :
  5735. begin
  5736. readchar;
  5737. readpreproc:=_RECKKLAMMER;
  5738. end;
  5739. '+' :
  5740. begin
  5741. readchar;
  5742. readpreproc:=_PLUS;
  5743. end;
  5744. '-' :
  5745. begin
  5746. readchar;
  5747. readpreproc:=_MINUS;
  5748. end;
  5749. '*' :
  5750. begin
  5751. readchar;
  5752. readpreproc:=_STAR;
  5753. end;
  5754. '/' :
  5755. begin
  5756. readchar;
  5757. readpreproc:=_SLASH;
  5758. end;
  5759. '=' :
  5760. begin
  5761. readchar;
  5762. readpreproc:=_EQ;
  5763. end;
  5764. '>' :
  5765. begin
  5766. readchar;
  5767. if c='=' then
  5768. begin
  5769. readchar;
  5770. readpreproc:=_GTE;
  5771. end
  5772. else
  5773. readpreproc:=_GT;
  5774. end;
  5775. '<' :
  5776. begin
  5777. readchar;
  5778. case c of
  5779. '>' :
  5780. begin
  5781. readchar;
  5782. readpreproc:=_NE;
  5783. end;
  5784. '=' :
  5785. begin
  5786. readchar;
  5787. readpreproc:=_LTE;
  5788. end;
  5789. else
  5790. readpreproc:=_LT;
  5791. end;
  5792. end;
  5793. #26 :
  5794. begin
  5795. readpreproc:=_EOF;
  5796. checkpreprocstack;
  5797. end;
  5798. else
  5799. begin
  5800. Illegal_Char(c);
  5801. readpreproc:=NOTOKEN;
  5802. end;
  5803. end;
  5804. end;
  5805. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5806. var
  5807. hs : texprvalue;
  5808. begin
  5809. hs:=preproc_comp_expr(nil);
  5810. if hs.isInt then
  5811. begin
  5812. value:=hs.asInt64;
  5813. result:=true;
  5814. end
  5815. else
  5816. begin
  5817. hs.error('Integer',place);
  5818. result:=false;
  5819. end;
  5820. hs.free;
  5821. end;
  5822. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  5823. var
  5824. hs : texprvalue;
  5825. begin
  5826. hs:=preproc_comp_expr(conform_to);
  5827. if hs.def.typ=setdef then
  5828. begin
  5829. value:=hs.asSet;
  5830. result:=true;
  5831. end
  5832. else
  5833. begin
  5834. hs.error('Set',place);
  5835. result:=false;
  5836. end;
  5837. hs.free;
  5838. end;
  5839. function tscannerfile.asmgetchar : char;
  5840. begin
  5841. readchar;
  5842. repeat
  5843. case c of
  5844. #26 :
  5845. begin
  5846. reload;
  5847. if (c=#26) and not assigned(inputfile.next) then
  5848. end_of_file;
  5849. continue;
  5850. end;
  5851. else
  5852. begin
  5853. asmgetchar:=c;
  5854. exit;
  5855. end;
  5856. end;
  5857. until false;
  5858. end;
  5859. {$ifdef EXTDEBUG}
  5860. function tscannerfile.DumpPointer: string;
  5861. var
  5862. i: Integer;
  5863. begin
  5864. Result:='';
  5865. if inputpointer=nil then exit;
  5866. i:=0;
  5867. While (inputpointer[I]<>#0) and (i<200) do
  5868. inc(i);
  5869. Setlength(result,I);
  5870. move(inputpointer^,Result[1],I);
  5871. result:='<'+inttostr(inputstart)+'>'+result;
  5872. end;
  5873. {$endif EXTDEBUG}
  5874. {*****************************************************************************
  5875. Helpers
  5876. *****************************************************************************}
  5877. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5878. begin
  5879. if dm in [directive_all, directive_turbo] then
  5880. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5881. if dm in [directive_all, directive_mac] then
  5882. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5883. end;
  5884. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5885. begin
  5886. if dm in [directive_all, directive_turbo] then
  5887. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5888. if dm in [directive_all, directive_mac] then
  5889. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5890. end;
  5891. {*****************************************************************************
  5892. Initialization
  5893. *****************************************************************************}
  5894. procedure InitScanner;
  5895. begin
  5896. InitWideString(patternw);
  5897. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5898. mac_scannerdirectives:=TFPHashObjectList.Create;
  5899. { Common directives and conditionals }
  5900. AddDirective('I',directive_all, @dir_include);
  5901. AddDirective('DEFINE',directive_all, @dir_define);
  5902. AddDirective('UNDEF',directive_all, @dir_undef);
  5903. AddConditional('IF',directive_all, @dir_if);
  5904. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5905. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5906. AddConditional('ELSE',directive_all, @dir_else);
  5907. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5908. AddConditional('ENDIF',directive_all, @dir_endif);
  5909. { Directives and conditionals for all modes except mode macpas}
  5910. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5911. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5912. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5913. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5914. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5915. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5916. { Directives and conditionals for mode macpas: }
  5917. AddDirective('SETC',directive_mac, @dir_setc);
  5918. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5919. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5920. AddConditional('IFC',directive_mac, @dir_if);
  5921. AddConditional('ELSEC',directive_mac, @dir_else);
  5922. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5923. AddConditional('ENDC',directive_mac, @dir_endif);
  5924. end;
  5925. procedure DoneScanner;
  5926. begin
  5927. turbo_scannerdirectives.Free;
  5928. mac_scannerdirectives.Free;
  5929. DoneWideString(patternw);
  5930. end;
  5931. end.