scanner.pas 218 KB

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