scanner.pas 201 KB

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