scanner.pas 217 KB

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