scanner.pas 188 KB

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