FoxSemanticChecker.Mod 402 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021
  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,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  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. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: Position; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, code, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  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.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. END FixProcedureType;
  725. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  726. VAR prev,this: SyntaxTree.Modifier;
  727. BEGIN
  728. this := modifiers;prev := NIL;
  729. WHILE (this # NIL) & (this.identifier # name) DO
  730. prev := this; this := this.nextModifier;
  731. END;
  732. IF this # NIL THEN
  733. IF this.expression # NIL THEN
  734. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  735. END;
  736. this.Resolved;
  737. position := this.position;
  738. RETURN TRUE
  739. ELSE
  740. RETURN FALSE
  741. END;
  742. END HasFlag;
  743. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  744. VAR prev,this: SyntaxTree.Modifier;
  745. BEGIN
  746. this := modifiers;prev := NIL;
  747. WHILE (this # NIL) & (this.identifier # name) DO
  748. prev := this; this := this.nextModifier;
  749. END;
  750. IF this # NIL THEN
  751. IF this.expression = NIL THEN
  752. Error(this.position,Diagnostics.Invalid,"expected expression value");
  753. ELSE
  754. this.SetExpression(ConstantExpression(this.expression));
  755. IF CheckIntegerValue(this.expression,value) THEN END;
  756. END;
  757. this.Resolved;
  758. position := this.position;
  759. RETURN TRUE
  760. ELSE RETURN FALSE
  761. END;
  762. END HasValue;
  763. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  764. VAR prev,this: SyntaxTree.Modifier;
  765. BEGIN
  766. this := modifiers;prev := NIL;
  767. WHILE (this # NIL) & (this.identifier # name) DO
  768. prev := this; this := this.nextModifier;
  769. END;
  770. IF this # NIL THEN
  771. IF this.expression = NIL THEN
  772. Error(this.position,Diagnostics.Invalid,"expected expression value");
  773. ELSE
  774. this.SetExpression(ConstantExpression(this.expression));
  775. IF CheckStringValue(this.expression,value) THEN END;
  776. END;
  777. this.Resolved;
  778. position := this.position;
  779. RETURN TRUE
  780. ELSE RETURN FALSE
  781. END;
  782. END HasStringValue;
  783. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  784. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  785. BEGIN
  786. IF cellsAreObjects THEN RETURN FALSE END;
  787. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  788. IF svalue = "A2" THEN
  789. RETURN TRUE
  790. END;
  791. END;
  792. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  793. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  794. END;
  795. RETURN FALSE;
  796. (*
  797. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  798. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  799. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  800. IF svalue[0] = "~" THEN
  801. Strings.TrimLeft(svalue, "~");
  802. IF svalue = backendName THEN
  803. RETURN TRUE;
  804. END;
  805. ELSIF svalue # backendName THEN
  806. RETURN TRUE;
  807. END;
  808. END;
  809. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  810. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  811. END;
  812. RETURN FALSE;
  813. *)
  814. END SkipImplementation;
  815. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  816. VAR this: SyntaxTree.Modifier;
  817. BEGIN
  818. this := modifiers;
  819. WHILE this # NIL DO
  820. IF ~this.resolved THEN
  821. IF checkUse THEN
  822. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  823. ELSE
  824. this.SetExpression(ResolveExpression(this.expression));
  825. this.Resolved;
  826. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  827. END;
  828. END;
  829. this := this.nextModifier
  830. END;
  831. END CheckModifiers;
  832. (**
  833. resolve procedure type
  834. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  835. **)
  836. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  837. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  838. BEGIN
  839. IF TypeNeedsResolution(procedureType) THEN
  840. modifiers := procedureType.modifiers;
  841. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  842. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  843. procedureType.SetInterrupt(TRUE);
  844. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  845. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  846. IF useDarwinCCalls THEN (*fld*)
  847. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  848. ELSE
  849. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  850. END
  851. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  852. procedureType.SetNoReturn(TRUE);
  853. END;
  854. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  855. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  856. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  857. CheckModifiers(modifiers, TRUE);
  858. modifiers := procedureType.returnTypeModifiers;
  859. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  860. CheckModifiers(modifiers, TRUE);
  861. typeFixes.Add(procedureType,currentScope);
  862. procedureType.SetHasPointers(procedureType.isDelegate);
  863. procedureType.SetState(SyntaxTree.Resolved);
  864. END;
  865. resolvedType := ResolvedType(procedureType)
  866. END VisitProcedureType;
  867. (** check and resolve record type
  868. - check base type: must be record, math array or array-structured object type
  869. - check declarations
  870. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  871. **)
  872. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  873. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  874. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  875. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  876. hasPointers: BOOLEAN;
  877. modifiers: SyntaxTree.Modifier;
  878. value: LONGINT;
  879. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  880. BEGIN
  881. type := type.resolved;
  882. IF (type IS SyntaxTree.PointerType) &
  883. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  884. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  885. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  886. RETURN TRUE
  887. ELSE
  888. RETURN FALSE
  889. END;
  890. END IsPointerToRecord;
  891. BEGIN
  892. IF TypeNeedsResolution(x) THEN
  893. hasPointers := FALSE;
  894. modifiers := x.modifiers;
  895. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  896. CheckModifiers(modifiers, TRUE);
  897. IF x.baseType # NIL THEN
  898. position := x.baseType.position;
  899. baseType := ResolveType(x.baseType);
  900. resolved := baseType.resolved;
  901. hasPointers := hasPointers OR resolved.hasPointers;
  902. IF x.isObject THEN (* object *)
  903. ASSERT(x.pointerType # NIL);
  904. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  905. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  906. baseType := NIL
  907. ELSIF IsPointerToRecord(resolved,recordType) THEN
  908. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  909. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  910. ELSE
  911. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  912. END;
  913. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  914. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  915. ELSIF IsPointerToRecord(resolved,recordType) THEN
  916. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  917. ELSIF resolved IS SyntaxTree.RecordType THEN
  918. ELSE
  919. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  920. END;
  921. ELSE
  922. IF resolved IS SyntaxTree.RecordType THEN
  923. ELSE
  924. Error(position, Diagnostics.Invalid,"record does not extend record")
  925. END;
  926. END;
  927. x.SetBaseType(baseType);
  928. IF x.Level() > 15 THEN
  929. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  930. (* note:
  931. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  932. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  933. inheritance history of a type.
  934. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  935. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  936. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  937. *)
  938. END;
  939. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  940. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  941. END;
  942. END;
  943. Declarations(x.recordScope, FALSE);
  944. ResolveArrayStructure(x);
  945. (* computation of sizes and offsets skipped -> done in backend / system *)
  946. recordBase := x.GetBaseRecord();
  947. IF recordBase = NIL THEN numberMethods := 0
  948. ELSE numberMethods := recordBase.recordScope.numberMethods
  949. END;
  950. isRealtime := TRUE;
  951. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  952. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  953. END;
  954. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  955. WHILE symbol # NIL DO
  956. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  957. IF symbol IS SyntaxTree.Variable THEN
  958. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  959. END;
  960. IF symbol IS SyntaxTree.Procedure THEN
  961. procedure := symbol(SyntaxTree.Procedure);
  962. IF procedure.super # NIL THEN
  963. procedure.SetMethodNumber(procedure.super.methodNumber)
  964. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  965. procedure.SetMethodNumber(numberMethods);
  966. INC(numberMethods);
  967. END;
  968. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  969. Error(procedure.position, Diagnostics.Invalid,"realtime procedure in non-realtime object")
  970. END;
  971. END;
  972. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  973. Error(symbol.position, Diagnostics.Invalid,"non-realtime symbol in realtime object")
  974. END;
  975. symbol := symbol.nextSymbol;
  976. END;
  977. IF isRealtime THEN x.SetRealtime(TRUE) END;
  978. x.recordScope.SetNumberMethods(numberMethods);
  979. (* TODO: is this needed anymore? *)
  980. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  981. Error(x.position,Diagnostics.Invalid,"object extends a record")
  982. END;
  983. IF (x.typeDeclaration = NIL) THEN
  984. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  985. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  986. (*
  987. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  988. AnonymousTypeDeclaration(x,name);
  989. *)
  990. ELSE
  991. AnonymousTypeDeclaration(x,"Anonymous");
  992. END;
  993. END;
  994. x.SetHasPointers(hasPointers);
  995. x.SetState(SyntaxTree.Resolved);
  996. END;
  997. resolvedType := ResolvedType(x);
  998. END VisitRecordType;
  999. (** check and resolve cell type
  1000. - check base type: must be cell
  1001. - check declarations
  1002. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1003. **)
  1004. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1005. VAR
  1006. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1007. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1008. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1009. recordBase: SyntaxTree.RecordType;
  1010. numberMethods, int: LONGINT;
  1011. real: LONGREAL;
  1012. bool: BOOLEAN;
  1013. set: SET;
  1014. v: SyntaxTree.Expression;
  1015. str: Scanner.StringType;
  1016. atype: SyntaxTree.ArrayType;
  1017. prev: SyntaxTree.Scope;
  1018. skip: BOOLEAN;
  1019. svalue: ARRAY 32 OF CHAR;
  1020. BEGIN
  1021. IF TypeNeedsResolution(x) THEN
  1022. recordBase := NIL;
  1023. IF cellsAreObjects THEN
  1024. IF x.baseType = NIL THEN
  1025. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1026. ImportModule(qualifiedIdentifier.prefix, x.position);
  1027. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1028. x.SetBaseType(ResolveType(x.baseType));
  1029. recordBase := x.GetBaseRecord();
  1030. IF recordBase = NIL THEN
  1031. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1032. END;
  1033. ELSE
  1034. x.SetBaseType(ResolveType(x.baseType));
  1035. END;
  1036. ELSE
  1037. x.SetBaseType(ResolveType(x.baseType));
  1038. END;
  1039. IF recordBase = NIL THEN numberMethods := 0
  1040. ELSE numberMethods := recordBase.recordScope.numberMethods
  1041. END;
  1042. modifier := x.modifiers;
  1043. (*IF ~x.isCellNet THEN*)
  1044. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1045. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1046. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1047. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1048. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1049. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1050. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1051. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1052. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1053. symbol := system.activeCellsCapabilities;
  1054. WHILE symbol # NIL DO
  1055. IF HasFlag(modifier, symbol.name, position) THEN END;
  1056. symbol := symbol.nextSymbol;
  1057. END;
  1058. modifier := x.modifiers;
  1059. WHILE (modifier # NIL) DO
  1060. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1061. IF modifier.expression # NIL THEN
  1062. v := ConstantExpression(modifier.expression);
  1063. property.SetValue(v);
  1064. IF IsIntegerValue(modifier.expression, int) THEN
  1065. (*property.SetValue(modifier.expression);*)
  1066. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1067. property.SetType(system.longintType);
  1068. ELSIF IsRealValue(modifier.expression, real) THEN
  1069. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1070. property.SetType(system.longrealType);
  1071. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1072. property.SetType(system.booleanType);
  1073. ELSIF IsSetValue(modifier.expression, set) THEN
  1074. property.SetType(system.setType);
  1075. ELSIF IsStringValue(modifier.expression, str) THEN
  1076. (*property.SetValue(modifier.expression);*)
  1077. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1078. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1079. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1080. property.SetType(atype);
  1081. ELSE
  1082. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1083. END;
  1084. ELSE (* flag property *)
  1085. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1086. property.SetType(system.booleanType);
  1087. END;
  1088. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1089. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1090. x.AddProperty(property);
  1091. modifier := modifier.nextModifier;
  1092. END;
  1093. CheckModifiers(modifier, FALSE);
  1094. Declarations(x.cellScope, SkipImplementation(x));
  1095. (* process parameters *)
  1096. prev := currentScope;
  1097. currentScope := x.cellScope;
  1098. parameter :=x.firstParameter;
  1099. WHILE (parameter # NIL) DO
  1100. VisitParameter(parameter);
  1101. type := parameter.type.resolved;
  1102. IF ~(type IS SyntaxTree.PortType) THEN
  1103. WHILE IsStaticArray(type, type, len) DO
  1104. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1105. END;
  1106. WHILE IsDynamicArray(type, type) DO
  1107. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1108. END;
  1109. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1110. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1111. END;
  1112. END;
  1113. parameter := parameter.nextParameter;
  1114. END;
  1115. currentScope := prev;
  1116. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1117. WHILE symbol # NIL DO
  1118. IF symbol IS SyntaxTree.Variable THEN
  1119. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1120. END;
  1121. symbol := symbol.nextSymbol;
  1122. END;
  1123. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1124. IF (x.typeDeclaration = NIL) THEN
  1125. AnonymousTypeDeclaration(x,"Anonymous");
  1126. END;
  1127. x.SetState(SyntaxTree.Resolved);
  1128. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1129. Warning(x.position, "Forbidden empty Body.");
  1130. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1131. Warning(x.position, "Non-empty body for an engine?");
  1132. END;
  1133. END;
  1134. resolvedType := ResolvedType(x);
  1135. END VisitCellType;
  1136. (* check if an object is an array-structured object type
  1137. - determine the array structure
  1138. - collect operators from top to bottom in the inheritance hierarchy
  1139. - check if LEN operator is declared
  1140. - determine number of possible index operators
  1141. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1142. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1143. *)
  1144. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1145. VAR
  1146. indexOperatorCount, i: LONGINT;
  1147. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1148. isTensor: BOOLEAN;
  1149. BEGIN
  1150. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1151. (* determine array structure *)
  1152. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1153. END;
  1154. IF recordType.HasArrayStructure() THEN
  1155. (* the object is an ASOT *)
  1156. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1157. (* reset array access operators *)
  1158. arrayAccessOperators.len := NIL;
  1159. arrayAccessOperators.generalRead := NIL;
  1160. arrayAccessOperators.generalWrite := NIL;
  1161. IF isTensor THEN
  1162. (* all operators of dimensionalities 1 to max *)
  1163. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1164. ELSE
  1165. (* all operators of certain dimensionality *)
  1166. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1167. END;
  1168. NEW(arrayAccessOperators.read, indexOperatorCount);
  1169. NEW(arrayAccessOperators.write, indexOperatorCount);
  1170. FOR i := 0 TO indexOperatorCount - 1 DO
  1171. arrayAccessOperators.read[i] := NIL;
  1172. arrayAccessOperators.write[i] := NIL
  1173. END;
  1174. (* collect access operators in the record scope *)
  1175. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1176. IF arrayAccessOperators.len = NIL THEN
  1177. (* TODO: think about making this operator optional for static array structures *)
  1178. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1179. END;
  1180. (* show error messages *)
  1181. IF isTensor THEN
  1182. (* require ARRAY [*] OF RANGE *)
  1183. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1184. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1185. ELSE
  1186. (* forbid ARRAY [*] OF RANGE *)
  1187. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1188. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1189. (* require RANGE, RANGE, ... RANGE *)
  1190. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1191. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1192. END;
  1193. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1194. ELSE
  1195. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1196. IF recordType.recordScope.firstOperator # NIL THEN
  1197. RETURN;
  1198. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1199. END
  1200. END
  1201. END ResolveArrayStructure;
  1202. (** collect array access operators in a record scope **)
  1203. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1204. VAR
  1205. baseType: SyntaxTree.Type;
  1206. operator: SyntaxTree.Operator;
  1207. isReadOperator, isGeneralOperator: BOOLEAN;
  1208. indexListSize, indexListKind, hashValue: LONGINT;
  1209. BEGIN
  1210. (* if a parent record scope exists, collect the operators there first *)
  1211. baseType := recordScope.ownerRecord.baseType;
  1212. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1213. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1214. END;
  1215. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1216. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1217. END;
  1218. (* go through all operators in the current record scope *)
  1219. operator := recordScope.firstOperator;
  1220. WHILE operator # NIL DO
  1221. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1222. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1223. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1224. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1225. IF isGeneralOperator THEN
  1226. IF isReadOperator THEN
  1227. arrayAccessOperators.generalRead := operator
  1228. ELSE
  1229. arrayAccessOperators.generalWrite := operator
  1230. END
  1231. ELSE
  1232. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1233. IF isReadOperator THEN
  1234. arrayAccessOperators.read[hashValue] := operator
  1235. ELSE
  1236. arrayAccessOperators.write[hashValue] := operator
  1237. END
  1238. END
  1239. END
  1240. ELSE
  1241. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1242. END;
  1243. operator := operator.nextOperator
  1244. END
  1245. END CollectArrayAccessOperators;
  1246. (** the hash value of an index operator **)
  1247. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1248. VAR result: LONGINT;
  1249. BEGIN
  1250. IF isTensor THEN
  1251. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1252. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1253. ELSE
  1254. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1255. END
  1256. ELSE
  1257. result := indexListKind
  1258. END;
  1259. RETURN result
  1260. END IndexOperatorHash;
  1261. (** 2 to the power of exponent **)
  1262. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1263. VAR result, i: LONGINT;
  1264. BEGIN
  1265. result := 1;
  1266. FOR i := 1 TO exponent DO
  1267. result := result * 2;
  1268. END;
  1269. RETURN result
  1270. END TwoToThePowerOf;
  1271. (** check if a LEN operator has a correct signature. i.e.
  1272. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1273. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1274. **)
  1275. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1276. VAR
  1277. procedureType: SyntaxTree.ProcedureType;
  1278. returnedArrayType: SyntaxTree.MathArrayType;
  1279. result: BOOLEAN;
  1280. BEGIN
  1281. result := FALSE;
  1282. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1283. IF (procedureType.numberParameters = 0) THEN
  1284. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1285. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1286. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1287. IF returnedArrayType.form = SyntaxTree.Open THEN
  1288. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1289. result := TRUE
  1290. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1291. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1292. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1293. result := TRUE
  1294. END
  1295. END
  1296. END
  1297. END
  1298. END;
  1299. IF result THEN
  1300. (* export symbol automatically *)
  1301. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1302. ELSE
  1303. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1304. END;
  1305. RETURN result
  1306. END CheckLenOperator;
  1307. (** check if an index operator has a correct signature. i.e.
  1308. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1309. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1310. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1311. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1312. - determine if it is a read or write operator (existance of return type)
  1313. - check index parameters
  1314. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1315. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1316. [LONGINT] -> binary 0 -> 0
  1317. [RANGE] -> binary 1 -> 1
  1318. [LONGINT, LONGINT] -> binary 00 -> 0
  1319. [LONGINT, RANGE] -> binary 01 -> 1
  1320. [RANGE, LONGINT] -> binary 10 -> 2
  1321. [RANGE, RANGE] -> binary 11 -> 3
  1322. etc.
  1323. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1324. - for read operators, check if return type matches the type of data that is read
  1325. - for write operators, check if last parameter type matches the type of data that is written
  1326. **)
  1327. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1328. VAR
  1329. elementType, otherElementType, dataType: SyntaxTree.Type;
  1330. procedureType: SyntaxTree.ProcedureType;
  1331. mathArrayType: SyntaxTree.MathArrayType;
  1332. parameter: SyntaxTree.Parameter;
  1333. parameterCount, rangeCount, i: LONGINT;
  1334. hasTypeError: BOOLEAN;
  1335. BEGIN
  1336. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1337. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1338. (* determine if it is a read or write operator *)
  1339. isReadOperator := (procedureType.returnType # NIL);
  1340. IF isReadOperator THEN
  1341. indexListSize := parameterCount;
  1342. ELSE
  1343. indexListSize := parameterCount - 1;
  1344. END;
  1345. IF indexListSize < 1 THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1347. RETURN FALSE
  1348. END;
  1349. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1350. (* general operator *)
  1351. isGeneralOperator := TRUE;
  1352. IF indexListSize > 1 THEN
  1353. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1354. RETURN FALSE
  1355. END;
  1356. (* ARRAY [*] OF RANGE*)
  1357. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1358. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1359. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1360. RETURN FALSE
  1361. END;
  1362. parameter := procedureType.firstParameter.nextParameter
  1363. ELSE
  1364. (* fixed-dim. operator *)
  1365. isGeneralOperator := FALSE;
  1366. (* check number of index parameters *)
  1367. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1368. (* for tensors, limited to a certain size *)
  1369. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1370. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1371. RETURN FALSE
  1372. END
  1373. ELSE
  1374. (* for non-tensors, depends on dimensionality *)
  1375. IF indexListSize # arrayStructure.Dimensionality() THEN
  1376. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1377. RETURN FALSE
  1378. END
  1379. END;
  1380. (* go through all index parameters
  1381. - count the number of ranges
  1382. - determine the index list kind number
  1383. *)
  1384. indexListKind := 0;
  1385. rangeCount := 0;
  1386. parameter := procedureType.firstParameter;
  1387. FOR i := 1 TO indexListSize DO
  1388. indexListKind := indexListKind * 2;
  1389. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1390. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1391. INC(indexListKind);
  1392. INC(rangeCount)
  1393. ELSE
  1394. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1395. RETURN FALSE
  1396. END;
  1397. parameter := parameter.nextParameter
  1398. END;
  1399. END;
  1400. (*
  1401. - for read operators: check type of last parameter
  1402. - for write operators: check return type
  1403. *)
  1404. IF isReadOperator THEN
  1405. dataType := procedureType.returnType (* the return type *)
  1406. ELSE
  1407. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1408. END;
  1409. elementType := arrayStructure.ElementType();
  1410. hasTypeError := FALSE;
  1411. IF isGeneralOperator THEN
  1412. (* ARRAY [?] OF <Element> *)
  1413. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1414. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1415. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1416. hasTypeError := TRUE
  1417. END
  1418. ELSE
  1419. hasTypeError := TRUE
  1420. END
  1421. ELSE
  1422. IF rangeCount = 0 THEN
  1423. (* <Element> *)
  1424. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1425. ELSE
  1426. (* ARRAY [*, *, ..., *] OF <Element> *)
  1427. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1428. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1429. IF mathArrayType.IsFullyDynamic() THEN
  1430. IF mathArrayType.Dimensionality() = rangeCount THEN
  1431. otherElementType := mathArrayType.ElementType();
  1432. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1433. ELSE
  1434. hasTypeError := TRUE
  1435. END
  1436. ELSE
  1437. hasTypeError := TRUE
  1438. END
  1439. ELSE
  1440. hasTypeError := TRUE
  1441. END
  1442. END
  1443. END;
  1444. IF hasTypeError THEN
  1445. IF isReadOperator THEN
  1446. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1447. ELSE
  1448. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1449. END;
  1450. RETURN FALSE
  1451. END;
  1452. (* export symbol automatically *)
  1453. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1454. RETURN TRUE
  1455. END CheckIndexOperator;
  1456. (** resolve all pending types (late resolving).
  1457. - type fixes are resolved at the end of the declaration phase
  1458. - type fixes may imply new type fixes that are also entered at the end of the list
  1459. **)
  1460. PROCEDURE FixTypes;
  1461. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1462. BEGIN
  1463. prevScope := currentScope;
  1464. p := typeFixes.Get(currentScope);
  1465. WHILE p # NIL DO
  1466. ASSERT(currentScope # NIL);
  1467. ASSERT(p IS SyntaxTree.Type);
  1468. IF p IS SyntaxTree.PointerType THEN
  1469. FixPointerType(p(SyntaxTree.PointerType))
  1470. ELSIF p IS SyntaxTree.ProcedureType THEN
  1471. FixProcedureType(p(SyntaxTree.ProcedureType))
  1472. ELSE
  1473. HALT(100);
  1474. END;
  1475. p := typeFixes.Get(currentScope);
  1476. END;
  1477. currentScope :=prevScope;
  1478. END FixTypes;
  1479. (**
  1480. resolve type x
  1481. - if x is nil then return nil
  1482. - if x cannot be resolved then the result is invalidType else the result is x
  1483. - the resolved type is entered into x.resolved
  1484. **)
  1485. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1486. VAR prev,resolved: SyntaxTree.Type;
  1487. BEGIN
  1488. prev := resolvedType;
  1489. resolvedType := SyntaxTree.invalidType;
  1490. IF x = NIL THEN resolvedType := NIL
  1491. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1492. END;
  1493. resolved := resolvedType;
  1494. resolvedType := prev;
  1495. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1496. RETURN resolved
  1497. END ResolveType;
  1498. (*** compatibility rules ***)
  1499. (**
  1500. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1501. **)
  1502. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1503. VAR result: SyntaxTree.Type;
  1504. BEGIN
  1505. result := SyntaxTree.invalidType;
  1506. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1507. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1508. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1509. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1510. ELSE result := type.resolved
  1511. END;
  1512. RETURN result
  1513. END RegularType;
  1514. (** returns signature compatibility of procedure types this and to
  1515. - if not compatible then error is reported
  1516. - compatibility means type equality
  1517. **)
  1518. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1519. VAR result: BOOLEAN;
  1520. BEGIN
  1521. result := SameType(to,this);
  1522. IF ~result THEN
  1523. Error(position,Diagnostics.Invalid,"signature incompatible");
  1524. IF VerboseErrorMessage THEN
  1525. Printout.Info("this",this);
  1526. Printout.Info("to",to);
  1527. END;
  1528. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1529. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1530. END;
  1531. RETURN result
  1532. END SignatureCompatible;
  1533. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1534. - for var parameters compatibility means same type except for
  1535. - formal is of open array of system byte
  1536. - formal is of record type
  1537. - formal is of open array type
  1538. - formal is of open math array type
  1539. - for value parameters compatibllity means assignment compatibility except for
  1540. - formal is of open array type
  1541. if compatible the return true else report error and return false
  1542. **)
  1543. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1544. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1545. BEGIN
  1546. formalType := RegularType(formal.position,formal.type);
  1547. actualType := RegularType(actual.position,actual.type);
  1548. error := FALSE;
  1549. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1550. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1551. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1552. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1553. END;
  1554. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1555. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1556. ELSIF ~IsVariable(actual) THEN
  1557. result := FALSE; error := TRUE;
  1558. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1559. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1560. ELSE
  1561. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1562. END;
  1563. IF VerboseErrorMessage THEN
  1564. Printout.Info("actual",actual);
  1565. Printout.Info("formal",formal);
  1566. END;
  1567. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1568. result := CompatibleTo(system,actualType,formalType);
  1569. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1570. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1571. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1572. IF IsArrayStructuredObjectType(actualType) THEN
  1573. actualType := MathArrayStructureOfType(actualType)
  1574. END;
  1575. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1576. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1577. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1578. END;
  1579. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1580. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1581. END;
  1582. ELSE
  1583. result := SameType(actualType,formalType)
  1584. END
  1585. ELSE
  1586. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1587. actualType := system.characterType;
  1588. END;
  1589. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1590. result := TRUE; (* special rule for WINAPI parameters *)
  1591. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1592. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1593. ELSE
  1594. result := CompatibleTo(system,actualType,formalType);
  1595. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1596. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1597. END;
  1598. END;
  1599. END;
  1600. IF ~result & ~error THEN
  1601. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1602. IF VerboseErrorMessage THEN
  1603. Printout.Info("actual",actual);
  1604. Printout.Info("formal",formal);
  1605. END;
  1606. END;
  1607. RETURN result
  1608. END ParameterCompatible;
  1609. (** check compatibility for expressions of the form left := right
  1610. - if compatible then return true else error report and return false
  1611. - check if left is variable
  1612. - check compatiblity
  1613. **)
  1614. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1615. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1616. BEGIN
  1617. result := FALSE;
  1618. leftType := RegularType(left.position,left.type);
  1619. rightType := RegularType(right.position,right.type);
  1620. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1621. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1622. END;
  1623. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1624. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1625. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1626. (* error already handled *)
  1627. result := TRUE;
  1628. ELSIF ~IsVariable(left) THEN
  1629. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1630. IF VerboseErrorMessage THEN
  1631. Printout.Info("left",left);
  1632. Printout.Info("right",right);
  1633. END;
  1634. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1635. result := TRUE;
  1636. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1637. result := TRUE
  1638. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1639. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1640. IF VerboseErrorMessage THEN
  1641. Printout.Info("left",left);
  1642. Printout.Info("right",right);
  1643. END;
  1644. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1645. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1646. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1647. ELSE
  1648. result := TRUE
  1649. END;
  1650. RETURN result
  1651. END AssignmentCompatible;
  1652. (*** values ***)
  1653. (** check and resolve integer value **)
  1654. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1655. VAR hugeint: HUGEINT;
  1656. BEGIN
  1657. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1658. value.SetType(Global.GetIntegerType(system,hugeint));
  1659. resolvedExpression := value
  1660. END VisitIntegerValue;
  1661. (** check and resolve real value **)
  1662. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1663. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1664. BEGIN
  1665. subtype := value(SyntaxTree.RealValue).subtype;
  1666. IF subtype = Scanner.Real THEN
  1667. type := system.realType
  1668. ELSIF subtype = Scanner.Longreal THEN
  1669. type := system.longrealType
  1670. ELSE
  1671. HALT(100)
  1672. END;
  1673. value.SetType(type);
  1674. resolvedExpression := value
  1675. END VisitRealValue;
  1676. (** check and resolve complex value **)
  1677. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1678. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1679. BEGIN
  1680. subtype := value(SyntaxTree.ComplexValue).subtype;
  1681. IF subtype = Scanner.Real THEN
  1682. type := system.complexType
  1683. ELSIF subtype = Scanner.Longreal THEN
  1684. type := system.longcomplexType
  1685. ELSE
  1686. HALT(100)
  1687. END;
  1688. value.SetType(type);
  1689. resolvedExpression := value
  1690. END VisitComplexValue;
  1691. (** check and resolve set value **)
  1692. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1693. BEGIN
  1694. value.SetType(system.setType);
  1695. resolvedExpression := value
  1696. END VisitSetValue;
  1697. (** check and resolve set value **)
  1698. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1699. BEGIN
  1700. value.SetType(SyntaxTree.invalidType);
  1701. resolvedExpression := value
  1702. END VisitMathArrayValue;
  1703. (** check and resolve boolean value **)
  1704. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1705. BEGIN
  1706. value.SetType(system.booleanType);
  1707. resolvedExpression := value
  1708. END VisitBooleanValue;
  1709. (** check and resolve string value **)
  1710. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1711. BEGIN
  1712. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1713. resolvedExpression := value
  1714. END VisitStringValue;
  1715. (** check and resolve character value **)
  1716. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1717. BEGIN
  1718. value.SetType(system.characterType);
  1719. resolvedExpression := value
  1720. END VisitCharacterValue;
  1721. (** check and resolve nil value **)
  1722. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1723. BEGIN
  1724. value.SetType(system.nilType);
  1725. resolvedExpression := value
  1726. END VisitNilValue;
  1727. (** check and resolve enumerator value **)
  1728. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1729. BEGIN
  1730. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1731. ASSERT(value.type # NIL);
  1732. resolvedExpression := value
  1733. END VisitEnumerationValue;
  1734. (*** expressions ***)
  1735. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1736. - check all elements on integer type
  1737. - if element range is constant, then check lower and upper bound
  1738. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1739. if an error occurs then report error and return invalidExpression
  1740. **)
  1741. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1742. VAR
  1743. i: LONGINT;
  1744. element: SyntaxTree.Expression;
  1745. constant: BOOLEAN;
  1746. elements: SyntaxTree.ExpressionList;
  1747. s: SET;
  1748. result: SyntaxTree.Expression;
  1749. value: SyntaxTree.Value;
  1750. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1751. VAR
  1752. left, right: SyntaxTree.Expression;
  1753. elementResult: SyntaxTree.Expression;
  1754. leftInteger, rightInteger, temp: LONGINT;
  1755. BEGIN
  1756. (* set context of range *)
  1757. IF element IS SyntaxTree.RangeExpression THEN
  1758. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1759. END;
  1760. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1761. IF elementResult = SyntaxTree.invalidExpression THEN
  1762. (* error already reported *)
  1763. constant := FALSE
  1764. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1765. (* the element is a range expression *)
  1766. (* extract left and right hand side of range *)
  1767. left := elementResult(SyntaxTree.RangeExpression).first;
  1768. right := elementResult(SyntaxTree.RangeExpression).last;
  1769. (* guaranteed by VisitRangeExpression: *)
  1770. ASSERT((left # NIL) & (right # NIL));
  1771. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1772. ELSE
  1773. (* the element is not a range expression *)
  1774. (* check type and add conversion if needed *)
  1775. IF IsIntegerType(elementResult.type.resolved) THEN
  1776. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1777. ELSE
  1778. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1779. elementResult := SyntaxTree.invalidExpression;
  1780. constant := FALSE
  1781. END;
  1782. left := elementResult;
  1783. right := elementResult
  1784. END;
  1785. IF elementResult # SyntaxTree.invalidExpression THEN
  1786. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1787. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1788. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1789. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1790. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1791. END
  1792. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1793. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1794. ELSE
  1795. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1796. s := {};
  1797. ELSE
  1798. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1799. IF leftInteger < 0 THEN leftInteger := 0 END;
  1800. (*!!!!!!!!! this is a hack !!!!!!! *)
  1801. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1802. extends the range x..31 to x..63 !!!!!! *)
  1803. s := s + {leftInteger..rightInteger};
  1804. END;
  1805. END;
  1806. ELSE
  1807. constant := FALSE;
  1808. END
  1809. END;
  1810. RETURN elementResult
  1811. END CheckElement;
  1812. BEGIN
  1813. result := set; constant := TRUE; s := {}; elements := set.elements;
  1814. IF elements # NIL THEN
  1815. FOR i := 0 TO elements.Length()-1 DO
  1816. element := elements.GetExpression(i);
  1817. element := CheckElement(element);
  1818. IF element = SyntaxTree.invalidExpression THEN
  1819. result := SyntaxTree.invalidExpression
  1820. END;
  1821. elements.SetExpression(i,element);
  1822. END;
  1823. END;
  1824. IF constant THEN
  1825. value := SyntaxTree.NewSetValue(set.position,s);
  1826. value.SetType(system.setType);
  1827. result.SetResolved(value);
  1828. END;
  1829. (* optimization possible
  1830. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1831. left this to the programmer...
  1832. *)
  1833. result.SetType(system.setType);
  1834. resolvedExpression := result;
  1835. END VisitSet;
  1836. (*
  1837. old variant: quite generic but needs better conversion handling, do this?
  1838. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1839. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1840. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1841. BEGIN
  1842. type := NIL;
  1843. numberElements := x.elements.Length();
  1844. FOR i := 0 TO numberElements-1 DO
  1845. expression := x.elements.GetExpression(i);
  1846. position := expression.position;
  1847. expression := ResolveExpression(x.elements.GetExpression(i));
  1848. x.elements.SetExpression(i,de);
  1849. IF type = NIL THEN
  1850. type := expression.type;
  1851. ELSIF CompatibleTo(system,expression.type,type) THEN
  1852. (* ok *)
  1853. ELSIF CompatibleTo(system,type,expression.type) THEN
  1854. type := expression.type
  1855. ELSE
  1856. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1857. type := SyntaxTree.invalidType;
  1858. END;
  1859. END;
  1860. isValue := TRUE;
  1861. FOR i := 0 TO numberElements-1 DO
  1862. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1863. x.elements.SetExpression(i,expression);
  1864. isValue := isValue & (expression.resolved # NIL);
  1865. END;
  1866. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1867. arrayType.SetArrayBase(type);
  1868. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1869. IF isValue THEN
  1870. value := SyntaxTree.NewMathArrayValue(position);
  1871. value.SetElements(x.elements);
  1872. x.SetResolved(value);
  1873. END;
  1874. x.SetType(arrayType);
  1875. resolvedExpression := x;
  1876. END VisitMathArrayExpression;
  1877. *)
  1878. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1879. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1880. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1881. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1882. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1883. BEGIN
  1884. numberElements := x.elements.Length();
  1885. FOR i := 0 TO numberElements-1 DO
  1886. expression := x.elements.GetExpression(i);
  1887. IF expression IS SyntaxTree.MathArrayExpression THEN
  1888. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1889. ELSE
  1890. position := expression.position;
  1891. expression := ResolveExpression(x.elements.GetExpression(i));
  1892. x.elements.SetExpression(i,expression);
  1893. IF type = NIL THEN
  1894. type := expression.type;
  1895. ELSIF CompatibleTo(system,expression.type,type) THEN
  1896. (* ok *)
  1897. ELSIF CompatibleTo(system,type,expression.type) THEN
  1898. type := expression.type
  1899. ELSE
  1900. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1901. type := SyntaxTree.invalidType;
  1902. END;
  1903. END;
  1904. END;
  1905. END RecursivelyFindType;
  1906. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1907. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1908. BEGIN
  1909. numberElements := x.elements.Length();
  1910. FOR i := 0 TO numberElements-1 DO
  1911. expression := x.elements.GetExpression(i);
  1912. IF expression IS SyntaxTree.MathArrayExpression THEN
  1913. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1914. ELSE
  1915. position := expression.position;
  1916. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1917. x.elements.SetExpression(i,expression);
  1918. isValue := isValue & (expression.resolved # NIL);
  1919. END;
  1920. END;
  1921. END RecursivelySetExpression;
  1922. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1923. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1924. arrayType: SyntaxTree.MathArrayType;
  1925. BEGIN
  1926. numberElements := x.elements.Length();
  1927. baseType := NIL;
  1928. gsize := 0;
  1929. FOR i := 0 TO numberElements-1 DO
  1930. expression := x.elements.GetExpression(i);
  1931. IF expression IS SyntaxTree.MathArrayExpression THEN
  1932. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1933. IF i=0 THEN
  1934. gsize := size;
  1935. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1936. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1937. ELSE expression.SetType(baseType)
  1938. END;
  1939. ELSIF baseType = NIL THEN baseType := type;
  1940. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1941. END;
  1942. END;
  1943. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1944. arrayType.SetArrayBase(baseType);
  1945. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1946. RETURN ResolveType(arrayType);
  1947. END RecursivelySetType;
  1948. BEGIN
  1949. type := NIL;
  1950. RecursivelyFindType(x);
  1951. isValue := TRUE;
  1952. RecursivelySetExpression(x);
  1953. arrayType := RecursivelySetType(x);
  1954. x.SetType(arrayType);
  1955. IF isValue THEN
  1956. value := SyntaxTree.NewMathArrayValue(x.position);
  1957. value.SetArray(x);
  1958. x.SetResolved(value);
  1959. value.SetType(arrayType);
  1960. END;
  1961. x.SetType(arrayType);
  1962. resolvedExpression := x;
  1963. END VisitMathArrayExpression;
  1964. (** check and resolve unary expression **)
  1965. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1966. VAR
  1967. left: SyntaxTree.Expression;
  1968. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1969. bool: BOOLEAN;
  1970. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1971. value: SyntaxTree.Value;
  1972. BEGIN
  1973. type := SyntaxTree.invalidType;
  1974. left := ResolveExpression(unaryExpression.left);
  1975. unaryExpression.SetLeft(left);
  1976. operator := unaryExpression.operator;
  1977. result := unaryExpression;
  1978. IF ~system.operatorDefined[operator] THEN
  1979. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1980. RETURN
  1981. ELSIF left.type = NIL THEN
  1982. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1983. resolvedExpression := SyntaxTree.invalidExpression;
  1984. RETURN
  1985. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1986. RETURN
  1987. END;
  1988. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1989. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1990. END;
  1991. IF operatorCall # NIL THEN
  1992. result := operatorCall;
  1993. type := operatorCall.type;
  1994. (* admissible operators
  1995. Minus number, set
  1996. Not boolean
  1997. *)
  1998. ELSE
  1999. CASE unaryExpression.operator OF
  2000. |Scanner.Minus:
  2001. IF IsIntegerType(left.type.resolved) THEN
  2002. IF left.resolved # NIL THEN
  2003. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2004. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2005. result.SetResolved(value);
  2006. type := Global.GetIntegerType(system,int);
  2007. value.SetType(type);
  2008. ELSE
  2009. type := left.type
  2010. END
  2011. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2012. IF IsRealValue(left,real) THEN
  2013. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2014. result.SetResolved(value);
  2015. type := left.type;
  2016. value.SetType(type);
  2017. ELSE
  2018. type := left.type;
  2019. END;
  2020. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2021. IF IsSetValue(left,set) THEN
  2022. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2023. result.SetResolved(value);
  2024. type := left.type;
  2025. value.SetType(type);
  2026. ELSE
  2027. type := left.type;
  2028. END;
  2029. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2030. IF IsComplexValue(left, real, imaginary) THEN
  2031. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2032. result.SetResolved(value);
  2033. type := left.type;
  2034. value.SetType(type);
  2035. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2036. ELSE
  2037. type := left.type;
  2038. END
  2039. ELSE
  2040. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2041. END;
  2042. |Scanner.Not:
  2043. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2044. IF IsBooleanValue(left,bool) THEN
  2045. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2046. result.SetResolved(value);
  2047. type := system.booleanType;
  2048. value.SetType(type);
  2049. ELSE
  2050. type := system.booleanType;
  2051. END;
  2052. ELSE
  2053. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2054. END;
  2055. |Scanner.Plus:
  2056. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2057. result := left; type := left.type;
  2058. ELSE
  2059. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2060. END;
  2061. (* ADDRESS OF *)
  2062. |Scanner.Address:
  2063. IF HasAddress(left) THEN
  2064. type := system.addressType;
  2065. ELSE
  2066. type := SyntaxTree.invalidType;
  2067. Error(left.position,Diagnostics.Invalid,"has no address");
  2068. Printout.Info("par", left);
  2069. END;
  2070. (* SIZE OF *)
  2071. |Scanner.Size:
  2072. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2073. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2074. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2075. value := SyntaxTree.NewIntegerValue(left.position, int);
  2076. result.SetResolved(value);
  2077. type := Global.GetIntegerType(system,int);
  2078. value.SetType(type)
  2079. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2080. ELSE
  2081. (* for variables, system sizeof could represent the physically occupied size
  2082. determined via the type descriptor, implement that ? *)
  2083. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2084. END
  2085. (* ALIAS OF *)
  2086. |Scanner.Alias:
  2087. type := left.type.resolved;
  2088. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2089. type := SyntaxTree.invalidType;
  2090. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2091. END;
  2092. ELSE
  2093. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2094. END;
  2095. END;
  2096. result.SetType(type);
  2097. resolvedExpression := result
  2098. END VisitUnaryExpression;
  2099. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2100. VAR
  2101. result: SyntaxTree.Expression;
  2102. array: SyntaxTree.MathArrayExpression;
  2103. value: SyntaxTree.MathArrayValue;
  2104. isValue: BOOLEAN;
  2105. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2106. BEGIN
  2107. type := type.resolved;
  2108. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2109. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2110. END;
  2111. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2112. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2113. END;
  2114. RETURN type
  2115. END BaseType;
  2116. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2117. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2118. BEGIN
  2119. numberElements := x.elements.Length();
  2120. FOR i := 0 TO numberElements-1 DO
  2121. expression := x.elements.GetExpression(i);
  2122. IF expression IS SyntaxTree.MathArrayExpression THEN
  2123. array := SyntaxTree.NewMathArrayExpression(position);
  2124. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2125. expression := array;
  2126. ELSE
  2127. position := expression.position;
  2128. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2129. isValue := isValue & (expression.resolved # NIL);
  2130. END;
  2131. to.elements.AddExpression(expression);
  2132. END;
  2133. END RecursivelyConvert;
  2134. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2135. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2136. arrayType: SyntaxTree.MathArrayType;
  2137. BEGIN
  2138. numberElements := x.elements.Length();
  2139. baseType := NIL;
  2140. gsize := 0;
  2141. FOR i := 0 TO numberElements-1 DO
  2142. expression := x.elements.GetExpression(i);
  2143. IF expression IS SyntaxTree.MathArrayExpression THEN
  2144. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2145. IF i=0 THEN
  2146. gsize := size;
  2147. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2148. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2149. ELSE expression.SetType(baseType)
  2150. END;
  2151. ELSIF baseType = NIL THEN baseType := type;
  2152. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2153. END;
  2154. END;
  2155. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2156. arrayType.SetArrayBase(baseType);
  2157. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2158. RETURN ResolveType(arrayType);
  2159. END RecursivelySetType;
  2160. BEGIN
  2161. result := SyntaxTree.invalidExpression;
  2162. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2163. result := expression (* do not convert *)
  2164. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2165. isValue := TRUE;
  2166. type := BaseType(type);
  2167. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2168. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2169. value := SyntaxTree.NewMathArrayValue(array.position);
  2170. value.SetArray(array);
  2171. value.SetType(RecursivelySetType(array));
  2172. result := value;
  2173. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2174. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2175. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2176. IF result = NIL THEN
  2177. result := SyntaxTree.invalidExpression;
  2178. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2179. IF VerboseErrorMessage THEN
  2180. Printout.Info("expression",expression);
  2181. Printout.Info("type",type);
  2182. END;
  2183. END;
  2184. END;
  2185. RETURN result
  2186. END MathArrayConversion;
  2187. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2188. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2189. BEGIN
  2190. result := expression; type := type.resolved;
  2191. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2192. (* skip, no conversion *)
  2193. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2194. int := expression(SyntaxTree.IntegerValue).hvalue;
  2195. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2196. int := Global.ConvertSigned(int,system.SizeOf(type));
  2197. result := SyntaxTree.NewIntegerValue(position,int);
  2198. result.SetType(type);
  2199. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2200. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2201. result := SyntaxTree.NewIntegerValue(position,int);
  2202. result.SetType(type);
  2203. ELSIF (type IS SyntaxTree.FloatType) THEN
  2204. result := SyntaxTree.NewRealValue(expression.position,int);
  2205. result.SetType(type);
  2206. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2207. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2208. result.SetType(type);
  2209. ELSIF (type IS SyntaxTree.SetType) THEN
  2210. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2213. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2216. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2217. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2218. END;
  2219. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.PortType) THEN
  2222. result := ConvertValue(position, expression, system.integerType);
  2223. ELSE
  2224. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2225. result := SyntaxTree.invalidExpression;
  2226. IF VerboseErrorMessage THEN
  2227. Printout.Info("expression",expression);
  2228. Printout.Info("type",type);
  2229. END;
  2230. END;
  2231. ELSIF IsRealValue(expression,real) THEN
  2232. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2233. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2234. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2237. int := ENTIERH(real);
  2238. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2239. result.SetType(type);
  2240. ELSIF (type IS SyntaxTree.FloatType) THEN
  2241. result := SyntaxTree.NewRealValue(position,real);
  2242. result.SetType(type);
  2243. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2244. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2245. result.SetType(type);
  2246. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2247. ELSIF (type IS SyntaxTree.PortType) THEN
  2248. result := ConvertValue(position, expression, system.integerType);
  2249. ELSE
  2250. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2251. result := SyntaxTree.invalidExpression;
  2252. END
  2253. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2254. IF (type IS SyntaxTree.ComplexType) THEN
  2255. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2256. result.SetType(type);
  2257. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2258. ELSE
  2259. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2260. result := SyntaxTree.invalidExpression;
  2261. END
  2262. ELSIF IsSetValue(expression,set) THEN
  2263. IF (type IS SyntaxTree.IntegerType) THEN
  2264. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2265. result.SetType(type);
  2266. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2267. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2268. result.SetType(type);
  2269. ELSIF (type IS SyntaxTree.PortType) THEN
  2270. result := ConvertValue(position, expression, system.integerType);
  2271. ELSE
  2272. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2273. result := SyntaxTree.invalidExpression;
  2274. END;
  2275. ELSIF IsStringValue(expression,string) THEN
  2276. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2277. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2278. result.SetType(type);
  2279. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2280. ELSE
  2281. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. ELSIF IsCharacterValue(expression,char) THEN
  2285. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2286. string[0] := char; string[1] := 0X;
  2287. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2288. result := SyntaxTree.NewStringValue(expression.position,string);
  2289. result.SetType(type);
  2290. ELSIF (type IS SyntaxTree.ByteType) THEN
  2291. (* do not simply set the new type as this could invalidate types of constants *)
  2292. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2293. result.SetType(type)
  2294. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2295. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2296. result.SetType(type);
  2297. ELSIF (type IS SyntaxTree.SetType) THEN
  2298. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2299. result.SetType(type);
  2300. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2301. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2302. result.SetType(type);
  2303. ELSIF (type IS SyntaxTree.PortType) THEN
  2304. result := ConvertValue(position, expression, system.integerType);
  2305. ELSE
  2306. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2307. result := SyntaxTree.invalidExpression;
  2308. END;
  2309. ELSIF expression IS SyntaxTree.NilValue THEN
  2310. IF type IS SyntaxTree.AddressType THEN
  2311. result := SyntaxTree.NewIntegerValue(position,0);
  2312. result.SetType(type);
  2313. ELSE
  2314. result := expression;
  2315. END;
  2316. (* nothing to be done *)
  2317. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2318. result := MathArrayConversion(position, expression,type);
  2319. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2320. int := expression(SyntaxTree.EnumerationValue).value;
  2321. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2322. int := Global.ConvertSigned(int,system.SizeOf(type));
  2323. result := SyntaxTree.NewIntegerValue(position,int);
  2324. result.SetType(type);
  2325. ELSE
  2326. result := expression;
  2327. END;
  2328. (* nothing to be done *)
  2329. ELSE
  2330. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2331. IF VerboseErrorMessage THEN
  2332. Printout.Info("expression",expression);
  2333. Printout.Info("type",type);
  2334. END;
  2335. result := SyntaxTree.invalidExpression;
  2336. END;
  2337. RETURN result
  2338. END ConvertValue;
  2339. (**
  2340. return a conversion of an expression to a given type
  2341. - if expression is already of same type then return expression
  2342. - if incompatible conversion then report error and return invalidExpression
  2343. **)
  2344. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2345. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2346. BEGIN
  2347. type := type.resolved;
  2348. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2349. result := expression;
  2350. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2351. ELSIF expression = NIL THEN (* NIL expression *)
  2352. ELSIF expression.type = NIL THEN
  2353. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2354. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2355. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2356. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2357. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2358. ELSIF expression.resolved # NIL THEN (* value *)
  2359. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2360. IF value IS SyntaxTree.Value THEN
  2361. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2362. result.SetResolved(value(SyntaxTree.Value));
  2363. result.SetType(value.type);
  2364. ELSE
  2365. result := value
  2366. END;
  2367. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2368. expressionList := SyntaxTree.NewExpressionList();
  2369. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2370. typeDeclaration.SetDeclaredType(type);
  2371. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2372. typeSymbol.SetType(typeDeclaration.type);
  2373. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2374. expressionList.AddExpression(expression);
  2375. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2376. result.SetType(type);
  2377. ELSIF IsArrayStructuredObjectType(type) THEN
  2378. (* no type can be converted to an array-structured object type *)
  2379. HALT(100)
  2380. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2381. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2382. result := MathArrayConversion(position, expression,type);
  2383. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2384. expression := ConvertToMathArray(expression);
  2385. type := MathArrayStructureOfType(type);
  2386. result := MathArrayConversion(position, expression, type)
  2387. ELSE
  2388. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2389. END;
  2390. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2391. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2392. OR ~(type IS SyntaxTree.ArrayType) THEN
  2393. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2394. END;
  2395. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2396. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2397. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2398. (*skip, no conversion*)
  2399. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2400. (* skip, no conversion *)
  2401. ELSE
  2402. ASSERT(~(type IS SyntaxTree.RangeType));
  2403. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2404. ASSERT(type # NIL);
  2405. END;
  2406. RETURN result
  2407. END NewConversion;
  2408. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2409. BEGIN
  2410. IF CompatibleTo(system,expression.type, type) THEN
  2411. RETURN NewConversion(position, expression, type, NIL);
  2412. ELSE
  2413. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2414. RETURN SyntaxTree.invalidExpression
  2415. END;
  2416. END CompatibleConversion;
  2417. (**
  2418. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2419. **)
  2420. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2421. VAR leftType,rightType: SyntaxTree.Type;
  2422. BEGIN
  2423. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2424. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2425. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2426. (* error already handled *)
  2427. ELSE
  2428. leftType := left.type.resolved; rightType := right.type.resolved;
  2429. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2430. right := NewConversion(right.position, right, leftType, NIL);
  2431. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2432. left := NewConversion(left.position,left,rightType,NIL);
  2433. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2434. right := NewConversion(right.position, right, leftType, NIL);
  2435. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2436. left := NewConversion(left.position,left,rightType,NIL);
  2437. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2438. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2439. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2440. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2441. ELSIF
  2442. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2443. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2444. (* must be the case LONGREAL / COMPLEX ) *)
  2445. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2446. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2447. ELSE
  2448. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2449. END;
  2450. END;
  2451. END ConvertOperands;
  2452. (** find and return best operator matching to parameter list (nil, if none)
  2453. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2454. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2455. **)
  2456. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2457. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2458. identifier: SyntaxTree.Identifier;
  2459. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2460. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2461. BEGIN
  2462. operator := scope.firstOperator;
  2463. WHILE(operator # NIL) DO
  2464. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2465. procedureType := operator.type(SyntaxTree.ProcedureType);
  2466. distance := Distance(system, procedureType,actualParameters);
  2467. IF (distance < Infinity) THEN
  2468. IF returnType # NIL THEN
  2469. IF procedureType.returnType = NIL THEN
  2470. distance := Infinity
  2471. ELSE
  2472. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2473. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2474. END;
  2475. END;
  2476. END;
  2477. (*
  2478. IF distance < Infinity THEN
  2479. TRACE(distance, operator);
  2480. Printout.Info("potential operator",operator);
  2481. ELSE
  2482. Printout.Info("operator not possible",operator);
  2483. END;
  2484. *)
  2485. IF distance < bestDistance THEN
  2486. bestDistance := distance;
  2487. bestOperator := operator;
  2488. END;
  2489. END;
  2490. operator := operator.nextOperator;
  2491. END;
  2492. (*
  2493. Printout.Info("taken operator",bestOperator);
  2494. *)
  2495. END FindInScope;
  2496. BEGIN
  2497. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2498. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2499. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2500. import := currentScope.ownerModule.moduleScope.firstImport;
  2501. WHILE (bestDistance > 0) & (import # NIL) DO
  2502. IF import.module # NIL THEN
  2503. identifier := Global.GetIdentifier(operator,import.module.case);
  2504. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2505. END;
  2506. import := import.nextImport;
  2507. END;
  2508. RETURN bestOperator
  2509. END FindOperator;
  2510. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2511. BEGIN
  2512. currentScope := scope;
  2513. END SetCurrentScope;
  2514. (**
  2515. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2516. - handle LEN and DIM operator for array-structured object types
  2517. - find operator, if found then
  2518. - if in other module then add import designator
  2519. - create symbol designator for operator
  2520. - if error then return invalidExpression, if no operator then return NIL
  2521. **)
  2522. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2523. VAR
  2524. operator: SyntaxTree.Operator;
  2525. import: SyntaxTree.Import;
  2526. expression, result: SyntaxTree.Expression;
  2527. designator: SyntaxTree.Designator;
  2528. actualParameters, tempList: SyntaxTree.ExpressionList;
  2529. recordType: SyntaxTree.RecordType;
  2530. castReturnType : SyntaxTree.MathArrayType;
  2531. BEGIN
  2532. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2533. result := SyntaxTree.invalidExpression
  2534. ELSIF leftExpression = NIL THEN
  2535. result := NIL
  2536. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2537. (* LEN or DIM operator on array-structured object type *)
  2538. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2539. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2540. IF recordType.arrayAccessOperators.len = NIL THEN
  2541. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2542. result := SyntaxTree.invalidExpression
  2543. ELSE
  2544. ASSERT(leftExpression IS SyntaxTree.Designator);
  2545. designator := leftExpression(SyntaxTree.Designator);
  2546. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2547. ASSERT(expression IS SyntaxTree.Designator);
  2548. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2549. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2550. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2551. result := designator
  2552. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2553. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2554. tempList := SyntaxTree.NewExpressionList();
  2555. tempList.AddExpression(rightExpression);
  2556. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2557. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2558. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2559. tempList := SyntaxTree.NewExpressionList();
  2560. tempList.AddExpression(designator);
  2561. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2562. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2563. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2564. END
  2565. END;
  2566. ELSE
  2567. (* import OCArrayBase if needed *)
  2568. IF ~arrayBaseImported THEN
  2569. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2570. (* operators on complex numbers *)
  2571. ImportModule(Global.ArrayBaseName,position);
  2572. arrayBaseImported := TRUE;
  2573. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2574. IF op = Global.Dim THEN
  2575. (* not existing in OCArrayBase *)
  2576. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2577. (* not existing in OCArrayBase *)
  2578. ELSE
  2579. ImportModule(Global.ArrayBaseName,position);
  2580. arrayBaseImported := TRUE;
  2581. END
  2582. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2583. ImportModule(Global.ArrayBaseName,position);
  2584. arrayBaseImported := TRUE
  2585. END;
  2586. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2587. (* LEN(RANGE) *)
  2588. ImportModule(Global.ArrayBaseName,position);
  2589. arrayBaseImported := TRUE;
  2590. END;
  2591. END;
  2592. actualParameters := SyntaxTree.NewExpressionList();
  2593. actualParameters.AddExpression(leftExpression);
  2594. IF rightExpression # NIL THEN
  2595. actualParameters.AddExpression(rightExpression)
  2596. END;
  2597. operator := FindOperator(system,op,actualParameters,resultType);
  2598. IF operator # NIL THEN
  2599. designator := NIL;
  2600. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2601. import := currentScope.ownerModule.moduleScope.firstImport;
  2602. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2603. import := import.nextImport;
  2604. END;
  2605. expression := NewSymbolDesignator(position,NIL,import);
  2606. designator := expression(SyntaxTree.Designator);
  2607. END;
  2608. expression := NewSymbolDesignator(position,designator,operator);
  2609. designator := expression(SyntaxTree.Designator);
  2610. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2611. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2612. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2613. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2614. result.SetType(castReturnType);
  2615. END;
  2616. ELSE
  2617. result := NIL;
  2618. END;
  2619. END;
  2620. RETURN result
  2621. END NewOperatorCall;
  2622. (** check and resolve binary expression **)
  2623. (*! clean up *)
  2624. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2625. VAR left,right,result: SyntaxTree.Expression;
  2626. leftType, rightType: SyntaxTree.Type;
  2627. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2628. cl,cr: CHAR;
  2629. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2630. type: SyntaxTree.Type;
  2631. value: SyntaxTree.Value;
  2632. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2633. integerConstantFolding: BOOLEAN;
  2634. list: SyntaxTree.ExpressionList;
  2635. PROCEDURE NewBool(v: BOOLEAN);
  2636. BEGIN
  2637. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2638. value.SetType(system.booleanType);
  2639. result.SetResolved(value);
  2640. type := system.booleanType
  2641. END NewBool;
  2642. PROCEDURE NewSet(v: SET);
  2643. BEGIN
  2644. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2645. value.SetType(system.setType);
  2646. result.SetResolved(value);
  2647. type := system.setType;
  2648. END NewSet;
  2649. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2650. BEGIN
  2651. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2652. (* type cast to "larger" type only if the value is still in the range *)
  2653. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2654. value.SetType(t);
  2655. END;
  2656. result.SetResolved(value);
  2657. type := value.type;
  2658. END NewInteger;
  2659. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2660. BEGIN
  2661. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2662. value.SetType(t);
  2663. result.SetResolved(value);
  2664. type := t;
  2665. END NewReal;
  2666. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2667. BEGIN
  2668. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2669. value.SetType(t);
  2670. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2671. result.SetResolved(value);
  2672. type := t;
  2673. END NewComplex;
  2674. BEGIN
  2675. type := SyntaxTree.invalidType;
  2676. left := ResolveExpression(binaryExpression.left);
  2677. right := ResolveExpression(binaryExpression.right);
  2678. binaryExpression.SetLeft(left);
  2679. binaryExpression.SetRight(right);
  2680. result := binaryExpression;
  2681. operator := binaryExpression.operator;
  2682. IF ~system.operatorDefined[operator] THEN
  2683. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2684. result := SyntaxTree.invalidExpression;
  2685. RETURN
  2686. END;
  2687. IF left.type = NIL THEN
  2688. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2689. result := SyntaxTree.invalidExpression;
  2690. RETURN;
  2691. END;
  2692. IF right.type = NIL THEN
  2693. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2694. result := SyntaxTree.invalidExpression;
  2695. RETURN;
  2696. END;
  2697. leftType := left.type.resolved; rightType := right.type.resolved;
  2698. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2699. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2700. END;
  2701. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2702. list := SyntaxTree.NewExpressionList();
  2703. list.AddExpression(right);
  2704. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2705. END;
  2706. IF operatorCall # NIL THEN
  2707. result := operatorCall;
  2708. type := operatorCall.type;
  2709. (* admissible operators:
  2710. Times, Plus, Minus numeric numeric numeric
  2711. set set set
  2712. Slash numeric numeric real /complex
  2713. set set set
  2714. Div , Mod integer integer integer
  2715. And, Or bool bool bool
  2716. Equal, Unequal basic basic bool
  2717. pointer pointer bool
  2718. object object bool
  2719. record record bool
  2720. string string bool
  2721. enumerator enumerator bool
  2722. Less, LessEqual,
  2723. Greater, GreaterEqual integer/real integer/real bool
  2724. enumerator enumerator bool
  2725. In integer set bool
  2726. Is pointer type bool
  2727. object type bool
  2728. record type bool
  2729. Upto: special abbreviation for a..b
  2730. *)
  2731. ELSIF (left.type = NIL) THEN
  2732. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2733. D.Str("nil type in "); D.Type(left); D.Ln;
  2734. result := SyntaxTree.invalidExpression;
  2735. ELSIF (right.type = NIL) THEN
  2736. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2737. result := SyntaxTree.invalidExpression;
  2738. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2739. result := SyntaxTree.invalidExpression;
  2740. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2741. HALT(100);
  2742. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2743. type := system.booleanType;
  2744. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2745. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2746. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2747. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2748. IF VerboseErrorMessage THEN
  2749. Printout.Info("left",left);
  2750. Printout.Info("right",right);
  2751. END;
  2752. ELSIF IsUnsafePointer(left.type) THEN
  2753. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2754. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2755. NewBool(TRUE)
  2756. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2757. NewBool(TRUE);
  2758. ELSIF IsUnextensibleRecord(left) THEN
  2759. NewBool(FALSE)
  2760. END
  2761. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2762. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2763. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2764. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2765. ELSIF operator = Scanner.In THEN (* left IN right *)
  2766. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2767. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2768. NewBool(il IN sr);
  2769. ELSE
  2770. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2771. left := NewConversion(left.position, left, system.longintType,NIL);
  2772. binaryExpression.SetLeft(left)
  2773. END;
  2774. type := system.booleanType;
  2775. END
  2776. ELSE
  2777. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2778. END
  2779. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2780. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2781. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2782. END;
  2783. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2784. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2785. END
  2786. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2787. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2788. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2789. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2790. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2791. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2792. ConvertOperands(left, right);
  2793. binaryExpression.SetLeft(left);
  2794. binaryExpression.SetRight(right);
  2795. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2796. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2797. END;
  2798. type := system.booleanType;
  2799. ELSE
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2801. END
  2802. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2803. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2804. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2805. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2806. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2807. CASE operator OF
  2808. |Scanner.Equal: NewBool(strl^=strr^);
  2809. |Scanner.Unequal:NewBool(strl^#strr^);
  2810. |Scanner.Less: NewBool(strl^<strr^);
  2811. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2812. |Scanner.Greater: NewBool(strl^>strr^);
  2813. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2814. ELSE
  2815. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2816. END;
  2817. END;
  2818. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2819. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2820. type := system.booleanType
  2821. ELSE
  2822. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2823. END;
  2824. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2825. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2826. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2827. type := system.booleanType;
  2828. ELSE
  2829. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2830. END
  2831. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2832. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2833. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2834. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2835. type := system.booleanType
  2836. ELSE
  2837. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2838. END;
  2839. ELSE
  2840. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2841. END;
  2842. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2843. type := system.booleanType;
  2844. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2845. type := system.booleanType;
  2846. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2847. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2848. THEN
  2849. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2850. IF (leftType # rightType) THEN
  2851. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2852. ConvertOperands(left,right); (* operands must be of the same type here *)
  2853. END;
  2854. binaryExpression.SetLeft(left);
  2855. binaryExpression.SetRight(right);
  2856. leftType := left.type.resolved;
  2857. rightType := right.type.resolved;
  2858. END;
  2859. type := leftType;
  2860. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2861. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2862. IF VerboseErrorMessage THEN
  2863. Printout.Info("left",left);
  2864. Printout.Info("right",right);
  2865. END;
  2866. ELSIF IsIntegerType(leftType) THEN
  2867. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2868. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2869. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2870. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2871. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2872. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2873. END;
  2874. END;
  2875. (* constant folding *)
  2876. (* bootstrap64
  2877. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2878. CASE operator OF
  2879. |Scanner.Plus: NewInteger(il+ir,left.type);
  2880. |Scanner.Minus: NewInteger(il-ir,left.type);
  2881. |Scanner.Times: NewInteger(il*ir,left.type);
  2882. |Scanner.Slash:
  2883. IF ir # 0 THEN
  2884. NewReal(il/ir, system.realType);
  2885. END;
  2886. |Scanner.Mod:
  2887. IF ir > 0 THEN
  2888. NewInteger(il MOD ir,left.type);
  2889. END;
  2890. |Scanner.Div:
  2891. IF ir > 0 THEN
  2892. NewInteger(il DIV ir,left.type);
  2893. END;
  2894. |Scanner.Equal: NewBool(il=ir);
  2895. |Scanner.Unequal:NewBool(il#ir);
  2896. |Scanner.Less: NewBool(il<ir);
  2897. |Scanner.LessEqual: NewBool(il<=ir);
  2898. |Scanner.Greater: NewBool(il>ir);
  2899. |Scanner.GreaterEqual: NewBool(il>=ir);
  2900. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2901. END;
  2902. ELS*)
  2903. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2904. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2905. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2906. CASE operator OF
  2907. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2908. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2909. |Scanner.Times: NewInteger(hl*hr,left.type);
  2910. |Scanner.Slash:
  2911. IF hr = 0 THEN
  2912. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2913. ELSE
  2914. IF type.sizeInBits = 64 THEN
  2915. NewReal(hl/hr,system.longrealType);
  2916. ELSE
  2917. NewReal(hl/hr,system.realType)
  2918. END
  2919. END;
  2920. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2921. |Scanner.Mod:
  2922. IF hr = 0 THEN
  2923. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2924. ELSE
  2925. NewInteger(hl MOD hr, left.type);
  2926. (* bootstrap64
  2927. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2928. *)
  2929. END;
  2930. |Scanner.Div:
  2931. IF hr = 0 THEN
  2932. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2933. ELSE
  2934. NewInteger(hl DIV hr, left.type);
  2935. (* bootstrap64
  2936. NewInteger(Machine.DivH(hl,hr),left.type);
  2937. *)
  2938. END;
  2939. (* *)
  2940. |Scanner.Equal: NewBool(hl=hr);
  2941. |Scanner.Unequal: NewBool(hl#hr);
  2942. |Scanner.Less: NewBool(hl<hr);
  2943. |Scanner.LessEqual: NewBool(hl<=hr);
  2944. |Scanner.Greater: NewBool(hl>hr);
  2945. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2946. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2947. END;
  2948. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2949. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2950. type := left.type
  2951. ELSIF (operator = Scanner.Slash) THEN
  2952. left := NewConversion(left.position,left,system.realType,NIL);
  2953. right := NewConversion(right.position,right,system.realType,NIL);
  2954. binaryExpression.SetLeft(left);
  2955. binaryExpression.SetRight(right);
  2956. type := system.realType
  2957. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2958. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2959. type := system.booleanType
  2960. ELSE
  2961. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2962. END;
  2963. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2964. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2965. CASE operator OF
  2966. |Scanner.Plus: NewReal(rl+rr,leftType);
  2967. |Scanner.Minus: NewReal(rl-rr,leftType);
  2968. |Scanner.Times:NewReal(rl*rr,leftType);
  2969. |Scanner.Slash:
  2970. IF rr = 0 THEN
  2971. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2972. ELSE
  2973. NewReal(rl/rr,leftType);
  2974. END
  2975. |Scanner.Equal: NewBool(rl=rr);
  2976. |Scanner.Unequal: NewBool(rl#rr);
  2977. |Scanner.Less: NewBool(rl<rr);
  2978. |Scanner.LessEqual: NewBool(rl<=rr);
  2979. |Scanner.Greater: NewBool(rl>rr);
  2980. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2981. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2982. END;
  2983. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2984. type := left.type
  2985. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2986. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2987. type := system.booleanType
  2988. ELSE
  2989. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2990. IF VerboseErrorMessage THEN
  2991. Printout.Info("left",left);
  2992. Printout.Info("right",right);
  2993. END;
  2994. END;
  2995. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2996. CASE operator OF
  2997. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2998. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2999. ELSE
  3000. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3001. IF VerboseErrorMessage THEN
  3002. Printout.Info("left", left);
  3003. Printout.Info("right", right)
  3004. END;
  3005. END;
  3006. IF ~error THEN
  3007. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3008. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3009. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3010. (* do constant folding *)
  3011. CASE operator OF
  3012. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3013. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3014. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3015. |Scanner.Slash:
  3016. divisor := c * c + d * d;
  3017. ASSERT(divisor # 0);
  3018. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3019. |Scanner.Equal: NewBool((a = c) & (b = d))
  3020. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3021. END
  3022. END
  3023. END
  3024. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3025. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3026. CASE operator OF
  3027. |Scanner.And: NewBool(bl & br);
  3028. |Scanner.Or: NewBool(bl OR br);
  3029. |Scanner.Equal: NewBool(bl = br);
  3030. |Scanner.Unequal: NewBool(bl # br);
  3031. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3032. END;
  3033. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3034. type := system.booleanType
  3035. ELSE
  3036. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3037. END;
  3038. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3039. (* constant folding *)
  3040. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3041. IF operator = Scanner.Equal THEN
  3042. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3043. ELSIF operator = Scanner.Unequal THEN
  3044. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3045. END;
  3046. END;
  3047. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3048. type := system.booleanType;
  3049. ELSE
  3050. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3051. END;
  3052. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3053. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3054. CASE operator OF
  3055. |Scanner.Plus: NewSet(sl + sr);
  3056. |Scanner.Minus: NewSet(sl - sr);
  3057. |Scanner.Times: NewSet(sl * sr);
  3058. |Scanner.Slash: NewSet(sl / sr);
  3059. |Scanner.Equal: NewBool(sl=sr);
  3060. |Scanner.Unequal: NewBool(sl#sr);
  3061. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3062. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3063. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3064. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3065. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3066. END;
  3067. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3068. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3069. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3070. THEN
  3071. type := system.booleanType
  3072. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3073. type := left.type
  3074. ELSE
  3075. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3076. END;
  3077. ELSIF IsCharacterType(left.type) THEN
  3078. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3079. CASE operator OF
  3080. |Scanner.Equal: NewBool(cl=cr);
  3081. |Scanner.Unequal: NewBool(cl#cr);
  3082. |Scanner.Less: NewBool(cl<cr);
  3083. |Scanner.LessEqual: NewBool(cl<=cr);
  3084. |Scanner.Greater: NewBool(cl>cr);
  3085. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3086. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3087. END;
  3088. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3089. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3090. type := system.booleanType
  3091. ELSE
  3092. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3093. END;
  3094. ELSE
  3095. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3096. END;
  3097. ELSE
  3098. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3099. END;
  3100. IF type = SyntaxTree.invalidType THEN
  3101. result := SyntaxTree.invalidExpression
  3102. ELSE
  3103. result.SetType(type)
  3104. END;
  3105. resolvedExpression := result
  3106. END VisitBinaryExpression;
  3107. (** resolve a range expression of the from <<first .. last BY step>>
  3108. - depending on the context different things are checked:
  3109. ArrayIndex:
  3110. - components must be integers
  3111. - replace missing lower bound with 0
  3112. - replace missing upper bound with MAX(LONGINT)
  3113. - replace missing step size with 1
  3114. SetElement:
  3115. - components must be integers
  3116. - replace missing lower bound with 0
  3117. - replace missing upper bound with MAX(SET)
  3118. - must not have step size
  3119. CaseGuard:
  3120. - components must be constant
  3121. - components must be integers or characters
  3122. - must have lower and upper bound present
  3123. - components are made compatible
  3124. - must not have step size
  3125. - if error: return invalidExpression
  3126. **)
  3127. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3128. VAR
  3129. hasError: BOOLEAN;
  3130. first, last, step: SyntaxTree.Expression;
  3131. BEGIN
  3132. hasError := FALSE;
  3133. first := x.first;
  3134. last := x.last;
  3135. step := x.step;
  3136. (* check lower bound *)
  3137. IF x.context = SyntaxTree.CaseGuard THEN
  3138. IF first = NIL THEN
  3139. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3140. hasError := TRUE
  3141. ELSE
  3142. first := ResolveExpression(first);
  3143. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3144. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3145. hasError := TRUE
  3146. ELSE
  3147. IF first IS SyntaxTree.StringValue THEN
  3148. (* add conversion from string to character *)
  3149. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3150. END
  3151. END;
  3152. (* check if expression is constant *)
  3153. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3154. (* error already reported *)
  3155. hasError := TRUE
  3156. END
  3157. END
  3158. ELSE (* ArrayIndex, SetElement *)
  3159. IF first = NIL THEN
  3160. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3161. END;
  3162. first := ResolveExpression(first);
  3163. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3164. first := NewConversion(first.position, first, system.longintType, NIL)
  3165. ELSE
  3166. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3167. hasError := TRUE
  3168. END
  3169. END;
  3170. (* check upper bound *)
  3171. IF x.context = SyntaxTree.CaseGuard THEN
  3172. IF last = NIL THEN
  3173. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3174. hasError := TRUE
  3175. ELSE
  3176. last := ResolveExpression(last);
  3177. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3178. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3179. hasError := TRUE
  3180. ELSE
  3181. IF last IS SyntaxTree.StringValue THEN
  3182. (* add conversion from string to character *)
  3183. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3184. END
  3185. END;
  3186. (* check if expression is constant *)
  3187. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3188. (* error already reported *)
  3189. hasError := TRUE
  3190. ELSE
  3191. (* try to make lower and upper bound compatible *)
  3192. ConvertOperands(first, last);
  3193. IF first.type.resolved # last.type.resolved THEN
  3194. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3195. hasError := TRUE
  3196. END
  3197. END
  3198. END
  3199. ELSE (* ArrayIndex, SetElement *)
  3200. IF last = NIL THEN
  3201. IF x.context = SyntaxTree.ArrayIndex THEN
  3202. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3203. ELSE
  3204. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3205. END
  3206. END;
  3207. last := ResolveExpression(last);
  3208. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3209. last := NewConversion(last.position, last, system.longintType, NIL)
  3210. ELSE
  3211. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3212. hasError := TRUE
  3213. END
  3214. END;
  3215. (* check step size *)
  3216. IF x.context = SyntaxTree.ArrayIndex THEN
  3217. IF step = NIL THEN
  3218. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3219. END;
  3220. step := ResolveExpression(step);
  3221. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3222. step := NewConversion(step.position, step, system.longintType, NIL)
  3223. ELSE
  3224. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3225. hasError := TRUE
  3226. END
  3227. ELSE (* SetElement, CaseGuard *)
  3228. IF step # NIL THEN
  3229. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3230. hasError := TRUE
  3231. END
  3232. END;
  3233. IF hasError THEN
  3234. resolvedExpression := SyntaxTree.invalidExpression
  3235. ELSE
  3236. x.SetFirst(first);
  3237. x.SetLast(last);
  3238. x.SetStep(step);
  3239. x.SetType(system.rangeType);
  3240. resolvedExpression := x;
  3241. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3242. END
  3243. END VisitRangeExpression;
  3244. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3245. BEGIN
  3246. x.SetType(NIL);
  3247. resolvedExpression := x;
  3248. END VisitTensorRangeExpression;
  3249. (** resolve the expression d and return result as designator
  3250. - resolve expression
  3251. - if expression is a designator then return designator else error message and return invalidDesignator
  3252. **)
  3253. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3254. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3255. BEGIN
  3256. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3257. resolved := ResolveExpression(d);
  3258. IF resolved = SyntaxTree.invalidExpression THEN
  3259. (* error should already have been reported *)
  3260. result := SyntaxTree.invalidDesignator;
  3261. ELSIF resolved IS SyntaxTree.Designator THEN
  3262. result := resolved(SyntaxTree.Designator);
  3263. ELSE
  3264. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3265. result := SyntaxTree.invalidDesignator;
  3266. END;
  3267. (* result.type might be nil. *)
  3268. RETURN result
  3269. END ResolveDesignator;
  3270. (**
  3271. symbol designator generated in this module
  3272. nothing to be resolved
  3273. **)
  3274. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3275. BEGIN
  3276. resolvedExpression := x;
  3277. END VisitSymbolDesignator;
  3278. (**
  3279. self designator generated in this module
  3280. nothing to be resolved
  3281. **)
  3282. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3283. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3284. BEGIN
  3285. (* check if in record scope *)
  3286. scope := currentScope;
  3287. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3288. scope := scope.outerScope;
  3289. END;
  3290. IF scope = NIL THEN (* in module scope *)
  3291. x.SetType(system.anyType);
  3292. ELSIF scope IS SyntaxTree.CellScope THEN
  3293. cell := scope(SyntaxTree.CellScope).ownerCell;
  3294. x.SetType(cell);
  3295. ELSE (* in record scope *)
  3296. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3297. IF (record # NIL) & (record.pointerType # NIL) THEN
  3298. type := ResolveType(record.pointerType);
  3299. x.SetType(type);
  3300. ELSE
  3301. x.SetType(record);
  3302. END;
  3303. END;
  3304. resolvedExpression := x;
  3305. END VisitSelfDesignator;
  3306. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3307. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3308. BEGIN
  3309. scope := currentScope;
  3310. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3311. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3312. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3313. returnType := procedureType.returnType;
  3314. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3315. THEN
  3316. x.SetType(returnType);
  3317. ELSE
  3318. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3319. x.SetType(SyntaxTree.invalidType);
  3320. END;
  3321. ELSE
  3322. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3323. x.SetType(SyntaxTree.invalidType);
  3324. END;
  3325. x.SetAssignable(TRUE);
  3326. resolvedExpression := x;
  3327. END VisitResultDesignator;
  3328. (**
  3329. return symbol designator as an expression
  3330. - if symbol is a constant then return the constant value expression
  3331. - else
  3332. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3333. - if symbol is a guarded variable then return a TypeGuardDesignator
  3334. - else return a symbol designator
  3335. **)
  3336. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3337. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3338. guardType: SyntaxTree.Type;
  3339. BEGIN
  3340. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3341. result := SyntaxTree.invalidExpression;
  3342. ASSERT(symbol # NIL);
  3343. (*
  3344. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3345. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3346. Error(position,Diagnostics.Invalid,"type not allowed here");
  3347. ELS *)
  3348. (* not needed any more as values are stored in the expression
  3349. IF symbol IS SyntaxTree.Constant THEN
  3350. result := symbol(SyntaxTree.Constant).value
  3351. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3352. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3353. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3354. ELSE
  3355. result := symbol(SyntaxTree.Constant).value
  3356. END;
  3357. ELSE
  3358. *)
  3359. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3360. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3361. THEN
  3362. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3363. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3364. left := NewDereferenceDesignator(position,left);
  3365. left.SetHidden(TRUE);
  3366. END;
  3367. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3368. scope := currentScope;
  3369. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3370. scope := scope.outerScope;
  3371. END;
  3372. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3373. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3374. END;
  3375. END;
  3376. 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);
  3377. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3378. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3379. ELSE
  3380. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3381. END;
  3382. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3383. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3384. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3385. result.SetType(symbol.type);
  3386. result.SetAssignable(assignable);
  3387. symbol.MarkUsed;
  3388. IF symbol IS SyntaxTree.Constant THEN
  3389. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3390. END;
  3391. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3392. variableAccessed := TRUE
  3393. END;
  3394. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3395. IF GetGuard(symbol,guardType) THEN
  3396. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3397. END;
  3398. END;
  3399. ASSERT(result.type # NIL);
  3400. RETURN result
  3401. END NewSymbolDesignator;
  3402. (** check and resolve an identifier designator "identifier"
  3403. - if identifier = self then return SelfDesignator
  3404. - else find symbol in current scope
  3405. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3406. **)
  3407. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3408. VAR symbol: SyntaxTree.Symbol;
  3409. BEGIN
  3410. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3411. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3412. IF symbol # NIL THEN
  3413. ResolveSymbol(symbol);
  3414. ASSERT(symbol.type # NIL);
  3415. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3416. ELSE
  3417. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3418. IF VerboseErrorMessage THEN
  3419. Printout.Info("undeclared identifier designator",identifierDesignator);
  3420. END;
  3421. resolvedExpression := SyntaxTree.invalidDesignator;
  3422. END;
  3423. END VisitIdentifierDesignator;
  3424. (** check and resolve a selector designator of the form left.designator
  3425. - if left is a pointer type then do auto dereferenciation
  3426. - left denotes a search scope:
  3427. - if left type is import type then set search scope to respective module
  3428. - if left type is enumeration type then set search scope to respective enumeration scope
  3429. - elsif left type is record type then set search scope to record scope
  3430. - search symbol in computed scope
  3431. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3432. **)
  3433. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3434. VAR
  3435. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3436. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3437. BEGIN
  3438. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3439. left := ResolveDesignator(selectorDesignator.left);
  3440. result := SyntaxTree.invalidDesignator;
  3441. IF left # NIL THEN
  3442. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3443. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3444. END;
  3445. scope := NIL;
  3446. IF left.type = NIL THEN
  3447. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3448. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3449. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3450. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3451. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3452. module := symbol(SyntaxTree.Import).module;
  3453. IF module # NIL THEN
  3454. scope := module.moduleScope
  3455. ELSE
  3456. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3457. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3458. END;
  3459. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3460. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3461. ASSERT(scope # NIL)
  3462. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3463. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3464. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3465. IF type IS SyntaxTree.EnumerationType THEN
  3466. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3467. ELSE
  3468. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3469. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3470. END;
  3471. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3472. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3473. ELSE
  3474. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3475. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3476. END;
  3477. symbol := NIL;
  3478. IF scope # NIL THEN
  3479. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3480. IF symbol # NIL THEN
  3481. ResolveSymbol(symbol);
  3482. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3483. symbol.MarkUsed
  3484. ELSE
  3485. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3486. IF VerboseErrorMessage THEN
  3487. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3488. Printout.Info("scope", scope);
  3489. Printout.Info("left", left);
  3490. Printout.Info("undeclared identifier",selectorDesignator);
  3491. Printout.Info("left resolved designator",left);
  3492. END
  3493. END;
  3494. END;
  3495. END;
  3496. resolvedExpression := result;
  3497. END VisitSelectorDesignator;
  3498. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3499. VAR len,idx: LONGINT;
  3500. BEGIN
  3501. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3502. IF idx < 0 THEN
  3503. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3504. ELSE
  3505. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3506. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3507. END;
  3508. END;
  3509. END;
  3510. END IndexCheck;
  3511. (*
  3512. - if index designator has not type, use newBaseType as its type
  3513. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3514. - special rule: if static array of dynamic array occurs, make it all dynamic
  3515. index designator type: new base type: new index designator type:
  3516. NIL z z
  3517. ARRAY [x, y] z ARRAY [x, y] OF z
  3518. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3519. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3520. *)
  3521. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3522. VAR
  3523. mathArrayType: SyntaxTree.MathArrayType;
  3524. makeDynamic: BOOLEAN;
  3525. BEGIN
  3526. IF indexDesignator.type = NIL THEN
  3527. indexDesignator.SetType(newBaseType)
  3528. ELSE
  3529. (* index designator must be a of math array type *)
  3530. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3531. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3532. (* determine if all arrays have to be made dynamic *)
  3533. makeDynamic :=
  3534. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3535. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3536. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3537. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3538. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3539. END;
  3540. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3541. mathArrayType.SetArrayBase(newBaseType)
  3542. END
  3543. END SetIndexBaseType;
  3544. (** check and append index list element to index designator of math array
  3545. - check validity of single index or array range
  3546. - compute new type
  3547. - if range then create new array type (calculate length of resulting array)
  3548. - otherwise take sourceArray.arrayBase as new type
  3549. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3550. **)
  3551. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3552. VAR
  3553. targetArray: SyntaxTree.MathArrayType;
  3554. first, last, step: SyntaxTree.Expression;
  3555. firstValue, lastValue, stepValue, length: LONGINT;
  3556. rangeExpression: SyntaxTree.RangeExpression;
  3557. BEGIN
  3558. IF indexListItem.type = SyntaxTree.invalidType THEN
  3559. (* error already handled *)
  3560. indexDesignator.parameters.AddExpression(indexListItem)
  3561. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3562. indexDesignator.HasRange;
  3563. indexDesignator.HasTensorRange;
  3564. indexDesignator.parameters.AddExpression(indexListItem);
  3565. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3566. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3567. IndexCheck(indexListItem, sourceArray.length);
  3568. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3569. indexDesignator.parameters.AddExpression(indexListItem)
  3570. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3571. indexDesignator.HasRange;
  3572. (* if the range is given as an array range expression, check the validity of its components *)
  3573. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3574. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3575. first := rangeExpression.first;
  3576. last := rangeExpression.last;
  3577. step := rangeExpression.step;
  3578. (* perform static checks on range components *)
  3579. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3580. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3581. END;
  3582. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3583. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3584. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3585. END
  3586. END;
  3587. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3588. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3589. END;
  3590. (* add conversions to size type *)
  3591. (* TODO: needed? *)
  3592. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3593. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3594. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3595. END;
  3596. IF indexDesignator.hasTensorRange THEN
  3597. (* the index designator's base type is a tensor: leave it as is *)
  3598. ELSE
  3599. (* append a new math array to the index designator's base type *)
  3600. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3601. IF ~error THEN
  3602. (*
  3603. (* optimization: calculate length of target array for static ranges *)
  3604. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3605. IF IsStaticallyOpenRange(rangeExpression) THEN
  3606. (* range is open ('*'): reuse source array length as target array length *)
  3607. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3608. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3609. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3610. IF lastValue = MAX(LONGINT) THEN
  3611. IF IsIntegerValue(sourceArray.length, length) THEN
  3612. lastValue := length - 1;
  3613. isStaticTargetArrayLength := TRUE
  3614. ELSE
  3615. isStaticTargetArrayLength := FALSE
  3616. END
  3617. ELSE
  3618. isStaticTargetArrayLength := TRUE
  3619. END;
  3620. IF isStaticTargetArrayLength THEN
  3621. (* calculate static target array length *)
  3622. IF firstValue > lastValue THEN
  3623. length := 0
  3624. ELSE
  3625. length := 1 + lastValue - firstValue;
  3626. IF length MOD stepValue = 0 THEN
  3627. length := length DIV stepValue
  3628. ELSE
  3629. length := length DIV stepValue + 1
  3630. END
  3631. END;
  3632. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3633. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3634. ASSERT(targetArray.form = SyntaxTree.Static)
  3635. END
  3636. END
  3637. END
  3638. *)
  3639. END;
  3640. SetIndexBaseType(indexDesignator, targetArray)
  3641. END;
  3642. indexDesignator.parameters.AddExpression(indexListItem)
  3643. ELSE
  3644. Error(position, Diagnostics.Invalid,"invalid index list item");
  3645. END;
  3646. END AppendMathIndex;
  3647. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3648. VAR parameters: SyntaxTree.ExpressionList;
  3649. BEGIN
  3650. parameters := index.parameters;
  3651. IF (expression.type = NIL) THEN
  3652. Error(position,Diagnostics.Invalid,"invalid index");
  3653. ELSIF IsIntegerType(expression.type.resolved) THEN
  3654. IF over IS SyntaxTree.ArrayType THEN
  3655. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3656. ELSIF over IS SyntaxTree.StringType THEN
  3657. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3658. END;
  3659. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3660. parameters.AddExpression(expression);
  3661. ELSE
  3662. Error(position,Diagnostics.Invalid,"invalid index");
  3663. END;
  3664. END AppendIndex;
  3665. (** convert an expression to math array type
  3666. if expression is of math array type: return expression itself
  3667. if expression is of array-structured object type: return an index operator call on it
  3668. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3669. otherwise: return invalid expression
  3670. **)
  3671. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3672. VAR
  3673. result: SyntaxTree.Expression;
  3674. mathArrayType: SyntaxTree.MathArrayType;
  3675. BEGIN
  3676. IF expression.type = NIL THEN
  3677. result := SyntaxTree.invalidExpression
  3678. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3679. (* expression of math array type *)
  3680. result := expression
  3681. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3682. (* expression of array-structured object type *)
  3683. mathArrayType := MathArrayStructureOfType(expression.type);
  3684. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3685. ELSE
  3686. result := SyntaxTree.invalidExpression
  3687. END;
  3688. RETURN result
  3689. END ConvertToMathArray;
  3690. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3691. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3692. VAR
  3693. result: SyntaxTree.ExpressionList;
  3694. i: LONGINT;
  3695. BEGIN
  3696. result := SyntaxTree.NewExpressionList();
  3697. FOR i := 1 TO itemCount DO
  3698. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3699. END;
  3700. RETURN result
  3701. END ListOfOpenRanges;
  3702. (** create a procedure call designator for an index operator call on an array-structured object type
  3703. - use given index list as actual parameters
  3704. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3705. **)
  3706. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3707. VAR
  3708. operator: SyntaxTree.Operator;
  3709. expression: SyntaxTree.Expression;
  3710. actualParameters, tempList: SyntaxTree.ExpressionList;
  3711. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3712. result, tempDesignator: SyntaxTree.Designator;
  3713. recordType: SyntaxTree.RecordType;
  3714. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3715. i, hashValue, indexListSize, indexListKind: LONGINT;
  3716. castReturnType: SyntaxTree.MathArrayType;
  3717. BEGIN
  3718. ASSERT(IsArrayStructuredObjectType(left.type));
  3719. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3720. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3721. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3722. indexListSize := indexList.Length();
  3723. indexListKind := 0;
  3724. containsNonRange := FALSE;
  3725. FOR i := 0 TO indexList.Length() - 1 DO
  3726. indexListKind := indexListKind * 2;
  3727. expression := indexList.GetExpression(i);
  3728. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3729. INC(indexListKind)
  3730. ELSE
  3731. containsNonRange := TRUE
  3732. END
  3733. END;
  3734. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3735. (* select applicable index operator
  3736. - try to look up optimal index operator
  3737. - if not present, use operator on ranges
  3738. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3739. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3740. *)
  3741. usesGeneralOperator := FALSE;
  3742. IF rhs # NIL THEN
  3743. (* write operator *)
  3744. IF hashValue = -1 THEN
  3745. operator := NIL
  3746. ELSE
  3747. operator := recordType.arrayAccessOperators.write[hashValue];
  3748. END;
  3749. IF operator = NIL THEN
  3750. usesPureRangeOperator := TRUE;
  3751. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3752. operator := recordType.arrayAccessOperators.generalWrite;
  3753. usesGeneralOperator := TRUE
  3754. ELSE
  3755. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3756. operator := recordType.arrayAccessOperators.write[hashValue];
  3757. END
  3758. END
  3759. ELSE
  3760. (* read operator *)
  3761. IF hashValue = -1 THEN
  3762. operator := NIL
  3763. ELSE
  3764. operator := recordType.arrayAccessOperators.read[hashValue];
  3765. END;
  3766. IF operator = NIL THEN
  3767. usesPureRangeOperator := TRUE;
  3768. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3769. operator := recordType.arrayAccessOperators.generalRead;
  3770. usesGeneralOperator := TRUE
  3771. ELSE
  3772. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3773. operator := recordType.arrayAccessOperators.read[hashValue];
  3774. END
  3775. END
  3776. END;
  3777. IF operator = NIL THEN
  3778. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3779. result := SyntaxTree.invalidDesignator;
  3780. ELSE
  3781. (* determine if reshaping is needed *)
  3782. needsReshaping := containsNonRange & usesPureRangeOperator;
  3783. (* import OCArrayBase if reshaping is needed *)
  3784. IF needsReshaping & ~arrayBaseImported THEN
  3785. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3786. arrayBaseImported := TRUE
  3787. END;
  3788. (* add the index list item to the list of actual parameters
  3789. - for general operators: add a single inline array containing the index list items as parameter
  3790. - otherwise: add all index list items as individual parameters
  3791. *)
  3792. actualParameters := SyntaxTree.NewExpressionList();
  3793. IF usesGeneralOperator THEN
  3794. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3795. END;
  3796. FOR i := 0 TO indexListSize - 1 DO
  3797. expression := indexList.GetExpression(i);
  3798. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3799. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3800. tempList := SyntaxTree.NewExpressionList();
  3801. tempList.AddExpression(expression);
  3802. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3803. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3804. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3805. END;
  3806. IF usesGeneralOperator THEN
  3807. tempMathArrayExpression.elements.AddExpression(expression);
  3808. ELSE
  3809. actualParameters.AddExpression(expression)
  3810. END
  3811. END;
  3812. IF usesGeneralOperator THEN
  3813. actualParameters.AddExpression(tempMathArrayExpression)
  3814. END;
  3815. IF rhs # NIL THEN
  3816. (* add actual parameter for RHS *)
  3817. IF needsReshaping THEN
  3818. (* reshape using OCArrayBase.ExpandDimensions *)
  3819. tempList := SyntaxTree.NewExpressionList();
  3820. (* source array *)
  3821. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3822. tempList.AddExpression(rhs);
  3823. ELSE
  3824. (* convert scalar to one-dimensional array *)
  3825. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3826. tempMathArrayExpression.elements.AddExpression(rhs);
  3827. tempList.AddExpression(tempMathArrayExpression)
  3828. END;
  3829. (* list of kept dimensions *)
  3830. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3831. FOR i := 0 TO indexListSize - 1 DO
  3832. expression := indexList.GetExpression(i);
  3833. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3834. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3835. ELSE
  3836. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3837. END
  3838. END;
  3839. tempList.AddExpression(tempMathArrayExpression);
  3840. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3841. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3842. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3843. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3844. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3845. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3846. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3847. expression.SetType(castReturnType);
  3848. ELSE
  3849. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3850. END;
  3851. actualParameters.AddExpression(expression)
  3852. ELSE
  3853. actualParameters.AddExpression(rhs)
  3854. END
  3855. END;
  3856. (* add dereference operator and create procedure call designator *)
  3857. ASSERT(left IS SyntaxTree.Designator);
  3858. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3859. ASSERT(expression IS SyntaxTree.Designator);
  3860. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3861. IF (rhs = NIL) & needsReshaping THEN
  3862. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3863. tempList := SyntaxTree.NewExpressionList();
  3864. FOR i := 0 TO indexList.Length() - 1 DO
  3865. expression := indexList.GetExpression(i);
  3866. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3867. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3868. ELSE
  3869. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3870. END
  3871. END;
  3872. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3873. END;
  3874. IF rhs = NIL THEN
  3875. (* special rule: index read operator calls are considered to be assignable *)
  3876. result.SetAssignable(TRUE)
  3877. END;
  3878. (* put information about this index operator call into the resulting designator *)
  3879. result.SetRelatedAsot(left);
  3880. result.SetRelatedIndexList(indexList)
  3881. END;
  3882. RETURN result
  3883. END NewIndexOperatorCall;
  3884. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3885. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3886. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3887. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3888. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3889. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3890. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3891. CONST trace = FALSE;
  3892. BEGIN
  3893. IF trace THEN
  3894. FOR i := 0 TO actualParameters.Length()-1 DO
  3895. Printout.Info("par", actualParameters.GetExpression(i));
  3896. END;
  3897. END;
  3898. operator := scope.firstOperator;
  3899. WHILE(operator # NIL) DO
  3900. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3901. procedureType := operator.type(SyntaxTree.ProcedureType);
  3902. distance := Distance(system, procedureType,actualParameters);
  3903. IF trace THEN Printout.Info("check op ",operator) END;
  3904. IF distance < bestDistance THEN
  3905. IF trace THEN Printout.Info("taken op",operator) END;
  3906. bestDistance := distance;
  3907. bestOperator := operator;
  3908. END;
  3909. END;
  3910. operator := operator.nextOperator;
  3911. END;
  3912. END FindInScope;
  3913. BEGIN
  3914. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3915. IF oper = 0 THEN (* index *)
  3916. identifier := SyntaxTree.NewIdentifier("[]");
  3917. ELSE
  3918. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3919. END;
  3920. WHILE (recordType # NIL) DO
  3921. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3922. recordType := recordType.GetBaseRecord();
  3923. END;
  3924. RETURN bestOperator
  3925. END FindOperator;
  3926. BEGIN
  3927. type := left.type.resolved;
  3928. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3929. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3930. actualParameters := SyntaxTree.NewExpressionList();
  3931. IF parameters # NIL THEN
  3932. FOR i := 0 TO parameters.Length()-1 DO
  3933. expression := ResolveExpression(parameters.GetExpression(i));
  3934. actualParameters.AddExpression(expression);
  3935. END;
  3936. END;
  3937. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3938. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3939. IF op # NIL THEN
  3940. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3941. ASSERT(expression IS SyntaxTree.Designator);
  3942. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3943. result.SetRelatedAsot(left);
  3944. result.SetRelatedIndexList(parameters);
  3945. (* check if write operator exists, for var parameters *)
  3946. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3947. actualParameters := SyntaxTree.NewExpressionList();
  3948. FOR i := 0 TO parameters.Length()-1 DO
  3949. expression := ResolveExpression(parameters.GetExpression(i));
  3950. actualParameters.AddExpression(expression);
  3951. END;
  3952. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3953. actualParameters.AddExpression(rhs);
  3954. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3955. IF op = NIL THEN rhs := NIL END;
  3956. END;
  3957. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3958. ELSE
  3959. result := NIL;
  3960. END;
  3961. RETURN result;
  3962. END NewObjectOperatorCall;
  3963. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3964. 1. convert bracket designator chains into a single one that contains separators
  3965. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3966. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3967. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3968. - if an array or math array is indexed over, create index designator
  3969. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3970. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3971. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3972. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3973. - if an array-structured object type is indexed over, create procedure call designator
  3974. e.g.: a[x, y] -> a^."[]"(x, y)
  3975. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3976. - a[i, *] = a[i][*]
  3977. - a[*, i] # a[*][i]
  3978. Because:
  3979. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3980. - 'i-th column' = a[*, i]
  3981. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3982. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3983. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3984. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3985. **)
  3986. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3987. VAR
  3988. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3989. indexDesignator: SyntaxTree.IndexDesignator;
  3990. designator: SyntaxTree.Designator;
  3991. type: SyntaxTree.Type;
  3992. recordType: SyntaxTree.RecordType;
  3993. expression, rhs: SyntaxTree.Expression;
  3994. indexList: SyntaxTree.ExpressionList;
  3995. i: LONGINT;
  3996. hasError, done: BOOLEAN;
  3997. PROCEDURE FinalizeIndexDesignator;
  3998. BEGIN
  3999. IF indexDesignator # NIL THEN
  4000. (* the end of a tensor has been reached: *)
  4001. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4002. SetIndexBaseType(indexDesignator, type);
  4003. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4004. designator := indexDesignator;
  4005. type := designator.type.resolved;
  4006. indexDesignator := NIL;
  4007. ASSERT(SyntaxTree.Resolved IN type.state)
  4008. END
  4009. END FinalizeIndexDesignator;
  4010. BEGIN
  4011. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4012. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4013. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4014. (* copy all index list entries including a separator to the left bracket designator *)
  4015. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4016. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4017. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4018. END;
  4019. (* propagate the related RHS *)
  4020. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4021. (* only resolve left bracket designator and use as final result *)
  4022. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4023. ELSE
  4024. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4025. designator := ResolveDesignator(bracketDesignator.left);
  4026. type := designator.type.resolved;
  4027. indexDesignator := NIL;
  4028. (*!!! clean up *)
  4029. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4030. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4031. IF resolvedExpression = NIL THEN
  4032. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4033. resolvedExpression := SyntaxTree.invalidDesignator
  4034. END;
  4035. RETURN;
  4036. END;
  4037. i := 0;
  4038. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4039. expression := bracketDesignator.parameters.GetExpression(i);
  4040. expression := ResolveExpression(expression);
  4041. bracketDesignator.parameters.SetExpression(i, expression);
  4042. IF expression = SyntaxTree.indexListSeparator THEN
  4043. (* finalize an existing index designator if needed *)
  4044. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4045. INC(i)
  4046. ELSE
  4047. (* do auto-dereferencing if needed *)
  4048. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4049. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4050. & (i=0)*)
  4051. THEN
  4052. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4053. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4054. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4055. designator := SyntaxTree.invalidDesignator;
  4056. type := SyntaxTree.invalidType
  4057. ELSE
  4058. FinalizeIndexDesignator;
  4059. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4060. type := designator.type.resolved
  4061. END
  4062. END;
  4063. (* create a new index designator, if needed *)
  4064. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4065. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4066. indexDesignator.SetAssignable(designator.assignable);
  4067. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4068. (* designator := indexDesignator *)
  4069. END;
  4070. IF type = SyntaxTree.invalidType THEN
  4071. (* error already handled *)
  4072. INC(i)
  4073. ELSIF type IS SyntaxTree.ArrayType THEN
  4074. (* indexing over an array *)
  4075. ASSERT(indexDesignator # NIL);
  4076. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4077. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4078. INC(i)
  4079. ELSIF type IS SyntaxTree.StringType THEN
  4080. (* indexing over an array *)
  4081. ASSERT(indexDesignator # NIL);
  4082. AppendIndex(expression.position, indexDesignator, expression, type);
  4083. type := type(SyntaxTree.StringType).baseType.resolved;
  4084. INC(i)
  4085. ELSIF type IS SyntaxTree.MathArrayType THEN
  4086. (* indexing over a math array *)
  4087. ASSERT(indexDesignator # NIL);
  4088. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4089. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4090. INC(i)
  4091. ELSIF IsArrayStructuredObjectType(type) THEN
  4092. (* indexing over ASOTs *)
  4093. FinalizeIndexDesignator;
  4094. ASSERT(type IS SyntaxTree.PointerType);
  4095. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4096. (*
  4097. - collect index list items from bracket designator that belong to ASOT
  4098. - check for errors
  4099. *)
  4100. indexList := SyntaxTree.NewExpressionList();
  4101. hasError := FALSE;
  4102. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4103. (* indexing over tensor ASOT:
  4104. - stop at index list end or separator
  4105. - dimensionality is given by number of index list items
  4106. *)
  4107. done := FALSE;
  4108. WHILE ~done DO
  4109. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4110. done := TRUE;
  4111. ELSE
  4112. expression := bracketDesignator.parameters.GetExpression(i);
  4113. IF expression = SyntaxTree.indexListSeparator THEN
  4114. done := TRUE;
  4115. ELSE
  4116. expression := ResolveExpression(expression);
  4117. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4118. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4119. hasError := TRUE
  4120. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4121. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4122. expression := SyntaxTree.invalidExpression;
  4123. hasError := TRUE
  4124. END;
  4125. indexList.AddExpression(expression)
  4126. END;
  4127. INC(i)
  4128. END
  4129. END
  4130. ELSE
  4131. (* indexing over non-tensor ASOT:
  4132. - ignore separators
  4133. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4134. *)
  4135. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4136. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4137. expression := bracketDesignator.parameters.GetExpression(i);
  4138. ELSE
  4139. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4140. END;
  4141. IF expression # SyntaxTree.indexListSeparator THEN
  4142. expression := ResolveExpression(expression);
  4143. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4144. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4145. expression := SyntaxTree.invalidExpression;
  4146. hasError := TRUE
  4147. END;
  4148. indexList.AddExpression(expression)
  4149. END;
  4150. INC(i)
  4151. END;
  4152. END;
  4153. IF hasError THEN
  4154. designator := SyntaxTree.invalidDesignator;
  4155. type := SyntaxTree.invalidType;
  4156. ELSE
  4157. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4158. and the last entry in the index list belongs to the array-structured object type in question.
  4159. E.g.: for a 2-dimensional array-structured object type:
  4160. - 'lhs := asot[1, 2]' -> read mode
  4161. - 'asot[1, 2] := rhs' -> write mode
  4162. - 'asot[1, 2, 3] := rhs' -> read mode
  4163. *)
  4164. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4165. rhs := bracketDesignator.relatedRhs
  4166. ELSE
  4167. rhs := NIL
  4168. END;
  4169. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4170. type := designator.type
  4171. END
  4172. ELSE
  4173. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4174. designator := SyntaxTree.invalidDesignator;
  4175. type := SyntaxTree.invalidType;
  4176. INC(i)
  4177. END
  4178. END
  4179. END;
  4180. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4181. resolvedExpression := designator
  4182. END
  4183. END VisitBracketDesignator;
  4184. (** check and resolve expression list
  4185. - resolve each expression in an expression list
  4186. - returns true if and only if all statements could have successfully been resolved
  4187. **)
  4188. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4189. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4190. BEGIN
  4191. result := TRUE;
  4192. FOR i := 0 TO expressionList.Length()-1 DO
  4193. expression := ResolveExpression(expressionList.GetExpression(i));
  4194. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4195. expressionList.SetExpression(i,expression);
  4196. END;
  4197. RETURN result
  4198. END ExpressionList;
  4199. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4200. BEGIN
  4201. type := type.resolved;
  4202. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4203. RETURN TRUE
  4204. ELSIF system.CanPassInRegister # NIL THEN
  4205. RETURN system.CanPassInRegister(type);
  4206. ELSE
  4207. RETURN FALSE
  4208. END;
  4209. END CanPassInRegister;
  4210. (** return procedure call designator left(actualParameters)
  4211. - check realtime procedure call in realtime procedure
  4212. - check number of parameters
  4213. - check parameter compatibility
  4214. return invalidDesignator if error
  4215. **)
  4216. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4217. VAR result: SyntaxTree.Designator;
  4218. numberFormalParameters, numberActualParameters: LONGINT;
  4219. formalType: SyntaxTree.ProcedureType;
  4220. formalParameter: SyntaxTree.Parameter;
  4221. actualParameter: SyntaxTree.Expression;
  4222. i: LONGINT;
  4223. BEGIN
  4224. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4225. result := SyntaxTree.invalidDesignator;
  4226. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4227. numberFormalParameters := formalType.numberParameters;
  4228. numberActualParameters := actualParameters.Length();
  4229. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4230. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4231. END;
  4232. IF ~ExpressionList(actualParameters) THEN
  4233. result := SyntaxTree.invalidDesignator
  4234. ELSE
  4235. IF numberActualParameters <= numberFormalParameters THEN
  4236. formalParameter := formalType.firstParameter;
  4237. FOR i := 0 TO numberActualParameters-1 DO
  4238. actualParameter := actualParameters.GetExpression(i);
  4239. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4240. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4241. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4242. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4243. ELSE
  4244. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4245. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4246. END;
  4247. actualParameters.SetExpression(i,actualParameter);
  4248. END;
  4249. formalParameter := formalParameter.nextParameter;
  4250. END;
  4251. WHILE (formalParameter # NIL) DO
  4252. IF formalParameter.defaultValue # NIL THEN
  4253. actualParameters.AddExpression(formalParameter.defaultValue);
  4254. formalParameter := formalParameter.nextParameter
  4255. ELSE
  4256. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4257. formalParameter := NIL;
  4258. END;
  4259. END;
  4260. ELSE
  4261. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4262. END;
  4263. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4264. result.SetAssignable(FALSE);
  4265. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4266. END;
  4267. RETURN result
  4268. END NewProcedureCallDesignator;
  4269. (**
  4270. builtin call designator generated in VisitParameterDesignator
  4271. -> nothing to be resolved
  4272. **)
  4273. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4274. BEGIN
  4275. resolvedExpression := x;
  4276. END VisitTypeGuardDesignator;
  4277. (**
  4278. builtin call designator generated in VisitParameterDesignator
  4279. -> nothing to be resolved
  4280. **)
  4281. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4282. BEGIN
  4283. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4284. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4285. ASSERT(resolvedExpression.type # NIL);
  4286. ELSIF ExpressionList(x.parameters) THEN
  4287. resolvedExpression := x;
  4288. END;
  4289. END VisitBuiltinCallDesignator;
  4290. (**
  4291. procedure call designator generated in VisitParameterDesignator
  4292. -> nothing to be resolved
  4293. **)
  4294. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4295. BEGIN
  4296. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4297. resolvedExpression := x;
  4298. END VisitProcedureCallDesignator;
  4299. (** return true if x is a variable else return false and report error **)
  4300. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4301. VAR result: BOOLEAN;
  4302. BEGIN
  4303. result := TRUE;
  4304. IF x = SyntaxTree.invalidExpression THEN
  4305. result := FALSE;
  4306. ELSIF ~IsVariable(x) THEN
  4307. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4308. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4309. result := FALSE;
  4310. END;
  4311. RETURN result
  4312. END CheckVariable;
  4313. (**
  4314. if expression x is of basic type then return true else report error and return false
  4315. **)
  4316. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN;
  4318. BEGIN
  4319. result := FALSE;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~IsBasicType(x.type) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4323. result := FALSE
  4324. ELSE result := TRUE
  4325. END;
  4326. RETURN result
  4327. END CheckBasicType;
  4328. (**
  4329. if expression x is of number type then return true else report error and return false
  4330. **)
  4331. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4332. VAR result: BOOLEAN;
  4333. BEGIN
  4334. result := FALSE;
  4335. IF x = SyntaxTree.invalidExpression THEN
  4336. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4337. Error(x.position,Diagnostics.Invalid,"is non number type");
  4338. ELSE result := TRUE
  4339. END;
  4340. RETURN result
  4341. END CheckNumberType;
  4342. (**
  4343. if expression x is of number or size type but not complex then return true else report error and return false
  4344. **)
  4345. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4346. VAR result: BOOLEAN;
  4347. BEGIN
  4348. result := FALSE;
  4349. IF x = SyntaxTree.invalidExpression THEN
  4350. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4351. Error(x.position,Diagnostics.Invalid,"is complex type");
  4352. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4353. Error(x.position,Diagnostics.Invalid,"is non number type");
  4354. ELSE result := TRUE
  4355. END;
  4356. RETURN result
  4357. END CheckNonComplexNumberSizeType;
  4358. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4359. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4360. BEGIN
  4361. result := FALSE; type := x.type.resolved;
  4362. IF x = SyntaxTree.invalidExpression THEN
  4363. 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
  4364. TRACE(type.sizeInBits);
  4365. TRACE(system.addressType.sizeInBits);
  4366. Error(x.position,Diagnostics.Invalid,"is no address type");
  4367. ELSE result := TRUE
  4368. END;
  4369. RETURN result
  4370. END CheckAddressType;
  4371. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4372. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4373. BEGIN
  4374. result := FALSE; type := x.type.resolved;
  4375. IF x = SyntaxTree.invalidExpression THEN
  4376. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4377. Error(x.position,Diagnostics.Invalid,"is no size type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckSizeType;
  4382. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4384. BEGIN
  4385. result := FALSE; type := x.type.resolved;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. 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
  4388. Error(x.position,Diagnostics.Invalid,"is no object type");
  4389. ELSE result := TRUE
  4390. END;
  4391. RETURN result
  4392. END CheckObjectType;
  4393. (**
  4394. if expression x is of integer type then return true else report error and return false
  4395. **)
  4396. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4397. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4398. BEGIN
  4399. result := FALSE; type := x.type.resolved;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4402. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4403. ELSE result := TRUE
  4404. END;
  4405. RETURN result
  4406. END CheckIntegerType;
  4407. (**
  4408. if expression x is of character type then return true else report error and return false
  4409. **)
  4410. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4411. VAR result: BOOLEAN;
  4412. BEGIN
  4413. result := FALSE;
  4414. IF x = SyntaxTree.invalidExpression THEN
  4415. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4416. Error(x.position,Diagnostics.Invalid,"is no character type");
  4417. ELSE result := TRUE
  4418. END;
  4419. RETURN result
  4420. END CheckCharacterType;
  4421. (**
  4422. if expression x is of real type then return true else report error and return false
  4423. **)
  4424. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4425. VAR result: BOOLEAN;
  4426. BEGIN
  4427. result := FALSE;
  4428. IF x = SyntaxTree.invalidExpression THEN
  4429. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4430. Error(x.position,Diagnostics.Invalid,"is no float type");
  4431. ELSE result := TRUE
  4432. END;
  4433. RETURN result
  4434. END CheckRealType;
  4435. (**
  4436. if expression x is of range type then return true else report error and return false
  4437. **)
  4438. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4439. VAR result: BOOLEAN;
  4440. BEGIN
  4441. result := FALSE;
  4442. IF x = SyntaxTree.invalidExpression THEN
  4443. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4444. Error(x.position,Diagnostics.Invalid,"is no range type");
  4445. ELSE result := TRUE
  4446. END;
  4447. RETURN result
  4448. END CheckRangeType;
  4449. (**
  4450. if expression x is of boolean type then return true else report error and return false
  4451. **)
  4452. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4453. VAR result: BOOLEAN;
  4454. BEGIN
  4455. result := FALSE;
  4456. IF x = SyntaxTree.invalidExpression THEN
  4457. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4458. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4459. ELSE result := TRUE
  4460. END;
  4461. RETURN result
  4462. END CheckBooleanType;
  4463. (**
  4464. if expression x is of set type then return true else report error and return false
  4465. **)
  4466. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4467. VAR result: BOOLEAN;
  4468. BEGIN
  4469. result := FALSE;
  4470. IF x = SyntaxTree.invalidExpression THEN
  4471. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4472. Error(x.position,Diagnostics.Invalid,"is no set type");
  4473. ELSE result := TRUE
  4474. END;
  4475. RETURN result
  4476. END CheckSetType;
  4477. (**
  4478. if expression x is of string or array of character type then return true else report error and return false
  4479. **)
  4480. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4481. VAR result: BOOLEAN;
  4482. BEGIN
  4483. result := FALSE;
  4484. IF x = SyntaxTree.invalidExpression THEN
  4485. ELSIF ~IsStringType(x.type.resolved) THEN
  4486. Error(x.position,Diagnostics.Invalid,"is no string type");
  4487. ELSE result := TRUE
  4488. END;
  4489. RETURN result
  4490. END CheckStringType;
  4491. (**
  4492. if expression x is a type declaration type return true else report error and return false
  4493. **)
  4494. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4495. VAR result: BOOLEAN;
  4496. BEGIN
  4497. result := FALSE;
  4498. IF x = SyntaxTree.invalidExpression THEN
  4499. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4500. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4501. ELSE result := TRUE
  4502. END;
  4503. RETURN result
  4504. END CheckTypeDeclarationType;
  4505. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4506. VAR result: BOOLEAN;
  4507. BEGIN
  4508. result := FALSE;
  4509. IF x = SyntaxTree.invalidExpression THEN
  4510. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4511. result := TRUE;
  4512. value := x.resolved(SyntaxTree.IntegerValue).value;
  4513. ELSE
  4514. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4515. END;
  4516. RETURN result;
  4517. END CheckIntegerValue;
  4518. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4524. result := TRUE;
  4525. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4526. ELSE
  4527. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4528. END;
  4529. RETURN result;
  4530. END CheckStringValue;
  4531. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4532. BEGIN
  4533. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4534. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4535. ELSE
  4536. RETURN FALSE
  4537. END;
  4538. END IsUnsignedValue;
  4539. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4540. BEGIN
  4541. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4542. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4543. ELSE
  4544. RETURN FALSE
  4545. END
  4546. END IsAddressValue;
  4547. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4548. BEGIN
  4549. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4550. END IsAddressExpression;
  4551. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4552. BEGIN
  4553. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4554. END IsSizeExpression;
  4555. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4556. VAR result: BOOLEAN;
  4557. BEGIN
  4558. result := FALSE;
  4559. IF x = SyntaxTree.invalidExpression THEN
  4560. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4561. result := TRUE;
  4562. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4563. ELSE
  4564. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4565. END;
  4566. RETURN result;
  4567. END CheckEnumerationValue;
  4568. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4569. VAR result: BOOLEAN;
  4570. BEGIN
  4571. result := FALSE;
  4572. IF x = SyntaxTree.invalidExpression THEN
  4573. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4574. result := TRUE;
  4575. value := x.resolved(SyntaxTree.CharacterValue).value;
  4576. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4577. result := TRUE;
  4578. value := x.resolved(SyntaxTree.StringValue).value[0];
  4579. ELSE
  4580. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4581. END;
  4582. RETURN result;
  4583. END CheckCharacterValue;
  4584. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4585. VAR result: BOOLEAN;
  4586. BEGIN
  4587. result := FALSE;
  4588. IF x = SyntaxTree.invalidExpression THEN
  4589. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4590. value := x.resolved(SyntaxTree.IntegerValue).value;
  4591. IF (value > 0) OR includeZero & (value = 0) THEN
  4592. result := TRUE;
  4593. ELSE
  4594. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4595. END
  4596. ELSE
  4597. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4598. END;
  4599. RETURN result;
  4600. END CheckPositiveIntegerValue;
  4601. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4602. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4603. BEGIN
  4604. result := FALSE;
  4605. IF x = SyntaxTree.invalidExpression THEN
  4606. ELSE
  4607. type := x.type.resolved;
  4608. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4609. portType := type(SyntaxTree.PortType);
  4610. result := TRUE
  4611. ELSE
  4612. Error(x.position,Diagnostics.Invalid,"no port type");
  4613. END;
  4614. END;
  4615. RETURN result
  4616. END CheckPortType;
  4617. (* move to builtin procedure call statement ?
  4618. remove builtin procedure call designator ?
  4619. *)
  4620. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4621. VAR
  4622. numberActualParameters,numberFormalParameters: LONGINT;
  4623. formalParameter: SyntaxTree.Parameter;
  4624. actualParameter: SyntaxTree.Expression;
  4625. procedureType: SyntaxTree.ProcedureType;
  4626. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4627. inPort, outPort: SyntaxTree.PortType;
  4628. constructor: SyntaxTree.Procedure;
  4629. type0,type1,type2: SyntaxTree.Type;
  4630. type,base,parameterType: SyntaxTree.Type;
  4631. arrayType: SyntaxTree.ArrayType;
  4632. i,i0,i1: LONGINT;
  4633. r,r0,r1,im: LONGREAL;
  4634. c: CHAR;
  4635. id: LONGINT;
  4636. b: BOOLEAN;
  4637. first: LONGINT;
  4638. mathArrayType: SyntaxTree.MathArrayType;
  4639. customBuiltin: SyntaxTree.CustomBuiltin;
  4640. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4641. VAR resultB: BOOLEAN;
  4642. BEGIN
  4643. IF numberActualParameters < from THEN
  4644. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4645. result := SyntaxTree.invalidExpression;
  4646. resultB := FALSE;
  4647. ELSIF numberActualParameters > to THEN
  4648. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4649. result := SyntaxTree.invalidExpression;
  4650. resultB := FALSE;
  4651. ELSE
  4652. resultB := TRUE;
  4653. END;
  4654. RETURN resultB
  4655. END CheckArity;
  4656. BEGIN
  4657. type := NIL; result := NIL;
  4658. type0 := NIL; type1 := NIL; type2 := NIL;
  4659. numberActualParameters := actualParameters.Length();
  4660. IF numberActualParameters>0 THEN
  4661. parameter0 := actualParameters.GetExpression(0);
  4662. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4663. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4664. result := SyntaxTree.invalidExpression
  4665. END
  4666. END;
  4667. IF numberActualParameters >1 THEN
  4668. parameter1 := actualParameters.GetExpression(1);
  4669. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4670. ELSE
  4671. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4672. result := SyntaxTree.invalidExpression
  4673. END
  4674. END;
  4675. IF numberActualParameters >2 THEN
  4676. parameter2 := actualParameters.GetExpression(2);
  4677. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4678. ELSE
  4679. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4680. result := SyntaxTree.invalidExpression
  4681. END
  4682. END;
  4683. IF returnType # NIL THEN
  4684. id := Global.New;
  4685. result := NIL;
  4686. ELSE
  4687. id := builtin.id;
  4688. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4689. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4690. END;
  4691. END;
  4692. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4693. ELSIF result # NIL THEN type := result.type (* operator *)
  4694. ELSE
  4695. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4696. result(SyntaxTree.Designator).SetLeft(left);
  4697. IF returnType # NIL THEN
  4698. type := returnType;
  4699. END;
  4700. (* ---- ASSERT ----- *)
  4701. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4702. IF CheckBooleanType(parameter0) THEN
  4703. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4704. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4705. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4706. *)
  4707. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4708. Error(position,Diagnostics.Invalid,"assert failed");
  4709. END;
  4710. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4711. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4712. rules imposed by the architecture / current runtime
  4713. *)
  4714. END;
  4715. END;
  4716. (* ---- COPY ----- *)
  4717. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4718. IF~IsStringType(type0) THEN
  4719. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4720. END;
  4721. IF ~IsStringType(type1) THEN
  4722. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4723. ELSIF CheckVariable(parameter1) THEN
  4724. IF (type0 IS SyntaxTree.StringType) THEN
  4725. arrayType := type1(SyntaxTree.ArrayType);
  4726. IF arrayType.form = SyntaxTree.Static THEN
  4727. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4728. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4729. END;
  4730. END;
  4731. END;
  4732. END;
  4733. (* ---- INC, DEC----- *)
  4734. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4735. IF numberActualParameters = 1 THEN
  4736. parameter1 :=Global.NewIntegerValue(system,position,1);
  4737. actualParameters.AddExpression(parameter1);
  4738. END;
  4739. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4740. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4741. Error(position,Diagnostics.Invalid,"incompatible increment");
  4742. ELSE
  4743. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4744. actualParameters.SetExpression(1,parameter1);
  4745. END;
  4746. END;
  4747. (* ---- EXCL, INCL----- *)
  4748. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4749. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4750. IF IsIntegerValue(parameter1,i0) THEN
  4751. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4752. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4753. END;
  4754. END;
  4755. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4756. actualParameters.SetExpression(1,parameter1);
  4757. END;
  4758. (* ---- HALT, SYSTEM.HALT ----- *)
  4759. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4760. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4761. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4762. rules imposed by the architecture / current runtime
  4763. *)
  4764. END;
  4765. (* ---- WAIT ----- *)
  4766. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4767. IF CheckObjectType(parameter0) THEN
  4768. END;
  4769. (* ---- NEW ----- *)
  4770. ELSIF (id = Global.New) THEN
  4771. IF returnType # NIL THEN
  4772. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4773. ELSE
  4774. first := 1;
  4775. END;
  4776. IF CheckArity(first,Infinity) THEN
  4777. IF currentIsRealtime THEN
  4778. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4779. END;
  4780. (* check constructor *)
  4781. IF (first =0) OR CheckVariable(parameter0) THEN
  4782. IF type0 IS SyntaxTree.PointerType THEN
  4783. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4784. ELSIF type0 IS SyntaxTree.CellType THEN
  4785. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4786. ELSE
  4787. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4788. END;
  4789. IF type0 IS SyntaxTree.ArrayType THEN
  4790. arrayType := type0(SyntaxTree.ArrayType);
  4791. IF arrayType.form = SyntaxTree.Static THEN
  4792. i := first
  4793. ELSIF arrayType.form = SyntaxTree.Open THEN
  4794. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4795. ELSE HALT(100)
  4796. END;
  4797. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4798. i := first;
  4799. REPEAT
  4800. actualParameter := actualParameters.GetExpression(i);
  4801. IF CheckSizeType(actualParameter) THEN
  4802. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4803. actualParameters.SetExpression(i,actualParameter);
  4804. END;
  4805. INC(i);
  4806. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4807. END;
  4808. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4809. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4810. IF constructor = NIL THEN
  4811. IF CheckArity(first,first) THEN END;
  4812. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4813. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4814. ELSE
  4815. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4816. numberFormalParameters := procedureType.numberParameters;
  4817. IF numberActualParameters-first <= numberFormalParameters THEN
  4818. formalParameter := procedureType.firstParameter;
  4819. FOR i := first TO numberActualParameters-1 DO
  4820. actualParameter := actualParameters.GetExpression(i);
  4821. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4822. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4823. ELSE
  4824. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4825. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4826. END;
  4827. actualParameters.SetExpression(i,actualParameter);
  4828. END;
  4829. formalParameter := formalParameter.nextParameter;
  4830. END;
  4831. WHILE (formalParameter # NIL) DO
  4832. IF formalParameter.defaultValue # NIL THEN
  4833. actualParameters.AddExpression(formalParameter.defaultValue);
  4834. formalParameter := formalParameter.nextParameter
  4835. ELSE
  4836. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4837. formalParameter := NIL;
  4838. END;
  4839. END;
  4840. ELSE
  4841. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4842. END;
  4843. END;
  4844. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4845. mathArrayType := type0(SyntaxTree.MathArrayType);
  4846. IF mathArrayType.form = SyntaxTree.Static THEN
  4847. Error(position,Diagnostics.Invalid,"new on static array");
  4848. ELSE
  4849. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4850. i0 := first+1; i1 := Infinity;
  4851. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4852. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4853. i1 := i0;
  4854. ELSE HALT(100);
  4855. END;
  4856. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4857. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4858. base := ArrayBase(type0,MAX(LONGINT));
  4859. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4860. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4861. IF ~CompatibleTo(system,type0,parameterType) THEN
  4862. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4863. result := SyntaxTree.invalidExpression;
  4864. ELSE
  4865. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4866. END;
  4867. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4868. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4869. IF ~CompatibleTo(system,type1,parameterType) THEN
  4870. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4871. result := SyntaxTree.invalidExpression;
  4872. ELSE
  4873. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4874. END;
  4875. ELSE
  4876. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4877. i := first;
  4878. REPEAT
  4879. actualParameter := actualParameters.GetExpression(i);
  4880. IF CheckSizeType(actualParameter) THEN
  4881. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4882. actualParameters.SetExpression(i,actualParameter);
  4883. END;
  4884. INC(i);
  4885. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4886. END;
  4887. END;
  4888. END;
  4889. ELSIF type0 IS SyntaxTree.CellType THEN
  4890. IF ~(currentIsCellNet) THEN
  4891. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4892. ELSE
  4893. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4894. IF (constructor = NIL) & CheckArity(1,1) THEN
  4895. (* ok *)
  4896. ELSE
  4897. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4898. numberFormalParameters := procedureType.numberParameters;
  4899. DEC(numberActualParameters);
  4900. IF numberActualParameters <= numberFormalParameters THEN
  4901. formalParameter := procedureType.firstParameter;
  4902. FOR i := first TO numberActualParameters DO
  4903. actualParameter := actualParameters.GetExpression(i);
  4904. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4905. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4906. ELSE
  4907. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4908. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4909. END;
  4910. actualParameters.SetExpression(i,actualParameter);
  4911. END;
  4912. formalParameter := formalParameter.nextParameter;
  4913. END;
  4914. WHILE (formalParameter # NIL) DO
  4915. IF formalParameter.defaultValue # NIL THEN
  4916. actualParameters.AddExpression(formalParameter.defaultValue);
  4917. formalParameter := formalParameter.nextParameter
  4918. ELSE
  4919. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4920. formalParameter := NIL;
  4921. END;
  4922. END;
  4923. ELSE
  4924. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4925. END;
  4926. END;
  4927. END;
  4928. activeCellsStatement := TRUE;
  4929. ELSE
  4930. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4931. END;
  4932. END;
  4933. END;
  4934. (* ---- DISPOSE ----- *)
  4935. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4936. IF ~IsPointerType(parameter0.type) THEN
  4937. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4938. ELSIF ~IsDisposable(parameter0.type) THEN
  4939. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4940. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4941. END
  4942. (* ---- GETPROCEDURE ----- *)
  4943. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4944. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4945. IF CheckVariable(parameter2) THEN
  4946. IF ~GetProcedureAllowed(parameter2.type) THEN
  4947. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4948. END;
  4949. END;
  4950. END;
  4951. (* ---- ABS ----- *)
  4952. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4953. (* note: ABS on complex numbers is done using overloading *)
  4954. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4955. type := type0;
  4956. IF IsIntegerValue(parameter0,i0) THEN
  4957. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4958. type := Global.GetIntegerType(system,ABS(i0));
  4959. ELSIF IsRealValue(parameter0,r) THEN
  4960. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4961. END;
  4962. ELSE
  4963. type := SyntaxTree.invalidType;
  4964. END;
  4965. (* ---- ASH, ASR ----- *)
  4966. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4967. type := type0;
  4968. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4969. (*
  4970. ConvertOperands(parameter0,parameter1); (* same type *)
  4971. *)
  4972. type := parameter0.type;
  4973. IF IsIntegerValue(parameter0,i0) THEN
  4974. IF IsIntegerValue(parameter1,i1) THEN
  4975. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4976. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4977. result := ResolveExpression(result);
  4978. type := Global.GetIntegerType(system,i0);
  4979. END;
  4980. END;
  4981. IF type.resolved.sizeInBits < 32 THEN
  4982. type := system.longintType;
  4983. END;
  4984. (*!compatibility with release, remove when resolved
  4985. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4986. *)
  4987. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4988. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4989. actualParameters.SetExpression(0,parameter0);
  4990. actualParameters.SetExpression(1,parameter1);
  4991. END;
  4992. (* ---- CAP ----- *)
  4993. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4994. type := system.characterType;
  4995. IF CheckCharacterType (parameter0) THEN
  4996. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4997. actualParameters.SetExpression(0,parameter0);
  4998. IF IsCharacterValue(parameter0,c) THEN
  4999. IF (c <= "z") & (c >= "a") THEN
  5000. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5001. ELSE
  5002. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5003. END;
  5004. END;
  5005. END;
  5006. (* ---- CHR ----- *)
  5007. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5008. IF id = Global.Chr THEN
  5009. type := system.characterType
  5010. ELSE
  5011. type := system.characterType32
  5012. END;
  5013. IF CheckIntegerType(parameter0) THEN
  5014. IF IsIntegerValue(parameter0,i0) THEN
  5015. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5016. result := ResolveExpression(result);
  5017. ELSE
  5018. (*
  5019. result := NewConversion(parameter0.position,parameter0,type);
  5020. *)
  5021. END;
  5022. END
  5023. (* ---- ENTIER ----- *)
  5024. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5025. type := system.longintType;
  5026. IF CheckRealType(parameter0) THEN
  5027. IF IsRealValue(parameter0,r) THEN
  5028. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5029. type := Global.GetIntegerType(system,ENTIER(r));
  5030. END
  5031. END;
  5032. (* ---- ENTIERH ----- *)
  5033. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5034. type := system.hugeintType;
  5035. IF CheckRealType(parameter0) THEN
  5036. IF IsRealValue(parameter0,r) THEN
  5037. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5038. END
  5039. END;
  5040. (* ---- LEN ----- *)
  5041. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5042. type := system.longintType;
  5043. base := type0;
  5044. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5045. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5046. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5047. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5048. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5049. END;
  5050. type0 := base;
  5051. ELSE
  5052. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5053. type0 := parameter0.type.resolved;
  5054. actualParameters.SetExpression(0,parameter0);
  5055. base := type0;
  5056. END;
  5057. END;
  5058. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5059. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5060. IF i1 < 0 THEN
  5061. Error(position,Diagnostics.Invalid,"invalid dimension");
  5062. base := SyntaxTree.invalidType;
  5063. ELSE
  5064. base := ArrayBase(base,i1);
  5065. IF (base # NIL) & Indexable(base) THEN
  5066. ELSE
  5067. Error(position,Diagnostics.Invalid,"len on no array");
  5068. IF VerboseErrorMessage THEN
  5069. Printout.Info("base",base);
  5070. END;
  5071. base := SyntaxTree.invalidType;
  5072. END;
  5073. END;
  5074. IF numberActualParameters=2 THEN
  5075. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5076. actualParameters.SetExpression(1,parameter1);
  5077. ELSIF base IS SyntaxTree.MathArrayType THEN
  5078. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5079. END;
  5080. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5081. IF base IS SyntaxTree.ArrayType THEN
  5082. arrayType := base(SyntaxTree.ArrayType);
  5083. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5084. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5085. result := Global.NewIntegerValue(system,position,i);
  5086. type := result.type;(* arrayType.length.type;*)
  5087. ASSERT(type # NIL);
  5088. END;
  5089. ELSIF base IS SyntaxTree.MathArrayType THEN
  5090. mathArrayType := base(SyntaxTree.MathArrayType);
  5091. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5092. result := Global.NewIntegerValue(system,position,i);
  5093. type := result.type;
  5094. (*
  5095. type := mathArrayType.length.type;
  5096. *)
  5097. ASSERT(type # NIL);
  5098. END;
  5099. END;
  5100. END;
  5101. ELSE
  5102. type := system.longintType;
  5103. END;
  5104. (* ---- FIRST ---- *)
  5105. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5106. type := system.longintType;
  5107. IF CheckRangeType(parameter0) THEN END;
  5108. result.SetAssignable(parameter0.assignable)
  5109. (* ---- LAST ---- *)
  5110. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5111. type := system.longintType;
  5112. IF CheckRangeType(parameter0) THEN END;
  5113. result.SetAssignable(parameter0.assignable)
  5114. (* ---- STEP ---- *)
  5115. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5116. type := system.longintType;
  5117. IF CheckRangeType(parameter0) THEN END;
  5118. result.SetAssignable(parameter0.assignable)
  5119. (* ---- RE ---- *)
  5120. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5121. IF CheckNumberType(parameter0) THEN
  5122. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5123. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5124. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5125. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5126. type := parameter0.type
  5127. ELSE
  5128. type := system.realType
  5129. END
  5130. END;
  5131. result.SetAssignable(parameter0.assignable)
  5132. (* ---- IM ---- *)
  5133. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5134. IF CheckNumberType(parameter0) THEN
  5135. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5136. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5137. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5138. ELSE
  5139. type := system.realType;
  5140. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5141. END
  5142. END;
  5143. result.SetAssignable(parameter0.assignable)
  5144. (* ---- MAX ----- *)
  5145. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5146. IF numberActualParameters = 1 THEN
  5147. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5148. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5149. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5150. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5151. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5152. *)
  5153. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5154. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5155. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5156. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5157. ELSE Error(parameter0.position,Diagnostics.Invalid, "builtin function not applicable to this type");
  5158. END;
  5159. ELSE
  5160. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5161. END
  5162. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5163. ConvertOperands(parameter0,parameter1);
  5164. actualParameters.SetExpression(0,parameter0);
  5165. actualParameters.SetExpression(1,parameter1);
  5166. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5167. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5168. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5169. END;
  5170. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5171. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5172. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5173. END;
  5174. END;
  5175. type := parameter0.type;
  5176. ELSE type := SyntaxTree.invalidType;
  5177. END;
  5178. (* ---- MIN ----- *)
  5179. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5180. IF numberActualParameters = 1 THEN
  5181. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5182. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5183. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5184. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5185. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5186. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5187. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5188. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5189. END;
  5190. ELSE
  5191. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5192. END
  5193. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5194. ConvertOperands(parameter0,parameter1);
  5195. actualParameters.SetExpression(0,parameter0);
  5196. actualParameters.SetExpression(1,parameter1);
  5197. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5198. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5199. ELSE result.SetResolved(parameter1.resolved)
  5200. END;
  5201. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5202. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5203. ELSE result.SetResolved(parameter1.resolved)
  5204. END;
  5205. END;
  5206. type := parameter0.type;
  5207. ELSE type := SyntaxTree.invalidType;
  5208. END;
  5209. (* ---- ODD ----- *)
  5210. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5211. type := system.booleanType;
  5212. IF CheckIntegerType(parameter0) THEN
  5213. IF IsIntegerValue(parameter0,i0) THEN
  5214. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5215. type := system.booleanType;
  5216. END;
  5217. END;
  5218. (* ---- ORD ----- *)
  5219. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5220. IF id = Global.Ord THEN
  5221. type := system.integerType;
  5222. ELSE
  5223. type := system.longintType;
  5224. END;
  5225. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5226. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5227. actualParameters.SetExpression(0,parameter0);
  5228. (* IF CheckCharacterType(parameter0) THEN*)
  5229. IF IsCharacterValue(parameter0,c)THEN
  5230. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5231. type := Global.GetSignedIntegerType(system,ORD(c));
  5232. END;
  5233. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5234. END;
  5235. (* ---- SHORT ----- *)
  5236. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5237. type := type0;
  5238. IF IsSignedIntegerType(type) THEN
  5239. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5240. ELSIF type = system.integerType THEN type := system.shortintType
  5241. ELSIF type = system.longintType THEN type := system.integerType
  5242. ELSIF type = system.hugeintType THEN type:= system.longintType
  5243. ELSE
  5244. CASE type.sizeInBits OF
  5245. 16: type := Global.Integer8
  5246. |32: type := Global.Integer16
  5247. |64: type := Global.Integer32
  5248. END;
  5249. END;
  5250. ELSIF type IS SyntaxTree.FloatType THEN
  5251. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5252. ELSIF type = system.longrealType THEN type := system.realType
  5253. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5254. END;
  5255. ELSIF type IS SyntaxTree.ComplexType THEN
  5256. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5257. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5258. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5259. END;
  5260. ELSE
  5261. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5262. END;
  5263. IF (parameter0.resolved # NIL) THEN
  5264. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5265. IF parameter0 IS SyntaxTree.Value THEN
  5266. result.SetResolved(parameter0(SyntaxTree.Value));
  5267. END;
  5268. END;
  5269. (* ---- LONG ----- *)
  5270. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5271. type := type0;
  5272. IF IsSignedIntegerType(type) THEN
  5273. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5274. ELSIF type = system.longintType THEN type := system.hugeintType
  5275. ELSIF type = system.integerType THEN type := system.longintType
  5276. ELSIF type = system.shortintType THEN type := system.integerType
  5277. ELSE
  5278. CASE type.sizeInBits OF
  5279. 8: type := Global.Integer16
  5280. |16: type := Global.Integer32
  5281. |32: type := Global.Integer64
  5282. END;
  5283. END;
  5284. ELSIF type IS SyntaxTree.FloatType THEN
  5285. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5286. ELSIF type= system.realType THEN type := system.longrealType
  5287. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5288. END;
  5289. ELSIF type IS SyntaxTree.ComplexType THEN
  5290. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5291. ELSIF type = system.complexType THEN type := system.longcomplexType
  5292. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5293. END;
  5294. ELSE
  5295. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5296. END;
  5297. IF (parameter0.resolved # NIL) THEN
  5298. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5299. IF parameter0 IS SyntaxTree.Value THEN
  5300. result.SetResolved(parameter0(SyntaxTree.Value));
  5301. END;
  5302. END;
  5303. (* ---- SIZE OF ----- *)
  5304. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5305. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5306. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5307. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5308. type := system.integerType;
  5309. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5310. ELSE
  5311. (* for variables, system sizeof could represent the physically occupied size
  5312. determined via the type descriptor, implement that ? *)
  5313. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5314. END
  5315. (* ---- SYSTEM.TRACE -----*)
  5316. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5317. FOR i := 0 TO numberActualParameters-1 DO
  5318. parameter0 := actualParameters.GetExpression(i);
  5319. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5320. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5321. END;
  5322. END;
  5323. (* remaining issues can only be tested in backend *)
  5324. (* ---- ADDRESSOF----- *)
  5325. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5326. IF HasAddress(parameter0) THEN
  5327. type := system.addressType;
  5328. ELSE
  5329. type := SyntaxTree.invalidType;
  5330. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5331. END;
  5332. (* ---- BIT ----- *)
  5333. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5334. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5335. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5336. actualParameters.SetExpression(0,parameter0);
  5337. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5338. actualParameters.SetExpression(1,parameter1);
  5339. END;
  5340. type := system.booleanType;
  5341. (* ----- MSK ---- *)
  5342. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5343. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5344. ConvertOperands(parameter0,parameter1);
  5345. actualParameters.SetExpression(0,parameter0);
  5346. actualParameters.SetExpression(1,parameter1);
  5347. END;
  5348. type := parameter0.type;
  5349. (* ---- SYSTEM.GET64 ----- *)
  5350. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5351. IF CheckAddressType(parameter0) THEN
  5352. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5353. actualParameters.SetExpression(0,parameter0);
  5354. END;
  5355. type := system.hugeintType;
  5356. (* ---- SYSTEM.GET32 ----- *)
  5357. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5358. IF CheckAddressType(parameter0) THEN
  5359. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5360. actualParameters.SetExpression(0,parameter0);
  5361. END;
  5362. type := system.longintType;
  5363. (* ---- SYSTEM.GET16 ----- *)
  5364. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5365. IF CheckAddressType(parameter0) THEN
  5366. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5367. actualParameters.SetExpression(0,parameter0);
  5368. END;
  5369. type := system.integerType;
  5370. (* ---- SYSTEM.GET8 ----- *)
  5371. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5372. IF CheckAddressType(parameter0) THEN
  5373. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5374. actualParameters.SetExpression(0,parameter0);
  5375. END;
  5376. type := system.shortintType;
  5377. (* ---- SYSTEM.GetStackPointer ----- *)
  5378. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5379. type := system.addressType;
  5380. (* ---- SYSTEM.GetFramePointer ----- *)
  5381. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5382. type := system.addressType;
  5383. (* ---- SYSTEM.GetActivity ----- *)
  5384. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5385. type := system.objectType;
  5386. (* ---- SYSTEM.SetStackPointer ----- *)
  5387. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5388. IF CheckAddressType(parameter0) THEN
  5389. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5390. actualParameters.SetExpression(0,parameter0);
  5391. END;
  5392. (* ---- SYSTEM.SetFramePointer ----- *)
  5393. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5394. IF CheckAddressType(parameter0) THEN
  5395. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5396. actualParameters.SetExpression(0,parameter0);
  5397. END;
  5398. (* ---- SYSTEM.SetActivity ----- *)
  5399. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5400. IF CheckObjectType(parameter0) THEN
  5401. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5402. actualParameters.SetExpression(0,parameter0);
  5403. END;
  5404. (* ---- LSH, LSL, ROT, ROR ----- *)
  5405. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5406. type := type0;
  5407. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5408. actualParameters.SetExpression(1, parameter1);
  5409. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5410. IF id = Global.Lsh THEN
  5411. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5412. ELSIF id = Global.Rot THEN
  5413. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5414. ELSIF id = Global.Ror THEN
  5415. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5416. END;
  5417. END;
  5418. (* ---- SYSTEM.VAL ----- *)
  5419. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5420. IF CheckTypeDeclarationType(parameter0) THEN
  5421. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5422. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5423. result := SyntaxTree.invalidExpression;
  5424. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5425. ELSE
  5426. IF (parameter1.resolved # NIL) THEN
  5427. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5428. IF parameter0 IS SyntaxTree.Value THEN
  5429. result.SetResolved(parameter0(SyntaxTree.Value));
  5430. END;
  5431. END;
  5432. result.SetAssignable(parameter1.assignable);
  5433. END;
  5434. END;
  5435. (* ---- SYSTEM.GET ----- *)
  5436. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5437. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5438. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5439. actualParameters.SetExpression(0,parameter0);
  5440. END;
  5441. (* ---- SYSTEM.PUT ----- *)
  5442. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5443. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5444. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5445. actualParameters.SetExpression(0,parameter0);
  5446. END;
  5447. (* ---- SYSTEM.PUT64 ----- *)
  5448. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5449. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5450. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5451. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5452. actualParameters.SetExpression(0,parameter0);
  5453. actualParameters.SetExpression(1,parameter1);
  5454. END;
  5455. (* ---- SYSTEM.PUT32 ----- *)
  5456. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5457. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5458. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5459. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5460. actualParameters.SetExpression(0,parameter0);
  5461. actualParameters.SetExpression(1,parameter1);
  5462. END;
  5463. (* ---- SYSTEM.PUT16 ----- *)
  5464. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5465. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5466. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5467. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5468. actualParameters.SetExpression(0,parameter0);
  5469. actualParameters.SetExpression(1,parameter1);
  5470. END;
  5471. (* ---- SYSTEM.PUT8 ----- *)
  5472. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5473. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5474. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5475. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5476. actualParameters.SetExpression(0,parameter0);
  5477. actualParameters.SetExpression(1,parameter1);
  5478. END;
  5479. (* ---- SYSTEM.MOVE ----- *)
  5480. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5481. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5482. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5483. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5484. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5485. actualParameters.SetExpression(0,parameter0);
  5486. actualParameters.SetExpression(1,parameter1);
  5487. actualParameters.SetExpression(2,parameter2);
  5488. END;
  5489. (* ---- SYSTEM.NEW ----- *)
  5490. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5491. IF ~IsPointerType(parameter0.type) THEN
  5492. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5493. ELSIF CheckSizeType(parameter1) THEN
  5494. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5495. actualParameters.SetExpression(1,parameter1);
  5496. END;
  5497. (* ----SYSTEM.REF ---- *)
  5498. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5499. type := system.addressType
  5500. (* ---- INCR ----- *)
  5501. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5502. type := system.sizeType;
  5503. base := type0;
  5504. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5505. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5506. IF i1 < 0 THEN
  5507. Error(position,Diagnostics.Invalid,"invalid dimension");
  5508. base := SyntaxTree.invalidType;
  5509. ELSE
  5510. base := ArrayBase(base,i1);
  5511. IF (base # NIL) & Indexable(base) THEN
  5512. ELSE
  5513. Error(position,Diagnostics.Invalid,"len on no array");
  5514. IF VerboseErrorMessage THEN
  5515. Printout.Info("base",base);
  5516. END;
  5517. base := SyntaxTree.invalidType;
  5518. END;
  5519. END;
  5520. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5521. actualParameters.SetExpression(1,parameter1);
  5522. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5523. mathArrayType := base(SyntaxTree.MathArrayType);
  5524. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5525. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5526. type := system.longintType;
  5527. END;
  5528. END;
  5529. ELSE
  5530. type := system.longintType;
  5531. END;
  5532. (* ---- SUM ----- *)
  5533. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5534. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5535. (* ---- ALL ----- *)
  5536. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5537. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5538. (* ---- DIM ----- *)
  5539. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5540. type := system.sizeType;
  5541. IF type0 IS SyntaxTree.MathArrayType THEN
  5542. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5543. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5544. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5545. END;
  5546. ELSE
  5547. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5548. END;
  5549. (* ---- CAS ----- *)
  5550. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5551. IF type0.IsComposite () THEN
  5552. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5553. result := SyntaxTree.invalidExpression;
  5554. ELSIF ~IsVariable (parameter0) THEN
  5555. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5556. result := SyntaxTree.invalidExpression;
  5557. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5558. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5559. result := SyntaxTree.invalidExpression;
  5560. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5561. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5562. result := SyntaxTree.invalidExpression;
  5563. ELSE
  5564. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5565. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5566. type := type0;
  5567. END;
  5568. (* ---- RESHAPE ----- *)
  5569. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5570. IF type0 IS SyntaxTree.MathArrayType THEN
  5571. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5572. base := ArrayBase(type0,MAX(LONGINT));
  5573. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5574. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5575. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5576. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5577. IF ~CompatibleTo(system,type0,parameterType) THEN
  5578. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5579. result := SyntaxTree.invalidExpression;
  5580. ELSE
  5581. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5582. END;
  5583. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5584. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5585. IF ~CompatibleTo(system,type1,parameterType) THEN
  5586. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5587. result := SyntaxTree.invalidExpression;
  5588. ELSE
  5589. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5590. END;
  5591. ELSE
  5592. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5593. result := SyntaxTree.invalidExpression;
  5594. END;
  5595. (* ---- SYSTEM.TYPECODE ----- *)
  5596. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5597. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5598. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5599. type := type.resolved;
  5600. IF type IS SyntaxTree.PointerType THEN
  5601. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5602. END;
  5603. IF ~(type IS SyntaxTree.RecordType) THEN
  5604. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5605. END;
  5606. ELSE
  5607. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5608. END;
  5609. type := system.addressType;
  5610. (* -------- FLT --------- *)
  5611. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5612. type := system.realType;
  5613. IF IsRealValue(parameter0, r) THEN
  5614. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5615. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5616. i0 := i; i := ABS(i);
  5617. IF i # 0 THEN
  5618. i1 := 23;
  5619. IF i >= 2*800000H THEN
  5620. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5621. ELSIF i < 800000H THEN
  5622. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5623. END;
  5624. i := (i1 + 127)*800000H - 800000H + i;
  5625. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5626. END;
  5627. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5628. END;
  5629. (* ------- CONNECT -------*)
  5630. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5631. (*IF ~(currentIsCellNet) THEN
  5632. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5633. END;*)
  5634. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5635. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5636. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5637. END;
  5638. IF numberActualParameters = 3 THEN
  5639. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5640. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5641. END;
  5642. *)
  5643. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5644. actualParameters.SetExpression(2,parameter2);
  5645. END;
  5646. activeCellsStatement := TRUE;
  5647. (* ---------- DELEGATE --------*)
  5648. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5649. (*
  5650. IF ~(currentIsCellNet) THEN
  5651. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5652. END;
  5653. *)
  5654. IF ~CheckPortType(parameter1, inPort) THEN
  5655. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5656. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5657. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5658. ELSIF (outPort.direction # inPort.direction) THEN
  5659. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5660. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5661. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5662. END;
  5663. activeCellsStatement := TRUE;
  5664. (* --------- RECEIVE ---------*)
  5665. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5666. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5667. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5668. IF inPort.direction # SyntaxTree.InPort THEN
  5669. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5670. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5671. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5672. END;
  5673. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5674. IF ~SameType(parameter2.type, system.integerType) THEN
  5675. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5676. END;
  5677. END;
  5678. END;
  5679. (* --------- SEND ---------*)
  5680. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5681. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5682. IF CheckPortType(parameter0,outPort) THEN
  5683. IF outPort.direction # SyntaxTree.OutPort THEN
  5684. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5685. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5686. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5687. ELSE
  5688. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5689. actualParameters.SetExpression(1,parameter1);
  5690. END;
  5691. END;
  5692. (* ------- custom builtins ----- *)
  5693. ELSIF id = Global.systemSpecial THEN
  5694. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5695. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5696. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5697. type := procedureType.returnType;
  5698. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5699. (* go through all formal parameters *)
  5700. formalParameter := procedureType.firstParameter;
  5701. FOR i := 0 TO actualParameters.Length() - 1 DO
  5702. actualParameter := actualParameters.GetExpression(i);
  5703. IF actualParameter = SyntaxTree.invalidExpression THEN
  5704. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5705. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5706. ELSE
  5707. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5708. END;
  5709. actualParameters.SetExpression(i, actualParameter);
  5710. formalParameter := formalParameter.nextParameter
  5711. END
  5712. END
  5713. ELSE
  5714. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5715. result := SyntaxTree.invalidExpression;
  5716. END;
  5717. END;
  5718. IF result # SyntaxTree.invalidExpression THEN
  5719. type := ResolveType(type);
  5720. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5721. result.SetType(type);
  5722. END;
  5723. RETURN result
  5724. END NewBuiltinCallDesignator;
  5725. (** return type guard designator left(type)
  5726. - check if type can be extended (i.e. is no static record)
  5727. - check if type is a type extension of left.type
  5728. - returns new type guard designator
  5729. returns invalidDesignator = invalidExpression if error
  5730. **)
  5731. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5732. VAR result: SyntaxTree.Designator;
  5733. BEGIN
  5734. result := SyntaxTree.invalidDesignator;
  5735. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5736. Error(position,Diagnostics.Invalid,"no type extension of type");
  5737. IF VerboseErrorMessage THEN
  5738. Printout.Info("left",left);
  5739. Printout.Info("type",type);
  5740. END;
  5741. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5742. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5743. ELSIF IsUnsafePointer(left.type) THEN
  5744. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5745. ELSE
  5746. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5747. result.SetType(type);
  5748. result.SetAssignable(left.assignable);
  5749. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5750. END;
  5751. RETURN result
  5752. END NewTypeGuardDesignator;
  5753. (** check and resolve parameter designator left(expression list)
  5754. - check expression list
  5755. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5756. - elsif left is a procedure type then
  5757. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5758. - else return is a procedure call then return ProcedureCallDesignator
  5759. returns invalidDesignator = invalidExpression if error
  5760. **)
  5761. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5762. VAR
  5763. parameters: SyntaxTree.ExpressionList;
  5764. left: SyntaxTree.Designator;
  5765. result,expression: SyntaxTree.Expression;
  5766. typeDeclaration: SyntaxTree.TypeDeclaration;
  5767. type, expressionType: SyntaxTree.Type;
  5768. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5769. BEGIN
  5770. type := type.resolved;
  5771. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5772. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5773. END;
  5774. RETURN type
  5775. END BaseType;
  5776. BEGIN
  5777. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5778. result := SyntaxTree.invalidDesignator;
  5779. left := ResolveDesignator(designator.left);
  5780. IF left # SyntaxTree.invalidDesignator THEN
  5781. parameters := designator.parameters;
  5782. IF ExpressionList(parameters) THEN
  5783. IF (left.type = NIL) THEN
  5784. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5785. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5786. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5787. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5788. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5789. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5790. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5791. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5792. ELSE
  5793. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5794. END
  5795. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5796. expression := parameters.GetExpression(0);
  5797. type := typeDeclaration.declaredType.resolved;
  5798. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5799. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5800. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5801. OR (expressionType IS SyntaxTree.EnumerationType)
  5802. ) THEN
  5803. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5804. ELSE
  5805. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5806. END;
  5807. ELSE
  5808. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5809. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5810. result := SyntaxTree.invalidDesignator;
  5811. END;
  5812. ELSE
  5813. result := SyntaxTree.invalidDesignator
  5814. END;
  5815. END;
  5816. resolvedExpression := result;
  5817. END VisitParameterDesignator;
  5818. (** check dereference designator left^
  5819. - check if left is pointer type or left is object type
  5820. - return new dereference designator with type = left.baseType.type (if appropriate)
  5821. with error handling
  5822. returns invalidDesignator = invalidExpression if error
  5823. **)
  5824. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5825. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5826. BEGIN
  5827. result := SyntaxTree.invalidDesignator;
  5828. type := left.type;
  5829. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5830. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5831. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5832. result.SetAssignable(TRUE);
  5833. result.SetType(type);
  5834. result.SetHidden(left.isHidden);
  5835. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5836. type := type.resolved;
  5837. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5838. result.SetAssignable(TRUE);
  5839. result.SetType(type);
  5840. result.SetHidden(left.isHidden);
  5841. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5842. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5843. result.SetAssignable(TRUE);
  5844. result.SetType(type);
  5845. result.SetHidden(left.isHidden);
  5846. ELSE
  5847. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5848. IF VerboseErrorMessage THEN
  5849. Printout.Info("pointer", type);
  5850. Printout.Info("scope", currentScope);
  5851. END;
  5852. END;
  5853. RETURN result
  5854. END NewDereferenceDesignator;
  5855. (** check supercall designator left^
  5856. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5857. - return new supercall designator with type = left.type
  5858. with error handling
  5859. **)
  5860. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5861. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5862. objectScope: SyntaxTree.Scope;
  5863. BEGIN
  5864. result := SyntaxTree.invalidDesignator;
  5865. IF left = SyntaxTree.invalidDesignator THEN
  5866. (* error already handled *)
  5867. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5868. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5869. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5870. IF symbol IS SyntaxTree.Procedure THEN
  5871. procedure := symbol(SyntaxTree.Procedure);
  5872. objectScope := currentScope;
  5873. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5874. objectScope := objectScope.outerScope;
  5875. END;
  5876. IF (left.left = NIL) OR ~
  5877. (
  5878. (left.left IS SyntaxTree.SelfDesignator) OR
  5879. (left.left IS SyntaxTree.DereferenceDesignator)
  5880. & (left.left(SyntaxTree.Designator).left # NIL)
  5881. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5882. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5883. IF VerboseErrorMessage THEN
  5884. Printout.Info("left.left",left.left);
  5885. END;
  5886. ELSIF procedure.super # NIL THEN
  5887. result := SyntaxTree.NewSupercallDesignator(position,left);
  5888. result.SetType(left.type.resolved)
  5889. ELSE
  5890. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5891. END;
  5892. ELSE
  5893. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5894. END;
  5895. ELSE
  5896. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5897. END;
  5898. RETURN result
  5899. END NewSupercallDesignator;
  5900. (** check and semantically resolve arrow designator left^
  5901. - if left is procedure type -> result := SupercallDesignator
  5902. - else result := DereferenceDesignator
  5903. returns result via global variable resolvedExpression
  5904. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5905. **)
  5906. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5907. VAR left: SyntaxTree.Designator;
  5908. BEGIN
  5909. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5910. left := ResolveDesignator(arrowDesignator.left);
  5911. IF left # NIL THEN
  5912. IF (left.type = NIL) THEN
  5913. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5914. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5915. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5916. ELSE
  5917. IF IsPointerToObject(left.type) THEN
  5918. Warning(arrowDesignator.position, "forbidden dereference on object");
  5919. END;
  5920. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5921. END
  5922. END
  5923. END VisitArrowDesignator;
  5924. (** check and return expression
  5925. - if expression has no type then resolve expression
  5926. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5927. - return result
  5928. **)
  5929. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5930. VAR result,prev: SyntaxTree.Expression;
  5931. BEGIN
  5932. IF expression = NIL THEN result := NIL
  5933. ELSIF (expression.type = NIL) THEN
  5934. prev := resolvedExpression;
  5935. resolvedExpression := SyntaxTree.invalidExpression;
  5936. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5937. expression.SetType(SyntaxTree.invalidType);
  5938. END;
  5939. expression.Accept(SELF);
  5940. result := resolvedExpression;
  5941. IF currentIsRealtime THEN
  5942. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5943. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5944. END;
  5945. END;
  5946. (* designator modifiers for backends if they support it ...*)
  5947. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5948. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5949. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5950. END;
  5951. resolvedExpression := prev
  5952. ELSE
  5953. result := expression
  5954. END;
  5955. RETURN result
  5956. END ResolveExpression;
  5957. (**
  5958. check expression to be constant expression
  5959. - resolve expression
  5960. - if valid then check that of value type
  5961. report error and return invalidExpression if anything fails
  5962. **)
  5963. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5964. VAR position: Position;
  5965. BEGIN
  5966. position := expression.position;
  5967. expression := ResolveExpression(expression);
  5968. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5969. ELSIF (expression.resolved = NIL) THEN
  5970. Error(position,Diagnostics.Invalid,"expression is not constant");
  5971. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5972. expression := SyntaxTree.invalidExpression;
  5973. END;
  5974. RETURN expression
  5975. END ConstantExpression;
  5976. (** check expression to be constant integer
  5977. - resolve expresssion
  5978. - if valid then check that of integer value type
  5979. report error and return invalidExpression if anything fails
  5980. **)
  5981. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5982. VAR position: Position;
  5983. BEGIN
  5984. position := expression.position;
  5985. expression := ResolveExpression(expression);
  5986. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5987. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5988. expression := SyntaxTree.invalidExpression;
  5989. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5990. END;
  5991. RETURN expression
  5992. END ConstantInteger;
  5993. (** check expression as positive (>=0) constant integer
  5994. - resolve expression
  5995. - if valid then check that integer value
  5996. - if integer value then check that value >= 0
  5997. report error and return invalidExpression if anything fails
  5998. **)
  5999. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6000. VAR position: Position;
  6001. BEGIN
  6002. position := expression.position;
  6003. expression := ConstantExpression(expression);
  6004. IF expression = SyntaxTree.invalidExpression THEN
  6005. (* error already reported *)
  6006. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6007. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  6008. expression := SyntaxTree.invalidExpression
  6009. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6010. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6011. END;
  6012. RETURN expression
  6013. END ConstantIntegerGeq0;
  6014. (** check expression as condition
  6015. - resolve expression
  6016. - if valid expression then check that result type is boolean
  6017. report error and return invalidExpression if anything fails
  6018. **)
  6019. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6020. VAR position: Position;
  6021. BEGIN
  6022. position := expression.position;
  6023. expression := ResolveExpression(expression);
  6024. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6025. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6026. expression := SyntaxTree.invalidExpression;
  6027. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6028. END;
  6029. RETURN expression
  6030. END ResolveCondition;
  6031. (*** symbols ***)
  6032. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6033. BEGIN
  6034. x.Accept(SELF);
  6035. END ResolveSymbol;
  6036. (** check a symbol
  6037. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6038. **)
  6039. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6040. VAR scope: SyntaxTree.Scope;
  6041. BEGIN
  6042. (* visibility *)
  6043. scope := symbol.scope;
  6044. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6045. scope := scope.outerScope;
  6046. END;
  6047. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6048. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6049. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6050. IF VerboseErrorMessage THEN
  6051. Printout.Info("symbol",symbol);
  6052. END;
  6053. END;
  6054. END;
  6055. END CheckSymbolVisibility;
  6056. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6057. If node is currently being resolved then emit a cyclic definition error.
  6058. Return TRUE only if node is fully resolved.
  6059. **)
  6060. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6061. VAR result: BOOLEAN;
  6062. BEGIN
  6063. IF SyntaxTree.Resolved IN x.state THEN
  6064. result := FALSE
  6065. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6066. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6067. result := FALSE;
  6068. ELSE
  6069. result := TRUE;
  6070. x.SetState(SyntaxTree.BeingResolved)
  6071. END;
  6072. RETURN result
  6073. END SymbolNeedsResolution;
  6074. (** check and resolve a type declaration symbol = Type
  6075. - set type to declaration type
  6076. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6077. This is so because the type declaration itself does not have a type but it only stands for a type.
  6078. In the implementation of the compiler this made a lot much easier.
  6079. - resolve and set declared type
  6080. - check symbol
  6081. **)
  6082. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6083. VAR prevScope: SyntaxTree.Scope;
  6084. BEGIN
  6085. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6086. IF SymbolNeedsResolution(typeDeclaration) THEN
  6087. prevScope := currentScope;
  6088. currentScope := typeDeclaration.scope;
  6089. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6090. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6091. CheckSymbolVisibility(typeDeclaration);
  6092. typeDeclaration.SetState(SyntaxTree.Resolved);
  6093. currentScope := prevScope;
  6094. END;
  6095. END VisitTypeDeclaration;
  6096. (** check and resolve a constant declaration symbol = (constant) expression
  6097. - check expression
  6098. - set type and value
  6099. - check symbol
  6100. **)
  6101. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6102. VAR
  6103. expression: SyntaxTree.Expression;
  6104. type: SyntaxTree.Type;
  6105. name: Basic.SegmentedName;
  6106. replacement: Replacement;
  6107. BEGIN
  6108. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6109. IF SymbolNeedsResolution(constant) THEN
  6110. expression := constant.value;
  6111. IF replacements # NIL THEN
  6112. Global.GetSymbolSegmentedName(constant, name);
  6113. replacement := replacements;
  6114. WHILE (replacement # NIL) & (replacement.name # name) DO
  6115. replacement := replacement.next;
  6116. END;
  6117. IF replacement # NIL THEN
  6118. InfoSS(constant.position, "replacing constant", constant.name);
  6119. (*
  6120. NEW(stringReader, Strings.Length(replacement.string^));
  6121. stringReader.Set(replacement.string^);
  6122. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6123. NEW(parser, scanner, diagnostics);
  6124. expression := parser.Expression();
  6125. *)
  6126. expression := replacement.expression;
  6127. replacement.used := TRUE;
  6128. END;
  6129. END;
  6130. constant.SetType(SyntaxTree.invalidType);
  6131. expression := ConstantExpression(expression);
  6132. ASSERT(expression.type # NIL);
  6133. type := expression.type.resolved;
  6134. constant.SetType(type);
  6135. constant.SetValue(expression);
  6136. CheckSymbolVisibility(constant);
  6137. constant.SetState(SyntaxTree.Resolved);
  6138. END;
  6139. END VisitConstant;
  6140. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6141. VAR procedureAlignment: LONGINT;
  6142. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6143. (* least common multiple *)
  6144. VAR a,b: LONGINT;
  6145. BEGIN
  6146. a := a0; b := b0;
  6147. WHILE (a # b) DO
  6148. IF a < b THEN a := a+a0
  6149. ELSE b := b + b0
  6150. END;
  6151. END;
  6152. RETURN a
  6153. END LCM;
  6154. BEGIN
  6155. IF alignment > 1 THEN
  6156. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6157. IF (procedureAlignment > 1) THEN
  6158. alignment := LCM(alignment, procedureAlignment);
  6159. END;
  6160. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6161. END;
  6162. END AdaptStackAlignment;
  6163. (** check and resolve a variable / field
  6164. - check and set type
  6165. - negative check on open array type
  6166. - check symbol
  6167. **)
  6168. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6169. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6170. BEGIN
  6171. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6172. IF SymbolNeedsResolution(variable) THEN
  6173. modifiers := variable.modifiers;
  6174. (*
  6175. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6176. variable.AddFlags(flags);
  6177. *)
  6178. variable.SetType(ResolveType(variable.type));
  6179. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6180. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6181. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6182. END;
  6183. END;
  6184. CheckSymbolVisibility(variable);
  6185. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6186. variable.SetUntraced(TRUE);
  6187. IF ~ContainsPointer(variable.type) THEN
  6188. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6189. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6190. END;
  6191. END;
  6192. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6193. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6194. IF ~PowerOf2(value) THEN
  6195. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6196. ELSE
  6197. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6198. END;
  6199. END;
  6200. variable.SetAlignment(FALSE,value);
  6201. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6202. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6203. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6204. END;
  6205. variable.SetAlignment(TRUE, value);
  6206. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6207. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6208. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6209. END;
  6210. variable.SetFictive(value);
  6211. variable.SetOffset(value*system.dataUnit);
  6212. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6213. END;
  6214. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6215. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6216. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6217. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6218. END;
  6219. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6220. IF variable.initializer # NIL THEN
  6221. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6222. END;
  6223. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6224. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6225. pointerType.SetPointerBase(variable.type);
  6226. pointerType.SetHidden(TRUE);
  6227. variable.SetType(ResolveType(pointerType));
  6228. END;
  6229. variable.SetState(SyntaxTree.Resolved);
  6230. END;
  6231. END VisitVariable;
  6232. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6233. BEGIN
  6234. VisitVariable(property)
  6235. END VisitProperty;
  6236. (** check and resolve a (procedure) parameter
  6237. - check and set type
  6238. - check symbol
  6239. - check parameter kind and set read-only flags if appropriate
  6240. **)
  6241. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6242. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6243. BEGIN
  6244. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6245. IF SymbolNeedsResolution(parameter) THEN
  6246. modifiers := parameter.modifiers;
  6247. parameter.SetType(ResolveType(parameter.type));
  6248. ASSERT(parameter.type.resolved # NIL);
  6249. CheckSymbolVisibility(parameter);
  6250. IF parameter.defaultValue # NIL THEN
  6251. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6252. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6253. ELSE
  6254. expression := ConstantExpression(parameter.defaultValue);
  6255. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6256. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6257. parameter.SetDefaultValue(expression);
  6258. END;
  6259. END;
  6260. END;
  6261. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6262. parameter.SetUntraced(TRUE);
  6263. IF ~ContainsPointer(parameter.type) THEN
  6264. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6265. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6266. END;
  6267. END;
  6268. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6269. parameter.SetMoveable(TRUE);
  6270. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6271. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6272. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6273. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6274. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6275. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6276. END;
  6277. END;
  6278. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6279. parameter.SetState(SyntaxTree.Resolved);
  6280. END;
  6281. END VisitParameter;
  6282. (** check and resolve a procedure (with declaration and implementation scope)
  6283. - check the procedure type
  6284. - check if method (i.e. in record scope), if so then
  6285. - check if (unique) constructor
  6286. - check if (unique) finalizer
  6287. - check if super method available, if so then check signature
  6288. - of not in record scope then negative check on constructor flag
  6289. - of not in record scope then negative check on finalizer flag
  6290. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6291. - check procedure symbol
  6292. **)
  6293. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6294. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6295. procedureType: SyntaxTree.ProcedureType;
  6296. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6297. qualifiedType: SyntaxTree.QualifiedType;
  6298. value: LONGINT;
  6299. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6300. position: Position;
  6301. BEGIN
  6302. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6303. IF IsOberonInline(procedure) THEN
  6304. IF SyntaxTree.Public * procedure.access # {} THEN
  6305. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6306. END;
  6307. procedure.SetInline(FALSE);
  6308. procedure.SetOberonInline(TRUE);
  6309. END;
  6310. IF SymbolNeedsResolution(procedure) THEN
  6311. recentIsRealtime := currentIsRealtime;
  6312. recentIsBodyProcedure := currentIsBodyProcedure;
  6313. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6314. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6315. modifiers := procedureType.modifiers;
  6316. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6317. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6318. IF useDarwinCCalls THEN (*fld*)
  6319. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6320. ELSE
  6321. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6322. END
  6323. END;
  6324. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6325. procedureType.SetInterrupt(TRUE);
  6326. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6327. END;
  6328. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6329. procedureType.SetNoReturn(TRUE);
  6330. END;
  6331. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6332. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6333. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6334. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6335. END;
  6336. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6337. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6338. END;
  6339. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6340. IF ~PowerOf2(value) THEN
  6341. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6342. ELSE
  6343. procedureType.SetStackAlignment(value)
  6344. END;
  6345. END;
  6346. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6347. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6348. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6349. END;
  6350. CheckModifiers(modifiers, TRUE);
  6351. modifiers := procedureType.returnTypeModifiers;
  6352. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6353. CheckModifiers(modifiers, TRUE);
  6354. FixProcedureType(procedureType);
  6355. currentIsRealtime := procedureType.isRealtime;
  6356. currentIsBodyProcedure := procedure.isBodyProcedure;
  6357. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6358. THEN
  6359. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6360. END;
  6361. CheckSymbolVisibility(procedure);
  6362. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6363. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6364. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6365. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6366. END;
  6367. END;
  6368. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6369. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6370. procedureType.SetDelegate(TRUE);
  6371. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6372. IF record.pointerType.typeDeclaration = NIL THEN
  6373. selfParameter.SetType(record.pointerType);
  6374. ELSE
  6375. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6376. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6377. qualifiedType.SetResolved(record.pointerType);
  6378. selfParameter.SetType(qualifiedType);
  6379. END;
  6380. selfParameter.SetAccess(SyntaxTree.Hidden);
  6381. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6382. IF procedure.isConstructor THEN
  6383. (*! constructor is always visible, compatibility to paco
  6384. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6385. *)
  6386. procedure.MarkUsed;
  6387. IF procedureType.returnType # NIL THEN
  6388. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6389. END;
  6390. proc := procedure.scope.firstProcedure;
  6391. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6392. proc := proc.nextProcedure;
  6393. END;
  6394. IF proc # NIL THEN
  6395. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6396. ELSE
  6397. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6398. END;
  6399. END;
  6400. IF procedure.isFinalizer THEN
  6401. procedure.MarkUsed;
  6402. IF procedureType.returnType # NIL THEN
  6403. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6404. END;
  6405. IF procedureType.numberParameters # 0 THEN
  6406. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6407. END;
  6408. proc := procedure.scope.firstProcedure;
  6409. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6410. proc := proc.nextProcedure;
  6411. END;
  6412. IF proc # NIL THEN
  6413. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6414. ELSE
  6415. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6416. END;
  6417. END;
  6418. super := FindSuperProcedure(record.recordScope, procedure);
  6419. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6420. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6421. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6422. END;
  6423. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6424. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6425. END;
  6426. IF super.isFinal THEN
  6427. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6428. END;
  6429. procedure.SetSuper(super);
  6430. super.SetOverwritten(TRUE);
  6431. procedure.SetAccess(procedure.access+super.access);
  6432. procedure.MarkUsed;
  6433. END;
  6434. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6435. THEN
  6436. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6437. END;
  6438. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6439. IF cellsAreObjects THEN
  6440. procedureType.SetDelegate(TRUE);
  6441. END;
  6442. IF procedure.isConstructor THEN
  6443. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6444. END;
  6445. ELSIF procedure.isConstructor THEN
  6446. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6447. END;
  6448. Declarations(procedure.procedureScope, FALSE);
  6449. (* body resolution part done as late fix of the procedure type *)
  6450. procedure.SetState(SyntaxTree.Resolved);
  6451. currentIsRealtime := recentIsRealtime;
  6452. currentIsBodyProcedure := recentIsBodyProcedure;
  6453. END;
  6454. END VisitProcedure;
  6455. (**
  6456. a builtin procedure is a global item that may not be modified locally
  6457. instead the resolving of builtin procedure calls are done in the esignator
  6458. **)
  6459. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6460. VAR type: SyntaxTree.Type;
  6461. BEGIN
  6462. type := ResolveType(builtinProcedure.type);
  6463. END VisitBuiltin;
  6464. (* nopov *)
  6465. (** check and resolve operator
  6466. - operators are first checked as procedures
  6467. - then additional operator-specific checks are done
  6468. - note that only module-scope operators are checked here
  6469. (operators in a record scope are only allowed in the context of
  6470. array-structured object types and checked in 'ResolveArrayStructure')
  6471. - also note that inter-operator conformity is not checked here
  6472. **)
  6473. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6474. VAR
  6475. procedureType: SyntaxTree.ProcedureType;
  6476. leftType, rightType: SyntaxTree.Type;
  6477. identifierNumber: LONGINT; position: Position;
  6478. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6479. modifiers: SyntaxTree.Modifier;
  6480. (** whether a type is locally defined in the current module scope
  6481. for arrays, the base type must be locally defined **)
  6482. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6483. BEGIN
  6484. IF type = NIL THEN
  6485. RETURN FALSE
  6486. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6487. RETURN TRUE
  6488. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6489. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6490. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6491. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6492. ELSE
  6493. RETURN FALSE
  6494. END
  6495. END IsLocallyDefined;
  6496. BEGIN
  6497. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6498. procedureType := operator.type(SyntaxTree.ProcedureType);
  6499. modifiers := procedureType.modifiers;
  6500. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6501. CheckModifiers(modifiers, TRUE);
  6502. VisitProcedure(operator);
  6503. IF operator.scope IS SyntaxTree.RecordScope THEN
  6504. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6505. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6506. IF identifierNumber = -1 THEN
  6507. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6508. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6509. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6510. ELSE
  6511. IF procedureType.numberParameters < 1 THEN
  6512. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6513. ELSIF procedureType.numberParameters > 2 THEN
  6514. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6515. ELSE
  6516. (* determine operand types *)
  6517. leftType := procedureType.firstParameter.type;
  6518. IF procedureType.numberParameters > 1 THEN
  6519. rightType := procedureType.firstParameter.nextParameter.type
  6520. ELSE
  6521. rightType := NIL
  6522. END;
  6523. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6524. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6525. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6526. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6527. END
  6528. END;
  6529. (* TODO: refine the checks, think about how restrictive the checks should be
  6530. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6531. They might be used for intersection, union, complement of custom object types *)
  6532. (* defaults *)
  6533. hasReturnType := TRUE;
  6534. mustBeUnary := FALSE;
  6535. mustBeBinary := FALSE;
  6536. mustReturnBoolean := FALSE;
  6537. mustReturnInteger := FALSE;
  6538. mustHaveEquitypedOperands := FALSE;
  6539. (* operator-specific exceptions *)
  6540. CASE identifierNumber OF
  6541. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6542. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6543. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6544. mustBeBinary := TRUE
  6545. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6546. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6547. | Scanner.Times: mustBeBinary := TRUE
  6548. | Scanner.TimesTimes: mustBeBinary := TRUE
  6549. | Scanner.DotTimes: mustBeBinary := TRUE
  6550. | Scanner.PlusTimes: mustBeBinary := TRUE
  6551. | Scanner.Slash: mustBeBinary := TRUE
  6552. | Scanner.Backslash: mustBeBinary := TRUE
  6553. | Scanner.DotSlash: mustBeBinary := TRUE
  6554. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6555. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6556. | Scanner.Not: mustBeUnary := TRUE
  6557. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6558. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6559. | Scanner.Transpose: mustBeUnary := TRUE;
  6560. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6561. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6562. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6563. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6564. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6565. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6566. | Global.Abs: mustBeUnary := TRUE;
  6567. | Global.Ash: (* TODO: arity? *)
  6568. | Global.Cap: (* TODO: arity? *)
  6569. | Global.Chr: mustBeUnary := TRUE;
  6570. | Global.Entier: (* TODO: arity? *)
  6571. | Global.EntierH: (* TODO: arity? *)
  6572. | Global.Len: (* unary and binary *)
  6573. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6574. | Global.Max, Global.Min: (* unary and binary *)
  6575. | Global.Odd: (* TODO: arity? *)
  6576. | Global.Sum: (* TODO: arity? *)
  6577. | Global.All: (* TODO: arity? *)
  6578. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6579. | Scanner.Alias:
  6580. | Scanner.GreaterGreater, Scanner.LessLess:
  6581. mustBeBinary := TRUE; hasReturnType := FALSE;
  6582. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6583. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6584. END;
  6585. (* check parameter count *)
  6586. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6587. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6588. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6589. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6590. END;
  6591. (* check parameter types *)
  6592. (* TODO: is this used at all? *)
  6593. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6594. leftType := procedureType.firstParameter.type;
  6595. rightType := procedureType.firstParameter.nextParameter.type;
  6596. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6597. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6598. END
  6599. END;
  6600. (* check return type *)
  6601. IF hasReturnType THEN
  6602. IF procedureType.returnType = NIL THEN
  6603. Error(operator.position, Diagnostics.Invalid, "return type required")
  6604. ELSIF mustReturnBoolean THEN
  6605. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6606. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6607. END
  6608. ELSIF mustReturnInteger THEN
  6609. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6610. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6611. END
  6612. END
  6613. ELSIF procedureType.returnType # NIL THEN
  6614. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6615. END
  6616. END
  6617. END
  6618. END
  6619. END VisitOperator;
  6620. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6621. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6622. BEGIN
  6623. IF error THEN RETURN FALSE END;
  6624. prevScope := currentScope;
  6625. prevDiagnostics := diagnostics;
  6626. diagnostics := NIL; (* suppress error output *)
  6627. currentScope := module.moduleScope;
  6628. VisitImport(x);
  6629. IF ~error THEN
  6630. module.moduleScope.AddImport(x);
  6631. x.SetScope(module.moduleScope);
  6632. END;
  6633. currentScope := prevScope;
  6634. diagnostics := prevDiagnostics;
  6635. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6636. END AddImport;
  6637. (** check and resolve import
  6638. - check for name = SYSTEM
  6639. - check for forbidden self import
  6640. - search through global import cache: already imported?
  6641. - check if already imported indirectly
  6642. - import if necessary -> set module and enter into import cache
  6643. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6644. - after this import this direct import and all indirect imports are stored in the current module's import list
  6645. **)
  6646. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6647. VAR
  6648. module: SyntaxTree.Module;
  6649. moduleScope: SyntaxTree.ModuleScope;
  6650. import,reimport: SyntaxTree.Import;
  6651. filename: FileName;
  6652. prevScope: SyntaxTree.Scope;
  6653. BEGIN
  6654. IF SymbolNeedsResolution(x) THEN
  6655. prevScope := currentScope;
  6656. x.SetType(SyntaxTree.importType);
  6657. moduleScope := currentScope.ownerModule.moduleScope;
  6658. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6659. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6660. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6661. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6662. ELSE
  6663. (* search through global import list: already imported ? *)
  6664. IF (x.module = NIL) & (importCache # NIL) THEN
  6665. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6666. ELSE import := NIL
  6667. END;
  6668. IF x.module # NIL THEN (* already imported indirectly *)
  6669. module := x.module;
  6670. ELSIF import # NIL THEN (* already in module list *)
  6671. module := import.module;
  6672. ASSERT(module # NIL);
  6673. x.SetModule(module);
  6674. ELSE (* must be imported *)
  6675. Global.ModuleFileName(x.moduleName,x.context,filename);
  6676. IF symbolFileFormat # NIL THEN
  6677. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6678. IF module = NIL THEN
  6679. ErrorSS(x.position,"could not import",filename);
  6680. IF VerboseErrorMessage THEN
  6681. Printout.Info("import",x)
  6682. END
  6683. ELSE
  6684. (*
  6685. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6686. (*! should rather be done by importer *)
  6687. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6688. checker.importCache := importCache;
  6689. checker.arrayBaseImported := arrayBaseImported;
  6690. checker.global := global;
  6691. checker.Module(module); (* semantic check *)
  6692. error := error OR checker.error;
  6693. END;
  6694. *)
  6695. (*
  6696. ASSERT(SyntaxTree.Resolved IN module.state);
  6697. *)
  6698. x.SetModule(module);
  6699. IF importCache # NIL THEN
  6700. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6701. import.SetContext(x.context);
  6702. import.SetModule(module);
  6703. importCache.AddImport(import);
  6704. END;
  6705. END;
  6706. ELSE
  6707. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6708. END;
  6709. END;
  6710. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6711. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6712. END;
  6713. import := module.moduleScope.firstImport;
  6714. WHILE(import # NIL) DO
  6715. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6716. ASSERT(currentScope # NIL);
  6717. ASSERT(currentScope.ownerModule # NIL);
  6718. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6719. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6720. Error(x.position,Diagnostics.Invalid,"recursive import");
  6721. ELSE
  6722. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6723. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6724. IF reimport = NIL THEN (* indirect import *)
  6725. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6726. reimport.SetContext(import.context);
  6727. reimport.SetModule(import.module);
  6728. moduleScope.AddImport(reimport);
  6729. reimport.SetScope(moduleScope);
  6730. ELSE
  6731. ASSERT(import.module # NIL);
  6732. reimport.SetModule(import.module); (* direct or indirect import *)
  6733. END;
  6734. END;
  6735. import := import.nextImport;
  6736. END;
  6737. END;
  6738. END;
  6739. currentScope := prevScope;
  6740. (* ELSE nothing to be done *)
  6741. x.SetState(SyntaxTree.Resolved);
  6742. END;
  6743. END VisitImport;
  6744. (*** statements ***)
  6745. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6746. VAR prev,resolved: SyntaxTree.Statement;
  6747. BEGIN
  6748. prev := resolvedStatement;
  6749. resolvedStatement := x;
  6750. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6751. activeCellsStatement := FALSE;
  6752. x.Accept(SELF);
  6753. (* removed this, implementation restriction should be resolved by backend
  6754. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6755. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6756. END;
  6757. *)
  6758. resolved := resolvedStatement;
  6759. resolvedStatement := prev;
  6760. RETURN resolved
  6761. END ResolveStatement;
  6762. (** check and resolve statement sequence
  6763. - check all statements, replace if necessary
  6764. **)
  6765. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6766. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6767. BEGIN
  6768. IF statementSequence # NIL THEN (* else empty *)
  6769. FOR i := 0 TO statementSequence.Length()-1 DO
  6770. statement := statementSequence.GetStatement(i);
  6771. resolved := ResolveStatement(statement);
  6772. IF (resolved # statement) THEN
  6773. statementSequence.SetStatement(i,resolved);
  6774. END;
  6775. END;
  6776. END;
  6777. END StatementSequence;
  6778. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6779. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6780. - check if procedure is callable
  6781. - check return type = NIL (otherwise must be assignment statement)
  6782. **)
  6783. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6784. VAR call: SyntaxTree.Designator;
  6785. BEGIN
  6786. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6787. call := procedureCall.call;
  6788. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6789. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6790. END;
  6791. call := ResolveDesignator(call);
  6792. IF call = SyntaxTree.invalidDesignator THEN
  6793. (* error already handled *)
  6794. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6795. (* inline call in a statement *)
  6796. ELSIF ~IsCallable(call) THEN
  6797. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6798. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6799. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6800. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6801. END;
  6802. procedureCall.SetCall(call);
  6803. (*
  6804. IF call = SyntaxTree.invalidDesignator THEN
  6805. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6806. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6807. IF IsOberonInline(procedure) THEN
  6808. Warning(procedure.position,"call to inline proc");
  6809. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6810. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6811. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6812. resolvedStatement := block;
  6813. RETURN;
  6814. END;
  6815. END;
  6816. *)
  6817. END VisitProcedureCallStatement;
  6818. (** check and resolve assignment LHS := RHS
  6819. - resolve LHS and RHS
  6820. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6821. - check if assignment is compatible
  6822. - check if LHS is variable (i.e. assignable)
  6823. - convert RHS if necessary
  6824. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6825. - assignment between different ASOTs
  6826. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6827. - assignment to ASOT elements:
  6828. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6829. **)
  6830. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6831. VAR
  6832. left: SyntaxTree.Designator;
  6833. right, expression: SyntaxTree.Expression;
  6834. designator: SyntaxTree.Designator;
  6835. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6836. mathArrayType: SyntaxTree.MathArrayType;
  6837. BEGIN
  6838. right := ResolveExpression(assignment.right);
  6839. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6840. left := ResolveDesignator(assignment.left);
  6841. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6842. (* error already handled *)
  6843. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6844. (* LHS is index write operator call on ASOT *)
  6845. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6846. (* necessary ?
  6847. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6848. type := procedureType.firstParameter.type;
  6849. expression := procedureCallDesignator.parameters.GetExpression(0);
  6850. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6851. *)
  6852. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6853. ELSIF CheckVariable(left) THEN
  6854. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6855. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6856. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6857. (* conversion done by procedure call
  6858. (* try to convert to left argument *)
  6859. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6860. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6861. procedureCallDesignator.parameters.SetExpression(1, right);
  6862. END;
  6863. *)
  6864. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6865. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6866. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6867. ELSIF AssignmentCompatible(left, right) THEN
  6868. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6869. mathArrayType := MathArrayStructureOfType(left.type);
  6870. right := NewConversion(right.position, right, mathArrayType, NIL);
  6871. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6872. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6873. ELSE
  6874. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6875. assignment.SetLeft(left);
  6876. assignment.SetRight(right);
  6877. resolvedStatement := assignment
  6878. END
  6879. END
  6880. END
  6881. END VisitAssignment;
  6882. (** check and resolve assignment LHS := RHS
  6883. - resolve LHS and RHS
  6884. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6885. - check if assignment is compatible
  6886. - check if LHS is variable (i.e. assignable)
  6887. - convert RHS if necessary
  6888. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6889. - assignment between different ASOTs
  6890. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6891. - assignment to ASOT elements:
  6892. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6893. **)
  6894. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6895. VAR
  6896. left: SyntaxTree.Designator;
  6897. right: SyntaxTree.Expression;
  6898. inPort, outPort: SyntaxTree.PortType;
  6899. expression: SyntaxTree.Expression;
  6900. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6901. BEGIN
  6902. right := ResolveExpression(communication.right);
  6903. left := ResolveDesignator(communication.left);
  6904. communication.SetLeft(left);
  6905. communication.SetRight(right);
  6906. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6907. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6908. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6909. (* conversion done by procedure call
  6910. (* try to convert to left argument *)
  6911. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6912. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6913. procedureCallDesignator.parameters.SetExpression(1, right);
  6914. END;
  6915. *)
  6916. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6917. ELSE
  6918. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6919. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6920. (* error already handled *)
  6921. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6922. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6923. IF outPort.direction # SyntaxTree.OutPort THEN
  6924. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6925. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6926. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6927. ELSE
  6928. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6929. communication.SetRight(right)
  6930. END;
  6931. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6932. IF CheckVariable(left) THEN
  6933. IF inPort.direction # SyntaxTree.InPort THEN
  6934. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6935. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6936. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6937. END;
  6938. END;
  6939. ELSE
  6940. Error(communication.position, -1, "unsupported stream operation");
  6941. END;
  6942. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6943. IF outPort.direction # SyntaxTree.OutPort THEN
  6944. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6945. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6946. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6947. ELSE
  6948. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6949. communication.SetRight(right)
  6950. END;
  6951. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6952. IF CheckVariable(right) THEN
  6953. IF inPort.direction # SyntaxTree.InPort THEN
  6954. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6955. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6956. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6957. END;
  6958. END;
  6959. ELSE
  6960. Error(communication.position, -1, "unsupported operation");
  6961. END;
  6962. END;
  6963. END VisitCommunicationStatement;
  6964. (** check and resolve if/eslif part
  6965. - check condition
  6966. - check statement sequence
  6967. **)
  6968. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6969. VAR prevUnreachable, b: BOOLEAN;
  6970. BEGIN
  6971. prevUnreachable := currentIsUnreachable;
  6972. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6973. IF IsBooleanValue(ifPart.condition,b) THEN
  6974. IF b=FALSE THEN
  6975. currentIsUnreachable := TRUE
  6976. ELSIF b=TRUE THEN
  6977. true := TRUE
  6978. END;
  6979. END;
  6980. StatementSequence(ifPart.statements);
  6981. currentIsUnreachable := prevUnreachable;
  6982. END IfPart;
  6983. (** check and resolve if statement
  6984. - check if parts and else part statement sequence
  6985. **)
  6986. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6987. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6988. BEGIN
  6989. prevUnreachable := currentIsUnreachable;
  6990. ifPartTrue := FALSE;
  6991. IfPart(ifStatement.ifPart,ifPartTrue);
  6992. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6993. elsif := ifStatement.GetElsifPart(i);
  6994. IfPart(elsif,ifPartTrue);
  6995. END;
  6996. IF ifStatement.elsePart # NIL THEN
  6997. IF ifPartTrue THEN
  6998. currentIsUnreachable := TRUE
  6999. END;
  7000. StatementSequence(ifStatement.elsePart)
  7001. END;
  7002. currentIsUnreachable := prevUnreachable;
  7003. END VisitIfStatement;
  7004. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7005. VAR variable: SyntaxTree.Designator;
  7006. type,variableType: SyntaxTree.Type;
  7007. withEntry: WithEntry;
  7008. BEGIN
  7009. variable := ResolveDesignator(withPart.variable);
  7010. variableType := variable.type.resolved;
  7011. withPart.SetVariable(variable);
  7012. type := ResolveType(withPart.type);
  7013. withPart.SetType(type);
  7014. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7015. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7016. END;
  7017. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7018. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7019. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7020. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7021. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7022. IF VerboseErrorMessage THEN
  7023. Printout.Info("variable",variable)
  7024. END;
  7025. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7026. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7027. IF VerboseErrorMessage THEN
  7028. Printout.Info("variable",variable);
  7029. Printout.Info("type",type);
  7030. END;
  7031. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7032. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7033. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7034. IF VerboseErrorMessage THEN
  7035. Printout.Info("variable",variable);
  7036. Printout.Info("type",type);
  7037. END;
  7038. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7039. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7040. ELSE
  7041. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7042. NEW(withEntry);
  7043. withEntry.previous := withEntries;
  7044. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7045. withEntry.type := type;
  7046. withEntries := withEntry;
  7047. StatementSequence(withPart.statements);
  7048. withEntries := withEntries.previous;
  7049. END;
  7050. END WithPart;
  7051. (** check and resolve with statement WITH variable: type DO ... END;
  7052. - check type and variable
  7053. - check that variable type is type extension of type
  7054. - check that variable is a variable
  7055. - enter new with scope and enter guardedVariable with same name and reference to variable
  7056. - create if statement:
  7057. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7058. **)
  7059. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7060. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7061. BEGIN
  7062. prevScope := currentScope; symbol := NIL;
  7063. FOR i := 0 TO withStatement.WithParts()-1 DO
  7064. WithPart(withStatement.GetWithPart(i),symbol);
  7065. END;
  7066. IF withStatement.elsePart # NIL THEN
  7067. StatementSequence(withStatement.elsePart)
  7068. END;
  7069. currentScope := prevScope;
  7070. END VisitWithStatement;
  7071. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7072. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7073. - check 'first' < 'last' and no overlaps between different case labels
  7074. - check statement sequence
  7075. **)
  7076. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7077. VAR
  7078. i: LONGINT;
  7079. position: Position;
  7080. expression, left, right: SyntaxTree.Expression;
  7081. expressionType: SyntaxTree.Type;
  7082. l, r: LONGINT;
  7083. cl, cr: CHAR;
  7084. thiscases: SyntaxTree.CaseConstant;
  7085. BEGIN
  7086. thiscases := NIL;
  7087. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7088. expression := casePart.elements.GetExpression(i);
  7089. position := expression.position;
  7090. (* set context of range *)
  7091. IF expression IS SyntaxTree.RangeExpression THEN
  7092. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7093. END;
  7094. expression := ResolveExpression(expression);
  7095. IF expression = SyntaxTree.invalidExpression THEN
  7096. (* error already reported *)
  7097. expressionType := SyntaxTree.invalidType;
  7098. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7099. (* read out 'first' and 'last' *)
  7100. left := expression(SyntaxTree.RangeExpression).first;
  7101. right := expression(SyntaxTree.RangeExpression).last;
  7102. (* guaranteed by VisitRangeExpression: *)
  7103. ASSERT((left # NIL) & (right # NIL));
  7104. ASSERT(left.type.resolved = right.type.resolved);
  7105. left := CompatibleConversion(left.position, left, type);
  7106. right := CompatibleConversion(right.position, right, type);
  7107. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7108. expression(SyntaxTree.RangeExpression).SetLast(right);
  7109. expressionType := RegularType(position,left.type);
  7110. ELSE
  7111. expression := ConstantExpression(expression);
  7112. expression := CompatibleConversion(expression.position, expression, type);
  7113. (*
  7114. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7115. left := Global.NewCharacterValue(system,expression.position,cl);
  7116. expression := casePart.elements.GetExpression(i);
  7117. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7118. expression := left
  7119. END;
  7120. *)
  7121. casePart.elements.SetExpression(i,expression);
  7122. left := expression; right := expression;
  7123. expressionType := RegularType(position,expression.type)
  7124. END;
  7125. IF (expressionType = SyntaxTree.invalidType) THEN
  7126. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7127. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7128. expression := SyntaxTree.invalidExpression;
  7129. ELSE
  7130. l := 0; r := 0;
  7131. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7132. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7133. l := ORD(cl); r := ORD(cr);
  7134. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7135. ELSE
  7136. expression := SyntaxTree.invalidExpression
  7137. END;
  7138. IF expression # SyntaxTree.invalidExpression THEN
  7139. IF l>r THEN
  7140. Error(position,Diagnostics.Invalid,"empty case label")
  7141. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7142. Error(position,Diagnostics.Invalid,"duplicate case label");
  7143. ELSE
  7144. IF l < min THEN min := l END;
  7145. IF r > max THEN max := r END;
  7146. END;
  7147. END;
  7148. END;
  7149. casePart.elements.SetExpression(i,expression);
  7150. END;
  7151. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7152. casePart.SetConstants(thiscases);
  7153. StatementSequence(casePart.statements);
  7154. END CasePart;
  7155. (** check and resolve case statement CASE variable OF ... END;
  7156. - check variable
  7157. - check case parts
  7158. **)
  7159. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7160. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7161. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7162. BEGIN
  7163. expression := ResolveExpression(caseStatement.variable);
  7164. type := RegularType(expression.position,expression.type);
  7165. IF type = SyntaxTree.invalidType THEN
  7166. expression := SyntaxTree.invalidExpression;
  7167. ELSIF IsIntegerType(type) THEN
  7168. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7169. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7170. (*
  7171. expression := Global.NewCharacterValue(system,expression.position,ch);
  7172. *)
  7173. type := expression.type;
  7174. ELSIF IsCharacterType(type) THEN
  7175. ELSIF IsEnumerationType(type) THEN
  7176. ELSE
  7177. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7178. expression := SyntaxTree.invalidExpression;
  7179. END;
  7180. caseStatement.SetVariable(expression);
  7181. caseList := NIL;
  7182. min := MAX(LONGINT); max := MIN(LONGINT);
  7183. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7184. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7185. END;
  7186. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7187. msg := "huge sparse case table ";
  7188. Strings.AppendInt(msg, max-min);
  7189. Strings.Append(msg,"/");
  7190. Strings.AppendInt(msg, caseStatement.CaseParts());
  7191. Warning(caseStatement.position,msg);
  7192. END;
  7193. caseStatement.SetMinMax(min,max);
  7194. StatementSequence(caseStatement.elsePart);
  7195. IF expression.resolved # NIL THEN
  7196. IF IsCharacterValue(expression,ch) THEN
  7197. l := ORD(ch)
  7198. ELSIF IsIntegerValue(expression,l) THEN
  7199. END;
  7200. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7201. END;
  7202. END VisitCaseStatement;
  7203. (** check and resolve while statement
  7204. - check condition
  7205. - check statement sequence
  7206. **)
  7207. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7208. VAR prevIsUnreachable,b: BOOLEAN;
  7209. BEGIN
  7210. prevIsUnreachable := currentIsUnreachable;
  7211. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7212. IF IsBooleanValue(whileStatement.condition,b) THEN
  7213. IF b=FALSE THEN
  7214. currentIsUnreachable := TRUE
  7215. END;
  7216. END;
  7217. StatementSequence(whileStatement.statements);
  7218. currentIsUnreachable := prevIsUnreachable
  7219. END VisitWhileStatement;
  7220. (** check and resolve repeat statement
  7221. - check condition
  7222. - check statement sequence
  7223. **)
  7224. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7225. BEGIN
  7226. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7227. StatementSequence(repeatStatement.statements);
  7228. END VisitRepeatStatement;
  7229. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7230. VAR withEntry: WithEntry;
  7231. BEGIN
  7232. withEntry := withEntries;
  7233. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7234. withEntry := withEntry.previous
  7235. END;
  7236. IF withEntry = NIL THEN RETURN FALSE
  7237. ELSE
  7238. type := withEntry.type;
  7239. RETURN TRUE
  7240. END;
  7241. END GetGuard;
  7242. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7243. - check that variable is an integer variable
  7244. - check that from is integer typed with compatible type
  7245. - check that to has compatible type
  7246. - check that by is constant integer with compatible type
  7247. **)
  7248. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7249. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7250. BEGIN
  7251. designator := ResolveDesignator(forStatement.variable);
  7252. type := SyntaxTree.invalidType;
  7253. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7254. designator := SyntaxTree.invalidDesignator;
  7255. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7256. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7257. designator := SyntaxTree.invalidDesignator;
  7258. ELSIF CheckVariable(designator) THEN
  7259. type := designator.type;
  7260. END;
  7261. forStatement.SetVariable(designator);
  7262. expression := ResolveExpression(forStatement.from);
  7263. IF expression = SyntaxTree.invalidExpression THEN
  7264. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7265. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7266. expression := SyntaxTree.invalidExpression;
  7267. ELSIF type # SyntaxTree.invalidType THEN
  7268. expression := NewConversion(expression.position,expression,type,NIL)
  7269. END;
  7270. forStatement.SetFrom(expression);
  7271. expression := ResolveExpression(forStatement.to);
  7272. IF expression = SyntaxTree.invalidExpression THEN
  7273. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7274. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7275. expression := SyntaxTree.invalidExpression;
  7276. ELSIF type # SyntaxTree.invalidType THEN
  7277. expression := NewConversion(expression.position,expression,type,NIL)
  7278. END;
  7279. forStatement.SetTo(expression);
  7280. IF forStatement.by # NIL THEN
  7281. expression := ConstantInteger(forStatement.by);
  7282. ELSE
  7283. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7284. END;
  7285. IF expression = SyntaxTree.invalidExpression THEN
  7286. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7287. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7288. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7289. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7290. ELSIF type # SyntaxTree.invalidType THEN
  7291. expression := NewConversion(expression.position,expression,type,NIL)
  7292. END;
  7293. forStatement.SetBy(expression);
  7294. StatementSequence(forStatement.statements);
  7295. END VisitForStatement;
  7296. (** check and resolve loop statement LOOP StatementSequence END
  7297. - check statement sequence
  7298. **)
  7299. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7300. BEGIN
  7301. StatementSequence(loopStatement.statements)
  7302. END VisitLoopStatement;
  7303. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7304. BEGIN
  7305. StatementSequence(exitableBlock.statements);
  7306. END VisitExitableBlock;
  7307. (** check and resolve exit statement EXIT
  7308. - check that exit is within LOOP statement block
  7309. **)
  7310. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7311. VAR outer: SyntaxTree.Statement;
  7312. BEGIN
  7313. outer := exitStatement.outer;
  7314. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7315. outer := outer.outer;
  7316. END;
  7317. IF outer = NIL THEN
  7318. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7319. END;
  7320. END VisitExitStatement;
  7321. (** check and resolve return statement RETURN [expression]
  7322. - check expression (if any)
  7323. - check if in procedure scope
  7324. - if in procedure scope then check expression compatibility
  7325. - if not in procecdure scope then check on return without expression
  7326. **)
  7327. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7328. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7329. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7330. BEGIN
  7331. position := returnStatement.position;
  7332. expression := returnStatement.returnValue;
  7333. IF expression # NIL THEN
  7334. expression := ResolveExpression(expression);
  7335. returnStatement.SetReturnValue(expression);
  7336. END;
  7337. outer := returnStatement.outer;
  7338. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7339. outer := outer.outer
  7340. END;
  7341. IF (outer # NIL) THEN
  7342. scope := outer(SyntaxTree.Body).inScope;
  7343. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7344. IF (expression # NIL) THEN
  7345. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7346. END;
  7347. ELSE
  7348. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7349. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7350. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7351. END;
  7352. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7353. IF returnType # NIL THEN
  7354. returnType := returnType.resolved;
  7355. IF expression = NIL THEN
  7356. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7357. ELSIF expression.type = NIL THEN
  7358. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7359. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7360. Error(position,Diagnostics.Invalid,"return type not compatible");
  7361. IF VerboseErrorMessage THEN
  7362. Printout.Info("returnType",returnType);
  7363. Printout.Info("expression",expression);
  7364. END;
  7365. ELSE
  7366. expression := NewConversion(expression.position,expression,returnType,NIL);
  7367. returnStatement.SetReturnValue(expression);
  7368. END;
  7369. ELSIF expression # NIL THEN
  7370. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7371. END;
  7372. END;
  7373. END;
  7374. END VisitReturnStatement;
  7375. (** check and resolve await statement AWAIT(condition: Expression)
  7376. - check await condition
  7377. **)
  7378. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7379. VAR condition: SyntaxTree.Expression;
  7380. BEGIN
  7381. condition := ResolveCondition(awaitStatement.condition);
  7382. IF currentIsRealtime THEN
  7383. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7384. END;
  7385. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7386. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7387. END;
  7388. awaitStatement.SetCondition(condition);
  7389. END VisitAwaitStatement;
  7390. PROCEDURE CheckSystemImport(position: Position);
  7391. VAR import: SyntaxTree.Import;
  7392. BEGIN
  7393. import := currentScope.ownerModule.moduleScope.firstImport;
  7394. WHILE(import # NIL) DO
  7395. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7396. RETURN;
  7397. END;
  7398. import := import.nextImport;
  7399. END;
  7400. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7401. END CheckSystemImport;
  7402. (** check and resolve code statement: do nothing, must be done by assembler
  7403. **)
  7404. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7405. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7406. BEGIN
  7407. CheckSystemImport(code.position);
  7408. FOR i := 0 TO code.inRules.Length()-1 DO
  7409. statement := code.inRules.GetStatement(i);
  7410. IF statement IS SyntaxTree.Assignment THEN
  7411. WITH statement: SyntaxTree.Assignment DO
  7412. statement.SetRight(ResolveExpression(statement.right));
  7413. END;
  7414. ELSE
  7415. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7416. END;
  7417. END;
  7418. FOR i := 0 TO code.outRules.Length()-1 DO
  7419. statement := code.outRules.GetStatement(i);
  7420. IF statement IS SyntaxTree.Assignment THEN
  7421. WITH statement: SyntaxTree.Assignment DO
  7422. statement.SetLeft(ResolveDesignator(statement.left));
  7423. END;
  7424. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7425. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7426. ELSE
  7427. Printout.Info("out statement ", statement);
  7428. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7429. END;
  7430. END;
  7431. END VisitCode;
  7432. (** check and set flags of a statement block
  7433. - check for multiply occurence of a flag
  7434. - check and set priority only in bodies
  7435. - check for valid names
  7436. **)
  7437. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7438. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7439. flag: LONGINT; recordBody: SyntaxTree.Body;
  7440. PROCEDURE SetProtectedRecord;
  7441. VAR scope: SyntaxTree.Scope;
  7442. BEGIN
  7443. scope := currentScope;
  7444. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7445. scope := scope.outerScope
  7446. END;
  7447. IF scope # NIL THEN
  7448. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7449. END;
  7450. END SetProtectedRecord;
  7451. BEGIN
  7452. flags := {};
  7453. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7454. recordBody := block(SyntaxTree.Body)
  7455. ELSE
  7456. recordBody := NIL
  7457. END;
  7458. blockModifier := block.blockModifiers;
  7459. WHILE(blockModifier # NIL) DO
  7460. name := blockModifier.identifier;
  7461. expression := blockModifier.expression;
  7462. position := blockModifier.position;
  7463. flag := -1;
  7464. IF name=Global.NamePriority THEN
  7465. IF expression = NIL THEN
  7466. Error(position,Diagnostics.Invalid,"missing priority expression");
  7467. ELSIF recordBody = NIL THEN
  7468. Error(position,Diagnostics.Invalid,"priority not on record body");
  7469. ELSIF recordBody.priority # NIL THEN
  7470. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7471. ELSE
  7472. recordBody.SetPriority(expression);
  7473. END;
  7474. ELSIF expression # NIL THEN
  7475. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7476. ELSIF name=Global.NameExclusive THEN
  7477. IF block.isExclusive THEN
  7478. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7479. END;
  7480. block.SetExclusive(TRUE); SetProtectedRecord;
  7481. ELSIF name=Global.NameActive THEN
  7482. IF recordBody = NIL THEN
  7483. Error(position,Diagnostics.Invalid,"active not in record body");
  7484. ELSIF recordBody.isActive THEN
  7485. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7486. ELSE
  7487. recordBody.SetActive(TRUE); SetProtectedRecord;
  7488. END;
  7489. ELSIF name=Global.NameSafe THEN
  7490. IF recordBody = NIL THEN
  7491. Error(position,Diagnostics.Invalid,"safe not in record body");
  7492. ELSIF recordBody.isSafe THEN
  7493. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7494. ELSE
  7495. recordBody.SetSafe(TRUE);
  7496. SetProtectedRecord;
  7497. END;
  7498. ELSIF name=Global.NameRealtime THEN
  7499. IF recordBody = NIL THEN
  7500. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7501. ELSIF recordBody.isRealtime THEN
  7502. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7503. ELSE
  7504. recordBody.SetRealtime(TRUE);
  7505. block.SetRealtime(TRUE);
  7506. END;
  7507. ELSIF name=Global.NameUnchecked THEN
  7508. IF block.isUnchecked THEN
  7509. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7510. ELSE
  7511. block.SetUnchecked(TRUE);
  7512. END;
  7513. ELSIF (name=Global.NameUncooperative) THEN
  7514. IF block.isUncooperative THEN
  7515. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7516. ELSE
  7517. block.SetUncooperative(TRUE);
  7518. END;
  7519. ELSE
  7520. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7521. END;
  7522. blockModifier := blockModifier.nextModifier;
  7523. END;
  7524. END BlockFlags;
  7525. (** check and resolve statement block
  7526. - check flags (exclusive)
  7527. - check statement sequence
  7528. **)
  7529. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7530. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7531. BEGIN
  7532. BlockFlags(statementBlock);
  7533. IF statementBlock.isExclusive THEN
  7534. (* check that not in exclusive block *)
  7535. IF currentIsExclusive THEN
  7536. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7537. ELSIF currentIsRealtime THEN
  7538. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7539. END;
  7540. END;
  7541. recentExclusive := currentIsExclusive;
  7542. recentUnreachable := currentIsUnreachable;
  7543. recentRealtime := currentIsRealtime;
  7544. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7545. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7546. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7547. StatementSequence(statementBlock.statements);
  7548. currentIsRealtime := recentRealtime;
  7549. currentIsExclusive := recentExclusive;
  7550. currentIsUnreachable := recentUnreachable;
  7551. END VisitStatementBlock;
  7552. (** check and resolve body
  7553. - check flags (active, priority, safe)
  7554. - check body and finally part
  7555. **)
  7556. PROCEDURE Body(body: SyntaxTree.Body);
  7557. BEGIN
  7558. VisitStatementBlock(body);
  7559. IF body.isActive THEN
  7560. IF ~currentIsBodyProcedure THEN
  7561. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7562. ELSIF body.priority # NIL THEN
  7563. body.SetPriority(ConstantInteger(body.priority));
  7564. END;
  7565. ELSIF body.isSafe THEN
  7566. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7567. ELSIF body.priority # NIL THEN
  7568. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7569. END;
  7570. IF body.code # NIL THEN
  7571. CheckSystemImport(body.position);
  7572. END;
  7573. StatementSequence(body.finally)
  7574. END Body;
  7575. (*** scopes ***)
  7576. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7577. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7578. VAR duplicateSymbol: BOOLEAN;
  7579. BEGIN
  7580. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7581. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7582. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7583. END;
  7584. scope.EnterSymbol(symbol,duplicateSymbol);
  7585. IF ~allowDuplicate & duplicateSymbol THEN
  7586. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7587. IF VerboseErrorMessage THEN
  7588. Printout.Info("multiply defined identifier",symbol);
  7589. Printout.Info("in scope",scope);
  7590. END;
  7591. END;
  7592. END Register;
  7593. (**
  7594. implementation: check and resolve an implementation part
  7595. **)
  7596. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7597. move implementation checker to a separate object ? *)
  7598. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7599. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7600. BEGIN
  7601. prevIsRealtime := currentIsRealtime;
  7602. prevIsBodyProcedure := currentIsBodyProcedure;
  7603. prevIsCellNet := currentIsCellNet;
  7604. prevScope := currentScope;
  7605. currentScope := scope;
  7606. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7607. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7608. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7609. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7610. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7611. (*
  7612. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7613. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7614. END;
  7615. *)
  7616. END;
  7617. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7618. (* module body, record bodies are wrapped into an artifical procedure *)
  7619. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7620. Body(scope(SyntaxTree.ProcedureScope).body)
  7621. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7622. Body(scope(SyntaxTree.ProcedureScope).body)
  7623. END;
  7624. END;
  7625. currentScope := prevScope;
  7626. currentIsRealtime := prevIsRealtime;
  7627. currentIsBodyProcedure := prevIsBodyProcedure;
  7628. currentIsCellNet := prevIsCellNet;
  7629. END Implementation;
  7630. (** implementation phase:
  7631. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7632. **)
  7633. PROCEDURE Implementations(x: SyntaxTree.Module);
  7634. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7635. BEGIN
  7636. prevPhase := phase;
  7637. phase := InlinePhase;
  7638. scope := x.firstScope;
  7639. WHILE(scope # NIL) DO
  7640. Implementation(scope);
  7641. scope := scope.nextScope;
  7642. END;
  7643. phase := ImplementationPhase;
  7644. scope := x.firstScope;
  7645. WHILE(scope # NIL) DO
  7646. Implementation(scope);
  7647. scope := scope.nextScope;
  7648. END;
  7649. phase := prevPhase;
  7650. END Implementations;
  7651. (** declaration phase:
  7652. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7653. - import lists (for module scopes)
  7654. - parameter list (for procedure scopes)
  7655. - constant declarations
  7656. - type declarations
  7657. - variable declarations
  7658. - procedure declarations
  7659. preformed in two stages:
  7660. - first all symbols are entered into the symbol table (with uniqueness check),
  7661. - then all symbols are resolved
  7662. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7663. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7664. **)
  7665. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7666. VAR
  7667. constant: SyntaxTree.Constant;
  7668. typeDeclaration: SyntaxTree.TypeDeclaration;
  7669. variable: SyntaxTree.Variable;
  7670. procedure: SyntaxTree.Procedure;
  7671. prevScope: SyntaxTree.Scope;
  7672. parameter: SyntaxTree.Parameter;
  7673. import: SyntaxTree.Import;
  7674. symbol: SyntaxTree.Symbol;
  7675. prevPhase: LONGINT;
  7676. prevError : BOOLEAN;
  7677. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7678. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7679. BEGIN
  7680. IF type.baseType # NIL THEN
  7681. baseType := type.baseType.resolved;
  7682. IF baseType IS SyntaxTree.PointerType THEN
  7683. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7684. END;
  7685. (*
  7686. IF baseType IS SyntaxTree.CellType THEN
  7687. DeclareCell(baseType(SyntaxTree.CellType));
  7688. END;
  7689. *)
  7690. END;
  7691. parameter := type.firstParameter;
  7692. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7693. (*
  7694. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7695. variable.SetType(parameter.type);
  7696. variable.SetAccess(SyntaxTree.Hidden);
  7697. variable.SetModifiers(parameter.modifiers);
  7698. currentScope.PushVariable(variable);
  7699. *)
  7700. Register(parameter,scope, FALSE);
  7701. parameter := parameter.nextParameter;
  7702. END;
  7703. property := type.firstProperty;
  7704. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7705. (*
  7706. variable := currentScope.FindVariable(property.name);
  7707. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7708. prop := variable(SyntaxTree.Property);
  7709. ELSE (* add, duplicate symbols detection later *)
  7710. prop := SyntaxTree.NewProperty(property.position, property.name);
  7711. currentScope.PushVariable(prop);
  7712. END;
  7713. prop.SetType(property.type);
  7714. prop.SetValue(property.value);
  7715. prop.SetAccess(SyntaxTree.Hidden);
  7716. *)
  7717. Register(property, scope, FALSE);
  7718. property := property.nextProperty;
  7719. END;
  7720. END DeclareCell;
  7721. BEGIN
  7722. prevError := error;
  7723. prevPhase := phase;
  7724. phase := DeclarationPhase;
  7725. prevScope := currentScope;
  7726. currentScope := scope;
  7727. error := FALSE;
  7728. (* first enter all symbols in scope *)
  7729. IF scope IS SyntaxTree.ModuleScope THEN
  7730. (* treat imports first for a module scope, , set default context if necessary *)
  7731. import := scope(SyntaxTree.ModuleScope).firstImport;
  7732. WHILE(import # NIL) DO
  7733. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7734. Register(import, currentScope, FALSE);
  7735. import := import.nextImport;
  7736. END;
  7737. import := scope(SyntaxTree.ModuleScope).firstImport;
  7738. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7739. ResolveSymbol(import);
  7740. import := import.nextImport;
  7741. END;
  7742. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7743. (* enter parameters for a procedure scope *)
  7744. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7745. WHILE(parameter # NIL) DO
  7746. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7747. END;
  7748. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7749. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7750. ELSIF scope IS SyntaxTree.CellScope THEN
  7751. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7752. IF~skipImplementation THEN
  7753. import := scope(SyntaxTree.CellScope).firstImport;
  7754. WHILE(import # NIL) DO
  7755. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7756. Register(import, currentScope, FALSE);
  7757. import := import.nextImport;
  7758. END;
  7759. import := scope(SyntaxTree.CellScope).firstImport;
  7760. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7761. ResolveSymbol(import);
  7762. import := import.nextImport;
  7763. END;
  7764. END;
  7765. END;
  7766. IF error THEN RETURN END;
  7767. IF skipImplementation THEN
  7768. scope.Clear;
  7769. END;
  7770. (* constants *)
  7771. constant := scope.firstConstant;
  7772. WHILE (constant # NIL) DO
  7773. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7774. END;
  7775. (* type declarations *)
  7776. typeDeclaration := scope.firstTypeDeclaration;
  7777. WHILE (typeDeclaration # NIL) DO
  7778. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7779. END;
  7780. (* variables *)
  7781. variable := scope.firstVariable;
  7782. WHILE (variable # NIL) DO
  7783. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7784. END;
  7785. (* procedures *)
  7786. procedure := scope.firstProcedure;
  7787. WHILE (procedure # NIL) DO
  7788. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7789. END;
  7790. (* now process all symbols without any presumption on the order *)
  7791. symbol := scope.firstSymbol;
  7792. WHILE(symbol # NIL) DO
  7793. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7794. ResolveSymbol(symbol);
  7795. END;
  7796. symbol := symbol.nextSymbol;
  7797. END;
  7798. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7799. symbol := scope.firstSymbol;
  7800. WHILE symbol # NIL DO
  7801. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7802. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7803. pointerFixes.Add(symbol, currentScope);
  7804. END;
  7805. IF ~symbol.type.resolved.isRealtime THEN
  7806. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7807. END;
  7808. END;
  7809. symbol := symbol.nextSymbol
  7810. END;
  7811. END;
  7812. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7813. Error(Basic.invalidPosition,Diagnostics.Invalid,"problems during offset computation in module");
  7814. END;
  7815. IF (scope.ownerModule # NIL) THEN
  7816. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7817. scope.ownerModule.AddScope(scope);
  7818. END;
  7819. phase := prevPhase;
  7820. currentScope := prevScope;
  7821. error := error OR prevError;
  7822. END Declarations;
  7823. (* nopov *)
  7824. (** check if all operators from one module are compatible to the ones in the other module
  7825. - check if there are not multiple operators with the same signature
  7826. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7827. - check for all operators whose signatures are compatible, whether the return types are compatible
  7828. note that:
  7829. - the return type is not considered to be part of the signature
  7830. - two signatures are considered compatible, if all of the operands are compatible
  7831. **)
  7832. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7833. VAR
  7834. thisOperator, thatOperator: SyntaxTree.Operator;
  7835. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7836. thisParameter, thatParameter: SyntaxTree.Parameter;
  7837. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7838. i: LONGINT;
  7839. BEGIN
  7840. currentScope := thisModuleScope;
  7841. hasError := FALSE;
  7842. (* go through all operators in the other module *)
  7843. thatOperator := thatModuleScope.firstOperator;
  7844. WHILE (thatOperator # NIL) & ~hasError DO
  7845. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7846. (* the other operator is accessible *)
  7847. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7848. (* the other operator is not the conversion operator *)
  7849. (* go through all operators in this module *)
  7850. thisOperator := thisModuleScope.firstOperator;
  7851. WHILE (thisOperator # NIL) & ~hasError DO
  7852. IF thisOperator # thatOperator THEN
  7853. (* the operators are not the same *)
  7854. IF thisOperator.name = thatOperator.name THEN
  7855. (* the operators share the same identifier *)
  7856. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7857. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7858. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7859. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7860. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7861. (* both operators have the same paramter count *)
  7862. thisParameter := thisProcedureType.firstParameter;
  7863. thatParameter := thatProcedureType.firstParameter;
  7864. operandsAreEqual := TRUE;
  7865. operandsAreCompatible := TRUE;
  7866. (* go through all parameters *)
  7867. FOR i := 1 TO thisProcedureType.numberParameters DO
  7868. ASSERT(thatParameter # NIL);
  7869. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7870. operandsAreEqual := FALSE;
  7871. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7872. operandsAreCompatible := FALSE
  7873. END
  7874. END;
  7875. thisParameter := thisParameter.nextParameter;
  7876. thatParameter := thatParameter.nextParameter
  7877. END;
  7878. IF operandsAreEqual THEN
  7879. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7880. hasError := TRUE
  7881. ELSIF operandsAreCompatible THEN
  7882. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7883. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7884. hasError := TRUE
  7885. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7886. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7887. hasError := TRUE
  7888. END
  7889. END
  7890. END
  7891. END
  7892. END;
  7893. thisOperator := thisOperator.nextOperator
  7894. END
  7895. END
  7896. END;
  7897. thatOperator := thatOperator.nextOperator
  7898. END
  7899. END CheckInterOperatorConformity;
  7900. (** check module:
  7901. - check module declaration
  7902. - add context, if necessary
  7903. - remove module from import cache, if necessary
  7904. - check declarations
  7905. - resolve all type fixes
  7906. - check implementation (bodies)
  7907. **)
  7908. PROCEDURE Module*(x: SyntaxTree.Module);
  7909. VAR (* nopov *)
  7910. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7911. BEGIN
  7912. prevScope := currentScope;
  7913. prevIsCellNet := currentIsCellNet;
  7914. module := x;
  7915. ASSERT(x # NIL);
  7916. global := system.globalScope[x.case];
  7917. x.moduleScope.SetGlobalScope(global);
  7918. currentScope := global;
  7919. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7920. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7921. RemoveModuleFromCache(importCache,x);
  7922. Declarations(x.moduleScope, FALSE);
  7923. FixTypes();
  7924. IF module.isCellNet THEN
  7925. currentIsCellNet := TRUE;
  7926. modifier := x.modifiers;
  7927. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7928. CheckModifiers(modifier, FALSE);
  7929. END;
  7930. (* nopov *)
  7931. IF ~error THEN
  7932. (* check if operators conform to each other within this module *)
  7933. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7934. (* go through all imports *)
  7935. import := x.moduleScope.firstImport;
  7936. WHILE import # NIL DO
  7937. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7938. (* check if all operators in this module conform to the ones of the imported module *)
  7939. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7940. END;
  7941. import := import.nextImport
  7942. END;
  7943. END;
  7944. Implementations(x);
  7945. module := NIL;
  7946. currentIsCellNet := prevIsCellNet;
  7947. currentScope := prevScope;
  7948. END Module;
  7949. END Checker;
  7950. Warnings*=OBJECT (SyntaxTree.Visitor)
  7951. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7952. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7953. BEGIN
  7954. SELF.diagnostics := diagnostics
  7955. END InitWarnings;
  7956. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  7957. BEGIN END VisitPortType;
  7958. (** types *)
  7959. PROCEDURE Type(x: SyntaxTree.Type);
  7960. BEGIN x.Accept(SELF)
  7961. END Type;
  7962. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7963. BEGIN END VisitType;
  7964. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7965. BEGIN END VisitBasicType;
  7966. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7967. BEGIN END VisitCharacterType;
  7968. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7969. BEGIN END VisitIntegerType;
  7970. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7971. BEGIN END VisitFloatType;
  7972. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7973. BEGIN END VisitQualifiedType;
  7974. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7975. BEGIN END VisitStringType;
  7976. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7977. BEGIN END VisitEnumerationType;
  7978. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7979. BEGIN END VisitRangeType;
  7980. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7981. BEGIN
  7982. IF ~(SyntaxTree.Warned IN x.state) THEN
  7983. x.SetState(SyntaxTree.Warned);
  7984. Type(x.arrayBase);
  7985. END;
  7986. END VisitArrayType;
  7987. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7988. BEGIN
  7989. IF ~(SyntaxTree.Warned IN x.state) THEN
  7990. x.SetState(SyntaxTree.Warned);
  7991. Type(x.arrayBase);
  7992. END;
  7993. END VisitMathArrayType;
  7994. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7995. BEGIN
  7996. IF ~(SyntaxTree.Warned IN x.state) THEN
  7997. x.SetState(SyntaxTree.Warned);
  7998. Type(x.pointerBase);
  7999. END;
  8000. END VisitPointerType;
  8001. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8002. BEGIN Scope(x.recordScope) END VisitRecordType;
  8003. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8004. BEGIN Scope(x.cellScope) END VisitCellType;
  8005. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8006. BEGIN END VisitProcedureType;
  8007. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8008. VAR msg: ARRAY 256 OF CHAR;
  8009. BEGIN
  8010. Global.GetSymbolName(x,msg);
  8011. Strings.Append(msg," ");
  8012. Strings.Append(msg,text);
  8013. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8014. END Warning;
  8015. (** symbols *)
  8016. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8017. BEGIN
  8018. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8019. IF ~(x IS SyntaxTree.Parameter) THEN
  8020. Warning(x,"never used");
  8021. END;
  8022. END;
  8023. x.Accept(SELF);
  8024. END Symbol;
  8025. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8026. BEGIN END VisitSymbol;
  8027. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8028. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8029. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8030. BEGIN END VisitConstant;
  8031. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8032. BEGIN END VisitVariable;
  8033. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8034. BEGIN END VisitProperty;
  8035. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8036. BEGIN END VisitParameter;
  8037. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8038. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8039. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8040. BEGIN END VisitOperator;
  8041. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8042. BEGIN END VisitImport;
  8043. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8044. VAR
  8045. symbol: SyntaxTree.Symbol;
  8046. BEGIN
  8047. symbol := scope.firstSymbol;
  8048. WHILE(symbol # NIL) DO
  8049. Symbol(symbol);
  8050. symbol := symbol.nextSymbol;
  8051. END;
  8052. END Scope;
  8053. PROCEDURE Module*(x: SyntaxTree.Module);
  8054. BEGIN
  8055. SELF.module := x;
  8056. Scope(x.moduleScope);
  8057. END Module;
  8058. END Warnings;
  8059. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8060. BEGIN
  8061. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8062. END IsOberonInline;
  8063. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8064. BEGIN
  8065. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8066. END Resolved;
  8067. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8068. VAR i: LONGINT;
  8069. BEGIN
  8070. i := 1;
  8071. WHILE i < x DO
  8072. i := i *2
  8073. END;
  8074. RETURN i=x
  8075. END PowerOf2;
  8076. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8077. BEGIN
  8078. RETURN
  8079. (scope # NIL) &
  8080. (scope IS SyntaxTree.ModuleScope)
  8081. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8082. OR
  8083. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8084. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8085. END IsCellNetScope;
  8086. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8087. BEGIN
  8088. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8089. END IsCellScope;
  8090. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8091. BEGIN
  8092. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8093. RETURN (scope # NIL) & IsCellNetScope(scope)
  8094. END InCellNetScope;
  8095. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8096. BEGIN
  8097. ASSERT(size MOD system.dataUnit = 0);
  8098. RETURN size DIV system.dataUnit
  8099. END ToMemoryUnits;
  8100. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8101. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8102. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8103. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8104. BEGIN
  8105. IF t = NIL THEN
  8106. RETURN TRUE
  8107. ELSE
  8108. t := t.resolved;
  8109. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8110. END;
  8111. END TypeAllowed;
  8112. BEGIN
  8113. type := type.resolved;
  8114. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8115. RETURN FALSE
  8116. ELSE
  8117. procedureType := type(SyntaxTree.ProcedureType);
  8118. numberParameters := procedureType.numberParameters;
  8119. RETURN
  8120. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8121. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8122. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8123. END;
  8124. END GetProcedureAllowed;
  8125. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8126. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8127. VAR import: SyntaxTree.Import;
  8128. BEGIN
  8129. import := importCache.ImportByModuleName(x.name,x.context);
  8130. IF import # NIL THEN
  8131. importCache.RemoveImporters(x.name,x.context);
  8132. END;
  8133. END RemoveModuleFromCache;
  8134. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8135. (* to <- this assignment compatibility *)
  8136. VAR result: BOOLEAN;
  8137. BEGIN
  8138. IF this= NIL THEN result := (to=NIL)
  8139. ELSIF to=NIL THEN result := FALSE
  8140. ELSE
  8141. (*! will be replaced by this:
  8142. ELSE result := this.CompatibleTo(to.resolved);
  8143. *)
  8144. this := this.resolved; to := to.resolved;
  8145. IF to=SyntaxTree.invalidType THEN result := FALSE
  8146. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8147. ELSIF to = this THEN
  8148. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8149. ELSIF to IS SyntaxTree.BasicType THEN
  8150. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8151. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8152. result := this.CompatibleTo(to.resolved)
  8153. ELSE
  8154. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8155. END
  8156. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8157. result := to.sizeInBits = this.sizeInBits;
  8158. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8159. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8160. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8161. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8162. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8163. result := TRUE;
  8164. ELSIF to IS SyntaxTree.AnyType THEN
  8165. 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);
  8166. ELSIF to IS SyntaxTree.ObjectType THEN
  8167. 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 *) ;
  8168. ELSIF to IS SyntaxTree.ByteType THEN
  8169. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8170. ELSIF to IS SyntaxTree.CharacterType THEN
  8171. result := IsCharacterType(this)
  8172. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8173. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8174. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8175. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8176. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8177. result := TRUE;
  8178. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8179. result := TRUE;
  8180. ELSE
  8181. result := FALSE
  8182. END;
  8183. ELSIF to IS SyntaxTree.PointerType THEN
  8184. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8185. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8186. & (~to.isRealtime OR this.isRealtime);
  8187. ELSIF to IS SyntaxTree.ProcedureType THEN
  8188. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8189. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8190. & (~to.isRealtime OR this.isRealtime)
  8191. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8192. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8193. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8194. ELSIF to IS SyntaxTree.RecordType THEN
  8195. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8196. ELSIF to IS SyntaxTree.ArrayType THEN
  8197. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8198. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8199. ELSIF StaticArrayCompatible(to, this) THEN
  8200. result := TRUE
  8201. ELSE
  8202. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8203. END;
  8204. ELSIF to IS SyntaxTree.MathArrayType THEN
  8205. IF this IS SyntaxTree.MathArrayType THEN
  8206. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8207. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8208. result := TRUE;
  8209. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8210. result := TRUE;
  8211. ELSE
  8212. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8213. END;
  8214. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8215. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8216. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8217. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8218. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8219. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8220. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8221. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8222. ELSE
  8223. result := FALSE
  8224. END;
  8225. (* an array-structured object type is compatible to the type of its array structure *)
  8226. ELSIF IsArrayStructuredObjectType(this) THEN
  8227. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8228. ELSE
  8229. result := FALSE;
  8230. END;
  8231. ELSIF to IS SyntaxTree.StringType THEN
  8232. result := FALSE;
  8233. ELSIF to IS SyntaxTree.EnumerationType THEN
  8234. result := IsEnumerationExtension(this,to);
  8235. ELSIF to IS SyntaxTree.PortType THEN
  8236. result := SameType(to, this)
  8237. ELSE
  8238. Printout.Info("CompatibleTo",to);
  8239. HALT(100); (* implement missing type check *)
  8240. END;
  8241. END;
  8242. RETURN result
  8243. END CompatibleTo;
  8244. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8245. VAR actualBase, formalBase: SyntaxTree.Type;
  8246. BEGIN
  8247. IF SameType(formal,actual) THEN
  8248. RETURN TRUE
  8249. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8250. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8251. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8252. RETURN
  8253. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8254. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8255. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8256. & StaticArrayCompatible(formalBase,actualBase)
  8257. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8258. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8259. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8260. RETURN
  8261. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8262. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8263. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8264. & StaticArrayCompatible(formalBase,actualBase)
  8265. ELSE RETURN FALSE
  8266. END;
  8267. END StaticArrayCompatible;
  8268. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8269. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8270. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8271. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8272. BEGIN
  8273. result := SameType(formal,actual);
  8274. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8275. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8276. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8277. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8278. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8279. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8280. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8281. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8282. & TC(formalBase, actualBase);
  8283. END;
  8284. RETURN result
  8285. END TC;
  8286. BEGIN
  8287. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8288. ELSE
  8289. arrayBase := formalType.arrayBase.resolved;
  8290. IF (actualType IS SyntaxTree.StringType) THEN
  8291. result := arrayBase IS SyntaxTree.CharacterType
  8292. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8293. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8294. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8295. result := TC(formalType, actualType);
  8296. ELSE
  8297. result := (arrayBase IS SyntaxTree.ByteType)
  8298. END;
  8299. END;
  8300. RETURN result
  8301. END OpenArrayCompatible;
  8302. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8303. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8304. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8305. BEGIN
  8306. IF actualType IS SyntaxTree.MathArrayType THEN
  8307. actualArray := actualType(SyntaxTree.MathArrayType);
  8308. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8309. (*
  8310. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8311. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8312. *)
  8313. actualBase := ArrayBase(actualType,Infinity);
  8314. formalBase := ArrayBase(formalType,Infinity);
  8315. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8316. ELSE
  8317. (*
  8318. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8319. *)
  8320. formalBase := Resolved(formalType.arrayBase);
  8321. actualBase := Resolved(actualArray.arrayBase);
  8322. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8323. (*
  8324. ARRAY [k] -> ARRAY [n]
  8325. *)
  8326. result := (formalType.staticLength = actualArray.staticLength)
  8327. ELSE
  8328. result := TRUE
  8329. END;
  8330. IF ~result THEN
  8331. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8332. ELSIF actualBase = NIL THEN result := FALSE
  8333. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8334. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8335. ELSE
  8336. result := SameType(formalBase,actualBase)
  8337. END;
  8338. END;
  8339. ELSE
  8340. result := FALSE
  8341. END;
  8342. RETURN result
  8343. END MathArrayCompatible;
  8344. (**
  8345. Math Array Type distance for assignments / parameter passings of the form
  8346. from -> to
  8347. variants:
  8348. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8349. allowed:
  8350. static -> static (& size match)
  8351. static -> open
  8352. static -> tensor
  8353. open -> open
  8354. open -> tensor
  8355. open -> static
  8356. tensor -> tensor
  8357. tensor -> open
  8358. tensor -> static
  8359. **)
  8360. (*! think about the metric here: is form matching more important than element type matching? *)
  8361. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8362. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8363. BEGIN
  8364. fromBase := Resolved(from.arrayBase);
  8365. toBase := Resolved(to.arrayBase);
  8366. i := Infinity;
  8367. IF from = to THEN
  8368. i := 0;
  8369. ELSIF (from.form = to.form) THEN
  8370. (* static -> static, open -> open, tensor -> tensor *)
  8371. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8372. IF fromBase = toBase THEN i := 0
  8373. ELSIF toBase = NIL THEN i := 1
  8374. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8375. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8376. ELSE
  8377. i := TypeDistance(system,fromBase, toBase, varpar);
  8378. END;
  8379. END;
  8380. ELSIF (to.form = SyntaxTree.Static) THEN
  8381. (* forbidden *)
  8382. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8383. (* static -> tensor, open -> tensor, tensor -> open *)
  8384. IF toBase=fromBase THEN i := 0;
  8385. ELSIF toBase = NIL THEN i := 1;
  8386. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8387. toBase := ArrayBase(toBase,Infinity);
  8388. IF (fromBase=toBase) THEN i := 0
  8389. ELSIF (toBase = NIL) THEN i:= 1
  8390. ELSIF (fromBase = NIL) THEN i := Infinity;
  8391. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8392. END;
  8393. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8394. fromBase := ArrayBase(fromBase,Infinity);
  8395. IF (fromBase=toBase) THEN i := 0
  8396. ELSIF (toBase = NIL) THEN i := 1
  8397. ELSIF (fromBase = NIL) THEN i := Infinity;
  8398. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8399. END;
  8400. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8401. END;
  8402. IF i # Infinity THEN INC(i,2) END;
  8403. ELSIF (from.form = SyntaxTree.Static) THEN
  8404. (* static -> open *)
  8405. IF toBase=fromBase THEN i := 0
  8406. ELSIF toBase = NIL THEN i := 1
  8407. ELSIF fromBase = NIL THEN i := Infinity
  8408. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8409. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8410. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8411. END;
  8412. IF i # Infinity THEN INC(i,1) END;
  8413. ELSE HALT(100); (* unknown case *)
  8414. END;
  8415. RETURN i;
  8416. END MathArrayTypeDistance;
  8417. (** compute and return the distance of two array types
  8418. - return the distance of the base types
  8419. **)
  8420. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8421. VAR i: LONGINT;
  8422. BEGIN
  8423. i := Infinity;
  8424. IF from = to THEN
  8425. i := 0
  8426. ELSE
  8427. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8428. (*
  8429. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8430. i := TypeDistance(from.base, to.base);
  8431. IF i >= 0 THEN INC(i) END
  8432. ELSIF (from.mode = open) & (to.mode = open) THEN
  8433. i := TypeDistance(from.base, to.base);
  8434. *)
  8435. END;
  8436. RETURN i
  8437. END ArrayTypeDistance;
  8438. (** compute the signature distance of a procedure and an actual parameter list
  8439. - if any of the parameters are not compatible, the result is infinite
  8440. - add up and return the distance over all parameters
  8441. **)
  8442. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8443. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8444. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8445. BEGIN
  8446. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8447. result := Infinity
  8448. ELSE
  8449. formalParameter := procedureType.firstParameter;
  8450. i := 0;
  8451. result := 0;
  8452. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8453. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8454. actualParameter := actualParameters.GetExpression(i);
  8455. ASSERT(formalParameter.type # NIL);
  8456. IF (actualParameter.type = NIL) THEN distance := Infinity
  8457. ELSE
  8458. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8459. END;
  8460. IF distance = Infinity THEN
  8461. result := Infinity;
  8462. ELSE
  8463. to := formalParameter.type.resolved;
  8464. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8465. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8466. (* already handled varpar *)
  8467. (*
  8468. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8469. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8470. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8471. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8472. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8473. END;
  8474. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8475. result := Infinity
  8476. END;
  8477. *)
  8478. INC(result, distance);
  8479. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8480. INC(result, distance);
  8481. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8482. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8483. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8484. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8485. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8486. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8487. END;
  8488. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8489. result := Infinity
  8490. END;
  8491. ELSE
  8492. result := Infinity
  8493. END;
  8494. ELSE
  8495. INC(result,distance);
  8496. END;
  8497. END;
  8498. (*
  8499. Printout.Info("actual=", actualParameter);
  8500. Printout.Info("formal=", formalParameter);
  8501. TRACE(result);
  8502. *)
  8503. formalParameter := formalParameter.nextParameter; INC(i);
  8504. END;
  8505. END;
  8506. ASSERT(result >= 0);
  8507. RETURN result
  8508. END Distance;
  8509. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8510. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8511. BEGIN
  8512. IF right.numberParameters # (procedureType.numberParameters) THEN
  8513. result := Infinity
  8514. ELSE
  8515. formalParameter := procedureType.firstParameter;
  8516. rightParameter := right.firstParameter;
  8517. i := 0;
  8518. result := 0;
  8519. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8520. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8521. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8522. IF distance = Infinity THEN
  8523. result := Infinity;
  8524. ELSE
  8525. INC(result,distance);
  8526. END;
  8527. formalParameter := formalParameter.nextParameter;
  8528. rightParameter := rightParameter.nextParameter;
  8529. END;
  8530. END;
  8531. ASSERT(result >= 0);
  8532. RETURN result
  8533. END ProcedureTypeDistance;
  8534. (** compute and return the distance between two types, used for computation of signature distance
  8535. from -> to
  8536. **)
  8537. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8538. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8539. BEGIN
  8540. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8541. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8542. END;
  8543. i := Infinity;
  8544. IF from = to THEN
  8545. i := 0
  8546. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8547. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8548. i := Infinity;
  8549. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8550. i := 10;
  8551. ELSIF (from IS SyntaxTree.StringType) THEN
  8552. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8553. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8554. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8555. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8556. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8557. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8558. i := 1
  8559. ELSIF (from IS SyntaxTree.NilType) THEN
  8560. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8561. (*
  8562. ELSIF (from = NoType) THEN
  8563. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8564. *)
  8565. ELSIF (from IS SyntaxTree.BasicType) THEN
  8566. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8567. IF varpar & (i # 0) THEN i := Infinity END;
  8568. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8569. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8570. ELSIF (from IS SyntaxTree.RecordType) THEN
  8571. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8572. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8573. IF to IS SyntaxTree.MathArrayType THEN
  8574. (*
  8575. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8576. i := Infinity;
  8577. ELSE
  8578. *)
  8579. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8580. (*
  8581. END;
  8582. *)
  8583. END
  8584. ELSIF (from IS SyntaxTree.PointerType) THEN
  8585. ptr := from(SyntaxTree.PointerType);
  8586. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8587. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8588. (* ELSE i := TypeDistance(ptr.base, to); *)
  8589. END
  8590. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8591. IF (to IS SyntaxTree.ProcedureType) THEN
  8592. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8593. END;
  8594. ELSIF (from IS SyntaxTree.PortType) THEN
  8595. IF (to IS SyntaxTree.PortType) THEN
  8596. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8597. i := 0;
  8598. END;
  8599. END;
  8600. (*no procedure test, procedure must be the same*)
  8601. END;
  8602. RETURN i
  8603. END TypeDistance;
  8604. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8605. BEGIN
  8606. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8607. END IsIntegerType;
  8608. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8609. BEGIN
  8610. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8611. END IsAddressType;
  8612. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8613. BEGIN
  8614. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8615. END IsSizeType;
  8616. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8617. BEGIN
  8618. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8619. END IsSignedIntegerType;
  8620. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8621. BEGIN
  8622. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8623. END IsUnsignedIntegerType;
  8624. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8625. VAR result: BOOLEAN;
  8626. BEGIN
  8627. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8628. value := x.resolved(SyntaxTree.IntegerValue).value;
  8629. result := TRUE
  8630. ELSE
  8631. result := FALSE
  8632. END;
  8633. RETURN result
  8634. END IsIntegerValue;
  8635. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8636. VAR result: BOOLEAN;
  8637. BEGIN
  8638. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8639. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8640. result := TRUE
  8641. ELSE
  8642. result := FALSE
  8643. END;
  8644. RETURN result
  8645. END IsEnumerationValue;
  8646. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8647. VAR result: BOOLEAN;
  8648. BEGIN
  8649. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8650. value := x.resolved(SyntaxTree.RealValue).value;
  8651. result := TRUE
  8652. ELSE
  8653. result := FALSE
  8654. END;
  8655. RETURN result
  8656. END IsRealValue;
  8657. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8658. VAR result: BOOLEAN;
  8659. BEGIN
  8660. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8661. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8662. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8663. result := TRUE
  8664. ELSE
  8665. result := FALSE
  8666. END;
  8667. RETURN result
  8668. END IsComplexValue;
  8669. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8670. VAR result: BOOLEAN;
  8671. BEGIN
  8672. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8673. value := x.resolved(SyntaxTree.CharacterValue).value;
  8674. result := TRUE
  8675. ELSE
  8676. result := FALSE
  8677. END;
  8678. RETURN result
  8679. END IsCharacterValue;
  8680. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8681. VAR result: BOOLEAN;
  8682. BEGIN
  8683. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8684. value := x.resolved(SyntaxTree.BooleanValue).value;
  8685. result := TRUE
  8686. ELSE
  8687. result := FALSE
  8688. END;
  8689. RETURN result
  8690. END IsBooleanValue;
  8691. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8692. VAR result: BOOLEAN;
  8693. BEGIN
  8694. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8695. value := x.resolved(SyntaxTree.SetValue).value;
  8696. result := TRUE
  8697. ELSE
  8698. result := FALSE
  8699. END;
  8700. RETURN result
  8701. END IsSetValue;
  8702. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8703. VAR result: BOOLEAN;
  8704. BEGIN
  8705. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8706. value := x.resolved(SyntaxTree.StringValue).value;
  8707. result := TRUE
  8708. ELSE
  8709. result := FALSE
  8710. END;
  8711. RETURN result
  8712. END IsStringValue;
  8713. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8714. BEGIN
  8715. x := x.resolved;
  8716. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8717. END Indexable;
  8718. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN
  8720. RETURN t1.SameType(t2.resolved);
  8721. END SameType;
  8722. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8723. BEGIN
  8724. IF t IS SyntaxTree.MathArrayType THEN
  8725. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8726. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8727. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8728. DEC(max);
  8729. END;
  8730. ELSIF t IS SyntaxTree.ArrayType THEN
  8731. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8732. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8733. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8734. END;
  8735. END;
  8736. RETURN t;
  8737. END ArrayBase;
  8738. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8739. BEGIN
  8740. type := type.resolved;
  8741. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8742. base := type(SyntaxTree.ArrayType).arrayBase;
  8743. RETURN TRUE;
  8744. END;
  8745. RETURN FALSE;
  8746. END IsOpenArray;
  8747. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8748. BEGIN
  8749. type := type.resolved;
  8750. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8751. base := type(SyntaxTree.ArrayType).arrayBase;
  8752. dim := type(SyntaxTree.ArrayType).staticLength;
  8753. RETURN TRUE
  8754. ELSE
  8755. RETURN FALSE
  8756. END;
  8757. END IsStaticArray;
  8758. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8759. BEGIN
  8760. type := type.resolved;
  8761. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8762. base := type(SyntaxTree.ArrayType).arrayBase;
  8763. RETURN TRUE
  8764. ELSE
  8765. RETURN FALSE
  8766. END;
  8767. END IsDynamicArray;
  8768. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8769. VAR i: LONGINT;
  8770. BEGIN
  8771. i := 0;
  8772. t := t.resolved;
  8773. IF t IS SyntaxTree.MathArrayType THEN
  8774. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8775. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8776. END;
  8777. ELSIF t IS SyntaxTree.ArrayType THEN
  8778. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8779. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8780. END;
  8781. END;
  8782. RETURN i
  8783. END Dimension;
  8784. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8785. BEGIN
  8786. RETURN expression.assignable;
  8787. END IsVariable;
  8788. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8789. BEGIN
  8790. IF (symbol IS SyntaxTree.Parameter) THEN
  8791. WITH symbol: SyntaxTree.Parameter DO
  8792. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8793. END;
  8794. ELSE
  8795. RETURN FALSE
  8796. END;
  8797. END IsVariableParameter;
  8798. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8799. VAR result: BOOLEAN;
  8800. BEGIN
  8801. IF type = NIL THEN result := FALSE
  8802. ELSE
  8803. type := type.resolved;
  8804. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8805. END;
  8806. RETURN result
  8807. END IsPointerType;
  8808. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8809. VAR result: BOOLEAN;
  8810. BEGIN
  8811. IF type = NIL THEN result := FALSE
  8812. ELSE
  8813. type := type.resolved;
  8814. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8815. END;
  8816. RETURN result
  8817. END IsUnsafePointer;
  8818. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8819. BEGIN
  8820. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8821. END IsDisposable;
  8822. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8823. VAR result: BOOLEAN;
  8824. BEGIN
  8825. IF type = NIL THEN result := FALSE
  8826. ELSE
  8827. type := type.resolved;
  8828. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8829. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8830. result := result OR (type IS SyntaxTree.ObjectType);
  8831. END;
  8832. RETURN result
  8833. END IsPointerToRecord;
  8834. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8835. VAR result: BOOLEAN;
  8836. BEGIN
  8837. IF type = NIL THEN result := FALSE
  8838. ELSE
  8839. type := type.resolved;
  8840. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8841. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8842. ;
  8843. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8844. result := result OR (type IS SyntaxTree.ObjectType);
  8845. END;
  8846. RETURN result
  8847. END IsPointerToObject;
  8848. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8849. BEGIN
  8850. IF type # NIL THEN
  8851. RETURN type.resolved.hasPointers
  8852. ELSE
  8853. RETURN FALSE
  8854. END;
  8855. END ContainsPointer;
  8856. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8857. BEGIN
  8858. IF type = NIL THEN RETURN FALSE END;
  8859. type := type.resolved;
  8860. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8861. END IsStringType;
  8862. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8863. BEGIN
  8864. IF type = NIL THEN RETURN FALSE END;
  8865. type := type.resolved;
  8866. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8867. END IsCharacterType;
  8868. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8869. BEGIN
  8870. IF type = NIL THEN RETURN FALSE END;
  8871. type := type.resolved;
  8872. RETURN (type IS SyntaxTree.EnumerationType)
  8873. END IsEnumerationType;
  8874. (** cf. section "Type extension (base type)" in the language report **)
  8875. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8876. VAR result: BOOLEAN;
  8877. BEGIN
  8878. ASSERT(base # NIL); ASSERT(extension # NIL);
  8879. base := base.resolved; extension := extension.resolved;
  8880. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8881. result := TRUE;
  8882. ELSE
  8883. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8884. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8885. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8886. END;
  8887. WHILE (extension # NIL) & (extension # base) DO
  8888. IF extension IS SyntaxTree.RecordType THEN
  8889. extension := extension(SyntaxTree.RecordType).baseType;
  8890. IF (extension # NIL) THEN extension := extension.resolved END;
  8891. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8892. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8893. END;
  8894. ELSE extension := NIL;
  8895. END;
  8896. END;
  8897. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8898. END;
  8899. RETURN result
  8900. END IsTypeExtension;
  8901. (** check if base is the base enumeration type of extension **)
  8902. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8903. BEGIN
  8904. base := base.resolved; extension := extension.resolved;
  8905. WHILE (extension # NIL) & (extension # base) DO
  8906. IF extension IS SyntaxTree.EnumerationType THEN
  8907. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8908. IF extension # NIL THEN extension := extension.resolved END;
  8909. ELSE
  8910. extension := NIL
  8911. END;
  8912. END;
  8913. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8914. END IsEnumerationExtension;
  8915. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8916. BEGIN
  8917. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8918. RETURN TRUE
  8919. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8920. RETURN TRUE
  8921. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8922. RETURN TRUE
  8923. ELSE
  8924. RETURN FALSE
  8925. END
  8926. END IsCallable;
  8927. (** compute and return the distance of two record types
  8928. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8929. **)
  8930. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8931. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8932. BEGIN
  8933. i := 0;
  8934. WHILE (from # NIL) & (from # to) DO
  8935. baseType := from.baseType;
  8936. IF (baseType # NIL) THEN
  8937. baseType := baseType.resolved;
  8938. IF baseType IS SyntaxTree.PointerType THEN
  8939. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8940. END;
  8941. IF baseType IS SyntaxTree.RecordType THEN
  8942. from := baseType(SyntaxTree.RecordType);
  8943. ELSE
  8944. from := NIL;
  8945. END;
  8946. ELSE
  8947. from := NIL
  8948. END;
  8949. INC(i)
  8950. END;
  8951. IF from = NIL THEN i := Infinity END;
  8952. RETURN i
  8953. END RecordTypeDistance;
  8954. (** compute and return the distance of two pointer types **)
  8955. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8956. BEGIN
  8957. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8958. RETURN Infinity;
  8959. ELSE
  8960. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8961. END;
  8962. END PointerTypeDistance;
  8963. (** check if expression contains a symbol designator pointing to a type declaration.
  8964. - if so then enter type declaration into typeDeclaration and return true else return false
  8965. **)
  8966. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8967. VAR result: BOOLEAN;
  8968. BEGIN
  8969. result := FALSE;
  8970. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8971. result := TRUE;
  8972. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8973. END;
  8974. RETURN result
  8975. END IsTypeDesignator;
  8976. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8977. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8978. VAR result: BOOLEAN;
  8979. BEGIN
  8980. type := type.resolved;
  8981. IF type IS SyntaxTree.PointerType THEN
  8982. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8983. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8984. result := TRUE
  8985. ELSE
  8986. result := type IS SyntaxTree.RecordType
  8987. END;
  8988. RETURN result
  8989. END IsExtensibleType;
  8990. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8991. BEGIN
  8992. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8993. (d IS SyntaxTree.SymbolDesignator) &
  8994. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8995. OR
  8996. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8997. END IsUnextensibleRecord;
  8998. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8999. BEGIN
  9000. IF IsUnextensibleRecord(d) THEN
  9001. RETURN FALSE
  9002. ELSE RETURN IsExtensibleType(d.type.resolved)
  9003. END;
  9004. END IsExtensibleDesignator;
  9005. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9006. BEGIN
  9007. type := type.resolved;
  9008. IF (type IS SyntaxTree.PointerType) THEN
  9009. RETURN TRUE
  9010. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9011. RETURN TRUE
  9012. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9013. RETURN TRUE
  9014. ELSIF (type IS SyntaxTree.BasicType) THEN
  9015. RETURN TRUE
  9016. END;
  9017. RETURN FALSE
  9018. END IsBasicType;
  9019. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9020. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9021. BEGIN
  9022. baseType := record.baseType;
  9023. IF (baseType # NIL) THEN
  9024. baseType := baseType.resolved;
  9025. IF (baseType IS SyntaxTree.PointerType) THEN
  9026. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9027. END;
  9028. END;
  9029. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9030. recordType := baseType(SyntaxTree.RecordType);
  9031. ELSE
  9032. recordType := NIL;
  9033. END;
  9034. RETURN recordType
  9035. END RecordBase;
  9036. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9037. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9038. BEGIN
  9039. baseRecord := RecordBase(scope.ownerRecord);
  9040. IF baseRecord = NIL THEN RETURN NIL END;
  9041. scope := baseRecord.recordScope;
  9042. procedureType := procedure.type.resolved;
  9043. IF procedure IS SyntaxTree.Operator THEN
  9044. operator := scope.firstOperator;
  9045. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9046. (*
  9047. Printout.Info("not same ",procedureType);
  9048. Printout.Info("with ",operator.type);
  9049. *)
  9050. operator := operator.nextOperator;
  9051. END;
  9052. super := operator;
  9053. ELSE
  9054. super := scope.firstProcedure;
  9055. WHILE (super # NIL) & (super.name # procedure.name) DO
  9056. super := super.nextProcedure;
  9057. END;
  9058. END;
  9059. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9060. RETURN super
  9061. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9062. RETURN super
  9063. ELSE
  9064. RETURN FindSuperProcedure(scope,procedure);
  9065. END;
  9066. END FindSuperProcedure;
  9067. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9068. VAR procedure: SyntaxTree.Procedure;
  9069. BEGIN
  9070. procedure := record.recordScope.constructor;
  9071. IF procedure = NIL THEN
  9072. record := RecordBase(record);
  9073. IF record # NIL THEN
  9074. procedure := GetConstructor(record)
  9075. END;
  9076. END;
  9077. RETURN procedure;
  9078. END GetConstructor;
  9079. (* enter a case into a list of cases in a sorted way and check for collision *)
  9080. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9081. VAR prev,this,new: SyntaxTree.CaseConstant;
  9082. BEGIN
  9083. this := root;
  9084. prev := NIL;
  9085. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9086. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9087. RETURN FALSE
  9088. ELSE
  9089. IF (this # NIL) & (this.min = max+1) THEN
  9090. this.min := min
  9091. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9092. prev.max := min
  9093. ELSE
  9094. NEW(new); new.min := min; new.max := max;
  9095. new.next := this;
  9096. IF prev = NIL THEN
  9097. root := new;
  9098. ELSE
  9099. prev.next := new
  9100. END
  9101. END;
  9102. RETURN TRUE
  9103. END;
  9104. END EnterCase;
  9105. (** generate and return a new checker object, errors are entered into diagnostics **)
  9106. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9107. VAR checker: Checker;
  9108. BEGIN
  9109. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9110. RETURN checker
  9111. END NewChecker;
  9112. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9113. VAR warnings: Warnings;
  9114. BEGIN
  9115. NEW(warnings, diagnostics); RETURN warnings;
  9116. END NewWarnings;
  9117. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9118. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9119. END IsRangeType;
  9120. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9121. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9122. END IsMathArrayType;
  9123. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9124. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9125. END IsArrayType;
  9126. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9127. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9128. END IsComplexType;
  9129. (** if a type is an array-structured object type *)
  9130. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9131. VAR recordType: SyntaxTree.RecordType;
  9132. BEGIN
  9133. IF type = NIL THEN
  9134. RETURN FALSE
  9135. ELSE
  9136. type := type.resolved;
  9137. IF type IS SyntaxTree.PointerType THEN
  9138. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9139. IF type IS SyntaxTree.RecordType THEN
  9140. recordType := type(SyntaxTree.RecordType);
  9141. RETURN recordType.isObject & recordType.HasArrayStructure()
  9142. ELSE
  9143. RETURN FALSE
  9144. END
  9145. ELSE
  9146. RETURN FALSE
  9147. END
  9148. END
  9149. END IsArrayStructuredObjectType;
  9150. (** the math array structure of a type
  9151. - for math arrays: the array itself
  9152. - for pointers: the math array structure of the pointer base
  9153. - for array-structured object types: the underlying structure
  9154. - for non-math arrays and all other types: NIL
  9155. **)
  9156. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9157. VAR
  9158. result: SyntaxTree.MathArrayType;
  9159. BEGIN
  9160. IF type = NIL THEN
  9161. result := NIL
  9162. ELSE
  9163. type := type.resolved;
  9164. IF type IS SyntaxTree.PointerType THEN
  9165. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9166. END;
  9167. IF type IS SyntaxTree.MathArrayType THEN
  9168. result := type(SyntaxTree.MathArrayType)
  9169. ELSIF type IS SyntaxTree.RecordType THEN
  9170. result := type(SyntaxTree.RecordType).arrayStructure
  9171. ELSE
  9172. result := NIL
  9173. END
  9174. END;
  9175. RETURN result
  9176. END MathArrayStructureOfType;
  9177. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9178. VAR
  9179. result: BOOLEAN;
  9180. rangeExpression: SyntaxTree.RangeExpression;
  9181. BEGIN
  9182. IF x IS SyntaxTree.RangeExpression THEN
  9183. rangeExpression := x(SyntaxTree.RangeExpression);
  9184. result := TRUE;
  9185. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9186. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9187. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9188. ELSE
  9189. result := FALSE
  9190. END;
  9191. RETURN result
  9192. END IsStaticRange;
  9193. (** whether a type is a math array of tensor form **)
  9194. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9195. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9196. END IsTensor;
  9197. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9198. BEGIN
  9199. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9200. length := type(SyntaxTree.MathArrayType).staticLength;
  9201. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9202. RETURN TRUE
  9203. ELSE
  9204. RETURN FALSE
  9205. END;
  9206. END IsStaticMathArray;
  9207. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9208. BEGIN
  9209. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9210. END SymbolHasAddress;
  9211. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9212. BEGIN
  9213. RETURN
  9214. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9215. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9216. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9217. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9218. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9219. ;
  9220. END HasAddress;
  9221. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9222. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9223. BEGIN
  9224. IF (e IS SyntaxTree.Designator) THEN
  9225. d := e(SyntaxTree.Designator);
  9226. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9227. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9228. e := d.left;
  9229. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9230. END;
  9231. IF d # NIL THEN
  9232. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9233. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9234. END;
  9235. END;
  9236. RETURN FALSE;
  9237. END IsLocalVariable;
  9238. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9239. BEGIN
  9240. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9241. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9242. ELSE
  9243. RETURN TRUE
  9244. END;
  9245. END IsStaticProcedure;
  9246. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9247. CONST OptimizeMethodTable = FALSE;
  9248. BEGIN
  9249. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9250. END InMethodTable;
  9251. END FoxSemanticChecker.
  9252. SystemTools.FreeDownTo FoxSemanticChecker ~