scanner.pas 240 KB

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