scanner.pas 198 KB

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