FoxSemanticChecker.Mod 397 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. replacements*: Replacement;
  93. cellsAreObjects: BOOLEAN;
  94. variableAccessed: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. error := FALSE;
  103. NEW(typeFixes);
  104. NEW(pointerFixes);
  105. resolvedType := NIL;
  106. resolvedExpression := NIL;
  107. resolvedStatement := NIL;
  108. currentScope := NIL;
  109. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  110. SELF.importCache := importCache;
  111. arrayBaseImported := FALSE;
  112. SELF.VerboseErrorMessage := verboseErrorMessage;
  113. global := NIL;
  114. phase := UndefinedPhase;
  115. currentIsRealtime := FALSE;
  116. currentIsUnreachable := FALSE;
  117. currentIsCellNet := FALSE;
  118. currentIsBodyProcedure := FALSE;
  119. currentIsExclusive := FALSE;
  120. withEntries := NIL;
  121. SELF.cellsAreObjects := system.cellsAreObjects;
  122. END InitChecker;
  123. (** report error **)
  124. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  125. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  126. BEGIN
  127. IF diagnostics # NIL THEN
  128. Basic.GetErrorMessage(code,message,errorMessage);
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  132. END;
  133. error := TRUE;
  134. END Error;
  135. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  136. VAR errModule: SyntaxTree.Module;
  137. BEGIN
  138. IF diagnostics # NIL THEN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  141. END;
  142. END Warning;
  143. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  144. VAR errorMessage: ARRAY 256 OF CHAR;
  145. BEGIN
  146. IF diagnostics # NIL THEN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  149. END;
  150. error := TRUE;
  151. END ErrorSS;
  152. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  153. VAR msg, msg2: ARRAY 256 OF CHAR;
  154. BEGIN
  155. IF diagnostics # NIL THEN
  156. COPY(msg1, msg);
  157. Strings.Append(msg, " = ");
  158. Basic.GetString(s, msg2);
  159. Strings.Append(msg, msg2);
  160. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  161. END;
  162. END InfoSS;
  163. (*** symbol lookup ***)
  164. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  165. **)
  166. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  167. VAR
  168. scope,baseScope: SyntaxTree.Scope;
  169. symbol, s: SyntaxTree.Symbol;
  170. ownerRecord,base: SyntaxTree.RecordType;
  171. BEGIN
  172. scope := inScope;
  173. symbol := NIL;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. s := NIL;
  177. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  178. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  179. END;
  180. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  181. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  182. ELSE
  183. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  184. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  185. symbol.MarkUsed;
  186. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  187. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  188. base := RecordBase(ownerRecord);
  189. IF (base # NIL) THEN
  190. baseScope := base.recordScope;
  191. symbol := Find(baseScope,name,FALSE);
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. ELSE
  196. symbol := NIL;
  197. END;
  198. END;
  199. END;
  200. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  201. END;
  202. IF (symbol # NIL) THEN
  203. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  204. ASSERT(phase = DeclarationPhase);
  205. ResolveSymbol(symbol)
  206. END;
  207. symbol.MarkUsed;
  208. END;
  209. RETURN symbol
  210. END Find;
  211. (*** types ***)
  212. (** find type declaration with name qualifiedIdentifier and return resolved type
  213. - check qualified identifier prefix, set scope to module scope if appropriate
  214. - check suffix in scope
  215. **)
  216. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  217. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  218. BEGIN
  219. result := NIL;
  220. prevScope := currentScope;
  221. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  222. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  223. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  224. IF symbol(SyntaxTree.Import).module = NIL THEN
  225. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  226. result := SyntaxTree.invalidType;
  227. symbol := NIL;
  228. ELSE
  229. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  230. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  231. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  232. IF VerboseErrorMessage THEN
  233. Printout.Info("scope", currentScope);
  234. Printout.Info("symbol", symbol);
  235. END;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  237. END;
  238. END;
  239. ELSE
  240. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  241. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  242. symbol := NIL;
  243. END;
  244. ELSE
  245. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  246. IF symbol = NIL THEN
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  248. IF VerboseErrorMessage THEN
  249. Printout.Info("Qualident",qualifiedIdentifier);
  250. Printout.Info("in scope",currentScope) ;
  251. END;
  252. END;
  253. END;
  254. IF symbol = NIL THEN (* error already handled *)
  255. typeDeclaration := NIL;
  256. result := SyntaxTree.invalidType;
  257. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  258. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSE
  262. currentScope := symbol.scope;
  263. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  264. result := ResolveType(typeDeclaration.declaredType);
  265. symbol.MarkUsed;
  266. ASSERT(result # NIL);
  267. END;
  268. currentScope := prevScope;
  269. RETURN result
  270. END ResolveNamedType;
  271. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  272. If node is currently being resolved then emit a cyclic definition error.
  273. Return TRUE only if node is fully resolved.
  274. **)
  275. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  276. VAR result: BOOLEAN;
  277. BEGIN
  278. IF SyntaxTree.Resolved IN x.state THEN
  279. result := FALSE
  280. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  281. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  282. result := FALSE;
  283. ELSE
  284. result := TRUE;
  285. x.SetState(SyntaxTree.BeingResolved)
  286. END;
  287. RETURN result
  288. END TypeNeedsResolution;
  289. (** Return invalid type if x is currently being resolved, return x otherwise**)
  290. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  291. BEGIN
  292. IF SyntaxTree.Resolved IN x.state THEN
  293. RETURN x
  294. ELSE
  295. RETURN SyntaxTree.invalidType
  296. END;
  297. END ResolvedType;
  298. PROCEDURE VisitType(x: SyntaxTree.Type);
  299. BEGIN
  300. ASSERT(x = SyntaxTree.invalidType);
  301. END VisitType;
  302. (** resolve basic type **)
  303. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  304. BEGIN
  305. IF TypeNeedsResolution(x) THEN
  306. x.SetState(SyntaxTree.Resolved);
  307. END;
  308. resolvedType := ResolvedType(x)
  309. END VisitBasicType;
  310. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitByteType;
  314. (** resolve character type **)
  315. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitCharacterType;
  319. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitBooleanType;
  323. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitSetType;
  327. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitAddressType;
  331. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitSizeType;
  335. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitAnyType;
  339. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitObjectType;
  343. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitNilType;
  347. (** resolve integer type **)
  348. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitIntegerType;
  352. (** resolve real type **)
  353. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitFloatType;
  357. (** resolve complex type **)
  358. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  359. BEGIN
  360. VisitBasicType(x);
  361. END VisitComplexType;
  362. (**
  363. resolve string type: nothing to be done
  364. **)
  365. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  366. BEGIN
  367. IF TypeNeedsResolution(x) THEN
  368. x.SetState(SyntaxTree.Resolved);
  369. END;
  370. resolvedType := ResolvedType(x)
  371. END VisitStringType;
  372. (**
  373. check enumeration scope: enter symbols and check for duplicate names
  374. **)
  375. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  376. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  377. BEGIN
  378. prevScope := currentScope;
  379. currentScope := x;
  380. e := x.firstConstant;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  408. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: LONGINT;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. x.SetArrayBase(pointerType);
  479. END;
  480. IF x.length # NIL THEN
  481. variableAccessed := FALSE;
  482. e := ResolveExpression(x.length);
  483. IF (e.resolved = NIL) THEN
  484. IF variableAccessed THEN
  485. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  486. END;
  487. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  488. ELSE
  489. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  490. END;
  491. END;
  492. IF arrayBase IS SyntaxTree.ArrayType THEN
  493. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  494. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  495. END;
  496. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  497. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  498. END;
  499. x.SetHasPointers(arrayBase.hasPointers);
  500. x.SetState(SyntaxTree.Resolved);
  501. END;
  502. resolvedType := ResolvedType(x);
  503. END VisitArrayType;
  504. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  505. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  506. BEGIN
  507. module := currentScope.ownerModule;
  508. IF module.name=name THEN
  509. (* do nothing *)
  510. ELSE
  511. moduleScope := module.moduleScope;
  512. import := moduleScope.FindImport(name);
  513. IF import = NIL THEN
  514. import := SyntaxTree.NewImport(position,name,name,TRUE);
  515. moduleScope.AddImport(import);
  516. Register(import,moduleScope,FALSE);
  517. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  518. VisitImport(import);
  519. ELSIF import.direct=FALSE THEN
  520. import.SetScope(module.moduleScope);
  521. import.SetDirect(TRUE);
  522. IF moduleScope.FindSymbol(import.name) = NIL THEN
  523. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  524. duplicate.SetContext(import.context);
  525. duplicate.SetModule(import.module);
  526. Register(duplicate,moduleScope,TRUE);
  527. VisitImport(duplicate);
  528. END;
  529. END;
  530. import.MarkUsed
  531. END;
  532. END ImportModule;
  533. (**
  534. resolve math array type
  535. - check base type
  536. - open math array of array forbidden
  537. - math array of tensor forbidden
  538. - static array of open array forbidden
  539. **)
  540. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  541. VAR arrayBase: SyntaxTree.Type;
  542. BEGIN
  543. IF TypeNeedsResolution(x) THEN
  544. x.SetArrayBase(ResolveType(x.arrayBase));
  545. IF x.length # NIL THEN
  546. x.SetLength(ConstantIntegerGeq0(x.length));
  547. END;
  548. arrayBase := x.arrayBase;
  549. IF arrayBase # NIL THEN
  550. arrayBase := arrayBase.resolved;
  551. IF arrayBase = SyntaxTree.invalidType THEN
  552. (* error already handled *)
  553. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  554. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  555. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  556. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  557. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  558. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  559. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  560. END;
  561. END;
  562. IF x.form = SyntaxTree.Static THEN
  563. x.SetIncrement(system.SizeOf(arrayBase));
  564. END;
  565. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  566. END;
  567. x.SetState(SyntaxTree.Resolved);
  568. END;
  569. resolvedType := ResolvedType(x);
  570. END VisitMathArrayType;
  571. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  572. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  573. (1) Rec = RECORD ... END; Ptr <---> Rec
  574. Ptr = POINTER TO Rec; ^ |
  575. | |
  576. TypeDesc TypeDesc
  577. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  578. ^ /
  579. | /
  580. TypeDesc <-- /
  581. *)
  582. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  583. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  584. BEGIN
  585. Strings.IntToStr(x.position,number);
  586. COPY(prefix,name);
  587. Strings.Append(name,"@");
  588. Strings.Append(name,number);
  589. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  590. typeDeclaration.SetDeclaredType(x);
  591. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  592. x.SetTypeDeclaration(typeDeclaration);
  593. currentScope.AddTypeDeclaration(typeDeclaration);
  594. typeDeclaration.SetScope(currentScope);
  595. END AnonymousTypeDeclaration;
  596. (**
  597. deferred pointer type resolving
  598. - resolve base type
  599. - check that base type is a record or array type
  600. - if error then set base type to invalid type
  601. **)
  602. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  603. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  604. BEGIN
  605. ASSERT(type.pointerBase # NIL);
  606. position := type.pointerBase.position;
  607. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  608. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  609. (* not for pointers, a type is needed for the records only
  610. IF type.typeDeclaration = NIL THEN
  611. AnonymousTypeDeclaration(type);
  612. END;
  613. *)
  614. END;
  615. resolved := ResolveType(type.pointerBase);
  616. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  617. type.SetPointerBase(resolved);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  619. recordType := resolved.resolved(SyntaxTree.RecordType);
  620. IF recordType.isObject & (recordType.baseType # NIL) THEN
  621. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  622. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  623. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  624. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  625. END;
  626. END;
  627. END;
  628. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  629. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  630. END;
  631. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  632. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  633. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  634. ELS
  635. *)
  636. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  638. END;
  639. END;
  640. ELSE
  641. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  642. type.SetPointerBase(SyntaxTree.invalidType)
  643. END
  644. END FixPointerType;
  645. (**
  646. resolve pointer type
  647. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  648. **)
  649. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  650. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  651. modifiers: SyntaxTree.Modifier; position: LONGINT;
  652. BEGIN
  653. IF TypeNeedsResolution(x) THEN
  654. modifiers := x.modifiers;
  655. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  656. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  657. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  658. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  659. (* inheritance cycle check
  660. example:
  661. A=POINTER TO RECORD(B) END;
  662. B=POINTER TO RECORD(A) END;
  663. *)
  664. IF x.pointerBase IS SyntaxTree.RecordType THEN
  665. recordType := x.pointerBase(SyntaxTree.RecordType);
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  685. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  686. x.SetSize(value)
  687. ELSE
  688. x.SetSize(system.SizeOf(system.longintType));
  689. END;
  690. x.SetState(SyntaxTree.Resolved);
  691. END;
  692. resolvedType := ResolvedType(x)
  693. END VisitPortType;
  694. (**
  695. deferred procedure type resolving
  696. - resolve return type
  697. - traverse and resolve parameters
  698. **)
  699. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  700. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  701. BEGIN
  702. resolved := ResolveType(procedureType.returnType);
  703. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  704. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  705. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  706. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  707. END;
  708. procedureType.SetReturnType(resolved);
  709. IF (resolved # NIL) THEN
  710. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  711. parameter.SetType(procedureType.returnType);
  712. parameter.SetAccess(SyntaxTree.Hidden);
  713. parameter.SetUntraced(procedureType.hasUntracedReturn);
  714. VisitParameter(parameter);
  715. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  716. END;
  717. (* process parameters *)
  718. parameter :=procedureType.firstParameter;
  719. WHILE (parameter # NIL) DO
  720. VisitParameter(parameter);
  721. parameter := parameter.nextParameter;
  722. END;
  723. END FixProcedureType;
  724. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  725. VAR prev,this: SyntaxTree.Modifier;
  726. BEGIN
  727. this := modifiers;prev := NIL;
  728. WHILE (this # NIL) & (this.identifier # name) DO
  729. prev := this; this := this.nextModifier;
  730. END;
  731. IF this # NIL THEN
  732. IF this.expression # NIL THEN
  733. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  734. END;
  735. this.Resolved;
  736. position := this.position;
  737. RETURN TRUE
  738. ELSE
  739. RETURN FALSE
  740. END;
  741. END HasFlag;
  742. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  743. VAR prev,this: SyntaxTree.Modifier;
  744. BEGIN
  745. this := modifiers;prev := NIL;
  746. WHILE (this # NIL) & (this.identifier # name) DO
  747. prev := this; this := this.nextModifier;
  748. END;
  749. IF this # NIL THEN
  750. IF this.expression = NIL THEN
  751. Error(this.position,Diagnostics.Invalid,"expected expression value");
  752. ELSE
  753. this.SetExpression(ConstantExpression(this.expression));
  754. IF CheckIntegerValue(this.expression,value) THEN END;
  755. END;
  756. this.Resolved;
  757. position := this.position;
  758. RETURN TRUE
  759. ELSE RETURN FALSE
  760. END;
  761. END HasValue;
  762. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  763. VAR this: SyntaxTree.Modifier;
  764. BEGIN
  765. this := modifiers;
  766. WHILE this # NIL DO
  767. IF ~this.resolved THEN
  768. IF checkUse THEN
  769. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  770. ELSE
  771. this.SetExpression(ResolveExpression(this.expression));
  772. this.Resolved;
  773. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  774. END;
  775. END;
  776. this := this.nextModifier
  777. END;
  778. END CheckModifiers;
  779. (**
  780. resolve procedure type
  781. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  782. **)
  783. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  784. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  785. BEGIN
  786. IF TypeNeedsResolution(procedureType) THEN
  787. modifiers := procedureType.modifiers;
  788. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  789. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  790. procedureType.SetInterrupt(TRUE);
  791. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  792. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  793. IF useDarwinCCalls THEN (*fld*)
  794. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  795. ELSE
  796. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  797. END
  798. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  799. procedureType.SetNoReturn(TRUE);
  800. END;
  801. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  802. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  803. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  804. CheckModifiers(modifiers, TRUE);
  805. modifiers := procedureType.returnTypeModifiers;
  806. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  807. CheckModifiers(modifiers, TRUE);
  808. typeFixes.Add(procedureType,currentScope);
  809. procedureType.SetHasPointers(procedureType.isDelegate);
  810. procedureType.SetState(SyntaxTree.Resolved);
  811. END;
  812. resolvedType := ResolvedType(procedureType)
  813. END VisitProcedureType;
  814. (** check and resolve record type
  815. - check base type: must be record, math array or array-structured object type
  816. - check declarations
  817. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  818. **)
  819. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  820. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  821. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  822. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  823. hasPointers: BOOLEAN;
  824. modifiers: SyntaxTree.Modifier;
  825. value: LONGINT;
  826. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  827. BEGIN
  828. type := type.resolved;
  829. IF (type IS SyntaxTree.PointerType) &
  830. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  831. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  832. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  833. RETURN TRUE
  834. ELSE
  835. RETURN FALSE
  836. END;
  837. END IsPointerToRecord;
  838. BEGIN
  839. IF TypeNeedsResolution(x) THEN
  840. hasPointers := FALSE;
  841. modifiers := x.modifiers;
  842. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  843. CheckModifiers(modifiers, TRUE);
  844. IF x.baseType # NIL THEN
  845. position := x.baseType.position;
  846. baseType := ResolveType(x.baseType);
  847. resolved := baseType.resolved;
  848. hasPointers := hasPointers OR resolved.hasPointers;
  849. IF x.isObject THEN (* object *)
  850. ASSERT(x.pointerType # NIL);
  851. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  852. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  853. baseType := NIL
  854. ELSIF IsPointerToRecord(resolved,recordType) THEN
  855. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  856. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  857. ELSE
  858. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  859. END;
  860. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  861. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  862. ELSIF IsPointerToRecord(resolved,recordType) THEN
  863. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  864. ELSIF resolved IS SyntaxTree.RecordType THEN
  865. ELSE
  866. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  867. END;
  868. ELSE
  869. IF resolved IS SyntaxTree.RecordType THEN
  870. ELSE
  871. Error(position, Diagnostics.Invalid,"record does not extend record")
  872. END;
  873. END;
  874. x.SetBaseType(baseType);
  875. IF x.Level() > 15 THEN
  876. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  877. (* note:
  878. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  879. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  880. inheritance history of a type.
  881. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  882. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  883. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  884. *)
  885. END;
  886. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  887. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  888. END;
  889. END;
  890. Declarations(x.recordScope);
  891. ResolveArrayStructure(x);
  892. (* computation of sizes and offsets skipped -> done in backend / system *)
  893. recordBase := x.GetBaseRecord();
  894. IF recordBase = NIL THEN numberMethods := 0
  895. ELSE numberMethods := recordBase.recordScope.numberMethods
  896. END;
  897. isRealtime := TRUE;
  898. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  899. WHILE symbol # NIL DO
  900. IF symbol IS SyntaxTree.Variable THEN
  901. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  902. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  903. END;
  904. IF symbol IS SyntaxTree.Procedure THEN
  905. procedure := symbol(SyntaxTree.Procedure);
  906. IF procedure.super # NIL THEN
  907. procedure.SetMethodNumber(procedure.super.methodNumber)
  908. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  909. procedure.SetMethodNumber(numberMethods);
  910. INC(numberMethods);
  911. END;
  912. END;
  913. symbol := symbol.nextSymbol;
  914. END;
  915. IF isRealtime THEN x.SetRealtime(TRUE) END;
  916. x.recordScope.SetNumberMethods(numberMethods);
  917. (* TODO: is this needed anymore? *)
  918. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  919. Error(x.position,Diagnostics.Invalid,"object extends a record")
  920. END;
  921. IF (x.typeDeclaration = NIL) THEN
  922. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  923. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  924. (*
  925. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  926. AnonymousTypeDeclaration(x,name);
  927. *)
  928. ELSE
  929. AnonymousTypeDeclaration(x,"Anonymous");
  930. END;
  931. END;
  932. x.SetHasPointers(hasPointers);
  933. x.SetState(SyntaxTree.Resolved);
  934. END;
  935. resolvedType := ResolvedType(x);
  936. END VisitRecordType;
  937. (** check and resolve cell type
  938. - check base type: must be cell
  939. - check declarations
  940. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  941. **)
  942. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  943. VAR
  944. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  945. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  946. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  947. recordBase: SyntaxTree.RecordType;
  948. numberMethods, int: LONGINT;
  949. real: LONGREAL;
  950. bool: BOOLEAN;
  951. set: SET;
  952. variable: SyntaxTree.Variable;
  953. v: SyntaxTree.Expression;
  954. str: Scanner.StringType;
  955. atype: SyntaxTree.ArrayType;
  956. prev: SyntaxTree.Scope;
  957. BEGIN
  958. IF TypeNeedsResolution(x) THEN
  959. recordBase := NIL;
  960. IF cellsAreObjects THEN
  961. IF x.baseType = NIL THEN
  962. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  963. ImportModule(qualifiedIdentifier.prefix, x.position);
  964. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  965. x.SetBaseType(ResolveType(x.baseType));
  966. recordBase := x.GetBaseRecord();
  967. IF recordBase = NIL THEN
  968. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  969. END;
  970. ELSE
  971. x.SetBaseType(ResolveType(x.baseType));
  972. END;
  973. ELSE
  974. x.SetBaseType(ResolveType(x.baseType));
  975. END;
  976. IF recordBase = NIL THEN numberMethods := 0
  977. ELSE numberMethods := recordBase.recordScope.numberMethods
  978. END;
  979. modifier := x.modifiers;
  980. (*IF ~x.isCellNet THEN*)
  981. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  982. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  983. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  984. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  985. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  986. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  987. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  988. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  989. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  990. symbol := system.activeCellsCapabilities;
  991. WHILE symbol # NIL DO
  992. IF HasFlag(modifier, symbol.name, position) THEN END;
  993. symbol := symbol.nextSymbol;
  994. END;
  995. modifier := x.modifiers;
  996. WHILE (modifier # NIL) DO
  997. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  998. IF modifier.expression # NIL THEN
  999. v := ConstantExpression(modifier.expression);
  1000. property.SetValue(v);
  1001. IF IsIntegerValue(modifier.expression, int) THEN
  1002. (*property.SetValue(modifier.expression);*)
  1003. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1004. property.SetType(system.longintType);
  1005. ELSIF IsRealValue(modifier.expression, real) THEN
  1006. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1007. property.SetType(system.longrealType);
  1008. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1009. property.SetType(system.booleanType);
  1010. ELSIF IsSetValue(modifier.expression, set) THEN
  1011. property.SetType(system.setType);
  1012. ELSIF IsStringValue(modifier.expression, str) THEN
  1013. (*property.SetValue(modifier.expression);*)
  1014. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1015. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1016. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1017. property.SetType(atype);
  1018. ELSE
  1019. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1020. END;
  1021. ELSE (* flag property *)
  1022. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1023. property.SetType(system.booleanType);
  1024. END;
  1025. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1026. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1027. x.AddProperty(property);
  1028. modifier := modifier.nextModifier;
  1029. END;
  1030. (*ELSE
  1031. (* no: this should not be allowed on cell net types or check against global scope ...
  1032. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1033. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1034. END;
  1035. END
  1036. *)
  1037. END;*)
  1038. CheckModifiers(modifier, FALSE);
  1039. Declarations(x.cellScope);
  1040. (* process parameters *)
  1041. prev := currentScope;
  1042. currentScope := x.cellScope;
  1043. parameter :=x.firstParameter;
  1044. WHILE (parameter # NIL) DO
  1045. VisitParameter(parameter);
  1046. type := parameter.type.resolved;
  1047. IF ~(type IS SyntaxTree.PortType) THEN
  1048. WHILE IsStaticArray(type, type, len) DO
  1049. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1050. END;
  1051. WHILE IsDynamicArray(type, type) DO
  1052. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1053. END;
  1054. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1055. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1056. END;
  1057. END;
  1058. parameter := parameter.nextParameter;
  1059. END;
  1060. currentScope := prev;
  1061. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1062. WHILE symbol # NIL DO
  1063. IF symbol IS SyntaxTree.Variable THEN
  1064. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1065. END;
  1066. symbol := symbol.nextSymbol;
  1067. END;
  1068. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1069. IF (x.typeDeclaration = NIL) THEN
  1070. AnonymousTypeDeclaration(x,"Anonymous");
  1071. END;
  1072. x.SetState(SyntaxTree.Resolved);
  1073. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1074. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1075. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1076. Warning(x.position, "Non-empty body for an engine?");
  1077. END;
  1078. END;
  1079. resolvedType := ResolvedType(x);
  1080. END VisitCellType;
  1081. (* check if an object is an array-structured object type
  1082. - determine the array structure
  1083. - collect operators from top to bottom in the inheritance hierarchy
  1084. - check if LEN operator is declared
  1085. - determine number of possible index operators
  1086. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1087. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1088. *)
  1089. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1090. VAR
  1091. indexOperatorCount, i: LONGINT;
  1092. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1093. isTensor: BOOLEAN;
  1094. BEGIN
  1095. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1096. (* determine array structure *)
  1097. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1098. END;
  1099. IF recordType.HasArrayStructure() THEN
  1100. (* the object is an ASOT *)
  1101. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1102. (* reset array access operators *)
  1103. arrayAccessOperators.len := NIL;
  1104. arrayAccessOperators.generalRead := NIL;
  1105. arrayAccessOperators.generalWrite := NIL;
  1106. IF isTensor THEN
  1107. (* all operators of dimensionalities 1 to max *)
  1108. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1109. ELSE
  1110. (* all operators of certain dimensionality *)
  1111. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1112. END;
  1113. NEW(arrayAccessOperators.read, indexOperatorCount);
  1114. NEW(arrayAccessOperators.write, indexOperatorCount);
  1115. FOR i := 0 TO indexOperatorCount - 1 DO
  1116. arrayAccessOperators.read[i] := NIL;
  1117. arrayAccessOperators.write[i] := NIL
  1118. END;
  1119. (* collect access operators in the record scope *)
  1120. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1121. IF arrayAccessOperators.len = NIL THEN
  1122. (* TODO: think about making this operator optional for static array structures *)
  1123. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1124. END;
  1125. (* show error messages *)
  1126. IF isTensor THEN
  1127. (* require ARRAY [*] OF RANGE *)
  1128. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1129. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1130. ELSE
  1131. (* forbid ARRAY [*] OF RANGE *)
  1132. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1133. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1134. (* require RANGE, RANGE, ... RANGE *)
  1135. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1136. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1137. END;
  1138. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1139. ELSE
  1140. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1141. IF recordType.recordScope.firstOperator # NIL THEN
  1142. RETURN;
  1143. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1144. END
  1145. END
  1146. END ResolveArrayStructure;
  1147. (** collect array access operators in a record scope **)
  1148. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1149. VAR
  1150. baseType: SyntaxTree.Type;
  1151. operator: SyntaxTree.Operator;
  1152. isReadOperator, isGeneralOperator: BOOLEAN;
  1153. indexListSize, indexListKind, hashValue: LONGINT;
  1154. BEGIN
  1155. (* if a parent record scope exists, collect the operators there first *)
  1156. baseType := recordScope.ownerRecord.baseType;
  1157. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1158. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1159. END;
  1160. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1161. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1162. END;
  1163. (* go through all operators in the current record scope *)
  1164. operator := recordScope.firstOperator;
  1165. WHILE operator # NIL DO
  1166. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1167. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1168. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1169. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1170. IF isGeneralOperator THEN
  1171. IF isReadOperator THEN
  1172. arrayAccessOperators.generalRead := operator
  1173. ELSE
  1174. arrayAccessOperators.generalWrite := operator
  1175. END
  1176. ELSE
  1177. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1178. IF isReadOperator THEN
  1179. arrayAccessOperators.read[hashValue] := operator
  1180. ELSE
  1181. arrayAccessOperators.write[hashValue] := operator
  1182. END
  1183. END
  1184. END
  1185. ELSE
  1186. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1187. END;
  1188. operator := operator.nextOperator
  1189. END
  1190. END CollectArrayAccessOperators;
  1191. (** the hash value of an index operator **)
  1192. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1193. VAR result: LONGINT;
  1194. BEGIN
  1195. IF isTensor THEN
  1196. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1197. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1198. ELSE
  1199. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1200. END
  1201. ELSE
  1202. result := indexListKind
  1203. END;
  1204. RETURN result
  1205. END IndexOperatorHash;
  1206. (** 2 to the power of exponent **)
  1207. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1208. VAR result, i: LONGINT;
  1209. BEGIN
  1210. result := 1;
  1211. FOR i := 1 TO exponent DO
  1212. result := result * 2;
  1213. END;
  1214. RETURN result
  1215. END TwoToThePowerOf;
  1216. (** check if a LEN operator has a correct signature. i.e.
  1217. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1218. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1219. **)
  1220. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1221. VAR
  1222. procedureType: SyntaxTree.ProcedureType;
  1223. returnedArrayType: SyntaxTree.MathArrayType;
  1224. result: BOOLEAN;
  1225. BEGIN
  1226. result := FALSE;
  1227. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1228. IF (procedureType.numberParameters = 0) THEN
  1229. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1230. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1231. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1232. IF returnedArrayType.form = SyntaxTree.Open THEN
  1233. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1234. result := TRUE
  1235. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1236. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1237. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1238. result := TRUE
  1239. END
  1240. END
  1241. END
  1242. END
  1243. END;
  1244. IF result THEN
  1245. (* export symbol automatically *)
  1246. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1247. ELSE
  1248. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1249. END;
  1250. RETURN result
  1251. END CheckLenOperator;
  1252. (** check if an index operator has a correct signature. i.e.
  1253. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1254. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1255. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1256. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1257. - determine if it is a read or write operator (existance of return type)
  1258. - check index parameters
  1259. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1260. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1261. [LONGINT] -> binary 0 -> 0
  1262. [RANGE] -> binary 1 -> 1
  1263. [LONGINT, LONGINT] -> binary 00 -> 0
  1264. [LONGINT, RANGE] -> binary 01 -> 1
  1265. [RANGE, LONGINT] -> binary 10 -> 2
  1266. [RANGE, RANGE] -> binary 11 -> 3
  1267. etc.
  1268. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1269. - for read operators, check if return type matches the type of data that is read
  1270. - for write operators, check if last parameter type matches the type of data that is written
  1271. **)
  1272. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1273. VAR
  1274. elementType, otherElementType, dataType: SyntaxTree.Type;
  1275. procedureType: SyntaxTree.ProcedureType;
  1276. mathArrayType: SyntaxTree.MathArrayType;
  1277. parameter: SyntaxTree.Parameter;
  1278. parameterCount, rangeCount, i: LONGINT;
  1279. hasTypeError: BOOLEAN;
  1280. BEGIN
  1281. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1282. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1283. (* determine if it is a read or write operator *)
  1284. isReadOperator := (procedureType.returnType # NIL);
  1285. IF isReadOperator THEN
  1286. indexListSize := parameterCount;
  1287. ELSE
  1288. indexListSize := parameterCount - 1;
  1289. END;
  1290. IF indexListSize < 1 THEN
  1291. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1292. RETURN FALSE
  1293. END;
  1294. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1295. (* general operator *)
  1296. isGeneralOperator := TRUE;
  1297. IF indexListSize > 1 THEN
  1298. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1299. RETURN FALSE
  1300. END;
  1301. (* ARRAY [*] OF RANGE*)
  1302. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1303. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1304. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1305. RETURN FALSE
  1306. END;
  1307. parameter := procedureType.firstParameter.nextParameter
  1308. ELSE
  1309. (* fixed-dim. operator *)
  1310. isGeneralOperator := FALSE;
  1311. (* check number of index parameters *)
  1312. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1313. (* for tensors, limited to a certain size *)
  1314. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1315. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1316. RETURN FALSE
  1317. END
  1318. ELSE
  1319. (* for non-tensors, depends on dimensionality *)
  1320. IF indexListSize # arrayStructure.Dimensionality() THEN
  1321. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1322. RETURN FALSE
  1323. END
  1324. END;
  1325. (* go through all index parameters
  1326. - count the number of ranges
  1327. - determine the index list kind number
  1328. *)
  1329. indexListKind := 0;
  1330. rangeCount := 0;
  1331. parameter := procedureType.firstParameter;
  1332. FOR i := 1 TO indexListSize DO
  1333. indexListKind := indexListKind * 2;
  1334. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1335. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1336. INC(indexListKind);
  1337. INC(rangeCount)
  1338. ELSE
  1339. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1340. RETURN FALSE
  1341. END;
  1342. parameter := parameter.nextParameter
  1343. END;
  1344. END;
  1345. (*
  1346. - for read operators: check type of last parameter
  1347. - for write operators: check return type
  1348. *)
  1349. IF isReadOperator THEN
  1350. dataType := procedureType.returnType (* the return type *)
  1351. ELSE
  1352. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1353. END;
  1354. elementType := arrayStructure.ElementType();
  1355. hasTypeError := FALSE;
  1356. IF isGeneralOperator THEN
  1357. (* ARRAY [?] OF <Element> *)
  1358. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1359. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1360. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1361. hasTypeError := TRUE
  1362. END
  1363. ELSE
  1364. hasTypeError := TRUE
  1365. END
  1366. ELSE
  1367. IF rangeCount = 0 THEN
  1368. (* <Element> *)
  1369. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1370. ELSE
  1371. (* ARRAY [*, *, ..., *] OF <Element> *)
  1372. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1373. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1374. IF mathArrayType.IsFullyDynamic() THEN
  1375. IF mathArrayType.Dimensionality() = rangeCount THEN
  1376. otherElementType := mathArrayType.ElementType();
  1377. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1378. ELSE
  1379. hasTypeError := TRUE
  1380. END
  1381. ELSE
  1382. hasTypeError := TRUE
  1383. END
  1384. ELSE
  1385. hasTypeError := TRUE
  1386. END
  1387. END
  1388. END;
  1389. IF hasTypeError THEN
  1390. IF isReadOperator THEN
  1391. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1392. ELSE
  1393. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1394. END;
  1395. RETURN FALSE
  1396. END;
  1397. (* export symbol automatically *)
  1398. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1399. RETURN TRUE
  1400. END CheckIndexOperator;
  1401. (** resolve all pending types (late resolving).
  1402. - type fixes are resolved at the end of the declaration phase
  1403. - type fixes may imply new type fixes that are also entered at the end of the list
  1404. **)
  1405. PROCEDURE FixTypes;
  1406. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1407. BEGIN
  1408. prevScope := currentScope;
  1409. p := typeFixes.Get(currentScope);
  1410. WHILE p # NIL DO
  1411. ASSERT(currentScope # NIL);
  1412. ASSERT(p IS SyntaxTree.Type);
  1413. IF p IS SyntaxTree.PointerType THEN
  1414. FixPointerType(p(SyntaxTree.PointerType))
  1415. ELSIF p IS SyntaxTree.ProcedureType THEN
  1416. FixProcedureType(p(SyntaxTree.ProcedureType))
  1417. ELSE
  1418. HALT(100);
  1419. END;
  1420. p := typeFixes.Get(currentScope);
  1421. END;
  1422. currentScope :=prevScope;
  1423. END FixTypes;
  1424. (**
  1425. resolve type x
  1426. - if x is nil then return nil
  1427. - if x cannot be resolved then the result is invalidType else the result is x
  1428. - the resolved type is entered into x.resolved
  1429. **)
  1430. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1431. VAR prev,resolved: SyntaxTree.Type;
  1432. BEGIN
  1433. prev := resolvedType;
  1434. resolvedType := SyntaxTree.invalidType;
  1435. IF x = NIL THEN resolvedType := NIL
  1436. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1437. END;
  1438. resolved := resolvedType;
  1439. resolvedType := prev;
  1440. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1441. RETURN resolved
  1442. END ResolveType;
  1443. (*** compatibility rules ***)
  1444. (**
  1445. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1446. **)
  1447. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1448. VAR result: SyntaxTree.Type;
  1449. BEGIN
  1450. result := SyntaxTree.invalidType;
  1451. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1452. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1453. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1454. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1455. ELSE result := type.resolved
  1456. END;
  1457. RETURN result
  1458. END RegularType;
  1459. (** returns signature compatibility of procedure types this and to
  1460. - if not compatible then error is reported
  1461. - compatibility means type equality
  1462. **)
  1463. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1464. VAR result: BOOLEAN;
  1465. BEGIN
  1466. result := SameType(to,this);
  1467. IF ~result THEN
  1468. Error(position,Diagnostics.Invalid,"signature incompatible");
  1469. IF VerboseErrorMessage THEN
  1470. Printout.Info("this",this);
  1471. Printout.Info("to",to);
  1472. END;
  1473. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1474. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1475. END;
  1476. RETURN result
  1477. END SignatureCompatible;
  1478. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1479. - for var parameters compatibility means same type except for
  1480. - formal is of open array of system byte
  1481. - formal is of record type
  1482. - formal is of open array type
  1483. - formal is of open math array type
  1484. - for value parameters compatibllity means assignment compatibility except for
  1485. - formal is of open array type
  1486. if compatible the return true else report error and return false
  1487. **)
  1488. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1489. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1490. BEGIN
  1491. formalType := RegularType(formal.position,formal.type);
  1492. actualType := RegularType(actual.position,actual.type);
  1493. error := FALSE;
  1494. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1496. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1497. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1498. END;
  1499. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1500. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1501. ELSIF ~IsVariable(actual) THEN
  1502. result := FALSE; error := TRUE;
  1503. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1504. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1505. ELSE
  1506. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1507. END;
  1508. IF VerboseErrorMessage THEN
  1509. Printout.Info("actual",actual);
  1510. Printout.Info("formal",formal);
  1511. END;
  1512. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1513. result := CompatibleTo(system,actualType,formalType);
  1514. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1515. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1516. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1517. IF IsArrayStructuredObjectType(actualType) THEN
  1518. actualType := MathArrayStructureOfType(actualType)
  1519. END;
  1520. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1521. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1522. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1523. END;
  1524. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1525. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1526. END;
  1527. ELSE
  1528. result := SameType(actualType,formalType)
  1529. END
  1530. ELSE
  1531. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1532. actualType := system.characterType;
  1533. END;
  1534. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1535. result := TRUE; (* special rule for WINAPI parameters *)
  1536. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1537. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1538. ELSE
  1539. result := CompatibleTo(system,actualType,formalType);
  1540. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1541. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1542. END;
  1543. END;
  1544. END;
  1545. IF ~result & ~error THEN
  1546. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1547. IF VerboseErrorMessage THEN
  1548. Printout.Info("actual",actual);
  1549. Printout.Info("formal",formal);
  1550. END;
  1551. END;
  1552. RETURN result
  1553. END ParameterCompatible;
  1554. (** check compatibility for expressions of the form left := right
  1555. - if compatible then return true else error report and return false
  1556. - check if left is variable
  1557. - check compatiblity
  1558. **)
  1559. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1560. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1561. BEGIN
  1562. result := FALSE;
  1563. leftType := RegularType(left.position,left.type);
  1564. rightType := RegularType(right.position,right.type);
  1565. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1566. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1567. END;
  1568. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1569. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1570. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1571. (* error already handled *)
  1572. result := TRUE;
  1573. ELSIF ~IsVariable(left) THEN
  1574. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1575. IF VerboseErrorMessage THEN
  1576. Printout.Info("left",left);
  1577. Printout.Info("right",right);
  1578. END;
  1579. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1580. result := TRUE;
  1581. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1582. result := TRUE
  1583. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1584. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1585. IF VerboseErrorMessage THEN
  1586. Printout.Info("left",left);
  1587. Printout.Info("right",right);
  1588. END;
  1589. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1590. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1591. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1592. ELSE
  1593. result := TRUE
  1594. END;
  1595. RETURN result
  1596. END AssignmentCompatible;
  1597. (*** values ***)
  1598. (** check and resolve integer value **)
  1599. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1600. VAR hugeint: HUGEINT;
  1601. BEGIN
  1602. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1603. value.SetType(Global.GetIntegerType(system,hugeint));
  1604. resolvedExpression := value
  1605. END VisitIntegerValue;
  1606. (** check and resolve real value **)
  1607. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1608. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1609. BEGIN
  1610. subtype := value(SyntaxTree.RealValue).subtype;
  1611. IF subtype = Scanner.Real THEN
  1612. type := system.realType
  1613. ELSIF subtype = Scanner.Longreal THEN
  1614. type := system.longrealType
  1615. ELSE
  1616. HALT(100)
  1617. END;
  1618. value.SetType(type);
  1619. resolvedExpression := value
  1620. END VisitRealValue;
  1621. (** check and resolve complex value **)
  1622. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1623. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1624. BEGIN
  1625. subtype := value(SyntaxTree.ComplexValue).subtype;
  1626. IF subtype = Scanner.Real THEN
  1627. type := system.complexType
  1628. ELSIF subtype = Scanner.Longreal THEN
  1629. type := system.longcomplexType
  1630. ELSE
  1631. HALT(100)
  1632. END;
  1633. value.SetType(type);
  1634. resolvedExpression := value
  1635. END VisitComplexValue;
  1636. (** check and resolve set value **)
  1637. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1638. BEGIN
  1639. value.SetType(system.setType);
  1640. resolvedExpression := value
  1641. END VisitSetValue;
  1642. (** check and resolve set value **)
  1643. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1644. BEGIN
  1645. value.SetType(SyntaxTree.invalidType);
  1646. resolvedExpression := value
  1647. END VisitMathArrayValue;
  1648. (** check and resolve boolean value **)
  1649. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1650. BEGIN
  1651. value.SetType(system.booleanType);
  1652. resolvedExpression := value
  1653. END VisitBooleanValue;
  1654. (** check and resolve string value **)
  1655. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1656. BEGIN
  1657. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1658. resolvedExpression := value
  1659. END VisitStringValue;
  1660. (** check and resolve character value **)
  1661. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1662. BEGIN
  1663. value.SetType(system.characterType);
  1664. resolvedExpression := value
  1665. END VisitCharacterValue;
  1666. (** check and resolve nil value **)
  1667. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1668. BEGIN
  1669. value.SetType(system.nilType);
  1670. resolvedExpression := value
  1671. END VisitNilValue;
  1672. (** check and resolve enumerator value **)
  1673. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1674. BEGIN
  1675. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1676. ASSERT(value.type # NIL);
  1677. resolvedExpression := value
  1678. END VisitEnumerationValue;
  1679. (*** expressions ***)
  1680. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1681. - check all elements on integer type
  1682. - if element range is constant, then check lower and upper bound
  1683. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1684. if an error occurs then report error and return invalidExpression
  1685. **)
  1686. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1687. VAR
  1688. i: LONGINT;
  1689. element: SyntaxTree.Expression;
  1690. constant: BOOLEAN;
  1691. elements: SyntaxTree.ExpressionList;
  1692. s: SET;
  1693. result: SyntaxTree.Expression;
  1694. value: SyntaxTree.Value;
  1695. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1696. VAR
  1697. left, right: SyntaxTree.Expression;
  1698. elementResult: SyntaxTree.Expression;
  1699. leftInteger, rightInteger, temp: LONGINT;
  1700. BEGIN
  1701. (* set context of range *)
  1702. IF element IS SyntaxTree.RangeExpression THEN
  1703. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1704. END;
  1705. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1706. IF elementResult = SyntaxTree.invalidExpression THEN
  1707. (* error already reported *)
  1708. constant := FALSE
  1709. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1710. (* the element is a range expression *)
  1711. (* extract left and right hand side of range *)
  1712. left := elementResult(SyntaxTree.RangeExpression).first;
  1713. right := elementResult(SyntaxTree.RangeExpression).last;
  1714. (* guaranteed by VisitRangeExpression: *)
  1715. ASSERT((left # NIL) & (right # NIL));
  1716. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1717. ELSE
  1718. (* the element is not a range expression *)
  1719. (* check type and add conversion if needed *)
  1720. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1721. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1722. ELSE
  1723. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1724. elementResult := SyntaxTree.invalidExpression;
  1725. constant := FALSE
  1726. END;
  1727. left := elementResult;
  1728. right := elementResult
  1729. END;
  1730. IF elementResult # SyntaxTree.invalidExpression THEN
  1731. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1732. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1733. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1734. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1735. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1736. END
  1737. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1738. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1739. ELSE
  1740. IF leftInteger > rightInteger THEN
  1741. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1742. END;
  1743. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1744. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1745. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1746. extends the range x..31 to x..63 !!!!!! *)
  1747. s := s + {leftInteger..rightInteger};
  1748. (* same but more explicit:
  1749. WHILE (leftInteger <= rightInteger) DO
  1750. INCL(s,leftInteger);
  1751. INC(leftInteger);
  1752. END;
  1753. *)
  1754. END;
  1755. ELSE
  1756. constant := FALSE;
  1757. END
  1758. END;
  1759. RETURN elementResult
  1760. END CheckElement;
  1761. BEGIN
  1762. result := set; constant := TRUE; s := {}; elements := set.elements;
  1763. IF elements # NIL THEN
  1764. FOR i := 0 TO elements.Length()-1 DO
  1765. element := elements.GetExpression(i);
  1766. element := CheckElement(element);
  1767. IF element = SyntaxTree.invalidExpression THEN
  1768. result := SyntaxTree.invalidExpression
  1769. END;
  1770. elements.SetExpression(i,element);
  1771. END;
  1772. END;
  1773. IF constant THEN
  1774. value := SyntaxTree.NewSetValue(set.position,s);
  1775. value.SetType(system.setType);
  1776. result.SetResolved(value);
  1777. END;
  1778. (* optimization possible
  1779. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1780. left this to the programmer...
  1781. *)
  1782. result.SetType(system.setType);
  1783. resolvedExpression := result;
  1784. END VisitSet;
  1785. (*
  1786. old variant: quite generic but needs better conversion handling, do this?
  1787. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1788. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1789. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1790. BEGIN
  1791. type := NIL;
  1792. numberElements := x.elements.Length();
  1793. FOR i := 0 TO numberElements-1 DO
  1794. expression := x.elements.GetExpression(i);
  1795. position := expression.position;
  1796. expression := ResolveExpression(x.elements.GetExpression(i));
  1797. x.elements.SetExpression(i,de);
  1798. IF type = NIL THEN
  1799. type := expression.type;
  1800. ELSIF CompatibleTo(system,expression.type,type) THEN
  1801. (* ok *)
  1802. ELSIF CompatibleTo(system,type,expression.type) THEN
  1803. type := expression.type
  1804. ELSE
  1805. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1806. type := SyntaxTree.invalidType;
  1807. END;
  1808. END;
  1809. isValue := TRUE;
  1810. FOR i := 0 TO numberElements-1 DO
  1811. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1812. x.elements.SetExpression(i,expression);
  1813. isValue := isValue & (expression.resolved # NIL);
  1814. END;
  1815. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1816. arrayType.SetArrayBase(type);
  1817. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1818. IF isValue THEN
  1819. value := SyntaxTree.NewMathArrayValue(position);
  1820. value.SetElements(x.elements);
  1821. x.SetResolved(value);
  1822. END;
  1823. x.SetType(arrayType);
  1824. resolvedExpression := x;
  1825. END VisitMathArrayExpression;
  1826. *)
  1827. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1828. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1829. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1830. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1831. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1832. BEGIN
  1833. numberElements := x.elements.Length();
  1834. FOR i := 0 TO numberElements-1 DO
  1835. expression := x.elements.GetExpression(i);
  1836. IF expression IS SyntaxTree.MathArrayExpression THEN
  1837. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1838. ELSE
  1839. position := expression.position;
  1840. expression := ResolveExpression(x.elements.GetExpression(i));
  1841. x.elements.SetExpression(i,expression);
  1842. IF type = NIL THEN
  1843. type := expression.type;
  1844. ELSIF CompatibleTo(system,expression.type,type) THEN
  1845. (* ok *)
  1846. ELSIF CompatibleTo(system,type,expression.type) THEN
  1847. type := expression.type
  1848. ELSE
  1849. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1850. type := SyntaxTree.invalidType;
  1851. END;
  1852. END;
  1853. END;
  1854. END RecursivelyFindType;
  1855. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1856. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1857. BEGIN
  1858. numberElements := x.elements.Length();
  1859. FOR i := 0 TO numberElements-1 DO
  1860. expression := x.elements.GetExpression(i);
  1861. IF expression IS SyntaxTree.MathArrayExpression THEN
  1862. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1863. ELSE
  1864. position := expression.position;
  1865. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1866. x.elements.SetExpression(i,expression);
  1867. isValue := isValue & (expression.resolved # NIL);
  1868. END;
  1869. END;
  1870. END RecursivelySetExpression;
  1871. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1872. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1873. arrayType: SyntaxTree.MathArrayType;
  1874. BEGIN
  1875. numberElements := x.elements.Length();
  1876. baseType := NIL;
  1877. gsize := 0;
  1878. FOR i := 0 TO numberElements-1 DO
  1879. expression := x.elements.GetExpression(i);
  1880. IF expression IS SyntaxTree.MathArrayExpression THEN
  1881. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1882. IF i=0 THEN
  1883. gsize := size;
  1884. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1885. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1886. ELSE expression.SetType(baseType)
  1887. END;
  1888. ELSIF baseType = NIL THEN baseType := type;
  1889. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1890. END;
  1891. END;
  1892. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1893. arrayType.SetArrayBase(baseType);
  1894. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1895. RETURN ResolveType(arrayType);
  1896. END RecursivelySetType;
  1897. BEGIN
  1898. type := NIL;
  1899. RecursivelyFindType(x);
  1900. isValue := TRUE;
  1901. RecursivelySetExpression(x);
  1902. arrayType := RecursivelySetType(x);
  1903. x.SetType(arrayType);
  1904. IF isValue THEN
  1905. value := SyntaxTree.NewMathArrayValue(x.position);
  1906. value.SetArray(x);
  1907. x.SetResolved(value);
  1908. value.SetType(arrayType);
  1909. END;
  1910. x.SetType(arrayType);
  1911. resolvedExpression := x;
  1912. END VisitMathArrayExpression;
  1913. (** check and resolve unary expression **)
  1914. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1915. VAR
  1916. left: SyntaxTree.Expression;
  1917. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1918. bool: BOOLEAN;
  1919. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1920. value: SyntaxTree.Value;
  1921. BEGIN
  1922. type := SyntaxTree.invalidType;
  1923. left := ResolveExpression(unaryExpression.left);
  1924. unaryExpression.SetLeft(left);
  1925. operator := unaryExpression.operator;
  1926. result := unaryExpression;
  1927. IF ~system.operatorDefined[operator] THEN
  1928. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1929. RETURN
  1930. ELSIF left.type = NIL THEN
  1931. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1932. resolvedExpression := SyntaxTree.invalidExpression;
  1933. RETURN
  1934. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1935. RETURN
  1936. END;
  1937. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1938. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1939. END;
  1940. IF operatorCall # NIL THEN
  1941. result := operatorCall;
  1942. type := operatorCall.type;
  1943. (* admissible operators
  1944. Minus number, set
  1945. Not boolean
  1946. *)
  1947. ELSE
  1948. CASE unaryExpression.operator OF
  1949. |Scanner.Minus:
  1950. IF IsIntegerType(left.type.resolved) THEN
  1951. IF left.resolved # NIL THEN
  1952. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1953. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1954. result.SetResolved(value);
  1955. type := Global.GetIntegerType(system,int);
  1956. value.SetType(type);
  1957. ELSE
  1958. type := left.type
  1959. END
  1960. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1961. IF IsRealValue(left,real) THEN
  1962. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1963. result.SetResolved(value);
  1964. type := left.type;
  1965. value.SetType(type);
  1966. ELSE
  1967. type := left.type;
  1968. END;
  1969. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1970. IF IsSetValue(left,set) THEN
  1971. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1972. result.SetResolved(value);
  1973. type := left.type;
  1974. value.SetType(type);
  1975. ELSE
  1976. type := left.type;
  1977. END;
  1978. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1979. IF IsComplexValue(left, real, imaginary) THEN
  1980. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1981. result.SetResolved(value);
  1982. type := left.type;
  1983. value.SetType(type);
  1984. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1985. ELSE
  1986. type := left.type;
  1987. END
  1988. ELSE
  1989. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1990. END;
  1991. |Scanner.Not:
  1992. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1993. IF IsBooleanValue(left,bool) THEN
  1994. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1995. result.SetResolved(value);
  1996. type := system.booleanType;
  1997. value.SetType(type);
  1998. ELSE
  1999. type := system.booleanType;
  2000. END;
  2001. ELSE
  2002. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2003. END;
  2004. |Scanner.Plus:
  2005. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2006. result := left; type := left.type;
  2007. ELSE
  2008. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2009. END;
  2010. (* ADDRESS OF *)
  2011. |Scanner.Address:
  2012. IF HasAddress(left) THEN
  2013. type := system.addressType;
  2014. ELSE
  2015. type := SyntaxTree.invalidType;
  2016. Error(left.position,Diagnostics.Invalid,"has no address");
  2017. Printout.Info("par", left);
  2018. END;
  2019. (* SIZE OF *)
  2020. |Scanner.Size:
  2021. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2022. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2023. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2024. value := SyntaxTree.NewIntegerValue(left.position, int);
  2025. result.SetResolved(value);
  2026. type := Global.GetIntegerType(system,int);
  2027. value.SetType(type)
  2028. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2029. ELSE
  2030. (* for variables, system sizeof could represent the physically occupied size
  2031. determined via the type descriptor, implement that ? *)
  2032. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2033. END
  2034. (* ALIAS OF *)
  2035. |Scanner.Alias:
  2036. type := left.type.resolved;
  2037. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2038. type := SyntaxTree.invalidType;
  2039. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2040. END;
  2041. ELSE
  2042. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2043. END;
  2044. END;
  2045. result.SetType(type);
  2046. resolvedExpression := result
  2047. END VisitUnaryExpression;
  2048. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2049. VAR
  2050. result: SyntaxTree.Expression;
  2051. array: SyntaxTree.MathArrayExpression;
  2052. value: SyntaxTree.MathArrayValue;
  2053. isValue: BOOLEAN;
  2054. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2055. BEGIN
  2056. type := type.resolved;
  2057. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2058. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2059. END;
  2060. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2061. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2062. END;
  2063. RETURN type
  2064. END BaseType;
  2065. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2066. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2067. BEGIN
  2068. numberElements := x.elements.Length();
  2069. FOR i := 0 TO numberElements-1 DO
  2070. expression := x.elements.GetExpression(i);
  2071. IF expression IS SyntaxTree.MathArrayExpression THEN
  2072. array := SyntaxTree.NewMathArrayExpression(position);
  2073. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2074. expression := array;
  2075. ELSE
  2076. position := expression.position;
  2077. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2078. isValue := isValue & (expression.resolved # NIL);
  2079. END;
  2080. to.elements.AddExpression(expression);
  2081. END;
  2082. END RecursivelyConvert;
  2083. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2084. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2085. arrayType: SyntaxTree.MathArrayType;
  2086. BEGIN
  2087. numberElements := x.elements.Length();
  2088. baseType := NIL;
  2089. gsize := 0;
  2090. FOR i := 0 TO numberElements-1 DO
  2091. expression := x.elements.GetExpression(i);
  2092. IF expression IS SyntaxTree.MathArrayExpression THEN
  2093. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2094. IF i=0 THEN
  2095. gsize := size;
  2096. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2097. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2098. ELSE expression.SetType(baseType)
  2099. END;
  2100. ELSIF baseType = NIL THEN baseType := type;
  2101. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2102. END;
  2103. END;
  2104. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2105. arrayType.SetArrayBase(baseType);
  2106. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2107. RETURN ResolveType(arrayType);
  2108. END RecursivelySetType;
  2109. BEGIN
  2110. result := SyntaxTree.invalidExpression;
  2111. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2112. result := expression (* do not convert *)
  2113. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2114. isValue := TRUE;
  2115. type := BaseType(type);
  2116. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2117. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2118. value := SyntaxTree.NewMathArrayValue(array.position);
  2119. value.SetArray(array);
  2120. value.SetType(RecursivelySetType(array));
  2121. result := value;
  2122. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2123. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2124. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2125. IF result = NIL THEN
  2126. result := SyntaxTree.invalidExpression;
  2127. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2128. IF VerboseErrorMessage THEN
  2129. Printout.Info("expression",expression);
  2130. Printout.Info("type",type);
  2131. END;
  2132. END;
  2133. END;
  2134. RETURN result
  2135. END MathArrayConversion;
  2136. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2137. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2138. BEGIN
  2139. result := expression; type := type.resolved;
  2140. IF (expression IS SyntaxTree.IntegerValue) THEN
  2141. int := expression(SyntaxTree.IntegerValue).hvalue;
  2142. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2143. int := Global.ConvertSigned(int,system.SizeOf(type));
  2144. result := SyntaxTree.NewIntegerValue(position,int);
  2145. result.SetType(type);
  2146. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2147. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2148. result := SyntaxTree.NewIntegerValue(position,int);
  2149. result.SetType(type);
  2150. ELSIF (type IS SyntaxTree.FloatType) THEN
  2151. result := SyntaxTree.NewRealValue(expression.position,int);
  2152. result.SetType(type);
  2153. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2154. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2155. result.SetType(type);
  2156. ELSIF (type IS SyntaxTree.SetType) THEN
  2157. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2158. result.SetType(type);
  2159. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2160. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2161. result.SetType(type);
  2162. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2163. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2164. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2165. END;
  2166. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2167. result.SetType(type);
  2168. ELSIF (type IS SyntaxTree.PortType) THEN
  2169. result := ConvertValue(position, expression, system.integerType);
  2170. ELSE
  2171. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2172. result := SyntaxTree.invalidExpression;
  2173. IF VerboseErrorMessage THEN
  2174. Printout.Info("expression",expression);
  2175. Printout.Info("type",type);
  2176. END;
  2177. END;
  2178. ELSIF IsRealValue(expression,real) THEN
  2179. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2180. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2181. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2182. result.SetType(type);
  2183. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2184. int := ENTIERH(real);
  2185. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2186. result.SetType(type);
  2187. ELSIF (type IS SyntaxTree.FloatType) THEN
  2188. result := SyntaxTree.NewRealValue(position,real);
  2189. result.SetType(type);
  2190. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2191. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2192. result.SetType(type);
  2193. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2194. ELSIF (type IS SyntaxTree.PortType) THEN
  2195. result := ConvertValue(position, expression, system.integerType);
  2196. ELSE
  2197. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2198. result := SyntaxTree.invalidExpression;
  2199. END
  2200. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2201. IF (type IS SyntaxTree.ComplexType) THEN
  2202. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2203. result.SetType(type);
  2204. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2205. ELSE
  2206. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2207. result := SyntaxTree.invalidExpression;
  2208. END
  2209. ELSIF IsSetValue(expression,set) THEN
  2210. IF (type IS SyntaxTree.IntegerType) THEN
  2211. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2214. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.PortType) THEN
  2217. result := ConvertValue(position, expression, system.integerType);
  2218. ELSE
  2219. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2220. result := SyntaxTree.invalidExpression;
  2221. END;
  2222. ELSIF IsStringValue(expression,string) THEN
  2223. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2224. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2225. result.SetType(type);
  2226. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2227. ELSE
  2228. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2229. result := SyntaxTree.invalidExpression;
  2230. END;
  2231. ELSIF IsCharacterValue(expression,char) THEN
  2232. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2233. string[0] := char; string[1] := 0X;
  2234. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2235. result := SyntaxTree.NewStringValue(expression.position,string);
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.ByteType) THEN
  2238. (* do not simply set the new type as this could invalidate types of constants *)
  2239. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2240. result.SetType(type)
  2241. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2242. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.SetType) THEN
  2245. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2248. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2249. result.SetType(type);
  2250. ELSIF (type IS SyntaxTree.PortType) THEN
  2251. result := ConvertValue(position, expression, system.integerType);
  2252. ELSE
  2253. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2254. result := SyntaxTree.invalidExpression;
  2255. END;
  2256. ELSIF expression IS SyntaxTree.NilValue THEN
  2257. IF type IS SyntaxTree.AddressType THEN
  2258. result := SyntaxTree.NewIntegerValue(position,0);
  2259. result.SetType(type);
  2260. ELSE
  2261. result := expression;
  2262. END;
  2263. (* nothing to be done *)
  2264. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2265. result := MathArrayConversion(position, expression,type);
  2266. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2267. int := expression(SyntaxTree.EnumerationValue).value;
  2268. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2269. int := Global.ConvertSigned(int,system.SizeOf(type));
  2270. result := SyntaxTree.NewIntegerValue(position,int);
  2271. result.SetType(type);
  2272. ELSE
  2273. result := expression;
  2274. END;
  2275. (* nothing to be done *)
  2276. ELSE
  2277. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2278. IF VerboseErrorMessage THEN
  2279. Printout.Info("expression",expression);
  2280. Printout.Info("type",type);
  2281. END;
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. RETURN result
  2285. END ConvertValue;
  2286. (**
  2287. return a conversion of an expression to a given type
  2288. - if expression is already of same type then return expression
  2289. - if incompatible conversion then report error and return invalidExpression
  2290. **)
  2291. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2292. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2293. BEGIN
  2294. type := type.resolved;
  2295. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2296. result := expression;
  2297. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2298. ELSIF expression = NIL THEN (* NIL expression *)
  2299. ELSIF expression.type = NIL THEN
  2300. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2301. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2302. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2303. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2304. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2305. ELSIF expression.resolved # NIL THEN (* value *)
  2306. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2307. IF value IS SyntaxTree.Value THEN
  2308. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2309. result.SetResolved(value(SyntaxTree.Value));
  2310. result.SetType(value.type);
  2311. ELSE
  2312. result := value
  2313. END;
  2314. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2315. expressionList := SyntaxTree.NewExpressionList();
  2316. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2317. typeDeclaration.SetDeclaredType(type);
  2318. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2319. typeSymbol.SetType(typeDeclaration.type);
  2320. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2321. expressionList.AddExpression(expression);
  2322. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2323. result.SetType(type);
  2324. ELSIF IsArrayStructuredObjectType(type) THEN
  2325. (* no type can be converted to an array-structured object type *)
  2326. HALT(100)
  2327. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2328. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2329. result := MathArrayConversion(position, expression,type);
  2330. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2331. expression := ConvertToMathArray(expression);
  2332. type := MathArrayStructureOfType(type);
  2333. result := MathArrayConversion(position, expression, type)
  2334. ELSE
  2335. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2336. END;
  2337. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2338. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2339. OR ~(type IS SyntaxTree.ArrayType) THEN
  2340. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2341. END;
  2342. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2343. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2344. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2345. (*skip, no conversion*)
  2346. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2347. (* skip, no conversion *)
  2348. ELSE
  2349. ASSERT(~(type IS SyntaxTree.RangeType));
  2350. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2351. ASSERT(type # NIL);
  2352. END;
  2353. RETURN result
  2354. END NewConversion;
  2355. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2356. BEGIN
  2357. IF CompatibleTo(system,expression.type, type) THEN
  2358. RETURN NewConversion(position, expression, type, NIL);
  2359. ELSE
  2360. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2361. RETURN SyntaxTree.invalidExpression
  2362. END;
  2363. END CompatibleConversion;
  2364. (**
  2365. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2366. **)
  2367. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2368. VAR leftType,rightType: SyntaxTree.Type;
  2369. BEGIN
  2370. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2371. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2372. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2373. (* error already handled *)
  2374. ELSE
  2375. leftType := left.type.resolved; rightType := right.type.resolved;
  2376. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2377. right := NewConversion(right.position, right, leftType, NIL);
  2378. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2379. left := NewConversion(left.position,left,rightType,NIL);
  2380. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2381. right := NewConversion(right.position, right, leftType, NIL);
  2382. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2383. left := NewConversion(left.position,left,rightType,NIL);
  2384. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2385. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2386. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2387. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2388. ELSIF
  2389. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2390. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2391. (* must be the case LONGREAL / COMPLEX ) *)
  2392. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2393. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2394. ELSE
  2395. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2396. END;
  2397. END;
  2398. END ConvertOperands;
  2399. (** find and return best operator matching to parameter list (nil, if none)
  2400. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2401. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2402. **)
  2403. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2404. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2405. identifier: SyntaxTree.Identifier;
  2406. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2407. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2408. BEGIN
  2409. operator := scope.firstOperator;
  2410. WHILE(operator # NIL) DO
  2411. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2412. procedureType := operator.type(SyntaxTree.ProcedureType);
  2413. distance := Distance(system, procedureType,actualParameters);
  2414. IF (distance < Infinity) THEN
  2415. IF returnType # NIL THEN
  2416. IF procedureType.returnType = NIL THEN
  2417. distance := Infinity
  2418. ELSE
  2419. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2420. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2421. END;
  2422. END;
  2423. END;
  2424. (*
  2425. IF distance < Infinity THEN
  2426. TRACE(distance, operator);
  2427. Printout.Info("potential operator",operator);
  2428. ELSE
  2429. Printout.Info("operator not possible",operator);
  2430. END;
  2431. *)
  2432. IF distance < bestDistance THEN
  2433. bestDistance := distance;
  2434. bestOperator := operator;
  2435. END;
  2436. END;
  2437. operator := operator.nextOperator;
  2438. END;
  2439. (*
  2440. Printout.Info("taken operator",bestOperator);
  2441. *)
  2442. END FindInScope;
  2443. BEGIN
  2444. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2445. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2446. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2447. import := currentScope.ownerModule.moduleScope.firstImport;
  2448. WHILE (bestDistance > 0) & (import # NIL) DO
  2449. IF import.module # NIL THEN
  2450. identifier := Global.GetIdentifier(operator,import.module.case);
  2451. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2452. END;
  2453. import := import.nextImport;
  2454. END;
  2455. RETURN bestOperator
  2456. END FindOperator;
  2457. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2458. BEGIN
  2459. currentScope := scope;
  2460. END SetCurrentScope;
  2461. (**
  2462. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2463. - handle LEN and DIM operator for array-structured object types
  2464. - find operator, if found then
  2465. - if in other module then add import designator
  2466. - create symbol designator for operator
  2467. - if error then return invalidExpression, if no operator then return NIL
  2468. **)
  2469. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2470. VAR
  2471. operator: SyntaxTree.Operator;
  2472. import: SyntaxTree.Import;
  2473. expression, result: SyntaxTree.Expression;
  2474. designator: SyntaxTree.Designator;
  2475. actualParameters, tempList: SyntaxTree.ExpressionList;
  2476. recordType: SyntaxTree.RecordType;
  2477. castReturnType : SyntaxTree.MathArrayType;
  2478. BEGIN
  2479. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2480. result := SyntaxTree.invalidExpression
  2481. ELSIF leftExpression = NIL THEN
  2482. result := NIL
  2483. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2484. (* LEN or DIM operator on array-structured object type *)
  2485. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2486. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2487. IF recordType.arrayAccessOperators.len = NIL THEN
  2488. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2489. result := SyntaxTree.invalidExpression
  2490. ELSE
  2491. ASSERT(leftExpression IS SyntaxTree.Designator);
  2492. designator := leftExpression(SyntaxTree.Designator);
  2493. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2494. ASSERT(expression IS SyntaxTree.Designator);
  2495. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2496. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2497. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2498. result := designator
  2499. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2500. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2501. tempList := SyntaxTree.NewExpressionList();
  2502. tempList.AddExpression(rightExpression);
  2503. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2504. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2505. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2506. tempList := SyntaxTree.NewExpressionList();
  2507. tempList.AddExpression(designator);
  2508. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2509. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2510. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2511. END
  2512. END;
  2513. ELSE
  2514. (* import OCArrayBase if needed *)
  2515. IF ~arrayBaseImported THEN
  2516. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2517. (* operators on complex numbers *)
  2518. ImportModule(Global.ArrayBaseName,position);
  2519. arrayBaseImported := TRUE;
  2520. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2521. IF op = Global.Dim THEN
  2522. (* not existing in OCArrayBase *)
  2523. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2524. (* not existing in OCArrayBase *)
  2525. ELSE
  2526. ImportModule(Global.ArrayBaseName,position);
  2527. arrayBaseImported := TRUE;
  2528. END
  2529. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2530. ImportModule(Global.ArrayBaseName,position);
  2531. arrayBaseImported := TRUE
  2532. END;
  2533. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2534. (* LEN(RANGE) *)
  2535. ImportModule(Global.ArrayBaseName,position);
  2536. arrayBaseImported := TRUE;
  2537. END;
  2538. END;
  2539. actualParameters := SyntaxTree.NewExpressionList();
  2540. actualParameters.AddExpression(leftExpression);
  2541. IF rightExpression # NIL THEN
  2542. actualParameters.AddExpression(rightExpression)
  2543. END;
  2544. operator := FindOperator(system,op,actualParameters,resultType);
  2545. IF operator # NIL THEN
  2546. designator := NIL;
  2547. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2548. import := currentScope.ownerModule.moduleScope.firstImport;
  2549. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2550. import := import.nextImport;
  2551. END;
  2552. expression := NewSymbolDesignator(position,NIL,import);
  2553. designator := expression(SyntaxTree.Designator);
  2554. END;
  2555. expression := NewSymbolDesignator(position,designator,operator);
  2556. designator := expression(SyntaxTree.Designator);
  2557. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2558. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2559. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2560. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2561. result.SetType(castReturnType);
  2562. END;
  2563. ELSE
  2564. result := NIL;
  2565. END;
  2566. END;
  2567. RETURN result
  2568. END NewOperatorCall;
  2569. (** check and resolve binary expression **)
  2570. (*! clean up *)
  2571. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2572. VAR left,right,result: SyntaxTree.Expression;
  2573. leftType, rightType: SyntaxTree.Type;
  2574. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2575. cl,cr: CHAR;
  2576. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2577. type: SyntaxTree.Type;
  2578. value: SyntaxTree.Value;
  2579. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2580. integerConstantFolding: BOOLEAN;
  2581. list: SyntaxTree.ExpressionList;
  2582. PROCEDURE NewBool(v: BOOLEAN);
  2583. BEGIN
  2584. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2585. value.SetType(system.booleanType);
  2586. result.SetResolved(value);
  2587. type := system.booleanType
  2588. END NewBool;
  2589. PROCEDURE NewSet(v: SET);
  2590. BEGIN
  2591. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2592. value.SetType(system.setType);
  2593. result.SetResolved(value);
  2594. type := system.setType;
  2595. END NewSet;
  2596. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2597. BEGIN
  2598. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2599. (* type cast to "larger" type only if the value is still in the range *)
  2600. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2601. value.SetType(t);
  2602. END;
  2603. result.SetResolved(value);
  2604. type := value.type;
  2605. END NewInteger;
  2606. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2607. BEGIN
  2608. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2609. value.SetType(t);
  2610. result.SetResolved(value);
  2611. type := t;
  2612. END NewReal;
  2613. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2614. BEGIN
  2615. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2616. value.SetType(t);
  2617. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2618. result.SetResolved(value);
  2619. type := t;
  2620. END NewComplex;
  2621. BEGIN
  2622. type := SyntaxTree.invalidType;
  2623. left := ResolveExpression(binaryExpression.left);
  2624. right := ResolveExpression(binaryExpression.right);
  2625. binaryExpression.SetLeft(left);
  2626. binaryExpression.SetRight(right);
  2627. result := binaryExpression;
  2628. operator := binaryExpression.operator;
  2629. IF ~system.operatorDefined[operator] THEN
  2630. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2631. result := SyntaxTree.invalidExpression;
  2632. RETURN
  2633. END;
  2634. IF left.type = NIL THEN
  2635. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2636. result := SyntaxTree.invalidExpression;
  2637. RETURN;
  2638. END;
  2639. IF right.type = NIL THEN
  2640. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2641. result := SyntaxTree.invalidExpression;
  2642. RETURN;
  2643. END;
  2644. leftType := left.type.resolved; rightType := right.type.resolved;
  2645. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2646. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2647. END;
  2648. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2649. list := SyntaxTree.NewExpressionList();
  2650. list.AddExpression(right);
  2651. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2652. END;
  2653. IF operatorCall # NIL THEN
  2654. result := operatorCall;
  2655. type := operatorCall.type;
  2656. (* admissible operators:
  2657. Times, Plus, Minus numeric numeric numeric
  2658. set set set
  2659. Slash numeric numeric real /complex
  2660. set set set
  2661. Div , Mod integer integer integer
  2662. And, Or bool bool bool
  2663. Equal, Unequal basic basic bool
  2664. pointer pointer bool
  2665. object object bool
  2666. record record bool
  2667. string string bool
  2668. enumerator enumerator bool
  2669. Less, LessEqual,
  2670. Greater, GreaterEqual integer/real integer/real bool
  2671. enumerator enumerator bool
  2672. In integer set bool
  2673. Is pointer type bool
  2674. object type bool
  2675. record type bool
  2676. Upto: special abbreviation for a..b
  2677. *)
  2678. ELSIF (left.type = NIL) THEN
  2679. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2680. D.Str("nil type in "); D.Type(left); D.Ln;
  2681. result := SyntaxTree.invalidExpression;
  2682. ELSIF (right.type = NIL) THEN
  2683. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2684. result := SyntaxTree.invalidExpression;
  2685. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2686. result := SyntaxTree.invalidExpression;
  2687. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2688. HALT(100);
  2689. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2690. type := system.booleanType;
  2691. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2692. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2693. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2694. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2695. IF VerboseErrorMessage THEN
  2696. Printout.Info("left",left);
  2697. Printout.Info("right",right);
  2698. END;
  2699. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2700. NewBool(TRUE)
  2701. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2702. NewBool(TRUE);
  2703. ELSIF IsUnextensibleRecord(left) THEN
  2704. NewBool(FALSE)
  2705. END
  2706. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2707. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2708. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2709. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2710. ELSIF operator = Scanner.In THEN (* left IN right *)
  2711. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2712. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2713. NewBool(il IN sr);
  2714. ELSE
  2715. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2716. left := NewConversion(left.position, left, system.longintType,NIL);
  2717. binaryExpression.SetLeft(left)
  2718. END;
  2719. type := system.booleanType;
  2720. END
  2721. ELSE
  2722. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2723. END
  2724. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2725. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2726. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2727. END;
  2728. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2729. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2730. END
  2731. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2732. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2733. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2734. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2735. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2736. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2737. ConvertOperands(left, right);
  2738. binaryExpression.SetLeft(left);
  2739. binaryExpression.SetRight(right);
  2740. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2741. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2742. END;
  2743. type := system.booleanType;
  2744. ELSE
  2745. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2746. END
  2747. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2748. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2749. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2750. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2751. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2752. CASE operator OF
  2753. |Scanner.Equal: NewBool(strl^=strr^);
  2754. |Scanner.Unequal:NewBool(strl^#strr^);
  2755. |Scanner.Less: NewBool(strl^<strr^);
  2756. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2757. |Scanner.Greater: NewBool(strl^>strr^);
  2758. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2759. ELSE
  2760. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2761. END;
  2762. END;
  2763. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2764. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2765. type := system.booleanType
  2766. ELSE
  2767. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2768. END;
  2769. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2770. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2771. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2772. type := system.booleanType;
  2773. ELSE
  2774. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2775. END
  2776. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2777. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2778. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2779. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2780. type := system.booleanType
  2781. ELSE
  2782. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2783. END;
  2784. ELSE
  2785. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2786. END;
  2787. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2788. type := system.booleanType;
  2789. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2790. type := system.booleanType;
  2791. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2792. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2793. THEN
  2794. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2795. IF (leftType # rightType) THEN
  2796. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2797. ConvertOperands(left,right); (* operands must be of the same type here *)
  2798. END;
  2799. binaryExpression.SetLeft(left);
  2800. binaryExpression.SetRight(right);
  2801. leftType := left.type.resolved;
  2802. rightType := right.type.resolved;
  2803. END;
  2804. type := leftType;
  2805. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2806. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2807. IF VerboseErrorMessage THEN
  2808. Printout.Info("left",left);
  2809. Printout.Info("right",right);
  2810. END;
  2811. ELSIF IsIntegerType(leftType) THEN
  2812. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2813. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2814. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2815. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2816. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2817. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2818. END;
  2819. END;
  2820. (* constant folding *)
  2821. (* bootstrap64
  2822. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2823. CASE operator OF
  2824. |Scanner.Plus: NewInteger(il+ir,left.type);
  2825. |Scanner.Minus: NewInteger(il-ir,left.type);
  2826. |Scanner.Times: NewInteger(il*ir,left.type);
  2827. |Scanner.Slash:
  2828. IF ir # 0 THEN
  2829. NewReal(il/ir, system.realType);
  2830. END;
  2831. |Scanner.Mod:
  2832. IF ir > 0 THEN
  2833. NewInteger(il MOD ir,left.type);
  2834. END;
  2835. |Scanner.Div:
  2836. IF ir > 0 THEN
  2837. NewInteger(il DIV ir,left.type);
  2838. END;
  2839. |Scanner.Equal: NewBool(il=ir);
  2840. |Scanner.Unequal:NewBool(il#ir);
  2841. |Scanner.Less: NewBool(il<ir);
  2842. |Scanner.LessEqual: NewBool(il<=ir);
  2843. |Scanner.Greater: NewBool(il>ir);
  2844. |Scanner.GreaterEqual: NewBool(il>=ir);
  2845. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2846. END;
  2847. ELS*)
  2848. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2849. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2850. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2851. CASE operator OF
  2852. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2853. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2854. |Scanner.Times: NewInteger(hl*hr,left.type);
  2855. |Scanner.Slash:
  2856. IF hr = 0 THEN
  2857. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2858. ELSE
  2859. IF type.sizeInBits = 64 THEN
  2860. NewReal(hl/hr,system.longrealType);
  2861. ELSE
  2862. NewReal(hl/hr,system.realType)
  2863. END
  2864. END;
  2865. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2866. |Scanner.Mod:
  2867. IF hr = 0 THEN
  2868. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2869. ELSE
  2870. NewInteger(hl MOD hr, left.type);
  2871. (* bootstrap64
  2872. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2873. *)
  2874. END;
  2875. |Scanner.Div:
  2876. IF hr = 0 THEN
  2877. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2878. ELSE
  2879. NewInteger(hl DIV hr, left.type);
  2880. (* bootstrap64
  2881. NewInteger(Machine.DivH(hl,hr),left.type);
  2882. *)
  2883. END;
  2884. (* *)
  2885. |Scanner.Equal: NewBool(hl=hr);
  2886. |Scanner.Unequal: NewBool(hl#hr);
  2887. |Scanner.Less: NewBool(hl<hr);
  2888. |Scanner.LessEqual: NewBool(hl<=hr);
  2889. |Scanner.Greater: NewBool(hl>hr);
  2890. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2891. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2892. END;
  2893. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2894. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2895. type := left.type
  2896. ELSIF (operator = Scanner.Slash) THEN
  2897. left := NewConversion(left.position,left,system.realType,NIL);
  2898. right := NewConversion(right.position,right,system.realType,NIL);
  2899. binaryExpression.SetLeft(left);
  2900. binaryExpression.SetRight(right);
  2901. type := system.realType
  2902. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2903. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2904. type := system.booleanType
  2905. ELSE
  2906. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2907. END;
  2908. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2909. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2910. CASE operator OF
  2911. |Scanner.Plus: NewReal(rl+rr,leftType);
  2912. |Scanner.Minus: NewReal(rl-rr,leftType);
  2913. |Scanner.Times:NewReal(rl*rr,leftType);
  2914. |Scanner.Slash:
  2915. IF rr = 0 THEN
  2916. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2917. ELSE
  2918. NewReal(rl/rr,leftType);
  2919. END
  2920. |Scanner.Equal: NewBool(rl=rr);
  2921. |Scanner.Unequal: NewBool(rl#rr);
  2922. |Scanner.Less: NewBool(rl<rr);
  2923. |Scanner.LessEqual: NewBool(rl<=rr);
  2924. |Scanner.Greater: NewBool(rl>rr);
  2925. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2926. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2927. END;
  2928. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2929. type := left.type
  2930. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2931. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2932. type := system.booleanType
  2933. ELSE
  2934. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2935. IF VerboseErrorMessage THEN
  2936. Printout.Info("left",left);
  2937. Printout.Info("right",right);
  2938. END;
  2939. END;
  2940. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2941. CASE operator OF
  2942. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2943. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2944. ELSE
  2945. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2946. IF VerboseErrorMessage THEN
  2947. Printout.Info("left", left);
  2948. Printout.Info("right", right)
  2949. END;
  2950. END;
  2951. IF ~error THEN
  2952. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2953. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2954. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2955. (* do constant folding *)
  2956. CASE operator OF
  2957. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2958. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2959. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2960. |Scanner.Slash:
  2961. divisor := c * c + d * d;
  2962. ASSERT(divisor # 0);
  2963. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2964. |Scanner.Equal: NewBool((a = c) & (b = d))
  2965. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2966. END
  2967. END
  2968. END
  2969. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2970. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2971. CASE operator OF
  2972. |Scanner.And: NewBool(bl & br);
  2973. |Scanner.Or: NewBool(bl OR br);
  2974. |Scanner.Equal: NewBool(bl = br);
  2975. |Scanner.Unequal: NewBool(bl # br);
  2976. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2977. END;
  2978. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2979. type := system.booleanType
  2980. ELSE
  2981. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2982. END;
  2983. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2984. (* constant folding *)
  2985. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2986. IF operator = Scanner.Equal THEN
  2987. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2988. ELSIF operator = Scanner.Unequal THEN
  2989. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2990. END;
  2991. END;
  2992. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2993. type := system.booleanType;
  2994. ELSE
  2995. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2996. END;
  2997. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2998. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2999. CASE operator OF
  3000. |Scanner.Plus: NewSet(sl + sr);
  3001. |Scanner.Minus: NewSet(sl - sr);
  3002. |Scanner.Times: NewSet(sl * sr);
  3003. |Scanner.Slash: NewSet(sl / sr);
  3004. |Scanner.Equal: NewBool(sl=sr);
  3005. |Scanner.Unequal: NewBool(sl#sr);
  3006. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3007. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3008. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3009. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3010. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3011. END;
  3012. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3013. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3014. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3015. THEN
  3016. type := system.booleanType
  3017. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3018. type := left.type
  3019. ELSE
  3020. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3021. END;
  3022. ELSIF IsCharacterType(left.type) THEN
  3023. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3024. CASE operator OF
  3025. |Scanner.Equal: NewBool(cl=cr);
  3026. |Scanner.Unequal: NewBool(cl#cr);
  3027. |Scanner.Less: NewBool(cl<cr);
  3028. |Scanner.LessEqual: NewBool(cl<=cr);
  3029. |Scanner.Greater: NewBool(cl>cr);
  3030. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3031. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3032. END;
  3033. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3034. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3035. type := system.booleanType
  3036. ELSE
  3037. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3038. END;
  3039. ELSE
  3040. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3041. END;
  3042. ELSE
  3043. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3044. END;
  3045. IF type = SyntaxTree.invalidType THEN
  3046. result := SyntaxTree.invalidExpression
  3047. ELSE
  3048. result.SetType(type)
  3049. END;
  3050. resolvedExpression := result
  3051. END VisitBinaryExpression;
  3052. (** resolve a range expression of the from <<first .. last BY step>>
  3053. - depending on the context different things are checked:
  3054. ArrayIndex:
  3055. - components must be integers
  3056. - replace missing lower bound with 0
  3057. - replace missing upper bound with MAX(LONGINT)
  3058. - replace missing step size with 1
  3059. SetElement:
  3060. - components must be integers
  3061. - replace missing lower bound with 0
  3062. - replace missing upper bound with MAX(SET)
  3063. - must not have step size
  3064. CaseGuard:
  3065. - components must be constant
  3066. - components must be integers or characters
  3067. - must have lower and upper bound present
  3068. - components are made compatible
  3069. - must not have step size
  3070. - if error: return invalidExpression
  3071. **)
  3072. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3073. VAR
  3074. hasError: BOOLEAN;
  3075. first, last, step: SyntaxTree.Expression;
  3076. BEGIN
  3077. hasError := FALSE;
  3078. first := x.first;
  3079. last := x.last;
  3080. step := x.step;
  3081. (* check lower bound *)
  3082. IF x.context = SyntaxTree.CaseGuard THEN
  3083. IF first = NIL THEN
  3084. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3085. hasError := TRUE
  3086. ELSE
  3087. first := ResolveExpression(first);
  3088. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3089. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3090. hasError := TRUE
  3091. ELSE
  3092. IF first IS SyntaxTree.StringValue THEN
  3093. (* add conversion from string to character *)
  3094. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3095. END
  3096. END;
  3097. (* check if expression is constant *)
  3098. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3099. (* error already reported *)
  3100. hasError := TRUE
  3101. END
  3102. END
  3103. ELSE (* ArrayIndex, SetElement *)
  3104. IF first = NIL THEN
  3105. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3106. END;
  3107. first := ResolveExpression(first);
  3108. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3109. first := NewConversion(first.position, first, system.longintType, NIL)
  3110. ELSE
  3111. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3112. hasError := TRUE
  3113. END
  3114. END;
  3115. (* check upper bound *)
  3116. IF x.context = SyntaxTree.CaseGuard THEN
  3117. IF last = NIL THEN
  3118. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3119. hasError := TRUE
  3120. ELSE
  3121. last := ResolveExpression(last);
  3122. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3123. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3124. hasError := TRUE
  3125. ELSE
  3126. IF last IS SyntaxTree.StringValue THEN
  3127. (* add conversion from string to character *)
  3128. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3129. END
  3130. END;
  3131. (* check if expression is constant *)
  3132. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3133. (* error already reported *)
  3134. hasError := TRUE
  3135. ELSE
  3136. (* try to make lower and upper bound compatible *)
  3137. ConvertOperands(first, last);
  3138. IF first.type.resolved # last.type.resolved THEN
  3139. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3140. hasError := TRUE
  3141. END
  3142. END
  3143. END
  3144. ELSE (* ArrayIndex, SetElement *)
  3145. IF last = NIL THEN
  3146. IF x.context = SyntaxTree.ArrayIndex THEN
  3147. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3148. ELSE
  3149. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3150. END
  3151. END;
  3152. last := ResolveExpression(last);
  3153. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3154. last := NewConversion(last.position, last, system.longintType, NIL)
  3155. ELSE
  3156. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3157. hasError := TRUE
  3158. END
  3159. END;
  3160. (* check step size *)
  3161. IF x.context = SyntaxTree.ArrayIndex THEN
  3162. IF step = NIL THEN
  3163. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3164. END;
  3165. step := ResolveExpression(step);
  3166. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3167. step := NewConversion(step.position, step, system.longintType, NIL)
  3168. ELSE
  3169. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3170. hasError := TRUE
  3171. END
  3172. ELSE (* SetElement, CaseGuard *)
  3173. IF step # NIL THEN
  3174. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3175. hasError := TRUE
  3176. END
  3177. END;
  3178. IF hasError THEN
  3179. resolvedExpression := SyntaxTree.invalidExpression
  3180. ELSE
  3181. x.SetFirst(first);
  3182. x.SetLast(last);
  3183. x.SetStep(step);
  3184. x.SetType(system.rangeType);
  3185. resolvedExpression := x;
  3186. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3187. END
  3188. END VisitRangeExpression;
  3189. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3190. BEGIN
  3191. x.SetType(NIL);
  3192. resolvedExpression := x;
  3193. END VisitTensorRangeExpression;
  3194. (** resolve the expression d and return result as designator
  3195. - resolve expression
  3196. - if expression is a designator then return designator else error message and return invalidDesignator
  3197. **)
  3198. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3199. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3200. BEGIN
  3201. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3202. resolved := ResolveExpression(d);
  3203. IF resolved = SyntaxTree.invalidExpression THEN
  3204. (* error should already have been reported *)
  3205. result := SyntaxTree.invalidDesignator;
  3206. ELSIF resolved IS SyntaxTree.Designator THEN
  3207. result := resolved(SyntaxTree.Designator);
  3208. ELSE
  3209. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3210. result := SyntaxTree.invalidDesignator;
  3211. END;
  3212. (* result.type might be nil. *)
  3213. RETURN result
  3214. END ResolveDesignator;
  3215. (**
  3216. symbol designator generated in this module
  3217. nothing to be resolved
  3218. **)
  3219. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3220. BEGIN
  3221. resolvedExpression := x;
  3222. END VisitSymbolDesignator;
  3223. (**
  3224. self designator generated in this module
  3225. nothing to be resolved
  3226. **)
  3227. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3228. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3229. BEGIN
  3230. (* check if in record scope *)
  3231. scope := currentScope;
  3232. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3233. scope := scope.outerScope;
  3234. END;
  3235. IF scope = NIL THEN (* in module scope *)
  3236. x.SetType(system.anyType);
  3237. ELSIF scope IS SyntaxTree.CellScope THEN
  3238. cell := scope(SyntaxTree.CellScope).ownerCell;
  3239. x.SetType(cell);
  3240. ELSE (* in record scope *)
  3241. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3242. IF (record # NIL) & (record.pointerType # NIL) THEN
  3243. type := ResolveType(record.pointerType);
  3244. x.SetType(type);
  3245. ELSE
  3246. x.SetType(record);
  3247. END;
  3248. END;
  3249. resolvedExpression := x;
  3250. END VisitSelfDesignator;
  3251. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3252. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3253. BEGIN
  3254. scope := currentScope;
  3255. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3256. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3257. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3258. returnType := procedureType.returnType;
  3259. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3260. THEN
  3261. x.SetType(returnType);
  3262. ELSE
  3263. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3264. x.SetType(SyntaxTree.invalidType);
  3265. END;
  3266. ELSE
  3267. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3268. x.SetType(SyntaxTree.invalidType);
  3269. END;
  3270. x.SetAssignable(TRUE);
  3271. resolvedExpression := x;
  3272. END VisitResultDesignator;
  3273. (**
  3274. return symbol designator as an expression
  3275. - if symbol is a constant then return the constant value expression
  3276. - else
  3277. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3278. - if symbol is a guarded variable then return a TypeGuardDesignator
  3279. - else return a symbol designator
  3280. **)
  3281. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3282. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3283. guardType: SyntaxTree.Type;
  3284. BEGIN
  3285. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3286. result := SyntaxTree.invalidExpression;
  3287. ASSERT(symbol # NIL);
  3288. (*
  3289. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3290. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3291. Error(position,Diagnostics.Invalid,"type not allowed here");
  3292. ELS *)
  3293. (* not needed any more as values are stored in the expression
  3294. IF symbol IS SyntaxTree.Constant THEN
  3295. result := symbol(SyntaxTree.Constant).value
  3296. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3297. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3298. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3299. ELSE
  3300. result := symbol(SyntaxTree.Constant).value
  3301. END;
  3302. ELSE
  3303. *)
  3304. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3305. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3306. THEN
  3307. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3308. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3309. left := NewDereferenceDesignator(position,left);
  3310. left.SetHidden(TRUE);
  3311. END;
  3312. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3313. scope := currentScope;
  3314. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3315. scope := scope.outerScope;
  3316. END;
  3317. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3318. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3319. END;
  3320. END;
  3321. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3322. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3323. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3324. ELSE
  3325. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3326. END;
  3327. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3328. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3329. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3330. result.SetType(symbol.type);
  3331. result.SetAssignable(assignable);
  3332. symbol.MarkUsed;
  3333. IF symbol IS SyntaxTree.Constant THEN
  3334. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3335. END;
  3336. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3337. variableAccessed := TRUE
  3338. END;
  3339. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3340. IF GetGuard(symbol,guardType) THEN
  3341. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3342. END;
  3343. END;
  3344. ASSERT(result.type # NIL);
  3345. RETURN result
  3346. END NewSymbolDesignator;
  3347. (** check and resolve an identifier designator "identifier"
  3348. - if identifier = self then return SelfDesignator
  3349. - else find symbol in current scope
  3350. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3351. **)
  3352. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3353. VAR symbol: SyntaxTree.Symbol;
  3354. BEGIN
  3355. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3356. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3357. IF symbol # NIL THEN
  3358. ResolveSymbol(symbol);
  3359. ASSERT(symbol.type # NIL);
  3360. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3361. ELSE
  3362. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3363. IF VerboseErrorMessage THEN
  3364. Printout.Info("undeclared identifier designator",identifierDesignator);
  3365. END;
  3366. resolvedExpression := SyntaxTree.invalidDesignator;
  3367. END;
  3368. END VisitIdentifierDesignator;
  3369. (** check and resolve a selector designator of the form left.designator
  3370. - if left is a pointer type then do auto dereferenciation
  3371. - left denotes a search scope:
  3372. - if left type is import type then set search scope to respective module
  3373. - if left type is enumeration type then set search scope to respective enumeration scope
  3374. - elsif left type is record type then set search scope to record scope
  3375. - search symbol in computed scope
  3376. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3377. **)
  3378. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3379. VAR
  3380. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3381. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3382. BEGIN
  3383. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3384. left := ResolveDesignator(selectorDesignator.left);
  3385. result := SyntaxTree.invalidDesignator;
  3386. IF left # NIL THEN
  3387. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3388. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3389. END;
  3390. scope := NIL;
  3391. IF left.type = NIL THEN
  3392. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3393. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3394. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3395. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3396. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3397. module := symbol(SyntaxTree.Import).module;
  3398. IF module # NIL THEN
  3399. scope := module.moduleScope
  3400. ELSE
  3401. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3402. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3403. END;
  3404. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3405. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3406. ASSERT(scope # NIL)
  3407. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3408. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3409. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3410. IF type IS SyntaxTree.EnumerationType THEN
  3411. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3412. ELSE
  3413. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3414. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3415. END;
  3416. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3417. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3418. ELSE
  3419. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3420. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3421. END;
  3422. symbol := NIL;
  3423. IF scope # NIL THEN
  3424. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3425. IF symbol # NIL THEN
  3426. ResolveSymbol(symbol);
  3427. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3428. symbol.MarkUsed
  3429. ELSE
  3430. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3431. IF VerboseErrorMessage THEN
  3432. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3433. Printout.Info("scope", scope);
  3434. Printout.Info("left", left);
  3435. Printout.Info("undeclared identifier",selectorDesignator);
  3436. Printout.Info("left resolved designator",left);
  3437. END
  3438. END;
  3439. END;
  3440. END;
  3441. resolvedExpression := result;
  3442. END VisitSelectorDesignator;
  3443. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3444. VAR len,idx: LONGINT;
  3445. BEGIN
  3446. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3447. IF idx < 0 THEN
  3448. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3449. ELSE
  3450. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3451. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3452. END;
  3453. END;
  3454. END;
  3455. END IndexCheck;
  3456. (*
  3457. - if index designator has not type, use newBaseType as its type
  3458. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3459. - special rule: if static array of dynamic array occurs, make it all dynamic
  3460. index designator type: new base type: new index designator type:
  3461. NIL z z
  3462. ARRAY [x, y] z ARRAY [x, y] OF z
  3463. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3464. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3465. *)
  3466. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3467. VAR
  3468. mathArrayType: SyntaxTree.MathArrayType;
  3469. makeDynamic: BOOLEAN;
  3470. BEGIN
  3471. IF indexDesignator.type = NIL THEN
  3472. indexDesignator.SetType(newBaseType)
  3473. ELSE
  3474. (* index designator must be a of math array type *)
  3475. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3476. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3477. (* determine if all arrays have to be made dynamic *)
  3478. makeDynamic :=
  3479. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3480. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3481. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3482. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3483. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3484. END;
  3485. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3486. mathArrayType.SetArrayBase(newBaseType)
  3487. END
  3488. END SetIndexBaseType;
  3489. (** check and append index list element to index designator of math array
  3490. - check validity of single index or array range
  3491. - compute new type
  3492. - if range then create new array type (calculate length of resulting array)
  3493. - otherwise take sourceArray.arrayBase as new type
  3494. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3495. **)
  3496. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3497. VAR
  3498. targetArray: SyntaxTree.MathArrayType;
  3499. first, last, step: SyntaxTree.Expression;
  3500. firstValue, lastValue, stepValue, length: LONGINT;
  3501. rangeExpression: SyntaxTree.RangeExpression;
  3502. BEGIN
  3503. IF indexListItem.type = SyntaxTree.invalidType THEN
  3504. (* error already handled *)
  3505. indexDesignator.parameters.AddExpression(indexListItem)
  3506. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3507. indexDesignator.HasRange;
  3508. indexDesignator.HasTensorRange;
  3509. indexDesignator.parameters.AddExpression(indexListItem);
  3510. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3511. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3512. IndexCheck(indexListItem, sourceArray.length);
  3513. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3514. indexDesignator.parameters.AddExpression(indexListItem)
  3515. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3516. indexDesignator.HasRange;
  3517. (* if the range is given as an array range expression, check the validity of its components *)
  3518. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3519. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3520. first := rangeExpression.first;
  3521. last := rangeExpression.last;
  3522. step := rangeExpression.step;
  3523. (* perform static checks on range components *)
  3524. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3525. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3526. END;
  3527. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3528. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3529. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3530. END
  3531. END;
  3532. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3533. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3534. END;
  3535. (* add conversions to size type *)
  3536. (* TODO: needed? *)
  3537. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3538. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3539. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3540. END;
  3541. IF indexDesignator.hasTensorRange THEN
  3542. (* the index designator's base type is a tensor: leave it as is *)
  3543. ELSE
  3544. (* append a new math array to the index designator's base type *)
  3545. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3546. IF ~error THEN
  3547. (*
  3548. (* optimization: calculate length of target array for static ranges *)
  3549. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3550. IF IsStaticallyOpenRange(rangeExpression) THEN
  3551. (* range is open ('*'): reuse source array length as target array length *)
  3552. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3553. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3554. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3555. IF lastValue = MAX(LONGINT) THEN
  3556. IF IsIntegerValue(sourceArray.length, length) THEN
  3557. lastValue := length - 1;
  3558. isStaticTargetArrayLength := TRUE
  3559. ELSE
  3560. isStaticTargetArrayLength := FALSE
  3561. END
  3562. ELSE
  3563. isStaticTargetArrayLength := TRUE
  3564. END;
  3565. IF isStaticTargetArrayLength THEN
  3566. (* calculate static target array length *)
  3567. IF firstValue > lastValue THEN
  3568. length := 0
  3569. ELSE
  3570. length := 1 + lastValue - firstValue;
  3571. IF length MOD stepValue = 0 THEN
  3572. length := length DIV stepValue
  3573. ELSE
  3574. length := length DIV stepValue + 1
  3575. END
  3576. END;
  3577. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3578. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3579. ASSERT(targetArray.form = SyntaxTree.Static)
  3580. END
  3581. END
  3582. END
  3583. *)
  3584. END;
  3585. SetIndexBaseType(indexDesignator, targetArray)
  3586. END;
  3587. indexDesignator.parameters.AddExpression(indexListItem)
  3588. ELSE
  3589. Error(position, Diagnostics.Invalid,"invalid index list item");
  3590. END;
  3591. END AppendMathIndex;
  3592. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3593. VAR parameters: SyntaxTree.ExpressionList;
  3594. BEGIN
  3595. parameters := index.parameters;
  3596. IF (expression.type = NIL) THEN
  3597. Error(position,Diagnostics.Invalid,"invalid index");
  3598. ELSIF IsIntegerType(expression.type.resolved) THEN
  3599. IF over IS SyntaxTree.ArrayType THEN
  3600. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3601. ELSIF over IS SyntaxTree.StringType THEN
  3602. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3603. END;
  3604. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3605. parameters.AddExpression(expression);
  3606. ELSE
  3607. Error(position,Diagnostics.Invalid,"invalid index");
  3608. END;
  3609. END AppendIndex;
  3610. (** convert an expression to math array type
  3611. if expression is of math array type: return expression itself
  3612. if expression is of array-structured object type: return an index operator call on it
  3613. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3614. otherwise: return invalid expression
  3615. **)
  3616. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3617. VAR
  3618. result: SyntaxTree.Expression;
  3619. mathArrayType: SyntaxTree.MathArrayType;
  3620. BEGIN
  3621. IF expression.type = NIL THEN
  3622. result := SyntaxTree.invalidExpression
  3623. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3624. (* expression of math array type *)
  3625. result := expression
  3626. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3627. (* expression of array-structured object type *)
  3628. mathArrayType := MathArrayStructureOfType(expression.type);
  3629. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3630. ELSE
  3631. result := SyntaxTree.invalidExpression
  3632. END;
  3633. RETURN result
  3634. END ConvertToMathArray;
  3635. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3636. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3637. VAR
  3638. result: SyntaxTree.ExpressionList;
  3639. i: LONGINT;
  3640. BEGIN
  3641. result := SyntaxTree.NewExpressionList();
  3642. FOR i := 1 TO itemCount DO
  3643. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3644. END;
  3645. RETURN result
  3646. END ListOfOpenRanges;
  3647. (** create a procedure call designator for an index operator call on an array-structured object type
  3648. - use given index list as actual parameters
  3649. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3650. **)
  3651. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3652. VAR
  3653. operator: SyntaxTree.Operator;
  3654. expression: SyntaxTree.Expression;
  3655. actualParameters, tempList: SyntaxTree.ExpressionList;
  3656. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3657. result, tempDesignator: SyntaxTree.Designator;
  3658. recordType: SyntaxTree.RecordType;
  3659. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3660. i, hashValue, indexListSize, indexListKind: LONGINT;
  3661. castReturnType: SyntaxTree.MathArrayType;
  3662. BEGIN
  3663. ASSERT(IsArrayStructuredObjectType(left.type));
  3664. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3665. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3666. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3667. indexListSize := indexList.Length();
  3668. indexListKind := 0;
  3669. containsNonRange := FALSE;
  3670. FOR i := 0 TO indexList.Length() - 1 DO
  3671. indexListKind := indexListKind * 2;
  3672. expression := indexList.GetExpression(i);
  3673. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3674. INC(indexListKind)
  3675. ELSE
  3676. containsNonRange := TRUE
  3677. END
  3678. END;
  3679. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3680. (* select applicable index operator
  3681. - try to look up optimal index operator
  3682. - if not present, use operator on ranges
  3683. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3684. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3685. *)
  3686. usesGeneralOperator := FALSE;
  3687. IF rhs # NIL THEN
  3688. (* write operator *)
  3689. IF hashValue = -1 THEN
  3690. operator := NIL
  3691. ELSE
  3692. operator := recordType.arrayAccessOperators.write[hashValue];
  3693. END;
  3694. IF operator = NIL THEN
  3695. usesPureRangeOperator := TRUE;
  3696. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3697. operator := recordType.arrayAccessOperators.generalWrite;
  3698. usesGeneralOperator := TRUE
  3699. ELSE
  3700. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3701. operator := recordType.arrayAccessOperators.write[hashValue];
  3702. END
  3703. END
  3704. ELSE
  3705. (* read operator *)
  3706. IF hashValue = -1 THEN
  3707. operator := NIL
  3708. ELSE
  3709. operator := recordType.arrayAccessOperators.read[hashValue];
  3710. END;
  3711. IF operator = NIL THEN
  3712. usesPureRangeOperator := TRUE;
  3713. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3714. operator := recordType.arrayAccessOperators.generalRead;
  3715. usesGeneralOperator := TRUE
  3716. ELSE
  3717. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3718. operator := recordType.arrayAccessOperators.read[hashValue];
  3719. END
  3720. END
  3721. END;
  3722. IF operator = NIL THEN
  3723. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3724. result := SyntaxTree.invalidDesignator;
  3725. ELSE
  3726. (* determine if reshaping is needed *)
  3727. needsReshaping := containsNonRange & usesPureRangeOperator;
  3728. (* import OCArrayBase if reshaping is needed *)
  3729. IF needsReshaping & ~arrayBaseImported THEN
  3730. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3731. arrayBaseImported := TRUE
  3732. END;
  3733. (* add the index list item to the list of actual parameters
  3734. - for general operators: add a single inline array containing the index list items as parameter
  3735. - otherwise: add all index list items as individual parameters
  3736. *)
  3737. actualParameters := SyntaxTree.NewExpressionList();
  3738. IF usesGeneralOperator THEN
  3739. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3740. END;
  3741. FOR i := 0 TO indexListSize - 1 DO
  3742. expression := indexList.GetExpression(i);
  3743. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3744. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3745. tempList := SyntaxTree.NewExpressionList();
  3746. tempList.AddExpression(expression);
  3747. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3748. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3749. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3750. END;
  3751. IF usesGeneralOperator THEN
  3752. tempMathArrayExpression.elements.AddExpression(expression);
  3753. ELSE
  3754. actualParameters.AddExpression(expression)
  3755. END
  3756. END;
  3757. IF usesGeneralOperator THEN
  3758. actualParameters.AddExpression(tempMathArrayExpression)
  3759. END;
  3760. IF rhs # NIL THEN
  3761. (* add actual parameter for RHS *)
  3762. IF needsReshaping THEN
  3763. (* reshape using OCArrayBase.ExpandDimensions *)
  3764. tempList := SyntaxTree.NewExpressionList();
  3765. (* source array *)
  3766. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3767. tempList.AddExpression(rhs);
  3768. ELSE
  3769. (* convert scalar to one-dimensional array *)
  3770. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3771. tempMathArrayExpression.elements.AddExpression(rhs);
  3772. tempList.AddExpression(tempMathArrayExpression)
  3773. END;
  3774. (* list of kept dimensions *)
  3775. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3776. FOR i := 0 TO indexListSize - 1 DO
  3777. expression := indexList.GetExpression(i);
  3778. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3779. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3780. ELSE
  3781. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3782. END
  3783. END;
  3784. tempList.AddExpression(tempMathArrayExpression);
  3785. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3786. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3787. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3788. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3789. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3790. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3791. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3792. expression.SetType(castReturnType);
  3793. ELSE
  3794. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3795. END;
  3796. actualParameters.AddExpression(expression)
  3797. ELSE
  3798. actualParameters.AddExpression(rhs)
  3799. END
  3800. END;
  3801. (* add dereference operator and create procedure call designator *)
  3802. ASSERT(left IS SyntaxTree.Designator);
  3803. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3804. ASSERT(expression IS SyntaxTree.Designator);
  3805. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3806. IF (rhs = NIL) & needsReshaping THEN
  3807. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3808. tempList := SyntaxTree.NewExpressionList();
  3809. FOR i := 0 TO indexList.Length() - 1 DO
  3810. expression := indexList.GetExpression(i);
  3811. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3812. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3813. ELSE
  3814. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3815. END
  3816. END;
  3817. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3818. END;
  3819. IF rhs = NIL THEN
  3820. (* special rule: index read operator calls are considered to be assignable *)
  3821. result.SetAssignable(TRUE)
  3822. END;
  3823. (* put information about this index operator call into the resulting designator *)
  3824. result.SetRelatedAsot(left);
  3825. result.SetRelatedIndexList(indexList)
  3826. END;
  3827. RETURN result
  3828. END NewIndexOperatorCall;
  3829. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3830. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3831. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3832. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3833. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3834. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3835. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3836. CONST trace = FALSE;
  3837. BEGIN
  3838. IF trace THEN
  3839. FOR i := 0 TO actualParameters.Length()-1 DO
  3840. Printout.Info("par", actualParameters.GetExpression(i));
  3841. END;
  3842. END;
  3843. operator := scope.firstOperator;
  3844. WHILE(operator # NIL) DO
  3845. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3846. procedureType := operator.type(SyntaxTree.ProcedureType);
  3847. distance := Distance(system, procedureType,actualParameters);
  3848. IF trace THEN Printout.Info("check op ",operator) END;
  3849. IF distance < bestDistance THEN
  3850. IF trace THEN Printout.Info("taken op",operator) END;
  3851. bestDistance := distance;
  3852. bestOperator := operator;
  3853. END;
  3854. END;
  3855. operator := operator.nextOperator;
  3856. END;
  3857. END FindInScope;
  3858. BEGIN
  3859. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3860. IF oper = 0 THEN (* index *)
  3861. identifier := SyntaxTree.NewIdentifier("[]");
  3862. ELSE
  3863. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3864. END;
  3865. WHILE (recordType # NIL) DO
  3866. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3867. recordType := recordType.GetBaseRecord();
  3868. END;
  3869. RETURN bestOperator
  3870. END FindOperator;
  3871. BEGIN
  3872. type := left.type.resolved;
  3873. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3874. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3875. actualParameters := SyntaxTree.NewExpressionList();
  3876. IF parameters # NIL THEN
  3877. FOR i := 0 TO parameters.Length()-1 DO
  3878. expression := ResolveExpression(parameters.GetExpression(i));
  3879. actualParameters.AddExpression(expression);
  3880. END;
  3881. END;
  3882. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3883. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3884. IF op # NIL THEN
  3885. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3886. ASSERT(expression IS SyntaxTree.Designator);
  3887. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3888. result.SetRelatedAsot(left);
  3889. result.SetRelatedIndexList(parameters);
  3890. (* check if write operator exists, for var parameters *)
  3891. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3892. actualParameters := SyntaxTree.NewExpressionList();
  3893. FOR i := 0 TO parameters.Length()-1 DO
  3894. expression := ResolveExpression(parameters.GetExpression(i));
  3895. actualParameters.AddExpression(expression);
  3896. END;
  3897. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3898. actualParameters.AddExpression(rhs);
  3899. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3900. IF op = NIL THEN rhs := NIL END;
  3901. END;
  3902. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3903. ELSE
  3904. result := NIL;
  3905. END;
  3906. RETURN result;
  3907. END NewObjectOperatorCall;
  3908. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3909. 1. convert bracket designator chains into a single one that contains separators
  3910. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3911. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3912. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3913. - if an array or math array is indexed over, create index designator
  3914. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3915. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3916. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3917. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3918. - if an array-structured object type is indexed over, create procedure call designator
  3919. e.g.: a[x, y] -> a^."[]"(x, y)
  3920. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3921. - a[i, *] = a[i][*]
  3922. - a[*, i] # a[*][i]
  3923. Because:
  3924. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3925. - 'i-th column' = a[*, i]
  3926. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3927. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3928. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3929. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3930. **)
  3931. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3932. VAR
  3933. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3934. indexDesignator: SyntaxTree.IndexDesignator;
  3935. designator: SyntaxTree.Designator;
  3936. type: SyntaxTree.Type;
  3937. recordType: SyntaxTree.RecordType;
  3938. expression, rhs: SyntaxTree.Expression;
  3939. indexList: SyntaxTree.ExpressionList;
  3940. i: LONGINT;
  3941. hasError, done: BOOLEAN;
  3942. PROCEDURE FinalizeIndexDesignator;
  3943. BEGIN
  3944. IF indexDesignator # NIL THEN
  3945. (* the end of a tensor has been reached: *)
  3946. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3947. SetIndexBaseType(indexDesignator, type);
  3948. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3949. designator := indexDesignator;
  3950. type := designator.type.resolved;
  3951. indexDesignator := NIL;
  3952. ASSERT(SyntaxTree.Resolved IN type.state)
  3953. END
  3954. END FinalizeIndexDesignator;
  3955. BEGIN
  3956. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3957. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3958. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3959. (* copy all index list entries including a separator to the left bracket designator *)
  3960. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3961. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3962. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3963. END;
  3964. (* propagate the related RHS *)
  3965. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3966. (* only resolve left bracket designator and use as final result *)
  3967. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3968. ELSE
  3969. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3970. designator := ResolveDesignator(bracketDesignator.left);
  3971. type := designator.type.resolved;
  3972. indexDesignator := NIL;
  3973. (*!!! clean up *)
  3974. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3975. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3976. IF resolvedExpression = NIL THEN
  3977. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  3978. resolvedExpression := SyntaxTree.invalidDesignator
  3979. END;
  3980. RETURN;
  3981. END;
  3982. i := 0;
  3983. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3984. expression := bracketDesignator.parameters.GetExpression(i);
  3985. expression := ResolveExpression(expression);
  3986. bracketDesignator.parameters.SetExpression(i, expression);
  3987. IF expression = SyntaxTree.indexListSeparator THEN
  3988. (* finalize an existing index designator if needed *)
  3989. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3990. INC(i)
  3991. ELSE
  3992. (* do auto-dereferencing if needed *)
  3993. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3994. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3995. & (i=0)*)
  3996. THEN
  3997. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3998. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3999. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4000. designator := SyntaxTree.invalidDesignator;
  4001. type := SyntaxTree.invalidType
  4002. ELSE
  4003. FinalizeIndexDesignator;
  4004. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4005. type := designator.type.resolved
  4006. END
  4007. END;
  4008. (* create a new index designator, if needed *)
  4009. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4010. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4011. indexDesignator.SetAssignable(designator.assignable);
  4012. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4013. (* designator := indexDesignator *)
  4014. END;
  4015. IF type = SyntaxTree.invalidType THEN
  4016. (* error already handled *)
  4017. INC(i)
  4018. ELSIF type IS SyntaxTree.ArrayType THEN
  4019. (* indexing over an array *)
  4020. ASSERT(indexDesignator # NIL);
  4021. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4022. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4023. INC(i)
  4024. ELSIF type IS SyntaxTree.StringType THEN
  4025. (* indexing over an array *)
  4026. ASSERT(indexDesignator # NIL);
  4027. AppendIndex(expression.position, indexDesignator, expression, type);
  4028. type := type(SyntaxTree.StringType).baseType.resolved;
  4029. INC(i)
  4030. ELSIF type IS SyntaxTree.MathArrayType THEN
  4031. (* indexing over a math array *)
  4032. ASSERT(indexDesignator # NIL);
  4033. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4034. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4035. INC(i)
  4036. ELSIF IsArrayStructuredObjectType(type) THEN
  4037. (* indexing over ASOTs *)
  4038. FinalizeIndexDesignator;
  4039. ASSERT(type IS SyntaxTree.PointerType);
  4040. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4041. (*
  4042. - collect index list items from bracket designator that belong to ASOT
  4043. - check for errors
  4044. *)
  4045. indexList := SyntaxTree.NewExpressionList();
  4046. hasError := FALSE;
  4047. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4048. (* indexing over tensor ASOT:
  4049. - stop at index list end or separator
  4050. - dimensionality is given by number of index list items
  4051. *)
  4052. done := FALSE;
  4053. WHILE ~done DO
  4054. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4055. done := TRUE;
  4056. ELSE
  4057. expression := bracketDesignator.parameters.GetExpression(i);
  4058. IF expression = SyntaxTree.indexListSeparator THEN
  4059. done := TRUE;
  4060. ELSE
  4061. expression := ResolveExpression(expression);
  4062. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4063. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4064. hasError := TRUE
  4065. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4066. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4067. expression := SyntaxTree.invalidExpression;
  4068. hasError := TRUE
  4069. END;
  4070. indexList.AddExpression(expression)
  4071. END;
  4072. INC(i)
  4073. END
  4074. END
  4075. ELSE
  4076. (* indexing over non-tensor ASOT:
  4077. - ignore separators
  4078. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4079. *)
  4080. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4081. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4082. expression := bracketDesignator.parameters.GetExpression(i);
  4083. ELSE
  4084. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4085. END;
  4086. IF expression # SyntaxTree.indexListSeparator THEN
  4087. expression := ResolveExpression(expression);
  4088. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4089. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4090. expression := SyntaxTree.invalidExpression;
  4091. hasError := TRUE
  4092. END;
  4093. indexList.AddExpression(expression)
  4094. END;
  4095. INC(i)
  4096. END;
  4097. END;
  4098. IF hasError THEN
  4099. designator := SyntaxTree.invalidDesignator;
  4100. type := SyntaxTree.invalidType;
  4101. ELSE
  4102. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4103. and the last entry in the index list belongs to the array-structured object type in question.
  4104. E.g.: for a 2-dimensional array-structured object type:
  4105. - 'lhs := asot[1, 2]' -> read mode
  4106. - 'asot[1, 2] := rhs' -> write mode
  4107. - 'asot[1, 2, 3] := rhs' -> read mode
  4108. *)
  4109. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4110. rhs := bracketDesignator.relatedRhs
  4111. ELSE
  4112. rhs := NIL
  4113. END;
  4114. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4115. type := designator.type
  4116. END
  4117. ELSE
  4118. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4119. designator := SyntaxTree.invalidDesignator;
  4120. type := SyntaxTree.invalidType;
  4121. INC(i)
  4122. END
  4123. END
  4124. END;
  4125. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4126. resolvedExpression := designator
  4127. END
  4128. END VisitBracketDesignator;
  4129. (** check and resolve expression list
  4130. - resolve each expression in an expression list
  4131. - returns true if and only if all statements could have successfully been resolved
  4132. **)
  4133. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4134. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4135. BEGIN
  4136. result := TRUE;
  4137. FOR i := 0 TO expressionList.Length()-1 DO
  4138. expression := ResolveExpression(expressionList.GetExpression(i));
  4139. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4140. expressionList.SetExpression(i,expression);
  4141. END;
  4142. RETURN result
  4143. END ExpressionList;
  4144. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4145. BEGIN
  4146. type := type.resolved;
  4147. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4148. RETURN TRUE
  4149. ELSIF system.CanPassInRegister # NIL THEN
  4150. RETURN system.CanPassInRegister(type);
  4151. ELSE
  4152. RETURN FALSE
  4153. END;
  4154. END CanPassInRegister;
  4155. (** return procedure call designator left(actualParameters)
  4156. - check realtime procedure call in realtime procedure
  4157. - check number of parameters
  4158. - check parameter compatibility
  4159. return invalidDesignator if error
  4160. **)
  4161. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4162. VAR result: SyntaxTree.Designator;
  4163. numberFormalParameters, numberActualParameters: LONGINT;
  4164. formalType: SyntaxTree.ProcedureType;
  4165. formalParameter: SyntaxTree.Parameter;
  4166. actualParameter: SyntaxTree.Expression;
  4167. i: LONGINT;
  4168. BEGIN
  4169. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4170. result := SyntaxTree.invalidDesignator;
  4171. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4172. numberFormalParameters := formalType.numberParameters;
  4173. numberActualParameters := actualParameters.Length();
  4174. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4175. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4176. END;
  4177. IF ~ExpressionList(actualParameters) THEN
  4178. result := SyntaxTree.invalidDesignator
  4179. ELSE
  4180. IF numberActualParameters <= numberFormalParameters THEN
  4181. formalParameter := formalType.firstParameter;
  4182. FOR i := 0 TO numberActualParameters-1 DO
  4183. actualParameter := actualParameters.GetExpression(i);
  4184. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4185. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4186. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4187. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4188. ELSE
  4189. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4190. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4191. END;
  4192. actualParameters.SetExpression(i,actualParameter);
  4193. END;
  4194. formalParameter := formalParameter.nextParameter;
  4195. END;
  4196. WHILE (formalParameter # NIL) DO
  4197. IF formalParameter.defaultValue # NIL THEN
  4198. actualParameters.AddExpression(formalParameter.defaultValue);
  4199. formalParameter := formalParameter.nextParameter
  4200. ELSE
  4201. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4202. formalParameter := NIL;
  4203. END;
  4204. END;
  4205. ELSE
  4206. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4207. END;
  4208. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4209. result.SetAssignable(FALSE);
  4210. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4211. END;
  4212. RETURN result
  4213. END NewProcedureCallDesignator;
  4214. (**
  4215. builtin call designator generated in VisitParameterDesignator
  4216. -> nothing to be resolved
  4217. **)
  4218. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4219. BEGIN
  4220. resolvedExpression := x;
  4221. END VisitTypeGuardDesignator;
  4222. (**
  4223. builtin call designator generated in VisitParameterDesignator
  4224. -> nothing to be resolved
  4225. **)
  4226. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4227. BEGIN
  4228. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4229. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4230. ASSERT(resolvedExpression.type # NIL);
  4231. ELSIF ExpressionList(x.parameters) THEN
  4232. resolvedExpression := x;
  4233. END;
  4234. END VisitBuiltinCallDesignator;
  4235. (**
  4236. procedure call designator generated in VisitParameterDesignator
  4237. -> nothing to be resolved
  4238. **)
  4239. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4240. BEGIN
  4241. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4242. resolvedExpression := x;
  4243. END VisitProcedureCallDesignator;
  4244. (** return true if x is a variable else return false and report error **)
  4245. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4246. VAR result: BOOLEAN;
  4247. BEGIN
  4248. result := TRUE;
  4249. IF x = SyntaxTree.invalidExpression THEN
  4250. result := FALSE;
  4251. ELSIF ~IsVariable(x) THEN
  4252. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4253. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4254. result := FALSE;
  4255. END;
  4256. RETURN result
  4257. END CheckVariable;
  4258. (**
  4259. if expression x is of basic type then return true else report error and return false
  4260. **)
  4261. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4262. VAR result: BOOLEAN;
  4263. BEGIN
  4264. result := FALSE;
  4265. IF x = SyntaxTree.invalidExpression THEN
  4266. ELSIF ~IsBasicType(x.type) THEN
  4267. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4268. result := FALSE
  4269. ELSE result := TRUE
  4270. END;
  4271. RETURN result
  4272. END CheckBasicType;
  4273. (**
  4274. if expression x is of number type then return true else report error and return false
  4275. **)
  4276. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4277. VAR result: BOOLEAN;
  4278. BEGIN
  4279. result := FALSE;
  4280. IF x = SyntaxTree.invalidExpression THEN
  4281. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4282. Error(x.position,Diagnostics.Invalid,"is non number type");
  4283. ELSE result := TRUE
  4284. END;
  4285. RETURN result
  4286. END CheckNumberType;
  4287. (**
  4288. if expression x is of number or size type but not complex then return true else report error and return false
  4289. **)
  4290. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4291. VAR result: BOOLEAN;
  4292. BEGIN
  4293. result := FALSE;
  4294. IF x = SyntaxTree.invalidExpression THEN
  4295. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4296. Error(x.position,Diagnostics.Invalid,"is complex type");
  4297. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4298. Error(x.position,Diagnostics.Invalid,"is non number type");
  4299. ELSE result := TRUE
  4300. END;
  4301. RETURN result
  4302. END CheckNonComplexNumberSizeType;
  4303. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4304. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4305. BEGIN
  4306. result := FALSE; type := x.type.resolved;
  4307. IF x = SyntaxTree.invalidExpression THEN
  4308. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4309. TRACE(type.sizeInBits);
  4310. TRACE(system.addressType.sizeInBits);
  4311. Error(x.position,Diagnostics.Invalid,"is no address type");
  4312. ELSE result := TRUE
  4313. END;
  4314. RETURN result
  4315. END CheckAddressType;
  4316. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4318. BEGIN
  4319. result := FALSE; type := x.type.resolved;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is no size type");
  4323. ELSE result := TRUE
  4324. END;
  4325. RETURN result
  4326. END CheckSizeType;
  4327. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4328. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4329. BEGIN
  4330. result := FALSE; type := x.type.resolved;
  4331. IF x = SyntaxTree.invalidExpression THEN
  4332. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4333. Error(x.position,Diagnostics.Invalid,"is no object type");
  4334. ELSE result := TRUE
  4335. END;
  4336. RETURN result
  4337. END CheckObjectType;
  4338. (**
  4339. if expression x is of integer type then return true else report error and return false
  4340. **)
  4341. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4342. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4343. BEGIN
  4344. result := FALSE; type := x.type.resolved;
  4345. IF x = SyntaxTree.invalidExpression THEN
  4346. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4347. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4348. ELSE result := TRUE
  4349. END;
  4350. RETURN result
  4351. END CheckIntegerType;
  4352. (**
  4353. if expression x is of character type then return true else report error and return false
  4354. **)
  4355. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4356. VAR result: BOOLEAN;
  4357. BEGIN
  4358. result := FALSE;
  4359. IF x = SyntaxTree.invalidExpression THEN
  4360. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4361. Error(x.position,Diagnostics.Invalid,"is no character type");
  4362. ELSE result := TRUE
  4363. END;
  4364. RETURN result
  4365. END CheckCharacterType;
  4366. (**
  4367. if expression x is of real type then return true else report error and return false
  4368. **)
  4369. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4370. VAR result: BOOLEAN;
  4371. BEGIN
  4372. result := FALSE;
  4373. IF x = SyntaxTree.invalidExpression THEN
  4374. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4375. Error(x.position,Diagnostics.Invalid,"is no float type");
  4376. ELSE result := TRUE
  4377. END;
  4378. RETURN result
  4379. END CheckRealType;
  4380. (**
  4381. if expression x is of range type then return true else report error and return false
  4382. **)
  4383. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4384. VAR result: BOOLEAN;
  4385. BEGIN
  4386. result := FALSE;
  4387. IF x = SyntaxTree.invalidExpression THEN
  4388. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4389. Error(x.position,Diagnostics.Invalid,"is no range type");
  4390. ELSE result := TRUE
  4391. END;
  4392. RETURN result
  4393. END CheckRangeType;
  4394. (**
  4395. if expression x is of boolean type then return true else report error and return false
  4396. **)
  4397. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4398. VAR result: BOOLEAN;
  4399. BEGIN
  4400. result := FALSE;
  4401. IF x = SyntaxTree.invalidExpression THEN
  4402. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4403. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4404. ELSE result := TRUE
  4405. END;
  4406. RETURN result
  4407. END CheckBooleanType;
  4408. (**
  4409. if expression x is of set type then return true else report error and return false
  4410. **)
  4411. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4412. VAR result: BOOLEAN;
  4413. BEGIN
  4414. result := FALSE;
  4415. IF x = SyntaxTree.invalidExpression THEN
  4416. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4417. Error(x.position,Diagnostics.Invalid,"is no set type");
  4418. ELSE result := TRUE
  4419. END;
  4420. RETURN result
  4421. END CheckSetType;
  4422. (**
  4423. if expression x is of string or array of character type then return true else report error and return false
  4424. **)
  4425. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4426. VAR result: BOOLEAN;
  4427. BEGIN
  4428. result := FALSE;
  4429. IF x = SyntaxTree.invalidExpression THEN
  4430. ELSIF ~IsStringType(x.type.resolved) THEN
  4431. Error(x.position,Diagnostics.Invalid,"is no string type");
  4432. ELSE result := TRUE
  4433. END;
  4434. RETURN result
  4435. END CheckStringType;
  4436. (**
  4437. if expression x is a type declaration type return true else report error and return false
  4438. **)
  4439. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4440. VAR result: BOOLEAN;
  4441. BEGIN
  4442. result := FALSE;
  4443. IF x = SyntaxTree.invalidExpression THEN
  4444. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4445. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4446. ELSE result := TRUE
  4447. END;
  4448. RETURN result
  4449. END CheckTypeDeclarationType;
  4450. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4451. VAR result: BOOLEAN;
  4452. BEGIN
  4453. result := FALSE;
  4454. IF x = SyntaxTree.invalidExpression THEN
  4455. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4456. result := TRUE;
  4457. value := x.resolved(SyntaxTree.IntegerValue).value;
  4458. ELSE
  4459. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4460. END;
  4461. RETURN result;
  4462. END CheckIntegerValue;
  4463. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4464. BEGIN
  4465. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4466. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4467. ELSE
  4468. RETURN FALSE
  4469. END;
  4470. END IsUnsignedValue;
  4471. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4472. BEGIN
  4473. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4474. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4475. ELSE
  4476. RETURN FALSE
  4477. END
  4478. END IsAddressValue;
  4479. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4480. BEGIN
  4481. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4482. END IsAddressExpression;
  4483. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4484. BEGIN
  4485. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4486. END IsSizeExpression;
  4487. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4488. VAR result: BOOLEAN;
  4489. BEGIN
  4490. result := FALSE;
  4491. IF x = SyntaxTree.invalidExpression THEN
  4492. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4493. result := TRUE;
  4494. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4495. ELSE
  4496. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4497. END;
  4498. RETURN result;
  4499. END CheckEnumerationValue;
  4500. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4501. VAR result: BOOLEAN;
  4502. BEGIN
  4503. result := FALSE;
  4504. IF x = SyntaxTree.invalidExpression THEN
  4505. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4506. result := TRUE;
  4507. value := x.resolved(SyntaxTree.CharacterValue).value;
  4508. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4509. result := TRUE;
  4510. value := x.resolved(SyntaxTree.StringValue).value[0];
  4511. ELSE
  4512. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4513. END;
  4514. RETURN result;
  4515. END CheckCharacterValue;
  4516. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4517. VAR result: BOOLEAN;
  4518. BEGIN
  4519. result := FALSE;
  4520. IF x = SyntaxTree.invalidExpression THEN
  4521. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4522. value := x.resolved(SyntaxTree.IntegerValue).value;
  4523. IF (value > 0) OR includeZero & (value = 0) THEN
  4524. result := TRUE;
  4525. ELSE
  4526. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4527. END
  4528. ELSE
  4529. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4530. END;
  4531. RETURN result;
  4532. END CheckPositiveIntegerValue;
  4533. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4534. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4535. BEGIN
  4536. result := FALSE;
  4537. IF x = SyntaxTree.invalidExpression THEN
  4538. ELSE
  4539. type := x.type.resolved;
  4540. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4541. portType := type(SyntaxTree.PortType);
  4542. result := TRUE
  4543. ELSE
  4544. Error(x.position,Diagnostics.Invalid,"no port type");
  4545. END;
  4546. END;
  4547. RETURN result
  4548. END CheckPortType;
  4549. (* move to builtin procedure call statement ?
  4550. remove builtin procedure call designator ?
  4551. *)
  4552. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4553. VAR
  4554. numberActualParameters,numberFormalParameters: LONGINT;
  4555. formalParameter: SyntaxTree.Parameter;
  4556. actualParameter: SyntaxTree.Expression;
  4557. procedureType: SyntaxTree.ProcedureType;
  4558. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4559. inPort, outPort: SyntaxTree.PortType;
  4560. constructor: SyntaxTree.Procedure;
  4561. type0,type1,type2: SyntaxTree.Type;
  4562. type,base,parameterType: SyntaxTree.Type;
  4563. arrayType: SyntaxTree.ArrayType;
  4564. i,i0,i1: LONGINT;
  4565. r,r0,r1,im: LONGREAL;
  4566. c: CHAR;
  4567. id: LONGINT;
  4568. b: BOOLEAN;
  4569. first: LONGINT;
  4570. mathArrayType: SyntaxTree.MathArrayType;
  4571. customBuiltin: SyntaxTree.CustomBuiltin;
  4572. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4573. VAR resultB: BOOLEAN;
  4574. BEGIN
  4575. IF numberActualParameters < from THEN
  4576. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4577. result := SyntaxTree.invalidExpression;
  4578. resultB := FALSE;
  4579. ELSIF numberActualParameters > to THEN
  4580. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4581. result := SyntaxTree.invalidExpression;
  4582. resultB := FALSE;
  4583. ELSE
  4584. resultB := TRUE;
  4585. END;
  4586. RETURN resultB
  4587. END CheckArity;
  4588. BEGIN
  4589. type := NIL; result := NIL;
  4590. type0 := NIL; type1 := NIL; type2 := NIL;
  4591. numberActualParameters := actualParameters.Length();
  4592. IF numberActualParameters>0 THEN
  4593. parameter0 := actualParameters.GetExpression(0);
  4594. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4595. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4596. result := SyntaxTree.invalidExpression
  4597. END
  4598. END;
  4599. IF numberActualParameters >1 THEN
  4600. parameter1 := actualParameters.GetExpression(1);
  4601. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4602. ELSE
  4603. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4604. result := SyntaxTree.invalidExpression
  4605. END
  4606. END;
  4607. IF numberActualParameters >2 THEN
  4608. parameter2 := actualParameters.GetExpression(2);
  4609. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4610. ELSE
  4611. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4612. result := SyntaxTree.invalidExpression
  4613. END
  4614. END;
  4615. IF returnType # NIL THEN
  4616. id := Global.New;
  4617. result := NIL;
  4618. ELSE
  4619. id := builtin.id;
  4620. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4621. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4622. END;
  4623. END;
  4624. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4625. ELSIF result # NIL THEN type := result.type (* operator *)
  4626. ELSE
  4627. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4628. result(SyntaxTree.Designator).SetLeft(left);
  4629. IF returnType # NIL THEN
  4630. type := returnType;
  4631. END;
  4632. (* ---- ASSERT ----- *)
  4633. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4634. IF CheckBooleanType(parameter0) THEN
  4635. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4636. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4637. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4638. *)
  4639. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4640. Error(position,Diagnostics.Invalid,"assert failed");
  4641. END;
  4642. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4643. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4644. rules imposed by the architecture / current runtime
  4645. *)
  4646. END;
  4647. END;
  4648. (* ---- COPY ----- *)
  4649. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4650. IF~IsStringType(type0) THEN
  4651. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4652. END;
  4653. IF ~IsStringType(type1) THEN
  4654. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4655. ELSIF CheckVariable(parameter1) THEN
  4656. IF (type0 IS SyntaxTree.StringType) THEN
  4657. arrayType := type1(SyntaxTree.ArrayType);
  4658. IF arrayType.form = SyntaxTree.Static THEN
  4659. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4660. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4661. END;
  4662. END;
  4663. END;
  4664. END;
  4665. (* ---- INC, DEC----- *)
  4666. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4667. IF numberActualParameters = 1 THEN
  4668. parameter1 :=Global.NewIntegerValue(system,position,1);
  4669. actualParameters.AddExpression(parameter1);
  4670. END;
  4671. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4672. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4673. Error(position,Diagnostics.Invalid,"incompatible increment");
  4674. ELSE
  4675. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4676. actualParameters.SetExpression(1,parameter1);
  4677. END;
  4678. END;
  4679. (* ---- EXCL, INCL----- *)
  4680. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4681. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4682. IF IsIntegerValue(parameter1,i0) THEN
  4683. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4684. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4685. END;
  4686. END;
  4687. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4688. actualParameters.SetExpression(1,parameter1);
  4689. END;
  4690. (* ---- HALT, SYSTEM.HALT ----- *)
  4691. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4692. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4693. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4694. rules imposed by the architecture / current runtime
  4695. *)
  4696. END;
  4697. (* ---- WAIT ----- *)
  4698. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4699. IF CheckObjectType(parameter0) THEN
  4700. END;
  4701. (* ---- NEW ----- *)
  4702. ELSIF (id = Global.New) THEN
  4703. IF returnType # NIL THEN
  4704. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4705. ELSE
  4706. first := 1;
  4707. END;
  4708. IF CheckArity(first,Infinity) THEN
  4709. IF currentIsRealtime THEN
  4710. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4711. END;
  4712. (* check constructor *)
  4713. IF (first =0) OR CheckVariable(parameter0) THEN
  4714. IF type0 IS SyntaxTree.PointerType THEN
  4715. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4716. END;
  4717. IF type0 IS SyntaxTree.ArrayType THEN
  4718. arrayType := type0(SyntaxTree.ArrayType);
  4719. IF arrayType.form = SyntaxTree.Static THEN
  4720. i := first
  4721. ELSIF arrayType.form = SyntaxTree.Open THEN
  4722. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4723. ELSE HALT(100)
  4724. END;
  4725. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4726. i := first;
  4727. REPEAT
  4728. actualParameter := actualParameters.GetExpression(i);
  4729. IF CheckSizeType(actualParameter) THEN
  4730. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4731. actualParameters.SetExpression(i,actualParameter);
  4732. END;
  4733. INC(i);
  4734. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4735. END;
  4736. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4737. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4738. IF constructor = NIL THEN
  4739. IF CheckArity(first,first) THEN END;
  4740. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4741. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4742. ELSE
  4743. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4744. numberFormalParameters := procedureType.numberParameters;
  4745. IF numberActualParameters-first <= numberFormalParameters THEN
  4746. formalParameter := procedureType.firstParameter;
  4747. FOR i := first TO numberActualParameters-1 DO
  4748. actualParameter := actualParameters.GetExpression(i);
  4749. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4750. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4751. ELSE
  4752. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4753. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4754. END;
  4755. actualParameters.SetExpression(i,actualParameter);
  4756. END;
  4757. formalParameter := formalParameter.nextParameter;
  4758. END;
  4759. WHILE (formalParameter # NIL) DO
  4760. IF formalParameter.defaultValue # NIL THEN
  4761. actualParameters.AddExpression(formalParameter.defaultValue);
  4762. formalParameter := formalParameter.nextParameter
  4763. ELSE
  4764. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4765. formalParameter := NIL;
  4766. END;
  4767. END;
  4768. ELSE
  4769. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4770. END;
  4771. END;
  4772. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4773. mathArrayType := type0(SyntaxTree.MathArrayType);
  4774. IF mathArrayType.form = SyntaxTree.Static THEN
  4775. Error(position,Diagnostics.Invalid,"new on static array");
  4776. ELSE
  4777. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4778. i0 := first+1; i1 := Infinity;
  4779. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4780. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4781. i1 := i0;
  4782. ELSE HALT(100);
  4783. END;
  4784. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4785. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4786. base := ArrayBase(type0,MAX(LONGINT));
  4787. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4788. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4789. IF ~CompatibleTo(system,type0,parameterType) THEN
  4790. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4791. result := SyntaxTree.invalidExpression;
  4792. ELSE
  4793. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4794. END;
  4795. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4796. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4797. IF ~CompatibleTo(system,type1,parameterType) THEN
  4798. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4799. result := SyntaxTree.invalidExpression;
  4800. ELSE
  4801. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4802. END;
  4803. ELSE
  4804. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4805. i := first;
  4806. REPEAT
  4807. actualParameter := actualParameters.GetExpression(i);
  4808. IF CheckSizeType(actualParameter) THEN
  4809. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4810. actualParameters.SetExpression(i,actualParameter);
  4811. END;
  4812. INC(i);
  4813. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4814. END;
  4815. END;
  4816. END;
  4817. ELSIF type0 IS SyntaxTree.CellType THEN
  4818. IF ~(currentIsCellNet) THEN
  4819. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4820. ELSE
  4821. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4822. IF (constructor = NIL) & CheckArity(1,1) THEN
  4823. (* ok *)
  4824. ELSE
  4825. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4826. numberFormalParameters := procedureType.numberParameters;
  4827. DEC(numberActualParameters);
  4828. IF numberActualParameters <= numberFormalParameters THEN
  4829. formalParameter := procedureType.firstParameter;
  4830. FOR i := first TO numberActualParameters DO
  4831. actualParameter := actualParameters.GetExpression(i);
  4832. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4833. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4834. ELSE
  4835. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4836. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4837. END;
  4838. actualParameters.SetExpression(i,actualParameter);
  4839. END;
  4840. formalParameter := formalParameter.nextParameter;
  4841. END;
  4842. WHILE (formalParameter # NIL) DO
  4843. IF formalParameter.defaultValue # NIL THEN
  4844. actualParameters.AddExpression(formalParameter.defaultValue);
  4845. formalParameter := formalParameter.nextParameter
  4846. ELSE
  4847. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4848. formalParameter := NIL;
  4849. END;
  4850. END;
  4851. ELSE
  4852. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4853. END;
  4854. END;
  4855. END;
  4856. activeCellsStatement := TRUE;
  4857. ELSE
  4858. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4859. END;
  4860. END;
  4861. END;
  4862. (* ---- DISPOSE ----- *)
  4863. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4864. IF ~IsPointerType(parameter0.type) THEN
  4865. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4866. ELSIF ~IsDisposable(parameter0.type) THEN
  4867. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4868. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4869. END
  4870. (* ---- GETPROCEDURE ----- *)
  4871. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4872. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4873. IF CheckVariable(parameter2) THEN
  4874. IF ~GetProcedureAllowed(parameter2.type) THEN
  4875. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4876. END;
  4877. END;
  4878. END;
  4879. (* ---- ABS ----- *)
  4880. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4881. (* note: ABS on complex numbers is done using overloading *)
  4882. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4883. type := type0;
  4884. IF IsIntegerValue(parameter0,i0) THEN
  4885. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4886. type := Global.GetIntegerType(system,ABS(i0));
  4887. ELSIF IsRealValue(parameter0,r) THEN
  4888. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4889. END;
  4890. ELSE
  4891. type := SyntaxTree.invalidType;
  4892. END;
  4893. (* ---- ASH, ASR ----- *)
  4894. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4895. type := type0;
  4896. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4897. (*
  4898. ConvertOperands(parameter0,parameter1); (* same type *)
  4899. *)
  4900. type := parameter0.type;
  4901. IF IsIntegerValue(parameter0,i0) THEN
  4902. IF IsIntegerValue(parameter1,i1) THEN
  4903. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4904. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4905. result := ResolveExpression(result);
  4906. type := Global.GetIntegerType(system,i0);
  4907. END;
  4908. END;
  4909. IF type.resolved.sizeInBits < 32 THEN
  4910. type := system.longintType;
  4911. END;
  4912. (*!compatibility with release, remove when resolved
  4913. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4914. *)
  4915. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4916. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4917. actualParameters.SetExpression(0,parameter0);
  4918. actualParameters.SetExpression(1,parameter1);
  4919. END;
  4920. (* ---- CAP ----- *)
  4921. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4922. type := system.characterType;
  4923. IF CheckCharacterType (parameter0) THEN
  4924. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4925. actualParameters.SetExpression(0,parameter0);
  4926. IF IsCharacterValue(parameter0,c) THEN
  4927. IF (c <= "z") & (c >= "a") THEN
  4928. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4929. ELSE
  4930. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4931. END;
  4932. END;
  4933. END;
  4934. (* ---- CHR ----- *)
  4935. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4936. IF id = Global.Chr THEN
  4937. type := system.characterType
  4938. ELSE
  4939. type := system.characterType32
  4940. END;
  4941. IF CheckIntegerType(parameter0) THEN
  4942. IF IsIntegerValue(parameter0,i0) THEN
  4943. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4944. result := ResolveExpression(result);
  4945. ELSE
  4946. (*
  4947. result := NewConversion(parameter0.position,parameter0,type);
  4948. *)
  4949. END;
  4950. END
  4951. (* ---- ENTIER ----- *)
  4952. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4953. type := system.longintType;
  4954. IF CheckRealType(parameter0) THEN
  4955. IF IsRealValue(parameter0,r) THEN
  4956. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4957. type := Global.GetIntegerType(system,ENTIER(r));
  4958. END
  4959. END;
  4960. (* ---- ENTIERH ----- *)
  4961. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4962. type := system.hugeintType;
  4963. IF CheckRealType(parameter0) THEN
  4964. IF IsRealValue(parameter0,r) THEN
  4965. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4966. END
  4967. END;
  4968. (* ---- LEN ----- *)
  4969. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4970. type := system.longintType;
  4971. base := type0;
  4972. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4973. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4974. actualParameters.SetExpression(0,parameter0);
  4975. type0 := parameter0.type.resolved;
  4976. base := type0;
  4977. END;
  4978. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4979. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4980. IF i1 < 0 THEN
  4981. Error(position,Diagnostics.Invalid,"invalid dimension");
  4982. base := SyntaxTree.invalidType;
  4983. ELSE
  4984. base := ArrayBase(base,i1);
  4985. IF (base # NIL) & Indexable(base) THEN
  4986. ELSE
  4987. Error(position,Diagnostics.Invalid,"len on no array");
  4988. IF VerboseErrorMessage THEN
  4989. Printout.Info("base",base);
  4990. END;
  4991. base := SyntaxTree.invalidType;
  4992. END;
  4993. END;
  4994. IF numberActualParameters=2 THEN
  4995. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4996. actualParameters.SetExpression(1,parameter1);
  4997. ELSIF base IS SyntaxTree.MathArrayType THEN
  4998. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4999. END;
  5000. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5001. IF base IS SyntaxTree.ArrayType THEN
  5002. arrayType := base(SyntaxTree.ArrayType);
  5003. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5004. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5005. result := Global.NewIntegerValue(system,position,i);
  5006. type := result.type;(* arrayType.length.type;*)
  5007. ASSERT(type # NIL);
  5008. END;
  5009. ELSIF base IS SyntaxTree.MathArrayType THEN
  5010. mathArrayType := base(SyntaxTree.MathArrayType);
  5011. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5012. result := Global.NewIntegerValue(system,position,i);
  5013. type := result.type;
  5014. (*
  5015. type := mathArrayType.length.type;
  5016. *)
  5017. ASSERT(type # NIL);
  5018. END;
  5019. END;
  5020. END;
  5021. ELSE
  5022. type := system.longintType;
  5023. END;
  5024. (* ---- FIRST ---- *)
  5025. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5026. type := system.longintType;
  5027. IF CheckRangeType(parameter0) THEN END;
  5028. result.SetAssignable(parameter0.assignable)
  5029. (* ---- LAST ---- *)
  5030. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5031. type := system.longintType;
  5032. IF CheckRangeType(parameter0) THEN END;
  5033. result.SetAssignable(parameter0.assignable)
  5034. (* ---- STEP ---- *)
  5035. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5036. type := system.longintType;
  5037. IF CheckRangeType(parameter0) THEN END;
  5038. result.SetAssignable(parameter0.assignable)
  5039. (* ---- RE ---- *)
  5040. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5041. IF CheckNumberType(parameter0) THEN
  5042. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5043. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5044. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5045. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5046. type := parameter0.type
  5047. ELSE
  5048. type := system.realType
  5049. END
  5050. END;
  5051. result.SetAssignable(parameter0.assignable)
  5052. (* ---- IM ---- *)
  5053. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5054. IF CheckNumberType(parameter0) THEN
  5055. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5056. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5057. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5058. ELSE
  5059. type := system.realType;
  5060. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5061. END
  5062. END;
  5063. result.SetAssignable(parameter0.assignable)
  5064. (* ---- MAX ----- *)
  5065. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5066. IF numberActualParameters = 1 THEN
  5067. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5068. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5069. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5070. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5071. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5072. *)
  5073. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5074. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5075. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5076. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5077. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5078. END;
  5079. ELSE
  5080. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5081. END
  5082. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5083. ConvertOperands(parameter0,parameter1);
  5084. actualParameters.SetExpression(0,parameter0);
  5085. actualParameters.SetExpression(1,parameter1);
  5086. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5087. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5088. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5089. END;
  5090. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5091. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5092. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5093. END;
  5094. END;
  5095. type := parameter0.type;
  5096. ELSE type := SyntaxTree.invalidType;
  5097. END;
  5098. (* ---- MIN ----- *)
  5099. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5100. IF numberActualParameters = 1 THEN
  5101. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5102. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5103. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5104. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5105. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5106. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5107. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5108. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5109. END;
  5110. ELSE
  5111. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5112. END
  5113. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5114. ConvertOperands(parameter0,parameter1);
  5115. actualParameters.SetExpression(0,parameter0);
  5116. actualParameters.SetExpression(1,parameter1);
  5117. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5118. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5119. ELSE result.SetResolved(parameter1.resolved)
  5120. END;
  5121. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5122. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5123. ELSE result.SetResolved(parameter1.resolved)
  5124. END;
  5125. END;
  5126. type := parameter0.type;
  5127. ELSE type := SyntaxTree.invalidType;
  5128. END;
  5129. (* ---- ODD ----- *)
  5130. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5131. type := system.booleanType;
  5132. IF CheckIntegerType(parameter0) THEN
  5133. IF IsIntegerValue(parameter0,i0) THEN
  5134. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5135. type := system.booleanType;
  5136. END;
  5137. END;
  5138. (* ---- ORD ----- *)
  5139. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5140. IF id = Global.Ord THEN
  5141. type := system.integerType;
  5142. ELSE
  5143. type := system.longintType;
  5144. END;
  5145. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5146. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5147. actualParameters.SetExpression(0,parameter0);
  5148. (* IF CheckCharacterType(parameter0) THEN*)
  5149. IF IsCharacterValue(parameter0,c)THEN
  5150. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5151. type := Global.GetSignedIntegerType(system,ORD(c));
  5152. END;
  5153. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5154. END;
  5155. (* ---- SHORT ----- *)
  5156. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5157. type := type0;
  5158. IF IsSignedIntegerType(type) THEN
  5159. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5160. ELSIF type = system.integerType THEN type := system.shortintType
  5161. ELSIF type = system.longintType THEN type := system.integerType
  5162. ELSIF type = system.hugeintType THEN type:= system.longintType
  5163. ELSE
  5164. CASE type.sizeInBits OF
  5165. 16: type := Global.Integer8
  5166. |32: type := Global.Integer16
  5167. |64: type := Global.Integer32
  5168. END;
  5169. END;
  5170. ELSIF type IS SyntaxTree.FloatType THEN
  5171. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5172. ELSIF type = system.longrealType THEN type := system.realType
  5173. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5174. END;
  5175. ELSIF type IS SyntaxTree.ComplexType THEN
  5176. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5177. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5178. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5179. END;
  5180. ELSE
  5181. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5182. END;
  5183. IF (parameter0.resolved # NIL) THEN
  5184. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5185. IF parameter0 IS SyntaxTree.Value THEN
  5186. result.SetResolved(parameter0(SyntaxTree.Value));
  5187. END;
  5188. END;
  5189. (* ---- LONG ----- *)
  5190. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5191. type := type0;
  5192. IF IsSignedIntegerType(type) THEN
  5193. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5194. ELSIF type = system.longintType THEN type := system.hugeintType
  5195. ELSIF type = system.integerType THEN type := system.longintType
  5196. ELSIF type = system.shortintType THEN type := system.integerType
  5197. ELSE
  5198. CASE type.sizeInBits OF
  5199. 8: type := Global.Integer16
  5200. |16: type := Global.Integer32
  5201. |32: type := Global.Integer64
  5202. END;
  5203. END;
  5204. ELSIF type IS SyntaxTree.FloatType THEN
  5205. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5206. ELSIF type= system.realType THEN type := system.longrealType
  5207. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5208. END;
  5209. ELSIF type IS SyntaxTree.ComplexType THEN
  5210. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5211. ELSIF type = system.complexType THEN type := system.longcomplexType
  5212. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5213. END;
  5214. ELSE
  5215. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5216. END;
  5217. IF (parameter0.resolved # NIL) THEN
  5218. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5219. IF parameter0 IS SyntaxTree.Value THEN
  5220. result.SetResolved(parameter0(SyntaxTree.Value));
  5221. END;
  5222. END;
  5223. (* ---- SIZE OF ----- *)
  5224. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5225. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5226. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5227. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5228. type := system.integerType;
  5229. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5230. ELSE
  5231. (* for variables, system sizeof could represent the physically occupied size
  5232. determined via the type descriptor, implement that ? *)
  5233. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5234. END
  5235. (* ---- SYSTEM.TRACE -----*)
  5236. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5237. FOR i := 0 TO numberActualParameters-1 DO
  5238. parameter0 := actualParameters.GetExpression(i);
  5239. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5240. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5241. END;
  5242. END;
  5243. (* remaining issues can only be tested in backend *)
  5244. (* ---- ADDRESSOF----- *)
  5245. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5246. IF HasAddress(parameter0) THEN
  5247. type := system.addressType;
  5248. ELSE
  5249. type := SyntaxTree.invalidType;
  5250. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5251. END;
  5252. (* ---- BIT ----- *)
  5253. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5254. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5255. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5256. actualParameters.SetExpression(0,parameter0);
  5257. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5258. actualParameters.SetExpression(1,parameter1);
  5259. END;
  5260. type := system.booleanType;
  5261. (* ----- MSK ---- *)
  5262. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5263. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5264. ConvertOperands(parameter0,parameter1);
  5265. actualParameters.SetExpression(0,parameter0);
  5266. actualParameters.SetExpression(1,parameter1);
  5267. END;
  5268. type := parameter0.type;
  5269. (* ---- SYSTEM.GET64 ----- *)
  5270. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5271. IF CheckAddressType(parameter0) THEN
  5272. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5273. actualParameters.SetExpression(0,parameter0);
  5274. END;
  5275. type := system.hugeintType;
  5276. (* ---- SYSTEM.GET32 ----- *)
  5277. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5278. IF CheckAddressType(parameter0) THEN
  5279. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5280. actualParameters.SetExpression(0,parameter0);
  5281. END;
  5282. type := system.longintType;
  5283. (* ---- SYSTEM.GET16 ----- *)
  5284. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5285. IF CheckAddressType(parameter0) THEN
  5286. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5287. actualParameters.SetExpression(0,parameter0);
  5288. END;
  5289. type := system.integerType;
  5290. (* ---- SYSTEM.GET8 ----- *)
  5291. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5292. IF CheckAddressType(parameter0) THEN
  5293. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5294. actualParameters.SetExpression(0,parameter0);
  5295. END;
  5296. type := system.shortintType;
  5297. (* ---- SYSTEM.GetStackPointer ----- *)
  5298. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5299. type := system.addressType;
  5300. (* ---- SYSTEM.GetFramePointer ----- *)
  5301. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5302. type := system.addressType;
  5303. (* ---- SYSTEM.GetActivity ----- *)
  5304. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5305. type := system.objectType;
  5306. (* ---- SYSTEM.SetStackPointer ----- *)
  5307. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5308. IF CheckAddressType(parameter0) THEN
  5309. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5310. actualParameters.SetExpression(0,parameter0);
  5311. END;
  5312. (* ---- SYSTEM.SetFramePointer ----- *)
  5313. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5314. IF CheckAddressType(parameter0) THEN
  5315. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5316. actualParameters.SetExpression(0,parameter0);
  5317. END;
  5318. (* ---- SYSTEM.SetActivity ----- *)
  5319. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5320. IF CheckObjectType(parameter0) THEN
  5321. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5322. actualParameters.SetExpression(0,parameter0);
  5323. END;
  5324. (* ---- LSH, LSL, ROT, ROR ----- *)
  5325. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5326. type := type0;
  5327. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5328. actualParameters.SetExpression(1, parameter1);
  5329. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5330. IF id = Global.Lsh THEN
  5331. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5332. ELSIF id = Global.Rot THEN
  5333. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5334. ELSIF id = Global.Ror THEN
  5335. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5336. END;
  5337. END;
  5338. (* ---- SYSTEM.VAL ----- *)
  5339. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5340. IF CheckTypeDeclarationType(parameter0) THEN
  5341. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5342. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5343. result := SyntaxTree.invalidExpression;
  5344. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5345. ELSE
  5346. IF (parameter1.resolved # NIL) THEN
  5347. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5348. IF parameter0 IS SyntaxTree.Value THEN
  5349. result.SetResolved(parameter0(SyntaxTree.Value));
  5350. END;
  5351. END;
  5352. result.SetAssignable(parameter1.assignable);
  5353. END;
  5354. END;
  5355. (* ---- SYSTEM.GET ----- *)
  5356. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5357. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5358. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5359. actualParameters.SetExpression(0,parameter0);
  5360. END;
  5361. (* ---- SYSTEM.PUT ----- *)
  5362. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5363. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5364. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5365. actualParameters.SetExpression(0,parameter0);
  5366. END;
  5367. (* ---- SYSTEM.PUT64 ----- *)
  5368. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5369. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5370. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5371. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5372. actualParameters.SetExpression(0,parameter0);
  5373. actualParameters.SetExpression(1,parameter1);
  5374. END;
  5375. (* ---- SYSTEM.PUT32 ----- *)
  5376. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5377. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5378. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5379. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5380. actualParameters.SetExpression(0,parameter0);
  5381. actualParameters.SetExpression(1,parameter1);
  5382. END;
  5383. (* ---- SYSTEM.PUT16 ----- *)
  5384. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5385. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5386. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5387. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5388. actualParameters.SetExpression(0,parameter0);
  5389. actualParameters.SetExpression(1,parameter1);
  5390. END;
  5391. (* ---- SYSTEM.PUT8 ----- *)
  5392. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5393. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5394. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5395. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5396. actualParameters.SetExpression(0,parameter0);
  5397. actualParameters.SetExpression(1,parameter1);
  5398. END;
  5399. (* ---- SYSTEM.MOVE ----- *)
  5400. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5401. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5402. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5403. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5404. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5405. actualParameters.SetExpression(0,parameter0);
  5406. actualParameters.SetExpression(1,parameter1);
  5407. actualParameters.SetExpression(2,parameter2);
  5408. END;
  5409. (* ---- SYSTEM.NEW ----- *)
  5410. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5411. IF ~IsPointerType(parameter0.type) THEN
  5412. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5413. ELSIF CheckSizeType(parameter1) THEN
  5414. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5415. actualParameters.SetExpression(1,parameter1);
  5416. END;
  5417. (* ----SYSTEM.REF ---- *)
  5418. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5419. type := system.addressType
  5420. (* ---- INCR ----- *)
  5421. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5422. type := system.sizeType;
  5423. base := type0;
  5424. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5425. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5426. IF i1 < 0 THEN
  5427. Error(position,Diagnostics.Invalid,"invalid dimension");
  5428. base := SyntaxTree.invalidType;
  5429. ELSE
  5430. base := ArrayBase(base,i1);
  5431. IF (base # NIL) & Indexable(base) THEN
  5432. ELSE
  5433. Error(position,Diagnostics.Invalid,"len on no array");
  5434. IF VerboseErrorMessage THEN
  5435. Printout.Info("base",base);
  5436. END;
  5437. base := SyntaxTree.invalidType;
  5438. END;
  5439. END;
  5440. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5441. actualParameters.SetExpression(1,parameter1);
  5442. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5443. mathArrayType := base(SyntaxTree.MathArrayType);
  5444. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5445. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5446. type := system.longintType;
  5447. END;
  5448. END;
  5449. ELSE
  5450. type := system.longintType;
  5451. END;
  5452. (* ---- SUM ----- *)
  5453. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5454. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5455. (* ---- ALL ----- *)
  5456. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5457. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5458. (* ---- DIM ----- *)
  5459. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5460. type := system.sizeType;
  5461. IF type0 IS SyntaxTree.MathArrayType THEN
  5462. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5463. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5464. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5465. END;
  5466. ELSE
  5467. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5468. END;
  5469. (* ---- CAS ----- *)
  5470. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5471. IF type0.IsComposite () THEN
  5472. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5473. result := SyntaxTree.invalidExpression;
  5474. ELSIF ~IsVariable (parameter0) THEN
  5475. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5476. result := SyntaxTree.invalidExpression;
  5477. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5478. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5479. result := SyntaxTree.invalidExpression;
  5480. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5481. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5482. result := SyntaxTree.invalidExpression;
  5483. ELSE
  5484. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5485. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5486. type := type0;
  5487. END;
  5488. (* ---- RESHAPE ----- *)
  5489. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5490. IF type0 IS SyntaxTree.MathArrayType THEN
  5491. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5492. base := ArrayBase(type0,MAX(LONGINT));
  5493. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5494. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5495. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5496. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5497. IF ~CompatibleTo(system,type0,parameterType) THEN
  5498. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5499. result := SyntaxTree.invalidExpression;
  5500. ELSE
  5501. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5502. END;
  5503. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5504. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5505. IF ~CompatibleTo(system,type1,parameterType) THEN
  5506. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5507. result := SyntaxTree.invalidExpression;
  5508. ELSE
  5509. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5510. END;
  5511. ELSE
  5512. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5513. result := SyntaxTree.invalidExpression;
  5514. END;
  5515. (* ---- SYSTEM.TYPECODE ----- *)
  5516. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5517. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5518. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5519. type := type.resolved;
  5520. IF type IS SyntaxTree.PointerType THEN
  5521. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5522. END;
  5523. IF ~(type IS SyntaxTree.RecordType) THEN
  5524. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5525. END;
  5526. ELSE
  5527. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5528. END;
  5529. type := system.addressType;
  5530. (* -------- FLT --------- *)
  5531. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5532. type := system.realType;
  5533. IF IsRealValue(parameter0, r) THEN
  5534. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5535. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5536. i0 := i; i := ABS(i);
  5537. IF i # 0 THEN
  5538. i1 := 23;
  5539. IF i >= 2*800000H THEN
  5540. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5541. ELSIF i < 800000H THEN
  5542. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5543. END;
  5544. i := (i1 + 127)*800000H - 800000H + i;
  5545. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5546. END;
  5547. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5548. END;
  5549. (* ------- CONNECT -------*)
  5550. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5551. IF ~(currentIsCellNet) THEN
  5552. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5553. END;
  5554. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5555. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5556. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5557. END;
  5558. IF numberActualParameters = 3 THEN
  5559. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5560. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5561. END;
  5562. *)
  5563. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5564. actualParameters.SetExpression(2,parameter2);
  5565. END;
  5566. activeCellsStatement := TRUE;
  5567. (* ---------- DELEGATE --------*)
  5568. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5569. IF ~(currentIsCellNet) THEN
  5570. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5571. END;
  5572. IF ~CheckPortType(parameter1, inPort) THEN
  5573. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5574. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5575. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5576. ELSIF (outPort.direction # inPort.direction) THEN
  5577. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5578. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5579. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5580. END;
  5581. activeCellsStatement := TRUE;
  5582. (* --------- RECEIVE ---------*)
  5583. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5584. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5585. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5586. IF inPort.direction # SyntaxTree.InPort THEN
  5587. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5588. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5589. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5590. END;
  5591. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5592. IF ~SameType(parameter2.type, system.integerType) THEN
  5593. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5594. END;
  5595. END;
  5596. END;
  5597. (* --------- SEND ---------*)
  5598. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5599. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5600. IF CheckPortType(parameter0,outPort) THEN
  5601. IF outPort.direction # SyntaxTree.OutPort THEN
  5602. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5603. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5604. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5605. ELSE
  5606. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5607. actualParameters.SetExpression(1,parameter1);
  5608. END;
  5609. END;
  5610. (* ------- custom builtins ----- *)
  5611. ELSIF id = Global.systemSpecial THEN
  5612. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5613. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5614. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5615. type := procedureType.returnType;
  5616. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5617. (* go through all formal parameters *)
  5618. formalParameter := procedureType.firstParameter;
  5619. FOR i := 0 TO actualParameters.Length() - 1 DO
  5620. actualParameter := actualParameters.GetExpression(i);
  5621. IF actualParameter = SyntaxTree.invalidExpression THEN
  5622. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5623. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5624. ELSE
  5625. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5626. END;
  5627. actualParameters.SetExpression(i, actualParameter);
  5628. formalParameter := formalParameter.nextParameter
  5629. END
  5630. END
  5631. ELSE
  5632. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5633. result := SyntaxTree.invalidExpression;
  5634. END;
  5635. END;
  5636. IF result # SyntaxTree.invalidExpression THEN
  5637. type := ResolveType(type);
  5638. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5639. result.SetType(type);
  5640. END;
  5641. RETURN result
  5642. END NewBuiltinCallDesignator;
  5643. (** return type guard designator left(type)
  5644. - check if type can be extended (i.e. is no static record)
  5645. - check if type is a type extension of left.type
  5646. - returns new type guard designator
  5647. returns invalidDesignator = invalidExpression if error
  5648. **)
  5649. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5650. VAR result: SyntaxTree.Designator;
  5651. BEGIN
  5652. result := SyntaxTree.invalidDesignator;
  5653. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5654. Error(position,Diagnostics.Invalid,"no type extension of type");
  5655. IF VerboseErrorMessage THEN
  5656. Printout.Info("left",left);
  5657. Printout.Info("type",type);
  5658. END;
  5659. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5660. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5661. ELSE
  5662. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5663. result.SetType(type);
  5664. result.SetAssignable(left.assignable);
  5665. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5666. END;
  5667. RETURN result
  5668. END NewTypeGuardDesignator;
  5669. (** check and resolve parameter designator left(expression list)
  5670. - check expression list
  5671. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5672. - elsif left is a procedure type then
  5673. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5674. - else return is a procedure call then return ProcedureCallDesignator
  5675. returns invalidDesignator = invalidExpression if error
  5676. **)
  5677. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5678. VAR
  5679. parameters: SyntaxTree.ExpressionList;
  5680. left: SyntaxTree.Designator;
  5681. result,expression: SyntaxTree.Expression;
  5682. typeDeclaration: SyntaxTree.TypeDeclaration;
  5683. type, expressionType: SyntaxTree.Type;
  5684. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5685. BEGIN
  5686. type := type.resolved;
  5687. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5688. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5689. END;
  5690. RETURN type
  5691. END BaseType;
  5692. BEGIN
  5693. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5694. result := SyntaxTree.invalidDesignator;
  5695. left := ResolveDesignator(designator.left);
  5696. IF left # SyntaxTree.invalidDesignator THEN
  5697. parameters := designator.parameters;
  5698. IF ExpressionList(parameters) THEN
  5699. IF (left.type = NIL) THEN
  5700. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5701. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5702. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5703. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5704. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5705. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5706. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5707. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5708. ELSE
  5709. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5710. END
  5711. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5712. expression := parameters.GetExpression(0);
  5713. type := typeDeclaration.declaredType.resolved;
  5714. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5715. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5716. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5717. OR (expressionType IS SyntaxTree.EnumerationType)
  5718. ) THEN
  5719. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5720. ELSE
  5721. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5722. END;
  5723. ELSE
  5724. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5725. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5726. result := SyntaxTree.invalidDesignator;
  5727. END;
  5728. ELSE
  5729. result := SyntaxTree.invalidDesignator
  5730. END;
  5731. END;
  5732. resolvedExpression := result;
  5733. END VisitParameterDesignator;
  5734. (** check dereference designator left^
  5735. - check if left is pointer type or left is object type
  5736. - return new dereference designator with type = left.baseType.type (if appropriate)
  5737. with error handling
  5738. returns invalidDesignator = invalidExpression if error
  5739. **)
  5740. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5741. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5742. BEGIN
  5743. result := SyntaxTree.invalidDesignator;
  5744. type := left.type;
  5745. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5746. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5747. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5748. result.SetAssignable(TRUE);
  5749. result.SetType(type);
  5750. result.SetHidden(left.isHidden);
  5751. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5752. type := type.resolved;
  5753. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5754. result.SetAssignable(TRUE);
  5755. result.SetType(type);
  5756. result.SetHidden(left.isHidden);
  5757. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5758. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5759. result.SetAssignable(TRUE);
  5760. result.SetType(type);
  5761. result.SetHidden(left.isHidden);
  5762. ELSE
  5763. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5764. IF VerboseErrorMessage THEN
  5765. Printout.Info("pointer", type);
  5766. Printout.Info("scope", currentScope);
  5767. END;
  5768. END;
  5769. RETURN result
  5770. END NewDereferenceDesignator;
  5771. (** check supercall designator left^
  5772. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5773. - return new supercall designator with type = left.type
  5774. with error handling
  5775. **)
  5776. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5777. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5778. objectScope: SyntaxTree.Scope;
  5779. BEGIN
  5780. result := SyntaxTree.invalidDesignator;
  5781. IF left = SyntaxTree.invalidDesignator THEN
  5782. (* error already handled *)
  5783. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5784. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5785. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5786. IF symbol IS SyntaxTree.Procedure THEN
  5787. procedure := symbol(SyntaxTree.Procedure);
  5788. objectScope := currentScope;
  5789. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5790. objectScope := objectScope.outerScope;
  5791. END;
  5792. IF (left.left = NIL) OR ~
  5793. (
  5794. (left.left IS SyntaxTree.SelfDesignator) OR
  5795. (left.left IS SyntaxTree.DereferenceDesignator)
  5796. & (left.left(SyntaxTree.Designator).left # NIL)
  5797. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5798. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5799. IF VerboseErrorMessage THEN
  5800. Printout.Info("left.left",left.left);
  5801. END;
  5802. ELSIF procedure.super # NIL THEN
  5803. result := SyntaxTree.NewSupercallDesignator(position,left);
  5804. result.SetType(left.type.resolved)
  5805. ELSE
  5806. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5807. END;
  5808. ELSE
  5809. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5810. END;
  5811. ELSE
  5812. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5813. END;
  5814. RETURN result
  5815. END NewSupercallDesignator;
  5816. (** check and semantically resolve arrow designator left^
  5817. - if left is procedure type -> result := SupercallDesignator
  5818. - else result := DereferenceDesignator
  5819. returns result via global variable resolvedExpression
  5820. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5821. **)
  5822. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5823. VAR left: SyntaxTree.Designator;
  5824. BEGIN
  5825. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5826. left := ResolveDesignator(arrowDesignator.left);
  5827. IF left # NIL THEN
  5828. IF (left.type = NIL) THEN
  5829. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5830. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5831. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5832. ELSE
  5833. IF IsPointerToObject(left.type) THEN
  5834. Warning(arrowDesignator.position, "forbidden dereference on object");
  5835. END;
  5836. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5837. END
  5838. END
  5839. END VisitArrowDesignator;
  5840. (** check and return expression
  5841. - if expression has no type then resolve expression
  5842. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5843. - return result
  5844. **)
  5845. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5846. VAR result,prev: SyntaxTree.Expression;
  5847. BEGIN
  5848. IF expression = NIL THEN result := NIL
  5849. ELSIF (expression.type = NIL) THEN
  5850. prev := resolvedExpression;
  5851. resolvedExpression := SyntaxTree.invalidExpression;
  5852. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5853. expression.SetType(SyntaxTree.invalidType);
  5854. END;
  5855. expression.Accept(SELF);
  5856. result := resolvedExpression;
  5857. IF currentIsRealtime THEN
  5858. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5859. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5860. END;
  5861. END;
  5862. (* designator modifiers for backends if they support it ...*)
  5863. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5864. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5865. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5866. END;
  5867. resolvedExpression := prev
  5868. ELSE
  5869. result := expression
  5870. END;
  5871. RETURN result
  5872. END ResolveExpression;
  5873. (**
  5874. check expression to be constant expression
  5875. - resolve expression
  5876. - if valid then check that of value type
  5877. report error and return invalidExpression if anything fails
  5878. **)
  5879. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5880. VAR position: LONGINT;
  5881. BEGIN
  5882. position := expression.position;
  5883. expression := ResolveExpression(expression);
  5884. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5885. ELSIF (expression.resolved = NIL) THEN
  5886. Error(position,Diagnostics.Invalid,"expression is not constant");
  5887. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5888. expression := SyntaxTree.invalidExpression;
  5889. END;
  5890. RETURN expression
  5891. END ConstantExpression;
  5892. (** check expression to be constant integer
  5893. - resolve expresssion
  5894. - if valid then check that of integer value type
  5895. report error and return invalidExpression if anything fails
  5896. **)
  5897. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5898. VAR position: LONGINT;
  5899. BEGIN
  5900. position := expression.position;
  5901. expression := ResolveExpression(expression);
  5902. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5903. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5904. expression := SyntaxTree.invalidExpression;
  5905. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5906. END;
  5907. RETURN expression
  5908. END ConstantInteger;
  5909. (** check expression as positive (>=0) constant integer
  5910. - resolve expression
  5911. - if valid then check that integer value
  5912. - if integer value then check that value >= 0
  5913. report error and return invalidExpression if anything fails
  5914. **)
  5915. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5916. VAR position: LONGINT;
  5917. BEGIN
  5918. position := expression.position;
  5919. expression := ConstantExpression(expression);
  5920. IF expression = SyntaxTree.invalidExpression THEN
  5921. (* error already reported *)
  5922. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5923. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5924. expression := SyntaxTree.invalidExpression
  5925. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5926. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5927. END;
  5928. RETURN expression
  5929. END ConstantIntegerGeq0;
  5930. (** check expression as condition
  5931. - resolve expression
  5932. - if valid expression then check that result type is boolean
  5933. report error and return invalidExpression if anything fails
  5934. **)
  5935. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5936. VAR position: LONGINT;
  5937. BEGIN
  5938. position := expression.position;
  5939. expression := ResolveExpression(expression);
  5940. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5941. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5942. expression := SyntaxTree.invalidExpression;
  5943. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5944. END;
  5945. RETURN expression
  5946. END ResolveCondition;
  5947. (*** symbols ***)
  5948. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5949. BEGIN
  5950. x.Accept(SELF);
  5951. END ResolveSymbol;
  5952. (** check a symbol
  5953. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5954. **)
  5955. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5956. VAR scope: SyntaxTree.Scope;
  5957. BEGIN
  5958. (* visibility *)
  5959. scope := symbol.scope;
  5960. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5961. scope := scope.outerScope;
  5962. END;
  5963. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5964. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5965. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5966. IF VerboseErrorMessage THEN
  5967. Printout.Info("symbol",symbol);
  5968. END;
  5969. END;
  5970. END;
  5971. END CheckSymbolVisibility;
  5972. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5973. If node is currently being resolved then emit a cyclic definition error.
  5974. Return TRUE only if node is fully resolved.
  5975. **)
  5976. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5977. VAR result: BOOLEAN;
  5978. BEGIN
  5979. IF SyntaxTree.Resolved IN x.state THEN
  5980. result := FALSE
  5981. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5982. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5983. result := FALSE;
  5984. ELSE
  5985. result := TRUE;
  5986. x.SetState(SyntaxTree.BeingResolved)
  5987. END;
  5988. RETURN result
  5989. END SymbolNeedsResolution;
  5990. (** check and resolve a type declaration symbol = Type
  5991. - set type to declaration type
  5992. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5993. This is so because the type declaration itself does not have a type but it only stands for a type.
  5994. In the implementation of the compiler this made a lot much easier.
  5995. - resolve and set declared type
  5996. - check symbol
  5997. **)
  5998. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5999. VAR prevScope: SyntaxTree.Scope;
  6000. BEGIN
  6001. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6002. IF SymbolNeedsResolution(typeDeclaration) THEN
  6003. prevScope := currentScope;
  6004. currentScope := typeDeclaration.scope;
  6005. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6006. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6007. CheckSymbolVisibility(typeDeclaration);
  6008. typeDeclaration.SetState(SyntaxTree.Resolved);
  6009. currentScope := prevScope;
  6010. END;
  6011. END VisitTypeDeclaration;
  6012. (** check and resolve a constant declaration symbol = (constant) expression
  6013. - check expression
  6014. - set type and value
  6015. - check symbol
  6016. **)
  6017. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6018. VAR
  6019. expression: SyntaxTree.Expression;
  6020. type: SyntaxTree.Type;
  6021. name: Basic.SegmentedName;
  6022. replacement: Replacement;
  6023. stringReader: Streams.StringReader;
  6024. scanner: Scanner.Scanner; parser: Parser.Parser;
  6025. BEGIN
  6026. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6027. IF SymbolNeedsResolution(constant) THEN
  6028. expression := constant.value;
  6029. IF replacements # NIL THEN
  6030. Global.GetSymbolSegmentedName(constant, name);
  6031. replacement := replacements;
  6032. WHILE (replacement # NIL) & (replacement.name # name) DO
  6033. replacement := replacement.next;
  6034. END;
  6035. IF replacement # NIL THEN
  6036. InfoSS(constant.position, "replacing constant", constant.name);
  6037. (*
  6038. NEW(stringReader, Strings.Length(replacement.string^));
  6039. stringReader.Set(replacement.string^);
  6040. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6041. NEW(parser, scanner, diagnostics);
  6042. expression := parser.Expression();
  6043. *)
  6044. expression := replacement.expression;
  6045. replacement.used := TRUE;
  6046. END;
  6047. END;
  6048. constant.SetType(SyntaxTree.invalidType);
  6049. expression := ConstantExpression(expression);
  6050. ASSERT(expression.type # NIL);
  6051. type := expression.type.resolved;
  6052. constant.SetType(type);
  6053. constant.SetValue(expression);
  6054. CheckSymbolVisibility(constant);
  6055. constant.SetState(SyntaxTree.Resolved);
  6056. END;
  6057. END VisitConstant;
  6058. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6059. VAR procedureAlignment: LONGINT;
  6060. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6061. (* least common multiple *)
  6062. VAR a,b: LONGINT;
  6063. BEGIN
  6064. a := a0; b := b0;
  6065. WHILE (a # b) DO
  6066. IF a < b THEN a := a+a0
  6067. ELSE b := b + b0
  6068. END;
  6069. END;
  6070. RETURN a
  6071. END LCM;
  6072. BEGIN
  6073. IF alignment > 1 THEN
  6074. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6075. IF (procedureAlignment > 1) THEN
  6076. alignment := LCM(alignment, procedureAlignment);
  6077. END;
  6078. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6079. END;
  6080. END AdaptStackAlignment;
  6081. (** check and resolve a variable / field
  6082. - check and set type
  6083. - negative check on open array type
  6084. - check symbol
  6085. **)
  6086. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6087. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6088. BEGIN
  6089. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6090. IF SymbolNeedsResolution(variable) THEN
  6091. modifiers := variable.modifiers;
  6092. (*
  6093. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6094. variable.AddFlags(flags);
  6095. *)
  6096. variable.SetType(ResolveType(variable.type));
  6097. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6098. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6099. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6100. END;
  6101. END;
  6102. CheckSymbolVisibility(variable);
  6103. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6104. variable.SetUntraced(TRUE);
  6105. IF ~ContainsPointer(variable.type) THEN
  6106. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6107. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6108. END;
  6109. END;
  6110. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6111. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6112. IF ~PowerOf2(value) THEN
  6113. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6114. ELSE
  6115. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6116. END;
  6117. END;
  6118. variable.SetAlignment(FALSE,value);
  6119. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6120. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6121. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6122. END;
  6123. variable.SetAlignment(TRUE, value);
  6124. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6125. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6126. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6127. END;
  6128. variable.SetFictive(TRUE);
  6129. variable.SetOffset(value*system.dataUnit);
  6130. END;
  6131. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6132. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6133. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6134. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6135. END;
  6136. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6137. IF variable.initializer # NIL THEN
  6138. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6139. END;
  6140. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6141. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6142. pointerType.SetPointerBase(variable.type);
  6143. pointerType.SetHidden(TRUE);
  6144. variable.SetType(ResolveType(pointerType));
  6145. END;
  6146. variable.SetState(SyntaxTree.Resolved);
  6147. END;
  6148. END VisitVariable;
  6149. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6150. BEGIN
  6151. VisitVariable(property)
  6152. END VisitProperty;
  6153. (** check and resolve a (procedure) parameter
  6154. - check and set type
  6155. - check symbol
  6156. - check parameter kind and set read-only flags if appropriate
  6157. **)
  6158. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6159. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6160. BEGIN
  6161. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6162. IF SymbolNeedsResolution(parameter) THEN
  6163. modifiers := parameter.modifiers;
  6164. parameter.SetType(ResolveType(parameter.type));
  6165. ASSERT(parameter.type.resolved # NIL);
  6166. CheckSymbolVisibility(parameter);
  6167. IF parameter.defaultValue # NIL THEN
  6168. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6169. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6170. ELSE
  6171. expression := ConstantExpression(parameter.defaultValue);
  6172. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6173. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6174. parameter.SetDefaultValue(expression);
  6175. END;
  6176. END;
  6177. END;
  6178. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6179. parameter.SetUntraced(TRUE);
  6180. IF ~ContainsPointer(parameter.type) THEN
  6181. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6182. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6183. END;
  6184. END;
  6185. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6186. parameter.SetMoveable(TRUE);
  6187. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6188. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6189. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6190. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6191. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6192. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6193. END;
  6194. END;
  6195. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6196. parameter.SetState(SyntaxTree.Resolved);
  6197. END;
  6198. END VisitParameter;
  6199. (** check and resolve a procedure (with declaration and implementation scope)
  6200. - check the procedure type
  6201. - check if method (i.e. in record scope), if so then
  6202. - check if (unique) constructor
  6203. - check if (unique) finalizer
  6204. - check if super method available, if so then check signature
  6205. - of not in record scope then negative check on constructor flag
  6206. - of not in record scope then negative check on finalizer flag
  6207. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6208. - check procedure symbol
  6209. **)
  6210. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6211. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6212. procedureType: SyntaxTree.ProcedureType;
  6213. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6214. qualifiedType: SyntaxTree.QualifiedType;
  6215. value: LONGINT;
  6216. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6217. position: LONGINT;
  6218. BEGIN
  6219. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6220. IF IsOberonInline(procedure) THEN
  6221. IF SyntaxTree.Public * procedure.access # {} THEN
  6222. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6223. END;
  6224. procedure.SetInline(FALSE);
  6225. procedure.SetOberonInline(TRUE);
  6226. END;
  6227. IF SymbolNeedsResolution(procedure) THEN
  6228. recentIsRealtime := currentIsRealtime;
  6229. recentIsBodyProcedure := currentIsBodyProcedure;
  6230. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6231. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6232. modifiers := procedureType.modifiers;
  6233. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6234. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6235. IF useDarwinCCalls THEN (*fld*)
  6236. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6237. ELSE
  6238. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6239. END
  6240. END;
  6241. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6242. procedureType.SetInterrupt(TRUE);
  6243. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6244. END;
  6245. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6246. procedureType.SetNoReturn(TRUE);
  6247. END;
  6248. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6249. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6250. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6251. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6252. END;
  6253. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6254. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6255. END;
  6256. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6257. IF ~PowerOf2(value) THEN
  6258. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6259. ELSE
  6260. procedureType.SetStackAlignment(value)
  6261. END;
  6262. END;
  6263. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6264. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6265. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6266. END;
  6267. CheckModifiers(modifiers, TRUE);
  6268. modifiers := procedureType.returnTypeModifiers;
  6269. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6270. CheckModifiers(modifiers, TRUE);
  6271. FixProcedureType(procedureType);
  6272. currentIsRealtime := procedureType.isRealtime;
  6273. currentIsBodyProcedure := procedure.isBodyProcedure;
  6274. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6275. THEN
  6276. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6277. END;
  6278. CheckSymbolVisibility(procedure);
  6279. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6280. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6281. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6282. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6283. END;
  6284. END;
  6285. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6286. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6287. procedureType.SetDelegate(TRUE);
  6288. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6289. IF record.pointerType.typeDeclaration = NIL THEN
  6290. selfParameter.SetType(record.pointerType);
  6291. ELSE
  6292. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6293. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6294. qualifiedType.SetResolved(record.pointerType);
  6295. selfParameter.SetType(qualifiedType);
  6296. END;
  6297. selfParameter.SetAccess(SyntaxTree.Hidden);
  6298. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6299. IF procedure.isConstructor THEN
  6300. (*! constructor is always visible, compatibility to paco
  6301. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6302. *)
  6303. procedure.MarkUsed;
  6304. IF procedureType.returnType # NIL THEN
  6305. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6306. END;
  6307. proc := procedure.scope.firstProcedure;
  6308. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6309. proc := proc.nextProcedure;
  6310. END;
  6311. IF proc # NIL THEN
  6312. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6313. ELSE
  6314. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6315. END;
  6316. END;
  6317. IF procedure.isFinalizer THEN
  6318. procedure.MarkUsed;
  6319. IF procedureType.returnType # NIL THEN
  6320. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6321. END;
  6322. IF procedureType.numberParameters # 0 THEN
  6323. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6324. END;
  6325. proc := procedure.scope.firstProcedure;
  6326. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6327. proc := proc.nextProcedure;
  6328. END;
  6329. IF proc # NIL THEN
  6330. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6331. ELSE
  6332. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6333. END;
  6334. END;
  6335. super := FindSuperProcedure(record.recordScope, procedure);
  6336. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6337. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6338. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6339. END;
  6340. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6341. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6342. END;
  6343. IF super.isFinal THEN
  6344. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6345. END;
  6346. procedure.SetSuper(super);
  6347. super.SetOverwritten(TRUE);
  6348. procedure.SetAccess(procedure.access+super.access);
  6349. procedure.MarkUsed;
  6350. END;
  6351. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6352. THEN
  6353. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6354. END;
  6355. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6356. IF cellsAreObjects THEN
  6357. procedureType.SetDelegate(TRUE);
  6358. END;
  6359. IF procedure.isConstructor THEN
  6360. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6361. END;
  6362. ELSIF procedure.isConstructor THEN
  6363. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6364. END;
  6365. Declarations(procedure.procedureScope);
  6366. (* body resolution part done as late fix of the procedure type *)
  6367. procedure.SetState(SyntaxTree.Resolved);
  6368. currentIsRealtime := recentIsRealtime;
  6369. currentIsBodyProcedure := recentIsBodyProcedure;
  6370. END;
  6371. END VisitProcedure;
  6372. (**
  6373. a builtin procedure is a global item that may not be modified locally
  6374. instead the resolving of builtin procedure calls are done in the esignator
  6375. **)
  6376. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6377. VAR type: SyntaxTree.Type;
  6378. BEGIN
  6379. type := ResolveType(builtinProcedure.type);
  6380. END VisitBuiltin;
  6381. (* nopov *)
  6382. (** check and resolve operator
  6383. - operators are first checked as procedures
  6384. - then additional operator-specific checks are done
  6385. - note that only module-scope operators are checked here
  6386. (operators in a record scope are only allowed in the context of
  6387. array-structured object types and checked in 'ResolveArrayStructure')
  6388. - also note that inter-operator conformity is not checked here
  6389. **)
  6390. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6391. VAR
  6392. procedureType: SyntaxTree.ProcedureType;
  6393. leftType, rightType: SyntaxTree.Type;
  6394. identifierNumber, position: LONGINT;
  6395. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6396. modifiers: SyntaxTree.Modifier;
  6397. (** whether a type is locally defined in the current module scope
  6398. for arrays, the base type must be locally defined **)
  6399. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6400. BEGIN
  6401. IF type = NIL THEN
  6402. RETURN FALSE
  6403. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6404. RETURN TRUE
  6405. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6406. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6407. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6408. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6409. ELSE
  6410. RETURN FALSE
  6411. END
  6412. END IsLocallyDefined;
  6413. BEGIN
  6414. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6415. procedureType := operator.type(SyntaxTree.ProcedureType);
  6416. modifiers := procedureType.modifiers;
  6417. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6418. CheckModifiers(modifiers, TRUE);
  6419. VisitProcedure(operator);
  6420. IF operator.scope IS SyntaxTree.RecordScope THEN
  6421. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6422. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6423. IF identifierNumber = -1 THEN
  6424. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6425. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6426. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6427. ELSE
  6428. IF procedureType.numberParameters < 1 THEN
  6429. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6430. ELSIF procedureType.numberParameters > 2 THEN
  6431. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6432. ELSE
  6433. (* determine operand types *)
  6434. leftType := procedureType.firstParameter.type;
  6435. IF procedureType.numberParameters > 1 THEN
  6436. rightType := procedureType.firstParameter.nextParameter.type
  6437. ELSE
  6438. rightType := NIL
  6439. END;
  6440. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6441. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6442. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6443. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6444. END
  6445. END;
  6446. (* TODO: refine the checks, think about how restrictive the checks should be
  6447. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6448. They might be used for intersection, union, complement of custom object types *)
  6449. (* defaults *)
  6450. hasReturnType := TRUE;
  6451. mustBeUnary := FALSE;
  6452. mustBeBinary := FALSE;
  6453. mustReturnBoolean := FALSE;
  6454. mustReturnInteger := FALSE;
  6455. mustHaveEquitypedOperands := FALSE;
  6456. (* operator-specific exceptions *)
  6457. CASE identifierNumber OF
  6458. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6459. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6460. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6461. mustBeBinary := TRUE
  6462. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6463. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6464. | Scanner.Times: mustBeBinary := TRUE
  6465. | Scanner.TimesTimes: mustBeBinary := TRUE
  6466. | Scanner.DotTimes: mustBeBinary := TRUE
  6467. | Scanner.PlusTimes: mustBeBinary := TRUE
  6468. | Scanner.Slash: mustBeBinary := TRUE
  6469. | Scanner.Backslash: mustBeBinary := TRUE
  6470. | Scanner.DotSlash: mustBeBinary := TRUE
  6471. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6472. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6473. | Scanner.Not: mustBeUnary := TRUE
  6474. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6475. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6476. | Scanner.Transpose: mustBeUnary := TRUE;
  6477. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6478. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6479. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6480. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6481. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6482. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6483. | Global.Abs: mustBeUnary := TRUE;
  6484. | Global.Ash: (* TODO: arity? *)
  6485. | Global.Cap: (* TODO: arity? *)
  6486. | Global.Chr: mustBeUnary := TRUE;
  6487. | Global.Entier: (* TODO: arity? *)
  6488. | Global.EntierH: (* TODO: arity? *)
  6489. | Global.Len: (* unary and binary *)
  6490. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6491. | Global.Max, Global.Min: (* unary and binary *)
  6492. | Global.Odd: (* TODO: arity? *)
  6493. | Global.Sum: (* TODO: arity? *)
  6494. | Global.All: (* TODO: arity? *)
  6495. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6496. | Scanner.Alias:
  6497. | Scanner.GreaterGreater, Scanner.LessLess:
  6498. mustBeBinary := TRUE; hasReturnType := FALSE;
  6499. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6500. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6501. END;
  6502. (* check parameter count *)
  6503. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6504. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6505. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6506. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6507. END;
  6508. (* check parameter types *)
  6509. (* TODO: is this used at all? *)
  6510. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6511. leftType := procedureType.firstParameter.type;
  6512. rightType := procedureType.firstParameter.nextParameter.type;
  6513. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6514. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6515. END
  6516. END;
  6517. (* check return type *)
  6518. IF hasReturnType THEN
  6519. IF procedureType.returnType = NIL THEN
  6520. Error(operator.position, Diagnostics.Invalid, "return type required")
  6521. ELSIF mustReturnBoolean THEN
  6522. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6523. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6524. END
  6525. ELSIF mustReturnInteger THEN
  6526. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6527. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6528. END
  6529. END
  6530. ELSIF procedureType.returnType # NIL THEN
  6531. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6532. END
  6533. END
  6534. END
  6535. END
  6536. END VisitOperator;
  6537. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6538. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6539. BEGIN
  6540. IF error THEN RETURN FALSE END;
  6541. prevScope := currentScope;
  6542. prevDiagnostics := diagnostics;
  6543. diagnostics := NIL; (* suppress error output *)
  6544. currentScope := module.moduleScope;
  6545. VisitImport(x);
  6546. IF ~error THEN
  6547. module.moduleScope.AddImport(x);
  6548. x.SetScope(module.moduleScope);
  6549. END;
  6550. currentScope := prevScope;
  6551. diagnostics := prevDiagnostics;
  6552. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6553. END AddImport;
  6554. (** check and resolve import
  6555. - check for name = SYSTEM
  6556. - check for forbidden self import
  6557. - search through global import cache: already imported?
  6558. - check if already imported indirectly
  6559. - import if necessary -> set module and enter into import cache
  6560. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6561. - after this import this direct import and all indirect imports are stored in the current module's import list
  6562. **)
  6563. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6564. VAR
  6565. module: SyntaxTree.Module;
  6566. moduleScope: SyntaxTree.ModuleScope;
  6567. import,reimport: SyntaxTree.Import;
  6568. filename: FileName;
  6569. prevScope: SyntaxTree.Scope;
  6570. BEGIN
  6571. IF SymbolNeedsResolution(x) THEN
  6572. prevScope := currentScope;
  6573. x.SetType(SyntaxTree.importType);
  6574. moduleScope := currentScope.ownerModule.moduleScope;
  6575. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6576. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6577. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6578. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6579. ELSE
  6580. (* search through global import list: already imported ? *)
  6581. IF (x.module = NIL) & (importCache # NIL) THEN
  6582. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6583. ELSE import := NIL
  6584. END;
  6585. IF x.module # NIL THEN (* already imported indirectly *)
  6586. module := x.module;
  6587. ELSIF import # NIL THEN (* already in module list *)
  6588. module := import.module;
  6589. ASSERT(module # NIL);
  6590. x.SetModule(module);
  6591. ELSE (* must be imported *)
  6592. Global.ModuleFileName(x.moduleName,x.context,filename);
  6593. IF symbolFileFormat # NIL THEN
  6594. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6595. IF module = NIL THEN
  6596. ErrorSS(x.position,"could not import",filename);
  6597. IF VerboseErrorMessage THEN
  6598. Printout.Info("import",x)
  6599. END
  6600. ELSE
  6601. (*
  6602. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6603. (*! should rather be done by importer *)
  6604. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6605. checker.importCache := importCache;
  6606. checker.arrayBaseImported := arrayBaseImported;
  6607. checker.global := global;
  6608. checker.Module(module); (* semantic check *)
  6609. error := error OR checker.error;
  6610. END;
  6611. *)
  6612. (*
  6613. ASSERT(SyntaxTree.Resolved IN module.state);
  6614. *)
  6615. x.SetModule(module);
  6616. IF importCache # NIL THEN
  6617. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6618. import.SetContext(x.context);
  6619. import.SetModule(module);
  6620. importCache.AddImport(import);
  6621. END;
  6622. END;
  6623. ELSE
  6624. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6625. END;
  6626. END;
  6627. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6628. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6629. END;
  6630. import := module.moduleScope.firstImport;
  6631. WHILE(import # NIL) DO
  6632. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6633. ASSERT(currentScope # NIL);
  6634. ASSERT(currentScope.ownerModule # NIL);
  6635. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6636. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6637. Error(x.position,Diagnostics.Invalid,"recursive import");
  6638. ELSE
  6639. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6640. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6641. IF reimport = NIL THEN (* indirect import *)
  6642. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6643. reimport.SetContext(import.context);
  6644. reimport.SetModule(import.module);
  6645. moduleScope.AddImport(reimport);
  6646. reimport.SetScope(moduleScope);
  6647. ELSE
  6648. ASSERT(import.module # NIL);
  6649. reimport.SetModule(import.module); (* direct or indirect import *)
  6650. END;
  6651. END;
  6652. import := import.nextImport;
  6653. END;
  6654. END;
  6655. END;
  6656. currentScope := prevScope;
  6657. (* ELSE nothing to be done *)
  6658. x.SetState(SyntaxTree.Resolved);
  6659. END;
  6660. END VisitImport;
  6661. (*** statements ***)
  6662. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6663. VAR prev,resolved: SyntaxTree.Statement;
  6664. BEGIN
  6665. prev := resolvedStatement;
  6666. resolvedStatement := x;
  6667. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6668. activeCellsStatement := FALSE;
  6669. x.Accept(SELF);
  6670. (* removed this, implementation restriction should be resolved by backend
  6671. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6672. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6673. END;
  6674. *)
  6675. resolved := resolvedStatement;
  6676. resolvedStatement := prev;
  6677. RETURN resolved
  6678. END ResolveStatement;
  6679. (** check and resolve statement sequence
  6680. - check all statements, replace if necessary
  6681. **)
  6682. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6683. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6684. BEGIN
  6685. IF statementSequence # NIL THEN (* else empty *)
  6686. FOR i := 0 TO statementSequence.Length()-1 DO
  6687. statement := statementSequence.GetStatement(i);
  6688. resolved := ResolveStatement(statement);
  6689. IF (resolved # statement) THEN
  6690. statementSequence.SetStatement(i,resolved);
  6691. END;
  6692. END;
  6693. END;
  6694. END StatementSequence;
  6695. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6696. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6697. - check if procedure is callable
  6698. - check return type = NIL (otherwise must be assignment statement)
  6699. **)
  6700. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6701. VAR call: SyntaxTree.Designator;
  6702. BEGIN
  6703. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6704. call := procedureCall.call;
  6705. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6706. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6707. END;
  6708. call := ResolveDesignator(call);
  6709. IF call = SyntaxTree.invalidDesignator THEN
  6710. (* error already handled *)
  6711. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6712. (* inline call in a statement *)
  6713. ELSIF ~IsCallable(call) THEN
  6714. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6715. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6716. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6717. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6718. END;
  6719. procedureCall.SetCall(call);
  6720. (*
  6721. IF call = SyntaxTree.invalidDesignator THEN
  6722. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6723. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6724. IF IsOberonInline(procedure) THEN
  6725. Warning(procedure.position,"call to inline proc");
  6726. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6727. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6728. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6729. resolvedStatement := block;
  6730. RETURN;
  6731. END;
  6732. END;
  6733. *)
  6734. END VisitProcedureCallStatement;
  6735. (** check and resolve assignment LHS := RHS
  6736. - resolve LHS and RHS
  6737. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6738. - check if assignment is compatible
  6739. - check if LHS is variable (i.e. assignable)
  6740. - convert RHS if necessary
  6741. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6742. - assignment between different ASOTs
  6743. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6744. - assignment to ASOT elements:
  6745. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6746. **)
  6747. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6748. VAR
  6749. left: SyntaxTree.Designator;
  6750. right, expression: SyntaxTree.Expression;
  6751. designator: SyntaxTree.Designator;
  6752. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6753. mathArrayType: SyntaxTree.MathArrayType;
  6754. BEGIN
  6755. right := ResolveExpression(assignment.right);
  6756. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6757. left := ResolveDesignator(assignment.left);
  6758. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6759. (* error already handled *)
  6760. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6761. (* LHS is index write operator call on ASOT *)
  6762. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6763. (* necessary ?
  6764. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6765. type := procedureType.firstParameter.type;
  6766. expression := procedureCallDesignator.parameters.GetExpression(0);
  6767. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6768. *)
  6769. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6770. ELSIF CheckVariable(left) THEN
  6771. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6772. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6773. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6774. (* conversion done by procedure call
  6775. (* try to convert to left argument *)
  6776. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6777. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6778. procedureCallDesignator.parameters.SetExpression(1, right);
  6779. END;
  6780. *)
  6781. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6782. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6783. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6784. ELSIF AssignmentCompatible(left, right) THEN
  6785. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6786. mathArrayType := MathArrayStructureOfType(left.type);
  6787. right := NewConversion(right.position, right, mathArrayType, NIL);
  6788. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6789. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6790. ELSE
  6791. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6792. assignment.SetLeft(left);
  6793. assignment.SetRight(right);
  6794. resolvedStatement := assignment
  6795. END
  6796. END
  6797. END
  6798. END VisitAssignment;
  6799. (** check and resolve assignment LHS := RHS
  6800. - resolve LHS and RHS
  6801. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6802. - check if assignment is compatible
  6803. - check if LHS is variable (i.e. assignable)
  6804. - convert RHS if necessary
  6805. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6806. - assignment between different ASOTs
  6807. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6808. - assignment to ASOT elements:
  6809. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6810. **)
  6811. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6812. VAR
  6813. left: SyntaxTree.Designator;
  6814. right: SyntaxTree.Expression;
  6815. inPort, outPort: SyntaxTree.PortType;
  6816. expression: SyntaxTree.Expression;
  6817. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6818. BEGIN
  6819. right := ResolveExpression(communication.right);
  6820. left := ResolveDesignator(communication.left);
  6821. communication.SetLeft(left);
  6822. communication.SetRight(right);
  6823. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6824. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6825. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6826. (* conversion done by procedure call
  6827. (* try to convert to left argument *)
  6828. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6829. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6830. procedureCallDesignator.parameters.SetExpression(1, right);
  6831. END;
  6832. *)
  6833. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6834. ELSE
  6835. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6836. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6837. (* error already handled *)
  6838. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6839. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6840. IF outPort.direction # SyntaxTree.OutPort THEN
  6841. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6842. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6843. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6844. ELSE
  6845. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6846. communication.SetRight(right)
  6847. END;
  6848. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6849. IF CheckVariable(left) THEN
  6850. IF inPort.direction # SyntaxTree.InPort THEN
  6851. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6852. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6853. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6854. END;
  6855. END;
  6856. ELSE
  6857. Error(communication.position, -1, "unsupported stream operation");
  6858. END;
  6859. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6860. IF outPort.direction # SyntaxTree.OutPort THEN
  6861. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6862. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6863. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6864. ELSE
  6865. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6866. communication.SetRight(right)
  6867. END;
  6868. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6869. IF CheckVariable(right) THEN
  6870. IF inPort.direction # SyntaxTree.InPort THEN
  6871. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6872. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6873. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6874. END;
  6875. END;
  6876. ELSE
  6877. Error(communication.position, -1, "unsupported operation");
  6878. END;
  6879. END;
  6880. END VisitCommunicationStatement;
  6881. (** check and resolve if/eslif part
  6882. - check condition
  6883. - check statement sequence
  6884. **)
  6885. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6886. VAR prevUnreachable, b: BOOLEAN;
  6887. BEGIN
  6888. prevUnreachable := currentIsUnreachable;
  6889. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6890. IF IsBooleanValue(ifPart.condition,b) THEN
  6891. IF b=FALSE THEN
  6892. currentIsUnreachable := TRUE
  6893. ELSIF b=TRUE THEN
  6894. true := TRUE
  6895. END;
  6896. END;
  6897. StatementSequence(ifPart.statements);
  6898. currentIsUnreachable := prevUnreachable;
  6899. END IfPart;
  6900. (** check and resolve if statement
  6901. - check if parts and else part statement sequence
  6902. **)
  6903. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6904. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6905. BEGIN
  6906. prevUnreachable := currentIsUnreachable;
  6907. ifPartTrue := FALSE;
  6908. IfPart(ifStatement.ifPart,ifPartTrue);
  6909. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6910. elsif := ifStatement.GetElsifPart(i);
  6911. IfPart(elsif,ifPartTrue);
  6912. END;
  6913. IF ifStatement.elsePart # NIL THEN
  6914. IF ifPartTrue THEN
  6915. currentIsUnreachable := TRUE
  6916. END;
  6917. StatementSequence(ifStatement.elsePart)
  6918. END;
  6919. currentIsUnreachable := prevUnreachable;
  6920. END VisitIfStatement;
  6921. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6922. VAR variable: SyntaxTree.Designator;
  6923. type,variableType: SyntaxTree.Type;
  6924. withEntry: WithEntry;
  6925. BEGIN
  6926. variable := ResolveDesignator(withPart.variable);
  6927. variableType := variable.type.resolved;
  6928. withPart.SetVariable(variable);
  6929. type := ResolveType(withPart.type);
  6930. withPart.SetType(type);
  6931. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6932. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6933. END;
  6934. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6935. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6936. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6937. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6938. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6939. IF VerboseErrorMessage THEN
  6940. Printout.Info("variable",variable)
  6941. END;
  6942. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6943. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6944. IF VerboseErrorMessage THEN
  6945. Printout.Info("variable",variable);
  6946. Printout.Info("type",type);
  6947. END;
  6948. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6949. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6950. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6951. IF VerboseErrorMessage THEN
  6952. Printout.Info("variable",variable);
  6953. Printout.Info("type",type);
  6954. END;
  6955. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6956. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6957. ELSE
  6958. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6959. NEW(withEntry);
  6960. withEntry.previous := withEntries;
  6961. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6962. withEntry.type := type;
  6963. withEntries := withEntry;
  6964. StatementSequence(withPart.statements);
  6965. withEntries := withEntries.previous;
  6966. END;
  6967. END WithPart;
  6968. (** check and resolve with statement WITH variable: type DO ... END;
  6969. - check type and variable
  6970. - check that variable type is type extension of type
  6971. - check that variable is a variable
  6972. - enter new with scope and enter guardedVariable with same name and reference to variable
  6973. - create if statement:
  6974. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6975. **)
  6976. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6977. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6978. BEGIN
  6979. prevScope := currentScope; symbol := NIL;
  6980. FOR i := 0 TO withStatement.WithParts()-1 DO
  6981. WithPart(withStatement.GetWithPart(i),symbol);
  6982. END;
  6983. IF withStatement.elsePart # NIL THEN
  6984. StatementSequence(withStatement.elsePart)
  6985. END;
  6986. currentScope := prevScope;
  6987. END VisitWithStatement;
  6988. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6989. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6990. - check 'first' < 'last' and no overlaps between different case labels
  6991. - check statement sequence
  6992. **)
  6993. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6994. VAR
  6995. i: LONGINT;
  6996. position: LONGINT;
  6997. expression, left, right: SyntaxTree.Expression;
  6998. expressionType: SyntaxTree.Type;
  6999. l, r: LONGINT;
  7000. cl, cr: CHAR;
  7001. thiscases: SyntaxTree.CaseConstant;
  7002. BEGIN
  7003. thiscases := NIL;
  7004. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7005. expression := casePart.elements.GetExpression(i);
  7006. position := expression.position;
  7007. (* set context of range *)
  7008. IF expression IS SyntaxTree.RangeExpression THEN
  7009. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7010. END;
  7011. expression := ResolveExpression(expression);
  7012. IF expression = SyntaxTree.invalidExpression THEN
  7013. (* error already reported *)
  7014. expressionType := SyntaxTree.invalidType;
  7015. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7016. (* read out 'first' and 'last' *)
  7017. left := expression(SyntaxTree.RangeExpression).first;
  7018. right := expression(SyntaxTree.RangeExpression).last;
  7019. (* guaranteed by VisitRangeExpression: *)
  7020. ASSERT((left # NIL) & (right # NIL));
  7021. ASSERT(left.type.resolved = right.type.resolved);
  7022. left := CompatibleConversion(left.position, left, type);
  7023. right := CompatibleConversion(right.position, right, type);
  7024. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7025. expression(SyntaxTree.RangeExpression).SetLast(right);
  7026. expressionType := RegularType(position,left.type);
  7027. ELSE
  7028. expression := ConstantExpression(expression);
  7029. expression := CompatibleConversion(expression.position, expression, type);
  7030. (*
  7031. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7032. left := Global.NewCharacterValue(system,expression.position,cl);
  7033. expression := casePart.elements.GetExpression(i);
  7034. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7035. expression := left
  7036. END;
  7037. *)
  7038. casePart.elements.SetExpression(i,expression);
  7039. left := expression; right := expression;
  7040. expressionType := RegularType(position,expression.type)
  7041. END;
  7042. IF (expressionType = SyntaxTree.invalidType) THEN
  7043. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7044. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7045. expression := SyntaxTree.invalidExpression;
  7046. ELSE
  7047. l := 0; r := 0;
  7048. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7049. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7050. l := ORD(cl); r := ORD(cr);
  7051. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7052. ELSE
  7053. expression := SyntaxTree.invalidExpression
  7054. END;
  7055. IF expression # SyntaxTree.invalidExpression THEN
  7056. IF l>r THEN
  7057. Error(position,Diagnostics.Invalid,"empty case label")
  7058. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7059. Error(position,Diagnostics.Invalid,"duplicate case label");
  7060. ELSE
  7061. IF l < min THEN min := l END;
  7062. IF r > max THEN max := r END;
  7063. END;
  7064. END;
  7065. END;
  7066. casePart.elements.SetExpression(i,expression);
  7067. END;
  7068. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7069. casePart.SetConstants(thiscases);
  7070. StatementSequence(casePart.statements);
  7071. END CasePart;
  7072. (** check and resolve case statement CASE variable OF ... END;
  7073. - check variable
  7074. - check case parts
  7075. **)
  7076. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7077. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7078. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7079. BEGIN
  7080. expression := ResolveExpression(caseStatement.variable);
  7081. type := RegularType(expression.position,expression.type);
  7082. IF type = SyntaxTree.invalidType THEN
  7083. expression := SyntaxTree.invalidExpression;
  7084. ELSIF IsIntegerType(type) THEN
  7085. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7086. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7087. (*
  7088. expression := Global.NewCharacterValue(system,expression.position,ch);
  7089. *)
  7090. type := expression.type;
  7091. ELSIF IsCharacterType(type) THEN
  7092. ELSIF IsEnumerationType(type) THEN
  7093. ELSE
  7094. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7095. expression := SyntaxTree.invalidExpression;
  7096. END;
  7097. caseStatement.SetVariable(expression);
  7098. caseList := NIL;
  7099. min := MAX(LONGINT); max := MIN(LONGINT);
  7100. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7101. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7102. END;
  7103. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7104. msg := "huge sparse case table ";
  7105. Strings.AppendInt(msg, max-min);
  7106. Strings.Append(msg,"/");
  7107. Strings.AppendInt(msg, caseStatement.CaseParts());
  7108. Warning(caseStatement.position,msg);
  7109. END;
  7110. caseStatement.SetMinMax(min,max);
  7111. StatementSequence(caseStatement.elsePart);
  7112. IF expression.resolved # NIL THEN
  7113. IF IsCharacterValue(expression,ch) THEN
  7114. l := ORD(ch)
  7115. ELSIF IsIntegerValue(expression,l) THEN
  7116. END;
  7117. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7118. END;
  7119. END VisitCaseStatement;
  7120. (** check and resolve while statement
  7121. - check condition
  7122. - check statement sequence
  7123. **)
  7124. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7125. VAR prevIsUnreachable,b: BOOLEAN;
  7126. BEGIN
  7127. prevIsUnreachable := currentIsUnreachable;
  7128. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7129. IF IsBooleanValue(whileStatement.condition,b) THEN
  7130. IF b=FALSE THEN
  7131. currentIsUnreachable := TRUE
  7132. END;
  7133. END;
  7134. StatementSequence(whileStatement.statements);
  7135. currentIsUnreachable := prevIsUnreachable
  7136. END VisitWhileStatement;
  7137. (** check and resolve repeat statement
  7138. - check condition
  7139. - check statement sequence
  7140. **)
  7141. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7142. BEGIN
  7143. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7144. StatementSequence(repeatStatement.statements);
  7145. END VisitRepeatStatement;
  7146. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7147. VAR withEntry: WithEntry;
  7148. BEGIN
  7149. withEntry := withEntries;
  7150. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7151. withEntry := withEntry.previous
  7152. END;
  7153. IF withEntry = NIL THEN RETURN FALSE
  7154. ELSE
  7155. type := withEntry.type;
  7156. RETURN TRUE
  7157. END;
  7158. END GetGuard;
  7159. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7160. - check that variable is an integer variable
  7161. - check that from is integer typed with compatible type
  7162. - check that to has compatible type
  7163. - check that by is constant integer with compatible type
  7164. **)
  7165. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7166. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7167. BEGIN
  7168. designator := ResolveDesignator(forStatement.variable);
  7169. type := SyntaxTree.invalidType;
  7170. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7171. designator := SyntaxTree.invalidDesignator;
  7172. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7173. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7174. designator := SyntaxTree.invalidDesignator;
  7175. ELSIF CheckVariable(designator) THEN
  7176. type := designator.type;
  7177. END;
  7178. forStatement.SetVariable(designator);
  7179. expression := ResolveExpression(forStatement.from);
  7180. IF expression = SyntaxTree.invalidExpression THEN
  7181. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7182. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7183. expression := SyntaxTree.invalidExpression;
  7184. ELSIF type # SyntaxTree.invalidType THEN
  7185. expression := NewConversion(expression.position,expression,type,NIL)
  7186. END;
  7187. forStatement.SetFrom(expression);
  7188. expression := ResolveExpression(forStatement.to);
  7189. IF expression = SyntaxTree.invalidExpression THEN
  7190. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7191. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7192. expression := SyntaxTree.invalidExpression;
  7193. ELSIF type # SyntaxTree.invalidType THEN
  7194. expression := NewConversion(expression.position,expression,type,NIL)
  7195. END;
  7196. forStatement.SetTo(expression);
  7197. IF forStatement.by # NIL THEN
  7198. expression := ConstantInteger(forStatement.by);
  7199. ELSE
  7200. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7201. END;
  7202. IF expression = SyntaxTree.invalidExpression THEN
  7203. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7204. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7205. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7206. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7207. ELSIF type # SyntaxTree.invalidType THEN
  7208. expression := NewConversion(expression.position,expression,type,NIL)
  7209. END;
  7210. forStatement.SetBy(expression);
  7211. StatementSequence(forStatement.statements);
  7212. END VisitForStatement;
  7213. (** check and resolve loop statement LOOP StatementSequence END
  7214. - check statement sequence
  7215. **)
  7216. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7217. BEGIN
  7218. StatementSequence(loopStatement.statements)
  7219. END VisitLoopStatement;
  7220. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7221. BEGIN
  7222. StatementSequence(exitableBlock.statements);
  7223. END VisitExitableBlock;
  7224. (** check and resolve exit statement EXIT
  7225. - check that exit is within LOOP statement block
  7226. **)
  7227. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7228. VAR outer: SyntaxTree.Statement;
  7229. BEGIN
  7230. outer := exitStatement.outer;
  7231. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7232. outer := outer.outer;
  7233. END;
  7234. IF outer = NIL THEN
  7235. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7236. END;
  7237. END VisitExitStatement;
  7238. (** check and resolve return statement RETURN [expression]
  7239. - check expression (if any)
  7240. - check if in procedure scope
  7241. - if in procedure scope then check expression compatibility
  7242. - if not in procecdure scope then check on return without expression
  7243. **)
  7244. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7245. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7246. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7247. BEGIN
  7248. position := returnStatement.position;
  7249. expression := returnStatement.returnValue;
  7250. IF expression # NIL THEN
  7251. expression := ResolveExpression(expression);
  7252. returnStatement.SetReturnValue(expression);
  7253. END;
  7254. outer := returnStatement.outer;
  7255. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7256. outer := outer.outer
  7257. END;
  7258. IF (outer # NIL) THEN
  7259. scope := outer(SyntaxTree.Body).inScope;
  7260. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7261. IF (expression # NIL) THEN
  7262. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7263. END;
  7264. ELSE
  7265. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7266. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7267. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7268. END;
  7269. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7270. IF returnType # NIL THEN
  7271. returnType := returnType.resolved;
  7272. IF expression = NIL THEN
  7273. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7274. ELSIF expression.type = NIL THEN
  7275. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7276. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7277. Error(position,Diagnostics.Invalid,"return type not compatible");
  7278. IF VerboseErrorMessage THEN
  7279. Printout.Info("returnType",returnType);
  7280. Printout.Info("expression",expression);
  7281. END;
  7282. ELSE
  7283. expression := NewConversion(expression.position,expression,returnType,NIL);
  7284. returnStatement.SetReturnValue(expression);
  7285. END;
  7286. ELSIF expression # NIL THEN
  7287. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7288. END;
  7289. END;
  7290. END;
  7291. END VisitReturnStatement;
  7292. (** check and resolve await statement AWAIT(condition: Expression)
  7293. - check await condition
  7294. **)
  7295. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7296. VAR condition: SyntaxTree.Expression;
  7297. BEGIN
  7298. condition := ResolveCondition(awaitStatement.condition);
  7299. IF currentIsRealtime THEN
  7300. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7301. END;
  7302. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7303. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7304. END;
  7305. awaitStatement.SetCondition(condition);
  7306. END VisitAwaitStatement;
  7307. PROCEDURE CheckSystemImport(position: LONGINT);
  7308. VAR import: SyntaxTree.Import;
  7309. BEGIN
  7310. import := currentScope.ownerModule.moduleScope.firstImport;
  7311. WHILE(import # NIL) DO
  7312. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7313. RETURN;
  7314. END;
  7315. import := import.nextImport;
  7316. END;
  7317. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7318. END CheckSystemImport;
  7319. (** check and resolve code statement: do nothing, must be done by assembler
  7320. **)
  7321. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7322. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7323. BEGIN
  7324. CheckSystemImport(code.position);
  7325. FOR i := 0 TO code.inRules.Length()-1 DO
  7326. statement := code.inRules.GetStatement(i);
  7327. IF statement IS SyntaxTree.Assignment THEN
  7328. WITH statement: SyntaxTree.Assignment DO
  7329. statement.SetRight(ResolveExpression(statement.right));
  7330. END;
  7331. ELSE
  7332. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7333. END;
  7334. END;
  7335. FOR i := 0 TO code.outRules.Length()-1 DO
  7336. statement := code.outRules.GetStatement(i);
  7337. IF statement IS SyntaxTree.Assignment THEN
  7338. WITH statement: SyntaxTree.Assignment DO
  7339. statement.SetLeft(ResolveDesignator(statement.left));
  7340. END;
  7341. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7342. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7343. ELSE
  7344. Printout.Info("out statement ", statement);
  7345. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7346. END;
  7347. END;
  7348. END VisitCode;
  7349. (** check and set flags of a statement block
  7350. - check for multiply occurence of a flag
  7351. - check and set priority only in bodies
  7352. - check for valid names
  7353. **)
  7354. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7355. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7356. flag: LONGINT; recordBody: SyntaxTree.Body;
  7357. PROCEDURE SetProtectedRecord;
  7358. VAR scope: SyntaxTree.Scope;
  7359. BEGIN
  7360. scope := currentScope;
  7361. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7362. scope := scope.outerScope
  7363. END;
  7364. IF scope # NIL THEN
  7365. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7366. END;
  7367. END SetProtectedRecord;
  7368. BEGIN
  7369. flags := {};
  7370. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7371. recordBody := block(SyntaxTree.Body)
  7372. ELSE
  7373. recordBody := NIL
  7374. END;
  7375. blockModifier := block.blockModifiers;
  7376. WHILE(blockModifier # NIL) DO
  7377. name := blockModifier.identifier;
  7378. expression := blockModifier.expression;
  7379. position := blockModifier.position;
  7380. flag := -1;
  7381. IF name=Global.NamePriority THEN
  7382. IF expression = NIL THEN
  7383. Error(position,Diagnostics.Invalid,"missing priority expression");
  7384. ELSIF recordBody = NIL THEN
  7385. Error(position,Diagnostics.Invalid,"priority not on record body");
  7386. ELSIF recordBody.priority # NIL THEN
  7387. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7388. ELSE
  7389. recordBody.SetPriority(expression);
  7390. END;
  7391. ELSIF expression # NIL THEN
  7392. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7393. ELSIF name=Global.NameExclusive THEN
  7394. IF block.isExclusive THEN
  7395. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7396. END;
  7397. block.SetExclusive(TRUE); SetProtectedRecord;
  7398. ELSIF name=Global.NameActive THEN
  7399. IF recordBody = NIL THEN
  7400. Error(position,Diagnostics.Invalid,"active not in record body");
  7401. ELSIF recordBody.isActive THEN
  7402. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7403. ELSE
  7404. recordBody.SetActive(TRUE); SetProtectedRecord;
  7405. END;
  7406. ELSIF name=Global.NameSafe THEN
  7407. IF recordBody = NIL THEN
  7408. Error(position,Diagnostics.Invalid,"safe not in record body");
  7409. ELSIF recordBody.isSafe THEN
  7410. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7411. ELSE
  7412. recordBody.SetSafe(TRUE);
  7413. SetProtectedRecord;
  7414. END;
  7415. ELSIF name=Global.NameRealtime THEN
  7416. IF recordBody = NIL THEN
  7417. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7418. ELSIF recordBody.isRealtime THEN
  7419. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7420. ELSE
  7421. recordBody.SetRealtime(TRUE);
  7422. block.SetRealtime(TRUE);
  7423. END;
  7424. ELSIF name=Global.NameUnchecked THEN
  7425. IF block.isUnchecked THEN
  7426. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7427. ELSE
  7428. block.SetUnchecked(TRUE);
  7429. END;
  7430. ELSIF (name=Global.NameUncooperative) THEN
  7431. IF block.isUncooperative THEN
  7432. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7433. ELSE
  7434. block.SetUncooperative(TRUE);
  7435. END;
  7436. ELSE
  7437. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7438. END;
  7439. blockModifier := blockModifier.nextModifier;
  7440. END;
  7441. END BlockFlags;
  7442. (** check and resolve statement block
  7443. - check flags (exclusive)
  7444. - check statement sequence
  7445. **)
  7446. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7447. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7448. BEGIN
  7449. BlockFlags(statementBlock);
  7450. IF statementBlock.isExclusive THEN
  7451. (* check that not in exclusive block *)
  7452. IF currentIsExclusive THEN
  7453. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7454. ELSIF currentIsRealtime THEN
  7455. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7456. END;
  7457. END;
  7458. recentExclusive := currentIsExclusive;
  7459. recentUnreachable := currentIsUnreachable;
  7460. recentRealtime := currentIsRealtime;
  7461. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7462. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7463. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7464. StatementSequence(statementBlock.statements);
  7465. currentIsRealtime := recentRealtime;
  7466. currentIsExclusive := recentExclusive;
  7467. currentIsUnreachable := recentUnreachable;
  7468. END VisitStatementBlock;
  7469. (** check and resolve body
  7470. - check flags (active, priority, safe)
  7471. - check body and finally part
  7472. **)
  7473. PROCEDURE Body(body: SyntaxTree.Body);
  7474. BEGIN
  7475. VisitStatementBlock(body);
  7476. IF body.isActive THEN
  7477. IF ~currentIsBodyProcedure THEN
  7478. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7479. ELSIF body.priority # NIL THEN
  7480. body.SetPriority(ConstantInteger(body.priority));
  7481. END;
  7482. ELSIF body.isSafe THEN
  7483. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7484. ELSIF body.priority # NIL THEN
  7485. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7486. END;
  7487. IF body.code # NIL THEN
  7488. CheckSystemImport(body.position);
  7489. END;
  7490. StatementSequence(body.finally)
  7491. END Body;
  7492. (*** scopes ***)
  7493. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7494. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7495. VAR duplicateSymbol: BOOLEAN;
  7496. BEGIN
  7497. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7498. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7499. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7500. END;
  7501. scope.EnterSymbol(symbol,duplicateSymbol);
  7502. IF ~allowDuplicate & duplicateSymbol THEN
  7503. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7504. IF VerboseErrorMessage THEN
  7505. Printout.Info("multiply defined identifier",symbol);
  7506. Printout.Info("in scope",scope);
  7507. END;
  7508. END;
  7509. END Register;
  7510. (**
  7511. implementation: check and resolve an implementation part
  7512. **)
  7513. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7514. move implementation checker to a separate object ? *)
  7515. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7516. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7517. BEGIN
  7518. prevIsRealtime := currentIsRealtime;
  7519. prevIsBodyProcedure := currentIsBodyProcedure;
  7520. prevIsCellNet := currentIsCellNet;
  7521. prevScope := currentScope;
  7522. currentScope := scope;
  7523. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7524. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7525. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7526. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7527. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7528. (*
  7529. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7530. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7531. END;
  7532. *)
  7533. END;
  7534. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7535. (* module body, record bodies are wrapped into an artifical procedure *)
  7536. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7537. Body(scope(SyntaxTree.ProcedureScope).body)
  7538. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7539. Body(scope(SyntaxTree.ProcedureScope).body)
  7540. END;
  7541. END;
  7542. currentScope := prevScope;
  7543. currentIsRealtime := prevIsRealtime;
  7544. currentIsBodyProcedure := prevIsBodyProcedure;
  7545. currentIsCellNet := prevIsCellNet;
  7546. END Implementation;
  7547. (** implementation phase:
  7548. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7549. **)
  7550. PROCEDURE Implementations(x: SyntaxTree.Module);
  7551. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7552. BEGIN
  7553. prevPhase := phase;
  7554. phase := InlinePhase;
  7555. scope := x.firstScope;
  7556. WHILE(scope # NIL) DO
  7557. Implementation(scope);
  7558. scope := scope.nextScope;
  7559. END;
  7560. phase := ImplementationPhase;
  7561. scope := x.firstScope;
  7562. WHILE(scope # NIL) DO
  7563. Implementation(scope);
  7564. scope := scope.nextScope;
  7565. END;
  7566. phase := prevPhase;
  7567. END Implementations;
  7568. (** declaration phase:
  7569. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7570. - import lists (for module scopes)
  7571. - parameter list (for procedure scopes)
  7572. - constant declarations
  7573. - type declarations
  7574. - variable declarations
  7575. - procedure declarations
  7576. preformed in two stages:
  7577. - first all symbols are entered into the symbol table (with uniqueness check),
  7578. - then all symbols are resolved
  7579. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7580. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7581. **)
  7582. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7583. VAR
  7584. constant: SyntaxTree.Constant;
  7585. typeDeclaration: SyntaxTree.TypeDeclaration;
  7586. variable: SyntaxTree.Variable;
  7587. procedure: SyntaxTree.Procedure;
  7588. prevScope: SyntaxTree.Scope;
  7589. parameter: SyntaxTree.Parameter;
  7590. import: SyntaxTree.Import;
  7591. symbol: SyntaxTree.Symbol;
  7592. prevPhase: LONGINT;
  7593. prevError : BOOLEAN;
  7594. type: SyntaxTree.Type;
  7595. atype : SyntaxTree.ArrayType;
  7596. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7597. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7598. BEGIN
  7599. IF type.baseType # NIL THEN
  7600. baseType := type.baseType.resolved;
  7601. IF baseType IS SyntaxTree.PointerType THEN
  7602. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7603. END;
  7604. IF baseType IS SyntaxTree.CellType THEN
  7605. DeclareCell(baseType(SyntaxTree.CellType));
  7606. END;
  7607. END;
  7608. parameter := type.firstParameter;
  7609. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7610. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7611. variable.SetType(parameter.type);
  7612. variable.SetAccess(SyntaxTree.Hidden);
  7613. variable.SetModifiers(parameter.modifiers);
  7614. currentScope.PushVariable(variable);
  7615. (*
  7616. Register(parameter,scope, FALSE);
  7617. *)
  7618. parameter := parameter.nextParameter;
  7619. END;
  7620. property := type.firstProperty;
  7621. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7622. variable := currentScope.FindVariable(property.name);
  7623. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7624. prop := variable(SyntaxTree.Property);
  7625. ELSE (* add, duplicate symbols detection later *)
  7626. prop := SyntaxTree.NewProperty(property.position, property.name);
  7627. currentScope.PushVariable(prop);
  7628. END;
  7629. prop.SetType(property.type);
  7630. prop.SetValue(property.value);
  7631. prop.SetAccess(SyntaxTree.Hidden);
  7632. property := property.nextProperty;
  7633. END;
  7634. END DeclareCell;
  7635. BEGIN
  7636. prevError := error;
  7637. prevPhase := phase;
  7638. phase := DeclarationPhase;
  7639. prevScope := currentScope;
  7640. currentScope := scope;
  7641. error := FALSE;
  7642. (* first enter all symbols in scope *)
  7643. IF scope IS SyntaxTree.ModuleScope THEN
  7644. (* treat imports first for a module scope, , set default context if necessary *)
  7645. import := scope(SyntaxTree.ModuleScope).firstImport;
  7646. WHILE(import # NIL) DO
  7647. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7648. Register(import, currentScope, FALSE);
  7649. import := import.nextImport;
  7650. END;
  7651. import := scope(SyntaxTree.ModuleScope).firstImport;
  7652. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7653. ResolveSymbol(import);
  7654. import := import.nextImport;
  7655. END;
  7656. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7657. (* enter parameters for a procedure scope *)
  7658. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7659. WHILE(parameter # NIL) DO
  7660. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7661. END;
  7662. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7663. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7664. ELSIF scope IS SyntaxTree.CellScope THEN
  7665. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7666. END;
  7667. IF error THEN RETURN END;
  7668. (* constants *)
  7669. constant := scope.firstConstant;
  7670. WHILE (constant # NIL) DO
  7671. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7672. END;
  7673. (* type declarations *)
  7674. typeDeclaration := scope.firstTypeDeclaration;
  7675. WHILE (typeDeclaration # NIL) DO
  7676. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7677. END;
  7678. (* variables *)
  7679. variable := scope.firstVariable;
  7680. WHILE (variable # NIL) DO
  7681. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7682. END;
  7683. (* procedures *)
  7684. procedure := scope.firstProcedure;
  7685. WHILE (procedure # NIL) DO
  7686. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7687. END;
  7688. (* now process all symbols without any presumption on the order *)
  7689. symbol := scope.firstSymbol;
  7690. WHILE(symbol # NIL) DO
  7691. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7692. ResolveSymbol(symbol);
  7693. END;
  7694. symbol := symbol.nextSymbol;
  7695. END;
  7696. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7697. symbol := scope.firstSymbol;
  7698. WHILE symbol # NIL DO
  7699. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7700. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7701. pointerFixes.Add(symbol, currentScope);
  7702. END;
  7703. IF ~symbol.type.resolved.isRealtime THEN
  7704. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7705. END;
  7706. END;
  7707. symbol := symbol.nextSymbol
  7708. END;
  7709. END;
  7710. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7711. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7712. END;
  7713. IF scope.ownerModule # NIL THEN
  7714. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7715. scope.ownerModule.AddScope(scope);
  7716. END;
  7717. phase := prevPhase;
  7718. currentScope := prevScope;
  7719. error := error OR prevError;
  7720. END Declarations;
  7721. (* nopov *)
  7722. (** check if all operators from one module are compatible to the ones in the other module
  7723. - check if there are not multiple operators with the same signature
  7724. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7725. - check for all operators whose signatures are compatible, whether the return types are compatible
  7726. note that:
  7727. - the return type is not considered to be part of the signature
  7728. - two signatures are considered compatible, if all of the operands are compatible
  7729. **)
  7730. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7731. VAR
  7732. thisOperator, thatOperator: SyntaxTree.Operator;
  7733. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7734. thisParameter, thatParameter: SyntaxTree.Parameter;
  7735. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7736. i: LONGINT;
  7737. BEGIN
  7738. currentScope := thisModuleScope;
  7739. hasError := FALSE;
  7740. (* go through all operators in the other module *)
  7741. thatOperator := thatModuleScope.firstOperator;
  7742. WHILE (thatOperator # NIL) & ~hasError DO
  7743. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7744. (* the other operator is accessible *)
  7745. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7746. (* the other operator is not the conversion operator *)
  7747. (* go through all operators in this module *)
  7748. thisOperator := thisModuleScope.firstOperator;
  7749. WHILE (thisOperator # NIL) & ~hasError DO
  7750. IF thisOperator # thatOperator THEN
  7751. (* the operators are not the same *)
  7752. IF thisOperator.name = thatOperator.name THEN
  7753. (* the operators share the same identifier *)
  7754. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7755. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7756. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7757. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7758. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7759. (* both operators have the same paramter count *)
  7760. thisParameter := thisProcedureType.firstParameter;
  7761. thatParameter := thatProcedureType.firstParameter;
  7762. operandsAreEqual := TRUE;
  7763. operandsAreCompatible := TRUE;
  7764. (* go through all parameters *)
  7765. FOR i := 1 TO thisProcedureType.numberParameters DO
  7766. ASSERT(thatParameter # NIL);
  7767. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7768. operandsAreEqual := FALSE;
  7769. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7770. operandsAreCompatible := FALSE
  7771. END
  7772. END;
  7773. thisParameter := thisParameter.nextParameter;
  7774. thatParameter := thatParameter.nextParameter
  7775. END;
  7776. IF operandsAreEqual THEN
  7777. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7778. hasError := TRUE
  7779. ELSIF operandsAreCompatible THEN
  7780. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7781. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7782. hasError := TRUE
  7783. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7784. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7785. hasError := TRUE
  7786. END
  7787. END
  7788. END
  7789. END
  7790. END;
  7791. thisOperator := thisOperator.nextOperator
  7792. END
  7793. END
  7794. END;
  7795. thatOperator := thatOperator.nextOperator
  7796. END
  7797. END CheckInterOperatorConformity;
  7798. (** check module:
  7799. - check module declaration
  7800. - add context, if necessary
  7801. - remove module from import cache, if necessary
  7802. - check declarations
  7803. - resolve all type fixes
  7804. - check implementation (bodies)
  7805. **)
  7806. PROCEDURE Module*(x: SyntaxTree.Module);
  7807. VAR (* nopov *)
  7808. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7809. BEGIN
  7810. prevScope := currentScope;
  7811. prevIsCellNet := currentIsCellNet;
  7812. module := x;
  7813. ASSERT(x # NIL);
  7814. global := system.globalScope[x.case];
  7815. x.moduleScope.SetGlobalScope(global);
  7816. currentScope := global;
  7817. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7818. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7819. RemoveModuleFromCache(importCache,x);
  7820. Declarations(x.moduleScope);
  7821. FixTypes();
  7822. IF module.isCellNet THEN
  7823. currentIsCellNet := TRUE;
  7824. modifier := x.modifiers;
  7825. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7826. CheckModifiers(modifier, FALSE);
  7827. END;
  7828. (* nopov *)
  7829. IF ~error THEN
  7830. (* check if operators conform to each other within this module *)
  7831. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7832. (* go through all imports *)
  7833. import := x.moduleScope.firstImport;
  7834. WHILE import # NIL DO
  7835. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7836. (* check if all operators in this module conform to the ones of the imported module *)
  7837. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7838. END;
  7839. import := import.nextImport
  7840. END;
  7841. END;
  7842. Implementations(x);
  7843. module := NIL;
  7844. currentIsCellNet := prevIsCellNet;
  7845. currentScope := prevScope;
  7846. END Module;
  7847. END Checker;
  7848. Warnings*=OBJECT (SyntaxTree.Visitor)
  7849. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7850. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7851. BEGIN
  7852. SELF.diagnostics := diagnostics
  7853. END InitWarnings;
  7854. (** types *)
  7855. PROCEDURE Type(x: SyntaxTree.Type);
  7856. BEGIN x.Accept(SELF)
  7857. END Type;
  7858. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7859. BEGIN END VisitType;
  7860. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7861. BEGIN END VisitBasicType;
  7862. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7863. BEGIN END VisitCharacterType;
  7864. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7865. BEGIN END VisitIntegerType;
  7866. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7867. BEGIN END VisitFloatType;
  7868. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7869. BEGIN END VisitQualifiedType;
  7870. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7871. BEGIN END VisitStringType;
  7872. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7873. BEGIN END VisitEnumerationType;
  7874. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7875. BEGIN END VisitRangeType;
  7876. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7877. BEGIN
  7878. IF ~(SyntaxTree.Warned IN x.state) THEN
  7879. x.SetState(SyntaxTree.Warned);
  7880. Type(x.arrayBase);
  7881. END;
  7882. END VisitArrayType;
  7883. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7884. BEGIN
  7885. IF ~(SyntaxTree.Warned IN x.state) THEN
  7886. x.SetState(SyntaxTree.Warned);
  7887. Type(x.arrayBase);
  7888. END;
  7889. END VisitMathArrayType;
  7890. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7891. BEGIN
  7892. IF ~(SyntaxTree.Warned IN x.state) THEN
  7893. x.SetState(SyntaxTree.Warned);
  7894. Type(x.pointerBase);
  7895. END;
  7896. END VisitPointerType;
  7897. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7898. BEGIN Scope(x.recordScope) END VisitRecordType;
  7899. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7900. BEGIN Scope(x.cellScope) END VisitCellType;
  7901. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7902. BEGIN END VisitProcedureType;
  7903. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7904. VAR msg: ARRAY 256 OF CHAR;
  7905. BEGIN
  7906. Global.GetSymbolName(x,msg);
  7907. Strings.Append(msg," ");
  7908. Strings.Append(msg,text);
  7909. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7910. END Warning;
  7911. (** symbols *)
  7912. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7913. BEGIN
  7914. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7915. IF ~(x IS SyntaxTree.Parameter) THEN
  7916. Warning(x,"never used");
  7917. END;
  7918. END;
  7919. x.Accept(SELF);
  7920. END Symbol;
  7921. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7922. BEGIN END VisitSymbol;
  7923. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7924. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7925. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7926. BEGIN END VisitConstant;
  7927. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7928. BEGIN END VisitVariable;
  7929. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7930. BEGIN END VisitProperty;
  7931. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7932. BEGIN END VisitParameter;
  7933. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7934. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7935. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7936. BEGIN END VisitOperator;
  7937. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7938. BEGIN END VisitImport;
  7939. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7940. VAR
  7941. symbol: SyntaxTree.Symbol;
  7942. BEGIN
  7943. symbol := scope.firstSymbol;
  7944. WHILE(symbol # NIL) DO
  7945. Symbol(symbol);
  7946. symbol := symbol.nextSymbol;
  7947. END;
  7948. END Scope;
  7949. PROCEDURE Module*(x: SyntaxTree.Module);
  7950. BEGIN
  7951. SELF.module := x;
  7952. Scope(x.moduleScope);
  7953. END Module;
  7954. END Warnings;
  7955. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7956. BEGIN
  7957. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7958. END IsOberonInline;
  7959. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7960. BEGIN
  7961. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7962. END Resolved;
  7963. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7964. VAR i: LONGINT;
  7965. BEGIN
  7966. i := 1;
  7967. WHILE i < x DO
  7968. i := i *2
  7969. END;
  7970. RETURN i=x
  7971. END PowerOf2;
  7972. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7973. BEGIN
  7974. RETURN
  7975. (scope # NIL) &
  7976. (scope IS SyntaxTree.ModuleScope)
  7977. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7978. OR
  7979. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7980. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7981. END IsCellNetScope;
  7982. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7983. BEGIN
  7984. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7985. END IsCellScope;
  7986. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7987. BEGIN
  7988. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7989. RETURN (scope # NIL) & IsCellNetScope(scope)
  7990. END InCellNetScope;
  7991. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7992. BEGIN
  7993. ASSERT(size MOD system.dataUnit = 0);
  7994. RETURN size DIV system.dataUnit
  7995. END ToMemoryUnits;
  7996. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7997. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7998. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7999. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8000. BEGIN
  8001. IF t = NIL THEN
  8002. RETURN TRUE
  8003. ELSE
  8004. t := t.resolved;
  8005. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8006. END;
  8007. END TypeAllowed;
  8008. BEGIN
  8009. type := type.resolved;
  8010. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8011. RETURN FALSE
  8012. ELSE
  8013. procedureType := type(SyntaxTree.ProcedureType);
  8014. numberParameters := procedureType.numberParameters;
  8015. RETURN
  8016. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8017. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8018. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8019. END;
  8020. END GetProcedureAllowed;
  8021. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8022. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8023. VAR import: SyntaxTree.Import;
  8024. BEGIN
  8025. import := importCache.ImportByModuleName(x.name,x.context);
  8026. IF import # NIL THEN
  8027. importCache.RemoveImporters(x.name,x.context);
  8028. END;
  8029. END RemoveModuleFromCache;
  8030. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8031. (* to <- this assignment compatibility *)
  8032. VAR result: BOOLEAN;
  8033. BEGIN
  8034. IF this= NIL THEN result := (to=NIL)
  8035. ELSIF to=NIL THEN result := FALSE
  8036. ELSE
  8037. (*! will be replaced by this:
  8038. ELSE result := this.CompatibleTo(to.resolved);
  8039. *)
  8040. this := this.resolved; to := to.resolved;
  8041. IF to=SyntaxTree.invalidType THEN result := FALSE
  8042. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8043. ELSIF to = this THEN
  8044. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8045. ELSIF to IS SyntaxTree.BasicType THEN
  8046. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8047. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8048. result := this.CompatibleTo(to.resolved)
  8049. ELSE
  8050. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8051. END
  8052. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8053. result := to.sizeInBits = this.sizeInBits;
  8054. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8055. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8056. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8057. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8058. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8059. result := TRUE;
  8060. ELSIF to IS SyntaxTree.AnyType THEN
  8061. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  8062. ELSIF to IS SyntaxTree.ObjectType THEN
  8063. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  8064. ELSIF to IS SyntaxTree.ByteType THEN
  8065. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8066. ELSIF to IS SyntaxTree.CharacterType THEN
  8067. result := IsCharacterType(this)
  8068. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8069. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8070. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  8071. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8072. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8073. result := TRUE;
  8074. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8075. result := TRUE;
  8076. ELSE
  8077. result := FALSE
  8078. END;
  8079. ELSIF to IS SyntaxTree.PointerType THEN
  8080. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8081. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8082. & (~to.isRealtime OR this.isRealtime);
  8083. ELSIF to IS SyntaxTree.ProcedureType THEN
  8084. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8085. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8086. & (~to.isRealtime OR this.isRealtime)
  8087. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8088. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8089. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8090. ELSIF to IS SyntaxTree.RecordType THEN
  8091. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8092. ELSIF to IS SyntaxTree.ArrayType THEN
  8093. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8094. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8095. ELSIF StaticArrayCompatible(to, this) THEN
  8096. result := TRUE
  8097. ELSE
  8098. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8099. END;
  8100. ELSIF to IS SyntaxTree.MathArrayType THEN
  8101. IF this IS SyntaxTree.MathArrayType THEN
  8102. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8103. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8104. result := TRUE;
  8105. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8106. result := TRUE;
  8107. ELSE
  8108. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8109. END;
  8110. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8111. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8112. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8113. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8114. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8115. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8116. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8117. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8118. ELSE
  8119. result := FALSE
  8120. END;
  8121. (* an array-structured object type is compatible to the type of its array structure *)
  8122. ELSIF IsArrayStructuredObjectType(this) THEN
  8123. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8124. ELSE
  8125. result := FALSE;
  8126. END;
  8127. ELSIF to IS SyntaxTree.StringType THEN
  8128. result := FALSE;
  8129. ELSIF to IS SyntaxTree.EnumerationType THEN
  8130. result := IsEnumerationExtension(this,to);
  8131. ELSIF to IS SyntaxTree.PortType THEN
  8132. result := SameType(to, this)
  8133. ELSE
  8134. Printout.Info("CompatibleTo",to);
  8135. HALT(100); (* implement missing type check *)
  8136. END;
  8137. END;
  8138. RETURN result
  8139. END CompatibleTo;
  8140. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8141. VAR actualBase, formalBase: SyntaxTree.Type;
  8142. BEGIN
  8143. IF SameType(formal,actual) THEN
  8144. RETURN TRUE
  8145. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8146. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8147. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8148. RETURN
  8149. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8150. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8151. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8152. & StaticArrayCompatible(formalBase,actualBase)
  8153. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8154. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8155. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8156. RETURN
  8157. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8158. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8159. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8160. & StaticArrayCompatible(formalBase,actualBase)
  8161. ELSE RETURN FALSE
  8162. END;
  8163. END StaticArrayCompatible;
  8164. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8165. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8166. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8167. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8168. BEGIN
  8169. result := SameType(formal,actual);
  8170. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8171. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8172. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8173. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8174. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8175. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8176. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8177. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8178. & TC(formalBase, actualBase);
  8179. END;
  8180. RETURN result
  8181. END TC;
  8182. BEGIN
  8183. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8184. ELSE
  8185. arrayBase := formalType.arrayBase.resolved;
  8186. IF (actualType IS SyntaxTree.StringType) THEN
  8187. result := arrayBase IS SyntaxTree.CharacterType
  8188. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8189. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8190. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8191. result := TC(formalType, actualType);
  8192. ELSE
  8193. result := (arrayBase IS SyntaxTree.ByteType)
  8194. END;
  8195. END;
  8196. RETURN result
  8197. END OpenArrayCompatible;
  8198. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8199. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8200. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8201. BEGIN
  8202. IF actualType IS SyntaxTree.MathArrayType THEN
  8203. actualArray := actualType(SyntaxTree.MathArrayType);
  8204. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8205. (*
  8206. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8207. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8208. *)
  8209. actualBase := ArrayBase(actualType,Infinity);
  8210. formalBase := ArrayBase(formalType,Infinity);
  8211. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8212. ELSE
  8213. (*
  8214. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8215. *)
  8216. formalBase := Resolved(formalType.arrayBase);
  8217. actualBase := Resolved(actualArray.arrayBase);
  8218. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8219. (*
  8220. ARRAY [k] -> ARRAY [n]
  8221. *)
  8222. result := (formalType.staticLength = actualArray.staticLength)
  8223. ELSE
  8224. result := TRUE
  8225. END;
  8226. IF ~result THEN
  8227. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8228. ELSIF actualBase = NIL THEN result := FALSE
  8229. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8230. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8231. ELSE
  8232. result := SameType(formalBase,actualBase)
  8233. END;
  8234. END;
  8235. ELSE
  8236. result := FALSE
  8237. END;
  8238. RETURN result
  8239. END MathArrayCompatible;
  8240. (**
  8241. Math Array Type distance for assignments / parameter passings of the form
  8242. from -> to
  8243. variants:
  8244. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8245. allowed:
  8246. static -> static (& size match)
  8247. static -> open
  8248. static -> tensor
  8249. open -> open
  8250. open -> tensor
  8251. open -> static
  8252. tensor -> tensor
  8253. tensor -> open
  8254. tensor -> static
  8255. **)
  8256. (*! think about the metric here: is form matching more important than element type matching? *)
  8257. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8258. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8259. BEGIN
  8260. fromBase := Resolved(from.arrayBase);
  8261. toBase := Resolved(to.arrayBase);
  8262. i := Infinity;
  8263. IF from = to THEN
  8264. i := 0;
  8265. ELSIF (from.form = to.form) THEN
  8266. (* static -> static, open -> open, tensor -> tensor *)
  8267. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8268. IF fromBase = toBase THEN i := 0
  8269. ELSIF toBase = NIL THEN i := 1
  8270. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8271. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8272. ELSE
  8273. i := TypeDistance(system,fromBase, toBase, varpar);
  8274. END;
  8275. END;
  8276. ELSIF (to.form = SyntaxTree.Static) THEN
  8277. (* forbidden *)
  8278. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8279. (* static -> tensor, open -> tensor, tensor -> open *)
  8280. IF toBase=fromBase THEN i := 0;
  8281. ELSIF toBase = NIL THEN i := 1;
  8282. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8283. toBase := ArrayBase(toBase,Infinity);
  8284. IF (fromBase=toBase) THEN i := 0
  8285. ELSIF (toBase = NIL) THEN i:= 1
  8286. ELSIF (fromBase = NIL) THEN i := Infinity;
  8287. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8288. END;
  8289. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8290. fromBase := ArrayBase(fromBase,Infinity);
  8291. IF (fromBase=toBase) THEN i := 0
  8292. ELSIF (toBase = NIL) THEN i := 1
  8293. ELSIF (fromBase = NIL) THEN i := Infinity;
  8294. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8295. END;
  8296. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8297. END;
  8298. IF i # Infinity THEN INC(i,2) END;
  8299. ELSIF (from.form = SyntaxTree.Static) THEN
  8300. (* static -> open *)
  8301. IF toBase=fromBase THEN i := 0
  8302. ELSIF toBase = NIL THEN i := 1
  8303. ELSIF fromBase = NIL THEN i := Infinity
  8304. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8305. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8306. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8307. END;
  8308. IF i # Infinity THEN INC(i,1) END;
  8309. ELSE HALT(100); (* unknown case *)
  8310. END;
  8311. RETURN i;
  8312. END MathArrayTypeDistance;
  8313. (** compute and return the distance of two array types
  8314. - return the distance of the base types
  8315. **)
  8316. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8317. VAR i: LONGINT;
  8318. BEGIN
  8319. i := Infinity;
  8320. IF from = to THEN
  8321. i := 0
  8322. ELSE
  8323. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8324. (*
  8325. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8326. i := TypeDistance(from.base, to.base);
  8327. IF i >= 0 THEN INC(i) END
  8328. ELSIF (from.mode = open) & (to.mode = open) THEN
  8329. i := TypeDistance(from.base, to.base);
  8330. *)
  8331. END;
  8332. RETURN i
  8333. END ArrayTypeDistance;
  8334. (** compute the signature distance of a procedure and an actual parameter list
  8335. - if any of the parameters are not compatible, the result is infinite
  8336. - add up and return the distance over all parameters
  8337. **)
  8338. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8339. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8340. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8341. BEGIN
  8342. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8343. result := Infinity
  8344. ELSE
  8345. formalParameter := procedureType.firstParameter;
  8346. i := 0;
  8347. result := 0;
  8348. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8349. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8350. actualParameter := actualParameters.GetExpression(i);
  8351. ASSERT(formalParameter.type # NIL);
  8352. IF (actualParameter.type = NIL) THEN distance := Infinity
  8353. ELSE
  8354. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8355. END;
  8356. IF distance = Infinity THEN
  8357. result := Infinity;
  8358. ELSE
  8359. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8360. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8361. (* already handled varpar *)
  8362. (*
  8363. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8364. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8365. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8366. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8367. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8368. END;
  8369. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8370. result := Infinity
  8371. END;
  8372. *)
  8373. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8374. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8375. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8376. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8377. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8378. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8379. END;
  8380. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8381. result := Infinity
  8382. END;
  8383. ELSE
  8384. result := Infinity
  8385. END;
  8386. ELSE
  8387. INC(result,distance);
  8388. END;
  8389. END;
  8390. formalParameter := formalParameter.nextParameter; INC(i);
  8391. END;
  8392. END;
  8393. ASSERT(result >= 0);
  8394. RETURN result
  8395. END Distance;
  8396. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8397. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8398. BEGIN
  8399. IF right.numberParameters # (procedureType.numberParameters) THEN
  8400. result := Infinity
  8401. ELSE
  8402. formalParameter := procedureType.firstParameter;
  8403. rightParameter := right.firstParameter;
  8404. i := 0;
  8405. result := 0;
  8406. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8407. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8408. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8409. IF distance = Infinity THEN
  8410. result := Infinity;
  8411. ELSE
  8412. INC(result,distance);
  8413. END;
  8414. formalParameter := formalParameter.nextParameter;
  8415. rightParameter := rightParameter.nextParameter;
  8416. END;
  8417. END;
  8418. ASSERT(result >= 0);
  8419. RETURN result
  8420. END ProcedureTypeDistance;
  8421. (** compute and return the distance between two types, used for computation of signature distance
  8422. from -> to
  8423. **)
  8424. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8425. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8426. BEGIN
  8427. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8428. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8429. END;
  8430. i := Infinity;
  8431. IF from = to THEN
  8432. i := 0
  8433. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8434. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8435. i := Infinity;
  8436. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8437. i := 1
  8438. ELSIF (from IS SyntaxTree.StringType) THEN
  8439. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8440. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8441. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8442. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8443. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8444. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8445. i := 1
  8446. ELSIF (from IS SyntaxTree.NilType) THEN
  8447. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8448. (*
  8449. ELSIF (from = NoType) THEN
  8450. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8451. *)
  8452. ELSIF (from IS SyntaxTree.BasicType) THEN
  8453. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8454. IF varpar & (i # 0) THEN i := Infinity END;
  8455. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8456. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8457. ELSIF (from IS SyntaxTree.RecordType) THEN
  8458. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8459. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8460. IF to IS SyntaxTree.MathArrayType THEN
  8461. (*
  8462. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8463. i := Infinity;
  8464. ELSE
  8465. *)
  8466. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8467. (*
  8468. END;
  8469. *)
  8470. END
  8471. ELSIF (from IS SyntaxTree.PointerType) THEN
  8472. ptr := from(SyntaxTree.PointerType);
  8473. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8474. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8475. (* ELSE i := TypeDistance(ptr.base, to); *)
  8476. END
  8477. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8478. IF (to IS SyntaxTree.ProcedureType) THEN
  8479. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8480. END;
  8481. ELSIF (from IS SyntaxTree.PortType) THEN
  8482. IF (to IS SyntaxTree.PortType) THEN
  8483. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8484. i := 0;
  8485. END;
  8486. END;
  8487. (*no procedure test, procedure must be the same*)
  8488. END;
  8489. RETURN i
  8490. END TypeDistance;
  8491. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8492. BEGIN
  8493. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8494. END IsIntegerType;
  8495. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8496. BEGIN
  8497. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8498. END IsAddressType;
  8499. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8500. BEGIN
  8501. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8502. END IsSizeType;
  8503. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8504. BEGIN
  8505. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8506. END IsSignedIntegerType;
  8507. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8508. BEGIN
  8509. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8510. END IsUnsignedIntegerType;
  8511. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8512. VAR result: BOOLEAN;
  8513. BEGIN
  8514. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8515. value := x.resolved(SyntaxTree.IntegerValue).value;
  8516. result := TRUE
  8517. ELSE
  8518. result := FALSE
  8519. END;
  8520. RETURN result
  8521. END IsIntegerValue;
  8522. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8523. VAR result: BOOLEAN;
  8524. BEGIN
  8525. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8526. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8527. result := TRUE
  8528. ELSE
  8529. result := FALSE
  8530. END;
  8531. RETURN result
  8532. END IsEnumerationValue;
  8533. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8534. VAR result: BOOLEAN;
  8535. BEGIN
  8536. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8537. value := x.resolved(SyntaxTree.RealValue).value;
  8538. result := TRUE
  8539. ELSE
  8540. result := FALSE
  8541. END;
  8542. RETURN result
  8543. END IsRealValue;
  8544. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8545. VAR result: BOOLEAN;
  8546. BEGIN
  8547. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8548. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8549. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8550. result := TRUE
  8551. ELSE
  8552. result := FALSE
  8553. END;
  8554. RETURN result
  8555. END IsComplexValue;
  8556. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8557. VAR result: BOOLEAN;
  8558. BEGIN
  8559. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8560. value := x.resolved(SyntaxTree.CharacterValue).value;
  8561. result := TRUE
  8562. ELSE
  8563. result := FALSE
  8564. END;
  8565. RETURN result
  8566. END IsCharacterValue;
  8567. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8568. VAR result: BOOLEAN;
  8569. BEGIN
  8570. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8571. value := x.resolved(SyntaxTree.BooleanValue).value;
  8572. result := TRUE
  8573. ELSE
  8574. result := FALSE
  8575. END;
  8576. RETURN result
  8577. END IsBooleanValue;
  8578. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8579. VAR result: BOOLEAN;
  8580. BEGIN
  8581. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8582. value := x.resolved(SyntaxTree.SetValue).value;
  8583. result := TRUE
  8584. ELSE
  8585. result := FALSE
  8586. END;
  8587. RETURN result
  8588. END IsSetValue;
  8589. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8590. VAR result: BOOLEAN;
  8591. BEGIN
  8592. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8593. value := x.resolved(SyntaxTree.StringValue).value;
  8594. result := TRUE
  8595. ELSE
  8596. result := FALSE
  8597. END;
  8598. RETURN result
  8599. END IsStringValue;
  8600. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8601. BEGIN
  8602. x := x.resolved;
  8603. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8604. END Indexable;
  8605. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8606. BEGIN
  8607. RETURN t1.SameType(t2.resolved);
  8608. END SameType;
  8609. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8610. BEGIN
  8611. IF t IS SyntaxTree.MathArrayType THEN
  8612. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8613. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8614. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8615. DEC(max);
  8616. END;
  8617. ELSIF t IS SyntaxTree.ArrayType THEN
  8618. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8619. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8620. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8621. END;
  8622. END;
  8623. RETURN t;
  8624. END ArrayBase;
  8625. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8626. BEGIN
  8627. type := type.resolved;
  8628. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8629. base := type(SyntaxTree.ArrayType).arrayBase;
  8630. RETURN TRUE;
  8631. END;
  8632. RETURN FALSE;
  8633. END IsOpenArray;
  8634. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8635. BEGIN
  8636. type := type.resolved;
  8637. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8638. base := type(SyntaxTree.ArrayType).arrayBase;
  8639. dim := type(SyntaxTree.ArrayType).staticLength;
  8640. RETURN TRUE
  8641. ELSE
  8642. RETURN FALSE
  8643. END;
  8644. END IsStaticArray;
  8645. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8646. BEGIN
  8647. type := type.resolved;
  8648. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8649. base := type(SyntaxTree.ArrayType).arrayBase;
  8650. RETURN TRUE
  8651. ELSE
  8652. RETURN FALSE
  8653. END;
  8654. END IsDynamicArray;
  8655. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8656. VAR i: LONGINT;
  8657. BEGIN
  8658. i := 0;
  8659. t := t.resolved;
  8660. IF t IS SyntaxTree.MathArrayType THEN
  8661. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8662. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8663. END;
  8664. ELSIF t IS SyntaxTree.ArrayType THEN
  8665. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8666. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8667. END;
  8668. END;
  8669. RETURN i
  8670. END Dimension;
  8671. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8672. BEGIN
  8673. RETURN expression.assignable;
  8674. END IsVariable;
  8675. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8676. BEGIN
  8677. IF (symbol IS SyntaxTree.Parameter) THEN
  8678. WITH symbol: SyntaxTree.Parameter DO
  8679. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8680. END;
  8681. ELSE
  8682. RETURN FALSE
  8683. END;
  8684. END IsVariableParameter;
  8685. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8686. VAR result: BOOLEAN;
  8687. BEGIN
  8688. IF type = NIL THEN result := FALSE
  8689. ELSE
  8690. type := type.resolved;
  8691. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8692. END;
  8693. RETURN result
  8694. END IsPointerType;
  8695. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8696. VAR result: BOOLEAN;
  8697. BEGIN
  8698. IF type = NIL THEN result := FALSE
  8699. ELSE
  8700. type := type.resolved;
  8701. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8702. END;
  8703. RETURN result
  8704. END IsUnsafePointer;
  8705. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8706. BEGIN
  8707. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8708. END IsDisposable;
  8709. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8710. VAR result: BOOLEAN;
  8711. BEGIN
  8712. IF type = NIL THEN result := FALSE
  8713. ELSE
  8714. type := type.resolved;
  8715. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8716. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8717. result := result OR (type IS SyntaxTree.ObjectType);
  8718. END;
  8719. RETURN result
  8720. END IsPointerToRecord;
  8721. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8722. VAR result: BOOLEAN;
  8723. BEGIN
  8724. IF type = NIL THEN result := FALSE
  8725. ELSE
  8726. type := type.resolved;
  8727. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8728. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8729. ;
  8730. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8731. result := result OR (type IS SyntaxTree.ObjectType);
  8732. END;
  8733. RETURN result
  8734. END IsPointerToObject;
  8735. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8736. BEGIN
  8737. IF type # NIL THEN
  8738. RETURN type.resolved.hasPointers
  8739. ELSE
  8740. RETURN FALSE
  8741. END;
  8742. END ContainsPointer;
  8743. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8744. BEGIN
  8745. IF type = NIL THEN RETURN FALSE END;
  8746. type := type.resolved;
  8747. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8748. END IsStringType;
  8749. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8750. BEGIN
  8751. IF type = NIL THEN RETURN FALSE END;
  8752. type := type.resolved;
  8753. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8754. END IsCharacterType;
  8755. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8756. BEGIN
  8757. IF type = NIL THEN RETURN FALSE END;
  8758. type := type.resolved;
  8759. RETURN (type IS SyntaxTree.EnumerationType)
  8760. END IsEnumerationType;
  8761. (** cf. section "Type extension (base type)" in the language report **)
  8762. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8763. VAR result: BOOLEAN;
  8764. BEGIN
  8765. ASSERT(base # NIL); ASSERT(extension # NIL);
  8766. base := base.resolved; extension := extension.resolved;
  8767. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8768. result := TRUE;
  8769. ELSE
  8770. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8771. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8772. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8773. END;
  8774. WHILE (extension # NIL) & (extension # base) DO
  8775. IF extension IS SyntaxTree.RecordType THEN
  8776. extension := extension(SyntaxTree.RecordType).baseType;
  8777. IF (extension # NIL) THEN extension := extension.resolved END;
  8778. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8779. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8780. END;
  8781. ELSE extension := NIL;
  8782. END;
  8783. END;
  8784. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8785. END;
  8786. RETURN result
  8787. END IsTypeExtension;
  8788. (** check if base is the base enumeration type of extension **)
  8789. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8790. BEGIN
  8791. base := base.resolved; extension := extension.resolved;
  8792. WHILE (extension # NIL) & (extension # base) DO
  8793. IF extension IS SyntaxTree.EnumerationType THEN
  8794. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8795. IF extension # NIL THEN extension := extension.resolved END;
  8796. ELSE
  8797. extension := NIL
  8798. END;
  8799. END;
  8800. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8801. END IsEnumerationExtension;
  8802. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8803. BEGIN
  8804. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8805. RETURN TRUE
  8806. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8807. RETURN TRUE
  8808. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8809. RETURN TRUE
  8810. ELSE
  8811. RETURN FALSE
  8812. END
  8813. END IsCallable;
  8814. (** compute and return the distance of two record types
  8815. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8816. **)
  8817. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8818. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8819. BEGIN
  8820. i := 0;
  8821. WHILE (from # NIL) & (from # to) DO
  8822. baseType := from.baseType;
  8823. IF (baseType # NIL) THEN
  8824. baseType := baseType.resolved;
  8825. IF baseType IS SyntaxTree.PointerType THEN
  8826. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8827. END;
  8828. IF baseType IS SyntaxTree.RecordType THEN
  8829. from := baseType(SyntaxTree.RecordType);
  8830. ELSE
  8831. from := NIL;
  8832. END;
  8833. ELSE
  8834. from := NIL
  8835. END;
  8836. INC(i)
  8837. END;
  8838. IF from = NIL THEN i := Infinity END;
  8839. RETURN i
  8840. END RecordTypeDistance;
  8841. (** compute and return the distance of two pointer types **)
  8842. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8843. BEGIN
  8844. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8845. RETURN Infinity;
  8846. ELSE
  8847. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8848. END;
  8849. END PointerTypeDistance;
  8850. (** check if expression contains a symbol designator pointing to a type declaration.
  8851. - if so then enter type declaration into typeDeclaration and return true else return false
  8852. **)
  8853. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8854. VAR result: BOOLEAN;
  8855. BEGIN
  8856. result := FALSE;
  8857. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8858. result := TRUE;
  8859. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8860. END;
  8861. RETURN result
  8862. END IsTypeDesignator;
  8863. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8864. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8865. VAR result: BOOLEAN;
  8866. BEGIN
  8867. type := type.resolved;
  8868. IF type IS SyntaxTree.PointerType THEN
  8869. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8870. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8871. result := TRUE
  8872. ELSE
  8873. result := type IS SyntaxTree.RecordType
  8874. END;
  8875. RETURN result
  8876. END IsExtensibleType;
  8877. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8878. BEGIN
  8879. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8880. (d IS SyntaxTree.SymbolDesignator) &
  8881. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8882. OR
  8883. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8884. END IsUnextensibleRecord;
  8885. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8886. BEGIN
  8887. IF IsUnextensibleRecord(d) THEN
  8888. RETURN FALSE
  8889. ELSE RETURN IsExtensibleType(d.type.resolved)
  8890. END;
  8891. END IsExtensibleDesignator;
  8892. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8893. BEGIN
  8894. type := type.resolved;
  8895. IF (type IS SyntaxTree.PointerType) THEN
  8896. RETURN TRUE
  8897. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8898. RETURN TRUE
  8899. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8900. RETURN TRUE
  8901. ELSIF (type IS SyntaxTree.BasicType) THEN
  8902. RETURN TRUE
  8903. END;
  8904. RETURN FALSE
  8905. END IsBasicType;
  8906. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8907. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8908. BEGIN
  8909. baseType := record.baseType;
  8910. IF (baseType # NIL) THEN
  8911. baseType := baseType.resolved;
  8912. IF (baseType IS SyntaxTree.PointerType) THEN
  8913. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8914. END;
  8915. END;
  8916. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8917. recordType := baseType(SyntaxTree.RecordType);
  8918. ELSE
  8919. recordType := NIL;
  8920. END;
  8921. RETURN recordType
  8922. END RecordBase;
  8923. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8924. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8925. BEGIN
  8926. baseRecord := RecordBase(scope.ownerRecord);
  8927. IF baseRecord = NIL THEN RETURN NIL END;
  8928. scope := baseRecord.recordScope;
  8929. procedureType := procedure.type.resolved;
  8930. IF procedure IS SyntaxTree.Operator THEN
  8931. operator := scope.firstOperator;
  8932. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8933. (*
  8934. Printout.Info("not same ",procedureType);
  8935. Printout.Info("with ",operator.type);
  8936. *)
  8937. operator := operator.nextOperator;
  8938. END;
  8939. super := operator;
  8940. ELSE
  8941. super := scope.firstProcedure;
  8942. WHILE (super # NIL) & (super.name # procedure.name) DO
  8943. super := super.nextProcedure;
  8944. END;
  8945. END;
  8946. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8947. RETURN super
  8948. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8949. RETURN super
  8950. ELSE
  8951. RETURN FindSuperProcedure(scope,procedure);
  8952. END;
  8953. END FindSuperProcedure;
  8954. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8955. VAR procedure: SyntaxTree.Procedure;
  8956. BEGIN
  8957. procedure := record.recordScope.constructor;
  8958. IF procedure = NIL THEN
  8959. record := RecordBase(record);
  8960. IF record # NIL THEN
  8961. procedure := GetConstructor(record)
  8962. END;
  8963. END;
  8964. RETURN procedure;
  8965. END GetConstructor;
  8966. (* enter a case into a list of cases in a sorted way and check for collision *)
  8967. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8968. VAR prev,this,new: SyntaxTree.CaseConstant;
  8969. BEGIN
  8970. this := root;
  8971. prev := NIL;
  8972. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8973. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8974. RETURN FALSE
  8975. ELSE
  8976. IF (this # NIL) & (this.min = max+1) THEN
  8977. this.min := min
  8978. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8979. prev.max := min
  8980. ELSE
  8981. NEW(new); new.min := min; new.max := max;
  8982. new.next := this;
  8983. IF prev = NIL THEN
  8984. root := new;
  8985. ELSE
  8986. prev.next := new
  8987. END
  8988. END;
  8989. RETURN TRUE
  8990. END;
  8991. END EnterCase;
  8992. (** generate and return a new checker object, errors are entered into diagnostics **)
  8993. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8994. VAR checker: Checker;
  8995. BEGIN
  8996. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache);
  8997. RETURN checker
  8998. END NewChecker;
  8999. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9000. VAR warnings: Warnings;
  9001. BEGIN
  9002. NEW(warnings, diagnostics); RETURN warnings;
  9003. END NewWarnings;
  9004. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9005. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9006. END IsRangeType;
  9007. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9008. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9009. END IsMathArrayType;
  9010. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9011. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9012. END IsArrayType;
  9013. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9014. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9015. END IsComplexType;
  9016. (** if a type is an array-structured object type *)
  9017. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9018. VAR recordType: SyntaxTree.RecordType;
  9019. BEGIN
  9020. IF type = NIL THEN
  9021. RETURN FALSE
  9022. ELSE
  9023. type := type.resolved;
  9024. IF type IS SyntaxTree.PointerType THEN
  9025. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9026. IF type IS SyntaxTree.RecordType THEN
  9027. recordType := type(SyntaxTree.RecordType);
  9028. RETURN recordType.isObject & recordType.HasArrayStructure()
  9029. ELSE
  9030. RETURN FALSE
  9031. END
  9032. ELSE
  9033. RETURN FALSE
  9034. END
  9035. END
  9036. END IsArrayStructuredObjectType;
  9037. (** the math array structure of a type
  9038. - for math arrays: the array itself
  9039. - for pointers: the math array structure of the pointer base
  9040. - for array-structured object types: the underlying structure
  9041. - for non-math arrays and all other types: NIL
  9042. **)
  9043. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9044. VAR
  9045. result: SyntaxTree.MathArrayType;
  9046. BEGIN
  9047. IF type = NIL THEN
  9048. result := NIL
  9049. ELSE
  9050. type := type.resolved;
  9051. IF type IS SyntaxTree.PointerType THEN
  9052. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9053. END;
  9054. IF type IS SyntaxTree.MathArrayType THEN
  9055. result := type(SyntaxTree.MathArrayType)
  9056. ELSIF type IS SyntaxTree.RecordType THEN
  9057. result := type(SyntaxTree.RecordType).arrayStructure
  9058. ELSE
  9059. result := NIL
  9060. END
  9061. END;
  9062. RETURN result
  9063. END MathArrayStructureOfType;
  9064. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9065. VAR
  9066. result: BOOLEAN;
  9067. rangeExpression: SyntaxTree.RangeExpression;
  9068. BEGIN
  9069. IF x IS SyntaxTree.RangeExpression THEN
  9070. rangeExpression := x(SyntaxTree.RangeExpression);
  9071. result := TRUE;
  9072. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9073. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9074. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9075. ELSE
  9076. result := FALSE
  9077. END;
  9078. RETURN result
  9079. END IsStaticRange;
  9080. (** whether a type is a math array of tensor form **)
  9081. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9082. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9083. END IsTensor;
  9084. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9085. BEGIN
  9086. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9087. length := type(SyntaxTree.MathArrayType).staticLength;
  9088. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9089. RETURN TRUE
  9090. ELSE
  9091. RETURN FALSE
  9092. END;
  9093. END IsStaticMathArray;
  9094. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9095. BEGIN
  9096. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9097. END SymbolHasAddress;
  9098. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9099. BEGIN
  9100. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9101. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9102. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9103. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9104. END HasAddress;
  9105. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9106. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9107. BEGIN
  9108. IF (e IS SyntaxTree.Designator) THEN
  9109. d := e(SyntaxTree.Designator);
  9110. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9111. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9112. e := d.left;
  9113. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9114. END;
  9115. IF d # NIL THEN
  9116. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9117. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9118. END;
  9119. END;
  9120. RETURN FALSE;
  9121. END IsLocalVariable;
  9122. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9123. BEGIN
  9124. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9125. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9126. ELSE
  9127. RETURN TRUE
  9128. END;
  9129. END IsStaticProcedure;
  9130. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9131. CONST OptimizeMethodTable = FALSE;
  9132. BEGIN
  9133. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9134. END InMethodTable;
  9135. END FoxSemanticChecker.