par.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336
  1. //____________________________________________________________
  2. //
  3. // PROGRAM: C preprocessor
  4. // MODULE: par.cpp
  5. // DESCRIPTION: Parser
  6. //
  7. // The contents of this file are subject to the Interbase Public
  8. // License Version 1.0 (the "License"); you may not use this file
  9. // except in compliance with the License. You may obtain a copy
  10. // of the License at http://www.Inprise.com/IPL.html
  11. //
  12. // Software distributed under the License is distributed on an
  13. // "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  14. // or implied. See the License for the specific language governing
  15. // rights and limitations under the License.
  16. //
  17. // The Original Code was created by Inprise Corporation
  18. // and its predecessors. Portions created by Inprise Corporation are
  19. // Copyright (C) Inprise Corporation.
  20. //
  21. // All Rights Reserved.
  22. // Contributor(s): ______________________________________.
  23. // Revision 1.2 2000/11/27 09:26:13 fsg
  24. // Fixed bugs in gpre to handle PYXIS forms
  25. // and allow edit.e and fred.e to go through
  26. // gpre without errors (and correct result).
  27. //
  28. // This is a partial fix until all
  29. // PYXIS datatypes are adjusted in frm_trn.c
  30. //
  31. // removed some compiler warnings too
  32. //
  33. // TMN (Mike Nordell) 11.APR.2001 - Reduce compiler warnings
  34. // TMN (Mike Nordell) APR-MAY.2001 - Conversion to C++
  35. // SWB (Stepen Boyd) 2007/03/21 - Supressed parsing of QLI keywords if -noqli
  36. // switch given on the command line.
  37. //
  38. //
  39. //____________________________________________________________
  40. //
  41. //
  42. #include "firebird.h"
  43. #include <setjmp.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "ibase.h"
  47. #include "../gpre/gpre.h"
  48. #include "../gpre/cmp_proto.h"
  49. #include "../gpre/exp_proto.h"
  50. #include "../gpre/gpre_proto.h"
  51. #include "../gpre/hsh_proto.h"
  52. #include "../gpre/gpre_meta.h"
  53. #include "../gpre/msc_proto.h"
  54. #include "../gpre/par_proto.h"
  55. #include "../gpre/sql_proto.h"
  56. #include "../common/utils_proto.h"
  57. #ifdef FTN_BLK_DATA
  58. static void block_data_list(const gpre_dbb*);
  59. #endif
  60. static bool match_parentheses();
  61. static act* par_any();
  62. static act* par_array_element();
  63. static act* par_at();
  64. static act* par_based();
  65. static act* par_begin();
  66. static blb* par_blob();
  67. static act* par_blob_action(act_t);
  68. static act* par_blob_field();
  69. static act* par_case();
  70. static act* par_clear_handles();
  71. static act* par_derived_from();
  72. static act* par_end_block();
  73. static act* par_end_error();
  74. static act* par_end_fetch();
  75. static act* par_end_for();
  76. static act* par_end_modify();
  77. static act* par_end_stream();
  78. static act* par_end_store(bool);
  79. static act* par_erase();
  80. static act* par_fetch();
  81. static act* par_finish();
  82. static act* par_for();
  83. static act* par_function();
  84. static act* par_left_brace();
  85. static act* par_modify();
  86. static act* par_on();
  87. static act* par_on_error();
  88. static act* par_open_blob(act_t, gpre_sym*);
  89. static bool par_options(gpre_req*, bool);
  90. static act* par_procedure();
  91. static act* par_ready();
  92. static act* par_returning_values();
  93. static act* par_right_brace();
  94. static act* par_release();
  95. static act* par_slice(act_t);
  96. static act* par_store();
  97. static act* par_start_stream();
  98. static act* par_start_transaction();
  99. static act* par_subroutine();
  100. static act* par_trans(act_t);
  101. static act* par_type();
  102. static act* par_variable();
  103. static act* scan_routine_header();
  104. static void set_external_flag();
  105. static bool terminator();
  106. static int brace_count;
  107. static int namespace_count;
  108. static bool routine_decl;
  109. static act* cur_statement;
  110. static act* cur_item;
  111. static gpre_lls* cur_for;
  112. static gpre_lls* cur_store;
  113. static gpre_lls* cur_fetch;
  114. static gpre_lls* cur_modify;
  115. static gpre_lls* cur_error;
  116. static gpre_lls* routine_stack;
  117. static gpre_fld* flag_field;
  118. //____________________________________________________________
  119. //
  120. // We have a token with a symbolic meaning. If appropriate,
  121. // parse an action segment. If not, return NULL.
  122. //
  123. act* PAR_action(const TEXT* base_dir)
  124. {
  125. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  126. if (!symbol)
  127. {
  128. cur_statement = NULL;
  129. return NULL;
  130. }
  131. if (gpreGlob.token_global.tok_keyword >= KW_start_actions &&
  132. gpreGlob.token_global.tok_keyword <= KW_end_actions)
  133. {
  134. const kwwords_t keyword = gpreGlob.token_global.tok_keyword;
  135. if (! gpreGlob.sw_no_qli)
  136. {
  137. switch (keyword)
  138. {
  139. case KW_READY:
  140. case KW_START_TRANSACTION:
  141. case KW_FINISH:
  142. case KW_COMMIT:
  143. case KW_PREPARE:
  144. case KW_RELEASE_REQUESTS:
  145. case KW_ROLLBACK:
  146. case KW_FUNCTION:
  147. case KW_SAVE:
  148. case KW_SUB:
  149. case KW_SUBROUTINE:
  150. CPR_eol_token();
  151. break;
  152. case KW_EXTERNAL:
  153. set_external_flag();
  154. return NULL;
  155. case KW_FOR:
  156. // Get the next token as it is without upcasing
  157. gpreGlob.override_case = true;
  158. CPR_token();
  159. break;
  160. default:
  161. CPR_token();
  162. }
  163. }
  164. else
  165. CPR_token();
  166. try {
  167. if (! gpreGlob.sw_no_qli)
  168. {
  169. switch (keyword)
  170. {
  171. case KW_INT:
  172. case KW_LONG:
  173. case KW_SHORT:
  174. case KW_CHAR:
  175. case KW_FLOAT:
  176. case KW_DOUBLE:
  177. //par_var_c(keyword);
  178. return NULL;
  179. case KW_ANY:
  180. return par_any();
  181. case KW_AT:
  182. return par_at();
  183. case KW_BASED:
  184. return par_based();
  185. case KW_CLEAR_HANDLES:
  186. return par_clear_handles();
  187. case KW_DATABASE:
  188. return PAR_database(false, base_dir);
  189. case KW_DERIVED_FROM:
  190. return par_derived_from();
  191. case KW_END_ERROR:
  192. return par_end_error();
  193. case KW_END_FOR:
  194. return cur_statement = par_end_for();
  195. case KW_END_MODIFY:
  196. return cur_statement = par_end_modify();
  197. case KW_END_STREAM:
  198. return cur_statement = par_end_stream();
  199. case KW_END_STORE:
  200. return cur_statement = par_end_store(false);
  201. case KW_END_STORE_SPECIAL:
  202. return cur_statement = par_end_store(true);
  203. case KW_ELEMENT:
  204. return par_array_element();
  205. case KW_ERASE:
  206. return cur_statement = par_erase();
  207. case KW_EVENT_INIT:
  208. return cur_statement = PAR_event_init(false);
  209. case KW_EVENT_WAIT:
  210. return cur_statement = PAR_event_wait(false);
  211. case KW_FETCH:
  212. return cur_statement = par_fetch();
  213. case KW_FINISH:
  214. return cur_statement = par_finish();
  215. case KW_FOR:
  216. return par_for();
  217. case KW_END_FETCH:
  218. return cur_statement = par_end_fetch();
  219. case KW_MODIFY:
  220. return par_modify();
  221. case KW_ON:
  222. return par_on();
  223. case KW_ON_ERROR:
  224. return par_on_error();
  225. case KW_READY:
  226. return cur_statement = par_ready();
  227. case KW_RELEASE_REQUESTS:
  228. return cur_statement = par_release();
  229. case KW_RETURNING_VALUES:
  230. return par_returning_values();
  231. case KW_START_STREAM:
  232. return cur_statement = par_start_stream();
  233. case KW_STORE:
  234. return par_store();
  235. case KW_START_TRANSACTION:
  236. return cur_statement = par_start_transaction();
  237. case KW_FUNCTION:
  238. return par_function();
  239. case KW_PROCEDURE:
  240. return par_procedure();
  241. case KW_PROC:
  242. break;
  243. case KW_SUBROUTINE:
  244. return par_subroutine();
  245. case KW_SUB:
  246. break;
  247. case KW_OPEN_BLOB:
  248. return cur_statement = par_open_blob(ACT_blob_open, 0);
  249. case KW_CREATE_BLOB:
  250. return cur_statement = par_open_blob(ACT_blob_create, 0);
  251. case KW_GET_SLICE:
  252. return cur_statement = par_slice(ACT_get_slice);
  253. case KW_PUT_SLICE:
  254. return cur_statement = par_slice(ACT_put_slice);
  255. case KW_GET_SEGMENT:
  256. return cur_statement = par_blob_action(ACT_get_segment);
  257. case KW_PUT_SEGMENT:
  258. return cur_statement = par_blob_action(ACT_put_segment);
  259. case KW_CLOSE_BLOB:
  260. return cur_statement = par_blob_action(ACT_blob_close);
  261. case KW_CANCEL_BLOB:
  262. return cur_statement = par_blob_action(ACT_blob_cancel);
  263. case KW_COMMIT:
  264. return cur_statement = par_trans(ACT_commit);
  265. case KW_SAVE:
  266. return cur_statement = par_trans(ACT_commit_retain_context);
  267. case KW_ROLLBACK:
  268. return cur_statement = par_trans(ACT_rollback);
  269. case KW_PREPARE:
  270. return cur_statement = par_trans(ACT_prepare);
  271. case KW_NAMESPACE:
  272. if (gpreGlob.sw_language == lang_internal || gpreGlob.sw_language == lang_cxx ||
  273. gpreGlob.sw_language == lang_cplusplus)
  274. {
  275. if (CPR_token())
  276. {
  277. if (gpreGlob.token_global.tok_keyword == KW_L_BRACE)
  278. {
  279. CPR_token();
  280. ++namespace_count;
  281. ++brace_count;
  282. return NULL;
  283. }
  284. }
  285. }
  286. break;
  287. case KW_L_BRACE:
  288. return par_left_brace();
  289. case KW_R_BRACE:
  290. return par_right_brace();
  291. case KW_END:
  292. return par_end_block();
  293. case KW_BEGIN:
  294. return par_begin();
  295. case KW_CASE:
  296. return par_case();
  297. case KW_EXEC:
  298. {
  299. if (!MSC_match(KW_SQL))
  300. break;
  301. gpreGlob.sw_sql = true;
  302. act* action = SQL_action(base_dir);
  303. gpreGlob.sw_sql = false;
  304. return action;
  305. }
  306. default:
  307. break;
  308. }
  309. }
  310. else
  311. {
  312. switch (keyword)
  313. {
  314. case KW_BASED:
  315. return par_based();
  316. case KW_EXEC:
  317. {
  318. if (!MSC_match(KW_SQL))
  319. break;
  320. gpreGlob.sw_sql = true;
  321. act* action = SQL_action(base_dir);
  322. gpreGlob.sw_sql = false;
  323. return action;
  324. }
  325. default:
  326. break;
  327. }
  328. }
  329. } // try
  330. catch (const gpre_exception&) {
  331. throw;
  332. }
  333. catch (const Firebird::fatal_exception&)
  334. {
  335. // CVC: a fatal exception should be propagated.
  336. // For example, a failure in our runtime.
  337. throw;
  338. }
  339. catch (const Firebird::Exception&)
  340. {
  341. gpreGlob.sw_sql = false;
  342. // This is to force GPRE to get the next symbol. Fix for bug #274. DROOT
  343. gpreGlob.token_global.tok_symbol = NULL;
  344. return NULL;
  345. }
  346. cur_statement = NULL;
  347. return NULL;
  348. }
  349. if (! gpreGlob.sw_no_qli)
  350. {
  351. for (; symbol; symbol = symbol->sym_homonym)
  352. {
  353. switch (symbol->sym_type)
  354. {
  355. case SYM_context:
  356. try {
  357. cur_statement = NULL;
  358. return par_variable();
  359. }
  360. catch (const gpre_exception&) {
  361. throw;
  362. }
  363. catch (const Firebird::fatal_exception&)
  364. {
  365. // CVC: a fatal exception should be propagated.
  366. throw;
  367. }
  368. catch (const Firebird::Exception&) {
  369. return 0;
  370. }
  371. case SYM_blob:
  372. try {
  373. cur_statement = NULL;
  374. return par_blob_field();
  375. }
  376. catch (const gpre_exception&) {
  377. throw;
  378. }
  379. catch (const Firebird::fatal_exception&)
  380. {
  381. // CVC: a fatal exception should be propagated.
  382. throw;
  383. }
  384. catch (const Firebird::Exception&) {
  385. return 0;
  386. }
  387. case SYM_relation:
  388. try {
  389. cur_statement = NULL;
  390. return par_type();
  391. }
  392. catch (const gpre_exception&) {
  393. throw;
  394. }
  395. catch (const Firebird::fatal_exception&)
  396. {
  397. // CVC: a fatal exception should be propagated.
  398. throw;
  399. }
  400. catch (const Firebird::Exception&) {
  401. return 0;
  402. }
  403. default:
  404. break;
  405. }
  406. }
  407. }
  408. cur_statement = NULL;
  409. CPR_token();
  410. return NULL;
  411. }
  412. //____________________________________________________________
  413. //
  414. // Parse a blob subtype -- either a signed number or a symbolic name.
  415. //
  416. SSHORT PAR_blob_subtype(gpre_dbb* db)
  417. {
  418. // Check for symbol type name
  419. if (gpreGlob.token_global.tok_type == tok_ident)
  420. {
  421. gpre_fld* field = NULL;
  422. gpre_rel* relation = MET_get_relation(db, "RDB$FIELDS", "");
  423. if (!relation || !(field = MET_field(relation, "RDB$FIELD_SUB_TYPE")))
  424. {
  425. PAR_error("error during BLOB SUB_TYPE lookup");
  426. }
  427. SSHORT const_subtype;
  428. if (!MET_type(field, gpreGlob.token_global.tok_string, &const_subtype))
  429. CPR_s_error("blob sub_type");
  430. PAR_get_token();
  431. return const_subtype;
  432. }
  433. return EXP_SSHORT_ordinal(true);
  434. }
  435. //____________________________________________________________
  436. //
  437. // Parse a DATABASE declaration. If successful, return
  438. // an action block.
  439. //
  440. act* PAR_database(bool sql, const TEXT* base_directory)
  441. {
  442. if (gpreGlob.token_global.tok_length >= NAME_SIZE)
  443. PAR_error("Database alias too long");
  444. TEXT s[MAXPATHLEN << 1];
  445. act* action = MSC_action(0, ACT_database);
  446. gpre_dbb* db = (gpre_dbb*) MSC_alloc(DBB_LEN);
  447. // Get handle name token, make symbol for handle, and
  448. // insert symbol into hash table
  449. gpre_sym* symbol = PAR_symbol(SYM_dummy);
  450. db->dbb_name = symbol;
  451. symbol->sym_type = SYM_database;
  452. symbol->sym_object = (gpre_ctx*) db;
  453. if (!MSC_match(KW_EQUALS))
  454. CPR_s_error("\"=\" in database declaration");
  455. if (MSC_match(KW_STATIC))
  456. db->dbb_scope = DBB_STATIC;
  457. else if (MSC_match(KW_EXTERN))
  458. db->dbb_scope = DBB_EXTERN;
  459. MSC_match(KW_COMPILETIME);
  460. // parse the compiletime options
  461. TEXT* string;
  462. for (;;)
  463. {
  464. if (MSC_match(KW_FILENAME) && !isQuoted(gpreGlob.token_global.tok_type))
  465. CPR_s_error("quoted file name");
  466. tok& token = gpreGlob.token_global;
  467. if (isQuoted(token.tok_type))
  468. {
  469. if (base_directory)
  470. {
  471. db->dbb_filename = string =
  472. (TEXT*) MSC_alloc(token.tok_length + static_cast<int>(strlen(base_directory)) + 1);
  473. MSC_copy_cat(base_directory, static_cast<int>(strlen(base_directory)),
  474. token.tok_string, token.tok_length, string);
  475. }
  476. else
  477. {
  478. db->dbb_filename = string = (TEXT *) MSC_alloc(token.tok_length + 1);
  479. MSC_copy(token.tok_string, token.tok_length, string);
  480. }
  481. token.tok_length += 2;
  482. }
  483. else if (MSC_match(KW_PASSWORD))
  484. {
  485. if (!isQuoted(token.tok_type))
  486. CPR_s_error("quoted password");
  487. db->dbb_c_password = string = (TEXT *) MSC_alloc(token.tok_length + 1);
  488. MSC_copy(token.tok_string, token.tok_length, string);
  489. }
  490. else if (MSC_match(KW_USER))
  491. {
  492. if (!isQuoted(token.tok_type))
  493. CPR_s_error("quoted user name");
  494. db->dbb_c_user = string = (TEXT *) MSC_alloc(token.tok_length + 1);
  495. MSC_copy(token.tok_string, token.tok_length, string);
  496. }
  497. else if (MSC_match(KW_LC_MESSAGES))
  498. {
  499. if (!isQuoted(token.tok_type))
  500. CPR_s_error("quoted language name");
  501. db->dbb_c_lc_messages = string = (TEXT *) MSC_alloc(token.tok_length + 1);
  502. MSC_copy(token.tok_string, token.tok_length, string);
  503. }
  504. else if (!sql && MSC_match(KW_LC_CTYPE))
  505. {
  506. if (!isQuoted(token.tok_type))
  507. CPR_s_error("quoted character set name");
  508. db->dbb_c_lc_ctype = string = (TEXT *) MSC_alloc(token.tok_length + 1);
  509. MSC_copy(token.tok_string, token.tok_length, string);
  510. }
  511. else
  512. break;
  513. PAR_get_token();
  514. }
  515. if ((gpreGlob.sw_auto) && (db->dbb_c_password || db->dbb_c_user || db->dbb_c_lc_ctype ||
  516. db->dbb_c_lc_messages))
  517. {
  518. CPR_warn("PASSWORD, USER and NAMES options require -manual switch to gpre.");
  519. }
  520. if (!db->dbb_filename)
  521. CPR_s_error("quoted file name");
  522. if (gpreGlob.default_user && !db->dbb_c_user)
  523. db->dbb_c_user = gpreGlob.default_user;
  524. if (gpreGlob.default_password && !db->dbb_c_password)
  525. db->dbb_c_password = gpreGlob.default_password;
  526. if (gpreGlob.module_lc_ctype && !db->dbb_c_lc_ctype)
  527. db->dbb_c_lc_ctype = gpreGlob.module_lc_ctype;
  528. if (gpreGlob.default_lc_ctype && !db->dbb_c_lc_ctype)
  529. db->dbb_c_lc_ctype = gpreGlob.default_lc_ctype;
  530. // parse the runtime options
  531. if (MSC_match(KW_RUNTIME))
  532. {
  533. if (MSC_match(KW_FILENAME))
  534. db->dbb_runtime = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  535. else if (MSC_match(KW_PASSWORD))
  536. db->dbb_r_password = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  537. else if (MSC_match(KW_USER))
  538. db->dbb_r_user = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  539. else if (MSC_match(KW_LC_MESSAGES))
  540. db->dbb_r_lc_messages = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  541. else if (!sql && MSC_match(KW_LC_CTYPE))
  542. db->dbb_r_lc_ctype = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  543. else
  544. db->dbb_runtime = sql ? SQL_var_or_string(false) : PAR_native_value(false, false);
  545. }
  546. #ifdef GPRE_ADA
  547. if ((gpreGlob.sw_language == lang_ada) && (gpreGlob.token_global.tok_keyword == KW_HANDLES))
  548. {
  549. PAR_get_token();
  550. if (isQuoted(gpreGlob.token_global.tok_type))
  551. CPR_s_error("quoted file name");
  552. int len = gpreGlob.token_global.tok_length;
  553. if (len > MAXPATHLEN - 2)
  554. len = MAXPATHLEN - 2;
  555. MSC_copy(gpreGlob.token_global.tok_string, len, s);
  556. s[len] = 0;
  557. strcat(s, ".");
  558. if (!gpreGlob.ada_package[0] || !strcmp(gpreGlob.ada_package, s))
  559. {
  560. fb_utils::copy_terminate(gpreGlob.ada_package, s, MAXPATHLEN);
  561. }
  562. else
  563. {
  564. fb_utils::snprintf(s, sizeof(s),
  565. "Ada handle package \"%s\" already in use, ignoring package %s",
  566. gpreGlob.ada_package, gpreGlob.token_global.tok_string);
  567. CPR_warn(s);
  568. }
  569. PAR_get_token();
  570. }
  571. #endif
  572. if (gpreGlob.sw_language != lang_fortran)
  573. MSC_match(KW_SEMI_COLON);
  574. bool found_error = false;
  575. try {
  576. if (!MET_database(db, true))
  577. found_error = true;
  578. }
  579. // CVC: It avoids countless errors if the db can't be loaded.
  580. catch (const Firebird::Exception& exc)
  581. {
  582. found_error = true;
  583. // CVC: Print the low level error. The lack of this caused me a lot of problems.
  584. // Granted, status_exception doesn't help, but fatal_exception carries a
  585. // meaningful message.
  586. CPR_error(exc.what());
  587. }
  588. if (found_error)
  589. {
  590. fb_utils::snprintf(s, sizeof(s), "Couldn't access database %s = '%s'",
  591. db->dbb_name->sym_string, db->dbb_filename);
  592. CPR_error(s);
  593. CPR_abort();
  594. }
  595. db->dbb_next = gpreGlob.isc_databases;
  596. gpreGlob.isc_databases = db;
  597. HSH_insert(symbol);
  598. // Load up the symbol (hash) table with relation names from this databases.
  599. MET_load_hash_table(db);
  600. #ifdef FTN_BLK_DATA
  601. if ((gpreGlob.sw_language == lang_fortran) && (db->dbb_scope != DBB_EXTERN))
  602. block_data_list(db);
  603. #endif
  604. // Since we have a real DATABASE statement, get rid of any artificial
  605. // databases that were created because of an INCLUDE SQLCA statement.
  606. for (gpre_dbb** db_ptr = &gpreGlob.isc_databases; *db_ptr;)
  607. {
  608. if ((*db_ptr)->dbb_flags & DBB_sqlca)
  609. *db_ptr = (*db_ptr)->dbb_next;
  610. else
  611. db_ptr = &(*db_ptr)->dbb_next;
  612. }
  613. return action;
  614. }
  615. //____________________________________________________________
  616. //
  617. // Parse end of statement. All languages except ADA leave
  618. // the trailing semi-colon dangling. ADA, however, must
  619. // eat the semi-colon as part of the statement. In any case,
  620. // return true is a semi-colon is/was there, otherwise return false.
  621. //
  622. bool PAR_end()
  623. {
  624. if ((gpreGlob.sw_language == lang_ada) || (gpreGlob.sw_language == lang_c) ||
  625. (isLangCpp(gpreGlob.sw_language))
  626. #ifdef GPRE_COBOL
  627. ||
  628. (gpreGlob.sw_language == lang_cobol && isAnsiCobol(gpreGlob.sw_cob_dialect))
  629. #endif
  630. )
  631. {
  632. return (MSC_match(KW_SEMI_COLON));
  633. }
  634. return (gpreGlob.token_global.tok_keyword == KW_SEMI_COLON);
  635. }
  636. //____________________________________________________________
  637. //
  638. // Report an error during parse and unwind.
  639. //
  640. void PAR_error(const TEXT* string)
  641. {
  642. CPR_error(string);
  643. PAR_unwind();
  644. }
  645. //____________________________________________________________
  646. //
  647. // Parse an event init statement, preparing
  648. // to wait on a number of named gpreGlob.events.
  649. //
  650. act* PAR_event_init(bool sql)
  651. {
  652. //char req_name[128];
  653. // make up statement node
  654. SQL_resolve_identifier("<identifier>", NULL, MAX_EVENT_SIZE);
  655. //SQL_resolve_identifier("<identifier>", req_name, sizeof(req_name));
  656. //strcpy(gpreGlob.token_global.tok_string, req_name); Why? It's already done.
  657. gpre_nod* init = MSC_node(nod_event_init, 4);
  658. init->nod_arg[0] = (gpre_nod*) PAR_symbol(SYM_dummy);
  659. init->nod_arg[3] = (gpre_nod*) gpreGlob.isc_databases;
  660. act* action = MSC_action(0, ACT_event_init);
  661. action->act_object = (ref*) init;
  662. // parse optional database handle
  663. if (!MSC_match(KW_LEFT_PAREN))
  664. {
  665. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  666. if (symbol && (symbol->sym_type == SYM_database))
  667. init->nod_arg[3] = (gpre_nod*) symbol->sym_object;
  668. else
  669. CPR_s_error("left parenthesis or database handle");
  670. PAR_get_token();
  671. if (!MSC_match(KW_LEFT_PAREN))
  672. CPR_s_error("left parenthesis");
  673. }
  674. // eat any number of event strings until a right paren is found,
  675. // pushing the gpreGlob.events onto a stack
  676. gpre_nod* node;
  677. gpre_lls* stack = NULL;
  678. int count = 0;
  679. while (true)
  680. {
  681. if (MSC_match(KW_RIGHT_PAREN))
  682. break;
  683. const gpre_sym* symbol;
  684. if (!sql && (symbol = gpreGlob.token_global.tok_symbol) && symbol->sym_type == SYM_context)
  685. {
  686. gpre_ctx* context;
  687. gpre_fld* field = EXP_field(&context);
  688. ref* reference = EXP_post_field(field, context, false);
  689. node = MSC_node(nod_field, 1);
  690. node->nod_arg[0] = (gpre_nod*) reference;
  691. }
  692. else
  693. {
  694. node = MSC_node(nod_null, 1);
  695. if (sql)
  696. node->nod_arg[0] = (gpre_nod*) SQL_var_or_string(false);
  697. else
  698. node->nod_arg[0] = (gpre_nod*) PAR_native_value(false, false);
  699. }
  700. MSC_push(node, &stack);
  701. count++;
  702. MSC_match(KW_COMMA);
  703. }
  704. // pop the event strings off the stack
  705. gpre_nod* event_list = init->nod_arg[1] = MSC_node(nod_list, (SSHORT) count);
  706. gpre_nod** ptr = event_list->nod_arg + count;
  707. while (stack)
  708. *--ptr = (gpre_nod*) MSC_pop(&stack);
  709. MSC_push((gpre_nod*) action, &gpreGlob.events);
  710. if (!sql)
  711. PAR_end();
  712. return action;
  713. }
  714. //____________________________________________________________
  715. //
  716. // Parse an event wait statement, preparing
  717. // to wait on a number of named gpreGlob.events.
  718. //
  719. act* PAR_event_wait(bool sql)
  720. {
  721. //char req_name[132];
  722. // this is a simple statement, just add a handle
  723. act* action = MSC_action(0, ACT_event_wait);
  724. SQL_resolve_identifier("<identifier>", NULL, MAX_EVENT_SIZE);
  725. //SQL_resolve_identifier("<identifier>", req_name, sizeof(req_name));
  726. //strcpy(gpreGlob.token_global.tok_string, req_name); redundant
  727. action->act_object = (ref*) PAR_symbol(SYM_dummy);
  728. if (!sql)
  729. PAR_end();
  730. return action;
  731. }
  732. //____________________________________________________________
  733. //
  734. // Perform any last minute stuff necessary at the end of pass1.
  735. //
  736. void PAR_fini()
  737. {
  738. if (cur_for)
  739. CPR_error("unterminated FOR statement");
  740. if (cur_modify)
  741. CPR_error("unterminated MODIFY statement");
  742. if (cur_store)
  743. CPR_error("unterminated STORE statement");
  744. if (cur_error)
  745. CPR_error("unterminated ON_ERROR clause");
  746. if (cur_item)
  747. CPR_error("unterminated ITEM statement");
  748. }
  749. //____________________________________________________________
  750. //
  751. // Get a token or unwind the parse
  752. // if we hit end of file
  753. //
  754. void PAR_get_token()
  755. {
  756. if (CPR_token() == NULL)
  757. {
  758. CPR_error("unexpected EOF");
  759. PAR_unwind();
  760. }
  761. //return NULL;
  762. }
  763. //____________________________________________________________
  764. //
  765. // Do any initialization necessary.
  766. // For one thing, set all current indicators
  767. // to null, since nothing is current. Also,
  768. // set up a block to hold the current routine,
  769. //
  770. // (The 'routine' indicator tells the code
  771. // generator where to put ports to support
  772. // recursive routines and Fortran's strange idea
  773. // of separate sub-modules. For PASCAL only, we
  774. // keep a stack of routines, and pay special attention
  775. // to the main routine.)
  776. //
  777. void PAR_init()
  778. {
  779. SQL_init();
  780. cur_error = cur_fetch = cur_for = cur_modify = cur_store = NULL;
  781. cur_statement = cur_item = NULL;
  782. gpreGlob.cur_routine = MSC_action(0, ACT_routine);
  783. gpreGlob.cur_routine->act_flags |= ACT_main;
  784. MSC_push((gpre_nod*) gpreGlob.cur_routine, &routine_stack);
  785. routine_decl = true;
  786. flag_field = NULL;
  787. brace_count = 0;
  788. namespace_count = 0;
  789. }
  790. static inline void gobble(SCHAR*& string)
  791. {
  792. const SCHAR* s1 = gpreGlob.token_global.tok_string;
  793. while (*s1)
  794. *string++ = *s1++;
  795. PAR_get_token();
  796. }
  797. //____________________________________________________________
  798. //
  799. // Parse a native expression as a string.
  800. //
  801. TEXT* PAR_native_value(bool array_ref, bool handle_ref)
  802. {
  803. SCHAR buffer[512];
  804. SCHAR* string = buffer;
  805. while (true)
  806. {
  807. // PAR_native_values copies the string constants. These are
  808. // passed to api calls. Make sure to enclose these with
  809. // double quotes.
  810. if (gpreGlob.sw_sql_dialect == 1)
  811. {
  812. if (isQuoted(gpreGlob.token_global.tok_type))
  813. {
  814. //const tok_t typ = gpreGlob.token_global.tok_type;
  815. gpreGlob.token_global.tok_length += 2;
  816. *string++ = '\"';
  817. gobble(string);
  818. *string++ = '\"';
  819. break;
  820. }
  821. }
  822. else if (gpreGlob.sw_sql_dialect == 2)
  823. {
  824. if (gpreGlob.token_global.tok_type == tok_dblquoted)
  825. PAR_error("Ambiguous use of double quotes in dialect 2");
  826. else if (gpreGlob.token_global.tok_type == tok_sglquoted)
  827. {
  828. gpreGlob.token_global.tok_length += 2;
  829. *string++ = '\"';
  830. gobble(string);
  831. *string++ = '\"';
  832. break;
  833. }
  834. }
  835. else if (gpreGlob.sw_sql_dialect == 3)
  836. {
  837. if (gpreGlob.token_global.tok_type == tok_sglquoted)
  838. {
  839. gpreGlob.token_global.tok_length += 2;
  840. *string++ = '\"';
  841. gobble(string);
  842. *string++ = '\"';
  843. break;
  844. }
  845. }
  846. if (gpreGlob.token_global.tok_keyword == KW_AMPERSAND || gpreGlob.token_global.tok_keyword == KW_ASTERISK)
  847. gobble(string);
  848. if (gpreGlob.token_global.tok_type != tok_ident)
  849. CPR_s_error("identifier");
  850. gobble(string);
  851. // For ADA, gobble '<attribute>
  852. if ((gpreGlob.sw_language == lang_ada) && (gpreGlob.token_global.tok_string[0] == '\'')) {
  853. gobble(string);
  854. }
  855. kwwords_t keyword = gpreGlob.token_global.tok_keyword;
  856. if (keyword == KW_LEFT_PAREN)
  857. {
  858. int parens = 1;
  859. while (parens)
  860. {
  861. const tok_t typ = gpreGlob.token_global.tok_type;
  862. if (isQuoted(typ))
  863. *string++ = (typ == tok_sglquoted) ? '\'' : '\"';
  864. gobble(string);
  865. if (isQuoted(typ))
  866. *string++ = (typ == tok_sglquoted) ? '\'' : '\"';
  867. keyword = gpreGlob.token_global.tok_keyword;
  868. if (keyword == KW_RIGHT_PAREN)
  869. parens--;
  870. else if (keyword == KW_LEFT_PAREN)
  871. parens++;
  872. }
  873. gobble(string);
  874. keyword = gpreGlob.token_global.tok_keyword;
  875. }
  876. while (keyword == KW_L_BRCKET)
  877. {
  878. int brackets = 1;
  879. while (brackets)
  880. {
  881. gobble(string);
  882. keyword = gpreGlob.token_global.tok_keyword;
  883. if (keyword == KW_R_BRCKET)
  884. brackets--;
  885. else if (keyword == KW_L_BRCKET)
  886. brackets++;
  887. }
  888. gobble(string);
  889. keyword = gpreGlob.token_global.tok_keyword;
  890. }
  891. #ifdef GPRE_PASCAL
  892. while ((keyword == KW_CARAT) && (gpreGlob.sw_language == lang_pascal))
  893. {
  894. gobble(string);
  895. keyword = gpreGlob.token_global.tok_keyword;
  896. }
  897. #endif
  898. if ((keyword == KW_DOT &&
  899. (!handle_ref || gpreGlob.sw_language == lang_c || gpreGlob.sw_language == lang_ada)) ||
  900. keyword == KW_POINTS || (keyword == KW_COLON && !gpreGlob.sw_sql && !array_ref))
  901. {
  902. gobble(string);
  903. }
  904. else
  905. break;
  906. }
  907. const unsigned int length = string - buffer;
  908. fb_assert(length < sizeof(buffer));
  909. string = (SCHAR*) MSC_alloc(length + 1);
  910. if (length)
  911. memcpy(string, buffer, length); // MSC_alloc filled the string with zeroes.
  912. return string;
  913. }
  914. //____________________________________________________________
  915. //
  916. // Find a pseudo-field for null. If there isn't one,
  917. // make one.
  918. //
  919. gpre_fld* PAR_null_field()
  920. {
  921. if (flag_field)
  922. return flag_field;
  923. flag_field = MET_make_field("gds__null_flag", dtype_short, sizeof(SSHORT), false);
  924. return flag_field;
  925. }
  926. //____________________________________________________________
  927. //
  928. // Parse the RESERVING clause of the start_transaction & set transaction
  929. // statements, creating a partial TPB in the process. The
  930. // TPB just hangs off the end of the transaction block.
  931. //
  932. void PAR_reserving( USHORT flags, bool parse_sql)
  933. {
  934. gpre_dbb* database;
  935. while (true)
  936. {
  937. // find a relation name, or maybe a list of them
  938. if (!parse_sql && terminator())
  939. break;
  940. do {
  941. gpre_rel* relation = EXP_relation();
  942. if (!relation)
  943. CPR_s_error("relation name");
  944. database = relation->rel_database;
  945. rrl* lock_block = (rrl*) MSC_alloc(RRL_LEN);
  946. lock_block->rrl_next = database->dbb_rrls;
  947. lock_block->rrl_relation = relation;
  948. database->dbb_rrls = lock_block;
  949. } while (MSC_match(KW_COMMA));
  950. // get the lock level and mode and apply them to all the
  951. // relations in the list
  952. MSC_match(KW_FOR);
  953. USHORT lock_level = (flags & TRA_con) ? isc_tpb_protected : isc_tpb_shared;
  954. USHORT lock_mode = isc_tpb_lock_read;
  955. if (MSC_match(KW_PROTECTED))
  956. lock_level = isc_tpb_protected;
  957. else if (MSC_match(KW_EXCLUSIVE))
  958. lock_level = isc_tpb_exclusive;
  959. else if (MSC_match(KW_SHARED))
  960. lock_level = isc_tpb_shared;
  961. if (MSC_match(KW_WRITE))
  962. {
  963. if (flags & TRA_ro)
  964. CPR_error("write lock requested for a read_only transaction");
  965. lock_mode = isc_tpb_lock_write;
  966. }
  967. else
  968. MSC_match(KW_READ);
  969. for (database = gpreGlob.isc_databases; database; database = database->dbb_next)
  970. {
  971. for (rrl* lock_block = database->dbb_rrls; lock_block; lock_block = lock_block->rrl_next)
  972. {
  973. if (!lock_block->rrl_lock_level)
  974. {
  975. fb_assert(lock_level <= MAX_UCHAR);
  976. fb_assert(lock_mode <= MAX_UCHAR);
  977. lock_block->rrl_lock_level = (UCHAR) lock_level;
  978. lock_block->rrl_lock_mode = (UCHAR) lock_mode;
  979. }
  980. }
  981. }
  982. if (!MSC_match(KW_COMMA))
  983. break;
  984. }
  985. }
  986. //____________________________________________________________
  987. //
  988. // Initialize the request and the ready.
  989. //
  990. gpre_req* PAR_set_up_dpb_info(rdy* ready, act* action, USHORT buffercount)
  991. {
  992. ready->rdy_database->dbb_buffercount = buffercount;
  993. gpre_req* request = MSC_request(REQ_ready);
  994. request->req_database = ready->rdy_database;
  995. request->req_actions = action;
  996. ready->rdy_request = request;
  997. return request;
  998. }
  999. //____________________________________________________________
  1000. //
  1001. // Make a symbol from the current token, and advance
  1002. // to the next token. If a symbol type other than
  1003. // SYM_dummy, the symbol can be overloaded, but not
  1004. // redefined.
  1005. //
  1006. gpre_sym* PAR_symbol(sym_t type)
  1007. {
  1008. gpre_sym* symbol;
  1009. for (symbol = gpreGlob.token_global.tok_symbol; symbol; symbol = symbol->sym_homonym)
  1010. if (type == SYM_dummy || symbol->sym_type == type)
  1011. {
  1012. TEXT s[ERROR_LENGTH];
  1013. fb_utils::snprintf(s, sizeof(s),
  1014. "symbol %s is already in use", gpreGlob.token_global.tok_string);
  1015. PAR_error(s);
  1016. }
  1017. symbol = MSC_symbol(SYM_cursor, gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length, 0);
  1018. PAR_get_token();
  1019. return symbol;
  1020. }
  1021. //____________________________________________________________
  1022. //
  1023. // There's been a parse error, so unwind out.
  1024. //
  1025. void PAR_unwind()
  1026. {
  1027. throw Firebird::LongJump();
  1028. }
  1029. //____________________________________________________________
  1030. //
  1031. // mark databases specified in start_transaction and set transaction
  1032. // statements.
  1033. //
  1034. void PAR_using_db()
  1035. {
  1036. while (true)
  1037. {
  1038. gpre_sym* symbol = MSC_find_symbol(gpreGlob.token_global.tok_symbol, SYM_database);
  1039. if (symbol)
  1040. {
  1041. gpre_dbb* db = (gpre_dbb*) symbol->sym_object;
  1042. db->dbb_flags |= DBB_in_trans;
  1043. }
  1044. else
  1045. CPR_s_error("database handle");
  1046. PAR_get_token();
  1047. if (!MSC_match(KW_COMMA))
  1048. break;
  1049. }
  1050. }
  1051. #ifdef FTN_BLK_DATA
  1052. //____________________________________________________________
  1053. //
  1054. //
  1055. // Damn fortran sometimes only allows global
  1056. // initializations in block data. This collects
  1057. // names of dbs to be so handled.
  1058. //
  1059. static void block_data_list( const gpre_dbb* db)
  1060. {
  1061. if (db->dbb_scope == DBB_EXTERN)
  1062. return;
  1063. const TEXT* name = db->dbb_name->sym_string;
  1064. const dbd* list = gpreGlob.global_db_list;
  1065. if (gpreGlob.global_db_count)
  1066. if (gpreGlob.global_db_count > MAX_DATABASES) {
  1067. PAR_error("Database limit exceeded: 32 databases per source file.");
  1068. }
  1069. else
  1070. {
  1071. for (const dbd* const end = gpreGlob.global_db_list + gpreGlob.global_db_count;
  1072. list < end; list++)
  1073. {
  1074. if (!strcmp(name, list->dbd_name))
  1075. return;
  1076. }
  1077. }
  1078. if (gpreGlob.global_db_count >= MAX_DATABASES)
  1079. return;
  1080. strcpy(list->dbd_name, name); // safe while dbd_name is defined bigger than dbb_name->sym_string
  1081. gpreGlob.global_db_count++;
  1082. }
  1083. #endif
  1084. //____________________________________________________________
  1085. //
  1086. //
  1087. // For reasons best left unnamed, we need
  1088. // to skip the contents of a parenthesized
  1089. // list
  1090. //
  1091. static bool match_parentheses()
  1092. {
  1093. USHORT paren_count = 0;
  1094. if (MSC_match(KW_LEFT_PAREN))
  1095. {
  1096. paren_count++;
  1097. while (paren_count)
  1098. {
  1099. if (MSC_match(KW_RIGHT_PAREN))
  1100. paren_count--;
  1101. else if (MSC_match(KW_LEFT_PAREN))
  1102. paren_count++;
  1103. else
  1104. PAR_get_token();
  1105. }
  1106. return true;
  1107. }
  1108. return false;
  1109. }
  1110. //____________________________________________________________
  1111. //
  1112. // Parse a free standing ANY expression.
  1113. //
  1114. static act* par_any()
  1115. {
  1116. // For time being flag as an error
  1117. PAR_error("Free standing any not supported");
  1118. return NULL; // make the compiler happy.
  1119. /*
  1120. gpre_sym* symbol = NULL;
  1121. // Make up request block. Since this might not be a database statement,
  1122. // stay ready to back out if necessay.
  1123. gpre_req* request = MSC_request(REQ_any);
  1124. par_options(request, true);
  1125. gpre_rse* rec_expr = EXP_rse(request, symbol);
  1126. EXP_rse_cleanup(rec_expr);
  1127. act* action = MSC_action(request, ACT_any);
  1128. request->req_rse = rec_expr;
  1129. gpre_ctx* context = rec_expr->rse_context[0];
  1130. gpre_rel* relation = context->ctx_relation;
  1131. request->req_database = relation->rel_database;
  1132. act* function = MSC_action(0, ACT_function);
  1133. function->act_object = (ref*) action;
  1134. function->act_next = gpreGlob.global_functions;
  1135. gpreGlob.global_functions = function;
  1136. return action;
  1137. */
  1138. }
  1139. //____________________________________________________________
  1140. //
  1141. // Parse a free reference to a database field in general
  1142. // program context. If the next keyword isn't a context
  1143. // varying, this isn't an array element reference.
  1144. //
  1145. static act* par_array_element()
  1146. {
  1147. if (!MSC_find_symbol(gpreGlob.token_global.tok_symbol, SYM_context))
  1148. return NULL;
  1149. gpre_ctx* context;
  1150. gpre_fld* field = EXP_field(&context);
  1151. gpre_req* request = context->ctx_request;
  1152. gpre_nod* node = EXP_array(request, field, false, false);
  1153. ref* reference = MSC_reference(&request->req_references);
  1154. reference->ref_expr = node;
  1155. gpre_fld* element = field->fld_array;
  1156. reference->ref_field = element;
  1157. element->fld_symbol = field->fld_symbol;
  1158. reference->ref_context = context;
  1159. node->nod_arg[0] = (gpre_nod*) reference;
  1160. act* action = MSC_action(request, ACT_variable);
  1161. action->act_object = reference;
  1162. return action;
  1163. }
  1164. //____________________________________________________________
  1165. //
  1166. // Parse an AT END clause.
  1167. //
  1168. static act* par_at()
  1169. {
  1170. if (!MSC_match(KW_END) || !cur_fetch)
  1171. return NULL;
  1172. act* action = (act*) cur_fetch->lls_object;
  1173. return MSC_action(action->act_request, ACT_at_end);
  1174. }
  1175. //____________________________________________________________
  1176. //
  1177. // Parse a BASED ON clause. If this
  1178. // is fortran and we don't have a database
  1179. // declared yet, don't parse it completely.
  1180. // If this is PLI look for a left paren or
  1181. // a semi colon to avoid stomping a
  1182. // DECLARE i FIXED BIN BASED (X);
  1183. // or DECLARE LIST (10) FIXED BINARY BASED;
  1184. //
  1185. static act* par_based()
  1186. {
  1187. bool notSegment = false; // a COBOL specific patch
  1188. MSC_match(KW_ON);
  1189. act* action = MSC_action(0, ACT_basedon);
  1190. bas* based_on = (bas*) MSC_alloc(BAS_LEN);
  1191. action->act_object = (ref*) based_on;
  1192. if ((gpreGlob.sw_language != lang_fortran) || gpreGlob.isc_databases)
  1193. {
  1194. TEXT s[ERROR_LENGTH];
  1195. gpre_rel* relation = EXP_relation();
  1196. if (!MSC_match(KW_DOT))
  1197. CPR_s_error("dot in qualified field reference");
  1198. SQL_resolve_identifier("<fieldname>", NULL, NAME_SIZE + 1);
  1199. if (gpreGlob.token_global.tok_length >= NAME_SIZE)
  1200. PAR_error("Field length too long");
  1201. gpre_fld* field = MET_field(relation, gpreGlob.token_global.tok_string);
  1202. if (!field)
  1203. {
  1204. fb_utils::snprintf(s, sizeof(s), "undefined field %s", gpreGlob.token_global.tok_string);
  1205. PAR_error(s);
  1206. }
  1207. if (SQL_DIALECT_V5 == gpreGlob.sw_sql_dialect)
  1208. {
  1209. switch (field->fld_dtype)
  1210. {
  1211. case dtype_sql_date:
  1212. case dtype_sql_time:
  1213. case dtype_int64:
  1214. PAR_error("BASED ON impermissible datatype for a dialect-1 program");
  1215. }
  1216. }
  1217. PAR_get_token();
  1218. char tmpChar[2]; // a COBOL specific patch
  1219. if (gpreGlob.sw_language == lang_cobol && gpreGlob.token_global.tok_keyword == KW_DOT) {
  1220. strcpy(tmpChar, gpreGlob.token_global.tok_string);
  1221. }
  1222. if (MSC_match(KW_DOT))
  1223. {
  1224. if (!MSC_match(KW_SEGMENT))
  1225. {
  1226. if (gpreGlob.sw_language != lang_cobol)
  1227. PAR_error("only .SEGMENT allowed after qualified field name");
  1228. else
  1229. {
  1230. strcpy(based_on->bas_terminator, tmpChar);
  1231. notSegment = true;
  1232. }
  1233. }
  1234. else if (!(field->fld_flags & FLD_blob))
  1235. {
  1236. fb_utils::snprintf(s, sizeof(s), "field %s is not a blob", field->fld_symbol->sym_string);
  1237. PAR_error(s);
  1238. }
  1239. // this flag is to solve KW_DOT problem in COBOL. Should be false for all other lang
  1240. if (!notSegment)
  1241. based_on->bas_flags |= BAS_segment;
  1242. }
  1243. based_on->bas_field = field;
  1244. }
  1245. else
  1246. {
  1247. based_on->bas_rel_name = (STR) MSC_alloc(gpreGlob.token_global.tok_length + 1);
  1248. MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length,
  1249. based_on->bas_rel_name->str_string);
  1250. PAR_get_token();
  1251. if (!MSC_match(KW_DOT))
  1252. PAR_error("expected qualified field name");
  1253. else
  1254. {
  1255. based_on->bas_fld_name = (STR) MSC_alloc(gpreGlob.token_global.tok_length + 1);
  1256. MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length,
  1257. based_on->bas_fld_name->str_string);
  1258. bool ambiguous_flag = false;
  1259. PAR_get_token();
  1260. if (MSC_match(KW_DOT))
  1261. {
  1262. based_on->bas_db_name = based_on->bas_rel_name;
  1263. based_on->bas_rel_name = based_on->bas_fld_name;
  1264. based_on->bas_fld_name = (STR) MSC_alloc(gpreGlob.token_global.tok_length + 1);
  1265. MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length,
  1266. based_on->bas_fld_name->str_string);
  1267. if (gpreGlob.token_global.tok_keyword == KW_SEGMENT)
  1268. ambiguous_flag = true;
  1269. PAR_get_token();
  1270. if (MSC_match(KW_DOT))
  1271. {
  1272. if (!MSC_match(KW_SEGMENT))
  1273. PAR_error("too many qualifiers on field name");
  1274. based_on->bas_flags |= BAS_segment;
  1275. ambiguous_flag = false;
  1276. }
  1277. }
  1278. if (ambiguous_flag)
  1279. based_on->bas_flags |= BAS_ambiguous;
  1280. }
  1281. }
  1282. switch (gpreGlob.sw_language)
  1283. {
  1284. case lang_internal:
  1285. case lang_fortran:
  1286. case lang_c:
  1287. case lang_cxx:
  1288. do {
  1289. MSC_push((gpre_nod*) PAR_native_value(false, false),
  1290. &based_on->bas_variables);
  1291. } while (MSC_match(KW_COMMA));
  1292. // bug_4031. based_on->bas_variables are now in reverse order.
  1293. // we must reverse the order so we can output them to the .c
  1294. // file correctly.
  1295. if (based_on->bas_variables->lls_next)
  1296. {
  1297. gpre_lls* t1 = based_on->bas_variables; // last one in the old list
  1298. gpre_lls* t2 = NULL; // last one in the new list
  1299. gpre_lls* hold = t2; // beginning of new list
  1300. // while we still have a next one, keep going thru
  1301. while (t1->lls_next)
  1302. {
  1303. // now find the last one in the list
  1304. while (t1->lls_next->lls_next)
  1305. t1 = t1->lls_next;
  1306. // if this is the first time thru, set hold
  1307. if (hold == NULL)
  1308. {
  1309. hold = t1->lls_next;
  1310. t2 = hold;
  1311. }
  1312. else
  1313. {
  1314. // not first time thru, add this one to the end
  1315. // of the new list
  1316. t2->lls_next = t1->lls_next;
  1317. t2 = t2->lls_next;
  1318. }
  1319. // now null out the last one, and start again
  1320. t1->lls_next = NULL;
  1321. t1 = based_on->bas_variables;
  1322. }
  1323. // ok, we're done, tack the original lls onto the very
  1324. // end of the new list.
  1325. t2->lls_next = t1;
  1326. if (hold)
  1327. based_on->bas_variables = hold;
  1328. }
  1329. default:
  1330. break;
  1331. }
  1332. if (notSegment)
  1333. return action;
  1334. if (gpreGlob.token_global.tok_keyword == KW_SEMI_COLON ||
  1335. (gpreGlob.sw_language == lang_cobol && gpreGlob.token_global.tok_keyword == KW_DOT))
  1336. {
  1337. strcpy(based_on->bas_terminator, gpreGlob.token_global.tok_string);
  1338. PAR_get_token();
  1339. }
  1340. return action;
  1341. }
  1342. //____________________________________________________________
  1343. //
  1344. // If this is a PASCAL program, and we're
  1345. // in a code block, then increment the
  1346. // brace count. If we're in a routine
  1347. // declaration, then we've reached the start
  1348. // of the code block and should mark it as
  1349. // a new routine.
  1350. //
  1351. static act* par_begin()
  1352. {
  1353. if (gpreGlob.sw_language == lang_pascal)
  1354. {
  1355. routine_decl = false;
  1356. gpreGlob.cur_routine->act_count++;
  1357. }
  1358. return NULL;
  1359. }
  1360. //____________________________________________________________
  1361. //
  1362. // Parse a blob handle and return the blob.
  1363. //
  1364. static blb* par_blob()
  1365. {
  1366. gpre_sym* symbol = MSC_find_symbol(gpreGlob.token_global.tok_symbol, SYM_blob);
  1367. if (!symbol)
  1368. CPR_s_error("blob handle");
  1369. PAR_get_token();
  1370. return (blb*) symbol->sym_object;
  1371. }
  1372. //____________________________________________________________
  1373. //
  1374. // Parse a GET_SEGMENT, PUT_SEGMENT, CLOSE_BLOB or CANCEL_BLOB.
  1375. //
  1376. static act* par_blob_action( act_t type)
  1377. {
  1378. blb* blob = par_blob();
  1379. act* action = MSC_action(blob->blb_request, type);
  1380. action->act_object = (ref*) blob;
  1381. // Need to eat the semicolon if present
  1382. if (gpreGlob.sw_language == lang_c)
  1383. MSC_match(KW_SEMI_COLON);
  1384. else
  1385. PAR_end();
  1386. return action;
  1387. }
  1388. //____________________________________________________________
  1389. //
  1390. // Parse a blob segment or blob field reference.
  1391. //
  1392. static act* par_blob_field()
  1393. {
  1394. const bool first = gpreGlob.token_global.tok_first;
  1395. blb* blob = par_blob();
  1396. act_t type = ACT_blob_handle;
  1397. if (MSC_match(KW_DOT))
  1398. {
  1399. if (MSC_match(KW_SEGMENT))
  1400. type = ACT_segment;
  1401. else if (MSC_match(KW_LENGTH))
  1402. type = ACT_segment_length;
  1403. else
  1404. CPR_s_error("SEGMENT or LENGTH");
  1405. }
  1406. act* action = MSC_action(blob->blb_request, type);
  1407. if (first)
  1408. action->act_flags |= ACT_first;
  1409. action->act_object = (ref*) blob;
  1410. return action;
  1411. }
  1412. //____________________________________________________________
  1413. //
  1414. // If this is a PASCAL program, and we're
  1415. // in a code block, then a case statement
  1416. // will end with an END, so it adds to the
  1417. // begin count.
  1418. //
  1419. static act* par_case()
  1420. {
  1421. if (gpreGlob.sw_language == lang_pascal && !routine_decl)
  1422. gpreGlob.cur_routine->act_count++;
  1423. return NULL;
  1424. }
  1425. //____________________________________________________________
  1426. //
  1427. // Parse degenerate CLEAR_HANDLES command.
  1428. //
  1429. static act* par_clear_handles()
  1430. {
  1431. return MSC_action(0, ACT_clear_handles);
  1432. }
  1433. //____________________________________________________________
  1434. //
  1435. // Parse a DERIVED_FROM clause. Like
  1436. // BASED ON but for C/C++ prototypes.
  1437. //
  1438. static act* par_derived_from()
  1439. {
  1440. if (gpreGlob.sw_language != lang_c && !isLangCpp(gpreGlob.sw_language)) {
  1441. return (NULL);
  1442. }
  1443. act* action = MSC_action(0, ACT_basedon);
  1444. bas* based_on = (bas*) MSC_alloc(BAS_LEN);
  1445. action->act_object = (ref*) based_on;
  1446. gpre_rel* relation = EXP_relation();
  1447. if (!MSC_match(KW_DOT))
  1448. CPR_s_error("dot in qualified field reference");
  1449. SQL_resolve_identifier("<Field Name>", NULL, NAME_SIZE);
  1450. gpre_fld* field = MET_field(relation, gpreGlob.token_global.tok_string);
  1451. if (!field)
  1452. {
  1453. TEXT s[ERROR_LENGTH];
  1454. fb_utils::snprintf(s, sizeof(s), "undefined field %s", gpreGlob.token_global.tok_string);
  1455. PAR_error(s);
  1456. }
  1457. PAR_get_token();
  1458. based_on->bas_field = field;
  1459. based_on->bas_variables = (gpre_lls*) MSC_alloc(LLS_LEN);
  1460. based_on->bas_variables->lls_next = NULL;
  1461. based_on->bas_variables->lls_object = (gpre_nod*) PAR_native_value(false, false);
  1462. strcpy(based_on->bas_terminator, gpreGlob.token_global.tok_string);
  1463. PAR_get_token();
  1464. return action;
  1465. }
  1466. //____________________________________________________________
  1467. //
  1468. //
  1469. // If the language is PASCAL, and if we're
  1470. // the body of a routine, every END counts
  1471. // against the number of BEGIN's and CASE's
  1472. // and when the count comes to zero, we SHOULD
  1473. // be at the end of the current routine, so
  1474. // pop it off the routine stack.
  1475. //
  1476. static act* par_end_block()
  1477. {
  1478. if (gpreGlob.sw_language == lang_pascal &&
  1479. !routine_decl && --gpreGlob.cur_routine->act_count == 0 && routine_stack)
  1480. {
  1481. gpreGlob.cur_routine = (act*) MSC_pop(&routine_stack);
  1482. }
  1483. return NULL;
  1484. }
  1485. //____________________________________________________________
  1486. //
  1487. // Parse an END_ERROR statement. Piece of cake.
  1488. //
  1489. static act* par_end_error()
  1490. {
  1491. // avoid parsing an ada exception end_error -
  1492. // check for a semicolon
  1493. if (!PAR_end() && gpreGlob.sw_language == lang_ada)
  1494. return NULL;
  1495. if (!cur_error)
  1496. PAR_error("END_ERROR used out of context");
  1497. if (!((act*) MSC_pop(&cur_error)))
  1498. return NULL;
  1499. // Need to eat the semicolon for c if present
  1500. if (gpreGlob.sw_language == lang_c)
  1501. MSC_match(KW_SEMI_COLON);
  1502. return MSC_action(0, ACT_enderror);
  1503. }
  1504. //____________________________________________________________
  1505. //
  1506. // Parse END_FETCH statement (clause?).
  1507. //
  1508. static act* par_end_fetch()
  1509. {
  1510. if (!cur_fetch)
  1511. PAR_error("END_FETCH used out of context");
  1512. act* begin_action = (act*) MSC_pop(&cur_fetch);
  1513. act* action = MSC_action(begin_action->act_request, ACT_hctef);
  1514. begin_action->act_pair = action;
  1515. action->act_pair = begin_action;
  1516. PAR_end();
  1517. return action;
  1518. }
  1519. //____________________________________________________________
  1520. //
  1521. // Parse a FOR loop terminator.
  1522. //
  1523. static act* par_end_for()
  1524. {
  1525. if (!cur_for)
  1526. PAR_error("unmatched END_FOR");
  1527. act* begin_action = (act*) MSC_pop(&cur_for);
  1528. if (!begin_action)
  1529. return NULL;
  1530. PAR_end();
  1531. gpre_req* request = begin_action->act_request;
  1532. // If the action is a blob for, make up a blob end.
  1533. if (begin_action->act_type == ACT_blob_for)
  1534. {
  1535. blb* blob = (blb*) begin_action->act_object;
  1536. act* action = MSC_action(request, ACT_endblob);
  1537. action->act_object = (ref*) blob;
  1538. begin_action->act_pair = action;
  1539. action->act_pair = begin_action;
  1540. HSH_remove(blob->blb_symbol);
  1541. blob->blb_flags |= BLB_symbol_released;
  1542. return action;
  1543. }
  1544. // If there isn't a database assigned, the FOR statement itself
  1545. // failed. Since an error has been given, just return quietly.
  1546. if (!request->req_database)
  1547. return NULL;
  1548. act* action = MSC_action(request, ACT_endfor);
  1549. begin_action->act_pair = action;
  1550. action->act_pair = begin_action;
  1551. EXP_rse_cleanup(request->req_rse);
  1552. for (blb* blob = request->req_blobs; blob; blob = blob->blb_next)
  1553. {
  1554. if (!(blob->blb_flags & BLB_symbol_released))
  1555. HSH_remove(blob->blb_symbol);
  1556. }
  1557. return action;
  1558. }
  1559. //____________________________________________________________
  1560. //
  1561. // Parse and process END_MODIFY. The processing mostly includes
  1562. // copying field references to proper context at proper level.
  1563. //
  1564. static act* par_end_modify()
  1565. {
  1566. if (!cur_modify)
  1567. PAR_error("unmatched END_MODIFY");
  1568. PAR_end();
  1569. upd* modify = (upd*) MSC_pop(&cur_modify);
  1570. if (gpreGlob.errors_global)
  1571. return NULL;
  1572. gpre_req* request = modify->upd_request;
  1573. // used at the end of this function
  1574. act* begin_action = request->req_actions;
  1575. while ((upd*) begin_action->act_object != modify)
  1576. begin_action = begin_action->act_next;
  1577. // Build assignments for all fields and null flags referenced
  1578. gpre_lls* stack = NULL;
  1579. int count = 0;
  1580. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  1581. {
  1582. if (reference->ref_context == modify->upd_source &&
  1583. reference->ref_level >= modify->upd_level &&
  1584. !reference->ref_master)
  1585. {
  1586. ref* change = MSC_reference(&modify->upd_references);
  1587. change->ref_context = modify->upd_update;
  1588. change->ref_field = reference->ref_field;
  1589. change->ref_source = reference;
  1590. change->ref_flags = reference->ref_flags;
  1591. gpre_nod* item = MSC_node(nod_assignment, 2);
  1592. item->nod_arg[0] = MSC_unary(nod_value, (gpre_nod*) change);
  1593. item->nod_arg[1] = MSC_unary(nod_field, (gpre_nod*) change);
  1594. MSC_push((gpre_nod*) item, &stack);
  1595. count++;
  1596. if (reference->ref_null)
  1597. {
  1598. ref* flag = MSC_reference(&modify->upd_references);
  1599. flag->ref_context = change->ref_context;
  1600. flag->ref_field = flag_field;
  1601. flag->ref_master = change;
  1602. flag->ref_source = reference->ref_null;
  1603. change->ref_null = flag;
  1604. item = MSC_node(nod_assignment, 2);
  1605. item->nod_arg[0] = MSC_unary(nod_value, (gpre_nod*) flag);
  1606. item->nod_arg[1] = MSC_unary(nod_field, (gpre_nod*) flag);
  1607. MSC_push((gpre_nod*) item, &stack);
  1608. count++;
  1609. }
  1610. }
  1611. }
  1612. // Build a list node of the assignments
  1613. gpre_nod* assignments = MSC_node(nod_list, (SSHORT) count);
  1614. modify->upd_assignments = assignments;
  1615. gpre_nod** ptr = assignments->nod_arg + count;
  1616. while (stack)
  1617. *--ptr = (gpre_nod*) MSC_pop(&stack);
  1618. act* action = MSC_action(request, ACT_endmodify);
  1619. action->act_object = (ref*) modify;
  1620. begin_action->act_pair = action;
  1621. action->act_pair = begin_action;
  1622. return action;
  1623. }
  1624. //____________________________________________________________
  1625. //
  1626. // Parse a stream END statement.
  1627. //
  1628. static act* par_end_stream()
  1629. {
  1630. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  1631. if (!symbol || symbol->sym_type != SYM_stream)
  1632. CPR_s_error("stream cursor");
  1633. gpre_req* request = (gpre_req*) symbol->sym_object;
  1634. HSH_remove(symbol);
  1635. EXP_rse_cleanup(request->req_rse);
  1636. PAR_get_token();
  1637. PAR_end();
  1638. return MSC_action(request, ACT_s_end);
  1639. }
  1640. //____________________________________________________________
  1641. //
  1642. // Process an END_STORE.
  1643. //
  1644. static act* par_end_store(bool special)
  1645. {
  1646. if (!cur_store)
  1647. PAR_error("unmatched END_STORE");
  1648. PAR_end();
  1649. act* begin_action = (act*) MSC_pop(&cur_store);
  1650. gpre_req* request = begin_action->act_request;
  1651. if (request->req_type == REQ_store)
  1652. {
  1653. if (gpreGlob.errors_global)
  1654. return NULL;
  1655. // Make up an assignment list for all field references
  1656. int count = 0;
  1657. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  1658. {
  1659. if (!reference->ref_master)
  1660. count++;
  1661. }
  1662. gpre_nod* const assignments = MSC_node(nod_list, (SSHORT) count);
  1663. request->req_node =
  1664. MSC_ternary(nod_store, (gpre_nod*) request->req_contexts, assignments, NULL);
  1665. count = 0;
  1666. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  1667. {
  1668. if (reference->ref_master)
  1669. continue;
  1670. gpre_nod* item = MSC_node(nod_assignment, 2);
  1671. item->nod_arg[0] = MSC_unary(nod_value, (gpre_nod*) reference);
  1672. item->nod_arg[1] = MSC_unary(nod_field, (gpre_nod*) reference);
  1673. assignments->nod_arg[count++] = item;
  1674. }
  1675. }
  1676. else
  1677. {
  1678. // if the request type is store2, we have store ...returning_values.
  1679. // The next action on the cur_store stack points to a upd structure
  1680. // which will give us the assignments for this one.
  1681. act* action2 = (act*) MSC_pop(&cur_store);
  1682. upd* return_values = (upd*) action2->act_object;
  1683. // Build assignments for all fields and null flags referenced
  1684. gpre_lls* stack = NULL;
  1685. int count = 0;
  1686. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  1687. {
  1688. if (reference->ref_context == return_values->upd_update &&
  1689. reference->ref_level >= return_values->upd_level &&
  1690. !reference->ref_master)
  1691. {
  1692. ref* change = MSC_reference(&return_values->upd_references);
  1693. change->ref_context = return_values->upd_update;
  1694. change->ref_field = reference->ref_field;
  1695. change->ref_source = reference;
  1696. change->ref_flags = reference->ref_flags;
  1697. gpre_nod* item = MSC_node(nod_assignment, 2);
  1698. item->nod_arg[0] = MSC_unary(nod_field, (gpre_nod*) change);
  1699. item->nod_arg[1] = MSC_unary(nod_value, (gpre_nod*) change);
  1700. MSC_push((gpre_nod*) item, &stack);
  1701. count++;
  1702. }
  1703. }
  1704. // Build a list node of the assignments
  1705. gpre_nod* const assignments = MSC_node(nod_list, (SSHORT) count);
  1706. return_values->upd_assignments = assignments;
  1707. gpre_nod** ptr = assignments->nod_arg + count;
  1708. while (stack)
  1709. *--ptr = (gpre_nod*) MSC_pop(&stack);
  1710. }
  1711. gpre_ctx* context = request->req_contexts;
  1712. if (context)
  1713. HSH_remove(context->ctx_symbol);
  1714. act* action;
  1715. if (special)
  1716. action = MSC_action(request, ACT_endstore_special);
  1717. else
  1718. action = MSC_action(request, ACT_endstore);
  1719. begin_action->act_pair = action;
  1720. action->act_pair = begin_action;
  1721. return action;
  1722. }
  1723. //____________________________________________________________
  1724. //
  1725. // Parse a ERASE statement.
  1726. //
  1727. static act* par_erase()
  1728. {
  1729. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  1730. if (!symbol || symbol->sym_type != SYM_context)
  1731. CPR_s_error("context variable");
  1732. gpre_ctx* source = symbol->sym_object;
  1733. gpre_req* request = source->ctx_request;
  1734. if (request->req_type != REQ_for && request->req_type != REQ_cursor)
  1735. PAR_error("invalid context for modify");
  1736. PAR_get_token();
  1737. PAR_end();
  1738. // Make an update block to hold everything known about the modify
  1739. upd* erase = (upd*) MSC_alloc(UPD_LEN);
  1740. erase->upd_request = request;
  1741. erase->upd_source = source;
  1742. act* action = MSC_action(request, ACT_erase);
  1743. action->act_object = (ref*) erase;
  1744. return action;
  1745. }
  1746. //____________________________________________________________
  1747. //
  1748. // Parse a stream FETCH statement.
  1749. //
  1750. static act* par_fetch()
  1751. {
  1752. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  1753. if (!symbol || symbol->sym_type != SYM_stream)
  1754. return NULL;
  1755. gpre_req* request = (gpre_req*) symbol->sym_object;
  1756. PAR_get_token();
  1757. PAR_end();
  1758. act* action = MSC_action(request, ACT_s_fetch);
  1759. MSC_push((gpre_nod*) action, &cur_fetch);
  1760. return action;
  1761. }
  1762. //____________________________________________________________
  1763. //
  1764. // Parse a FINISH statement.
  1765. //
  1766. static act* par_finish()
  1767. {
  1768. act* action = MSC_action(0, ACT_finish);
  1769. if (!terminator())
  1770. while (true)
  1771. {
  1772. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  1773. if (symbol && (symbol->sym_type == SYM_database))
  1774. {
  1775. rdy* ready = (rdy*) MSC_alloc(RDY_LEN);
  1776. ready->rdy_next = (rdy*) action->act_object;
  1777. action->act_object = (ref*) ready;
  1778. ready->rdy_database = (gpre_dbb*) symbol->sym_object;
  1779. CPR_eol_token();
  1780. }
  1781. else
  1782. CPR_s_error("database handle");
  1783. if (terminator())
  1784. break;
  1785. if (!MSC_match(KW_COMMA))
  1786. break;
  1787. }
  1788. if (gpreGlob.sw_language == lang_ada)
  1789. MSC_match(KW_SEMI_COLON);
  1790. return action;
  1791. }
  1792. //____________________________________________________________
  1793. //
  1794. // Parse a FOR clause, returning an action.
  1795. // We don't know where we are a host language FOR, a record looping
  1796. // FOR, or a blob FOR. Parse a little ahead and try to find out.
  1797. // Avoid stepping on user routines that use GDML keywords
  1798. //
  1799. static act* par_for()
  1800. {
  1801. TEXT s[ERROR_LENGTH];
  1802. gpre_sym* symbol = NULL;
  1803. bool dup_symbol = false;
  1804. tok& token = gpreGlob.token_global;
  1805. if (token.tok_keyword != KW_FIRST && token.tok_keyword != KW_LEFT_PAREN)
  1806. {
  1807. if (token.tok_symbol)
  1808. dup_symbol = true;
  1809. symbol = MSC_symbol(SYM_cursor, token.tok_string, token.tok_length, 0);
  1810. PAR_get_token();
  1811. if (!MSC_match(KW_IN))
  1812. {
  1813. MSC_free(symbol);
  1814. return NULL;
  1815. }
  1816. if (dup_symbol)
  1817. {
  1818. fb_utils::snprintf(s, sizeof(s), "symbol %s is already in use", token.tok_string);
  1819. PAR_error(s);
  1820. }
  1821. const gpre_sym* temp = token.tok_symbol;
  1822. if (temp && temp->sym_type == SYM_context)
  1823. return par_open_blob(ACT_blob_for, symbol);
  1824. }
  1825. // Make up request block. Since this might not be a database statement,
  1826. // stay ready to back out if necessay.
  1827. gpre_req* request = MSC_request(REQ_for);
  1828. gpre_rse* rec_expr;
  1829. if (!par_options(request, true) || !(rec_expr = EXP_rse(request, symbol)))
  1830. {
  1831. MSC_free_request(request);
  1832. return NULL;
  1833. }
  1834. act* action = MSC_action(request, ACT_for);
  1835. MSC_push((gpre_nod*) action, &cur_for);
  1836. request->req_rse = rec_expr;
  1837. gpre_ctx* context = rec_expr->rse_context[0];
  1838. gpre_rel* relation = context->ctx_relation;
  1839. request->req_database = relation->rel_database;
  1840. gpre_ctx** ptr = rec_expr->rse_context;
  1841. for (const gpre_ctx* const* const end = ptr + rec_expr->rse_count; ptr < end; ptr++)
  1842. {
  1843. context = *ptr;
  1844. context->ctx_next = request->req_contexts;
  1845. request->req_contexts = context;
  1846. }
  1847. return action;
  1848. }
  1849. //____________________________________________________________
  1850. //
  1851. // A function declaration is interesting in
  1852. // FORTRAN because it starts a new sub-module
  1853. // and we have to begin everything all over.
  1854. // In PASCAL it's interesting because it may
  1855. // indicate a good place to put message declarations.
  1856. // Unfortunately that requires a loose parse of the
  1857. // routine header, but what the hell...
  1858. //
  1859. static act* par_function()
  1860. {
  1861. if (gpreGlob.sw_language == lang_fortran)
  1862. return par_subroutine();
  1863. if (gpreGlob.sw_language == lang_pascal)
  1864. return par_procedure();
  1865. return NULL;
  1866. }
  1867. //____________________________________________________________
  1868. //
  1869. // Check a left brace (or whatever) for start of a new
  1870. // routine.
  1871. //
  1872. static act* par_left_brace()
  1873. {
  1874. if (brace_count++ - namespace_count > 0)
  1875. return NULL;
  1876. act* action = MSC_action(0, ACT_routine);
  1877. gpreGlob.cur_routine = action;
  1878. action->act_flags |= ACT_mark;
  1879. return action;
  1880. }
  1881. //____________________________________________________________
  1882. //
  1883. // Parse a MODIFY statement.
  1884. //
  1885. static act* par_modify()
  1886. {
  1887. // Set up modify and action blocks. This is done here to leave the
  1888. // structure in place to cleanly handle END_MODIFY under error conditions.
  1889. upd* modify = (upd*) MSC_alloc(UPD_LEN);
  1890. MSC_push((gpre_nod*) modify, &cur_modify);
  1891. // If the next token isn't a context variable, we can't continue
  1892. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  1893. if (!symbol || symbol->sym_type != SYM_context)
  1894. {
  1895. SCHAR s[ERROR_LENGTH];
  1896. fb_utils::snprintf(s, sizeof(s), "%s is not a valid context variable", gpreGlob.token_global.tok_string);
  1897. PAR_error(s);
  1898. }
  1899. gpre_ctx* source = symbol->sym_object;
  1900. gpre_req* request = source->ctx_request;
  1901. if (request->req_type != REQ_for && request->req_type != REQ_cursor)
  1902. PAR_error("invalid context for modify");
  1903. act* action = MSC_action(request, ACT_modify);
  1904. action->act_object = (ref*) modify;
  1905. PAR_get_token();
  1906. MSC_match(KW_USING);
  1907. // Make an update context by cloning the source context
  1908. gpre_ctx* update = MSC_context(request);
  1909. update->ctx_symbol = source->ctx_symbol;
  1910. update->ctx_relation = source->ctx_relation;
  1911. // Make an update block to hold everything known about the modify
  1912. modify->upd_request = request;
  1913. modify->upd_source = source;
  1914. modify->upd_update = update;
  1915. modify->upd_level = ++request->req_level;
  1916. return action;
  1917. }
  1918. //____________________________________________________________
  1919. //
  1920. // This rather degenerate routine exists to allow both:
  1921. //
  1922. // ON_ERROR
  1923. // ON ERROR
  1924. //
  1925. // so the more dim of our users avoid mistakes.
  1926. //
  1927. static act* par_on()
  1928. {
  1929. if (!MSC_match(KW_ERROR))
  1930. return NULL;
  1931. return par_on_error();
  1932. }
  1933. //____________________________________________________________
  1934. //
  1935. // Parse a trailing ON_ERROR clause.
  1936. //
  1937. static act* par_on_error()
  1938. {
  1939. if (!cur_statement)
  1940. PAR_error("ON_ERROR used out of context");
  1941. PAR_end();
  1942. act* action = MSC_action(0, ACT_on_error);
  1943. cur_statement->act_error = action;
  1944. action->act_object = (ref*) cur_statement;
  1945. MSC_push((gpre_nod*) action, &cur_error);
  1946. if (cur_statement->act_pair)
  1947. cur_statement->act_pair->act_error = action;
  1948. return action;
  1949. }
  1950. //____________________________________________________________
  1951. //
  1952. // Parse an "open blob" type statement. These include OPEN_BLOB,
  1953. // CREATE_BLOB, and blob FOR.
  1954. //
  1955. static act* par_open_blob( act_t act_op, gpre_sym* symbol)
  1956. {
  1957. // If somebody hasn't already parsed up a symbol for us, parse the
  1958. // symbol and the mandatory IN now.
  1959. if (!symbol)
  1960. {
  1961. symbol = PAR_symbol(SYM_dummy);
  1962. if (!MSC_match(KW_IN))
  1963. CPR_s_error("IN");
  1964. }
  1965. // The next thing we should find is a field reference. Get it.
  1966. gpre_ctx* context;
  1967. gpre_fld* field = EXP_field(&context);
  1968. if (!field)
  1969. return NULL;
  1970. TEXT s[ERROR_LENGTH];
  1971. if (!(field->fld_flags & FLD_blob))
  1972. {
  1973. fb_utils::snprintf(s, sizeof(s), "Field %s is not a blob", field->fld_symbol->sym_string);
  1974. PAR_error(s);
  1975. }
  1976. if (field->fld_array_info)
  1977. {
  1978. fb_utils::snprintf(s, sizeof(s), "Field %s is an array and can not be opened as a blob",
  1979. field->fld_symbol->sym_string);
  1980. PAR_error(s);
  1981. }
  1982. gpre_req* request = context->ctx_request;
  1983. ref* reference = EXP_post_field(field, context, false);
  1984. blb* blob = (blb*) MSC_alloc(BLB_LEN);
  1985. blob->blb_symbol = symbol;
  1986. blob->blb_reference = reference;
  1987. // See if we need a blob filter (do we have a subtype to subtype clause?)
  1988. for (;;)
  1989. {
  1990. if (MSC_match(KW_FILTER))
  1991. {
  1992. blob->blb_const_from_type = (MSC_match(KW_FROM)) ?
  1993. PAR_blob_subtype(request->req_database) : field->fld_sub_type;
  1994. if (!MSC_match(KW_TO))
  1995. CPR_s_error("TO");
  1996. blob->blb_const_to_type = PAR_blob_subtype(request->req_database);
  1997. }
  1998. else if (MSC_match(KW_STREAM))
  1999. blob->blb_type = isc_bpb_type_stream;
  2000. else
  2001. break;
  2002. }
  2003. if (!(blob->blb_seg_length = field->fld_seg_length))
  2004. blob->blb_seg_length = 512;
  2005. blob->blb_request = request;
  2006. blob->blb_next = request->req_blobs;
  2007. request->req_blobs = blob;
  2008. symbol->sym_type = SYM_blob;
  2009. symbol->sym_object = (gpre_ctx*) blob;
  2010. HSH_insert(symbol);
  2011. // You just inserted the context variable into the hash table.
  2012. // The current token however might be the same context variable.
  2013. // If so, get the symbol for it.
  2014. if (gpreGlob.token_global.tok_keyword == KW_none)
  2015. gpreGlob.token_global.tok_symbol = HSH_lookup(gpreGlob.token_global.tok_string);
  2016. act* action = MSC_action(request, act_op);
  2017. action->act_object = (ref*) blob;
  2018. if (act_op == ACT_blob_for)
  2019. MSC_push((gpre_nod*) action, &cur_for);
  2020. // Need to eat the semicolon if present
  2021. if (gpreGlob.sw_language == lang_c)
  2022. MSC_match(KW_SEMI_COLON);
  2023. else
  2024. PAR_end();
  2025. return action;
  2026. }
  2027. //____________________________________________________________
  2028. //
  2029. // Parse request options. Return true if successful, otherwise
  2030. // false. If a flag is set, don't give an error on false.
  2031. //
  2032. static bool par_options(gpre_req* request, bool flag)
  2033. {
  2034. if (!MSC_match(KW_LEFT_PAREN))
  2035. return true;
  2036. while (true)
  2037. {
  2038. if (MSC_match(KW_RIGHT_PAREN))
  2039. return true;
  2040. if (MSC_match(KW_REQUEST_HANDLE))
  2041. {
  2042. request->req_handle = PAR_native_value(false, true);
  2043. request->req_flags |= REQ_exp_hand;
  2044. }
  2045. else if (MSC_match(KW_TRANSACTION_HANDLE))
  2046. request->req_trans = PAR_native_value(false, true);
  2047. else if (MSC_match(KW_LEVEL))
  2048. request->req_request_level = PAR_native_value(false, false);
  2049. else
  2050. {
  2051. if (!flag)
  2052. CPR_s_error("request option");
  2053. return false;
  2054. }
  2055. MSC_match(KW_COMMA);
  2056. }
  2057. }
  2058. //____________________________________________________________
  2059. //
  2060. // If this is PLI, then we've got a new procedure.
  2061. //
  2062. // If this is PASCAL, then we've come upon
  2063. // a program, module, function, or procedure header.
  2064. // Alas and alack, we have to decide if this is
  2065. // a real header or a forward/external declaration.
  2066. //
  2067. // In either case, we make a mark-only action block,
  2068. // because that's real cheap. If it's a real routine,
  2069. // we make the action the current routine.
  2070. //
  2071. static act* par_procedure()
  2072. {
  2073. act* action;
  2074. if (gpreGlob.sw_language == lang_pascal)
  2075. {
  2076. routine_decl = true;
  2077. action = scan_routine_header();
  2078. if (!(action->act_flags & ACT_decl))
  2079. {
  2080. MSC_push((gpre_nod*) gpreGlob.cur_routine, &routine_stack);
  2081. gpreGlob.cur_routine = action;
  2082. }
  2083. }
  2084. else
  2085. action = NULL;
  2086. return action;
  2087. }
  2088. //____________________________________________________________
  2089. //
  2090. // Parse a READY statement.
  2091. //
  2092. static act* par_ready()
  2093. {
  2094. gpre_req* request;
  2095. gpre_sym* symbol;
  2096. gpre_dbb* db;
  2097. bool need_handle = false;
  2098. USHORT default_buffers = 0;
  2099. act* action = MSC_action(0, ACT_ready);
  2100. if (gpreGlob.token_global.tok_keyword == KW_CACHE)
  2101. CPR_s_error("database name or handle");
  2102. while (!terminator())
  2103. {
  2104. // this default mechanism is left here for backwards
  2105. // compatibility, but it is no longer documented and
  2106. // is not something we should maintain for all ready
  2107. // options since it needlessly complicates the ready
  2108. // statement without providing any extra functionality
  2109. if (MSC_match(KW_DEFAULT))
  2110. {
  2111. if (!MSC_match(KW_CACHE))
  2112. CPR_s_error("database name or handle");
  2113. default_buffers = atoi(gpreGlob.token_global.tok_string);
  2114. CPR_eol_token();
  2115. MSC_match(KW_BUFFERS);
  2116. continue;
  2117. }
  2118. rdy* ready = (rdy*) MSC_alloc(RDY_LEN);
  2119. ready->rdy_next = (rdy*) action->act_object;
  2120. action->act_object = (ref*) ready;
  2121. if (!(symbol = gpreGlob.token_global.tok_symbol) || symbol->sym_type != SYM_database)
  2122. {
  2123. ready->rdy_filename = PAR_native_value(false, false);
  2124. if (MSC_match(KW_AS))
  2125. need_handle = true;
  2126. }
  2127. if (!(symbol = gpreGlob.token_global.tok_symbol) || symbol->sym_type != SYM_database)
  2128. {
  2129. if (!gpreGlob.isc_databases || gpreGlob.isc_databases->dbb_next || need_handle)
  2130. {
  2131. need_handle = false;
  2132. CPR_s_error("database handle");
  2133. }
  2134. ready->rdy_database = gpreGlob.isc_databases;
  2135. }
  2136. need_handle = false;
  2137. if (!ready->rdy_database)
  2138. ready->rdy_database = (gpre_dbb*) symbol->sym_object;
  2139. if (terminator())
  2140. break;
  2141. CPR_eol_token();
  2142. // pick up the possible parameters, in any order
  2143. USHORT buffers = 0;
  2144. db = ready->rdy_database;
  2145. for (;;)
  2146. {
  2147. if (MSC_match(KW_CACHE))
  2148. {
  2149. buffers = atoi(gpreGlob.token_global.tok_string);
  2150. CPR_eol_token();
  2151. MSC_match(KW_BUFFERS);
  2152. }
  2153. else if (MSC_match(KW_USER))
  2154. db->dbb_r_user = PAR_native_value(false, false);
  2155. else if (MSC_match(KW_PASSWORD))
  2156. db->dbb_r_password = PAR_native_value(false, false);
  2157. else if (MSC_match(KW_LC_MESSAGES))
  2158. db->dbb_r_lc_messages = PAR_native_value(false, false);
  2159. else if (MSC_match(KW_LC_CTYPE))
  2160. {
  2161. db->dbb_r_lc_ctype = PAR_native_value(false, false);
  2162. db->dbb_know_subtype = 2;
  2163. }
  2164. else
  2165. break;
  2166. }
  2167. request = NULL;
  2168. if (buffers)
  2169. request = PAR_set_up_dpb_info(ready, action, buffers);
  2170. // if there are any options that take host variables as arguments,
  2171. // make sure that we generate variables for the request so that the
  2172. // dpb can be extended at runtime
  2173. if (db->dbb_r_user || db->dbb_r_password || db->dbb_r_lc_messages || db->dbb_r_lc_ctype)
  2174. {
  2175. if (!request)
  2176. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2177. request->req_flags |= REQ_extend_dpb;
  2178. }
  2179. // ... and if there are compile time user or password specified,
  2180. // make sure there will be a dpb generated for them
  2181. if (!request && (db->dbb_c_user || db->dbb_c_password ||
  2182. db->dbb_c_lc_messages || db->dbb_c_lc_ctype))
  2183. {
  2184. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2185. }
  2186. MSC_match(KW_COMMA);
  2187. }
  2188. PAR_end();
  2189. if (action->act_object)
  2190. {
  2191. if (default_buffers)
  2192. for (rdy* ready = (rdy*) action->act_object; ready; ready = ready->rdy_next)
  2193. {
  2194. if (!ready->rdy_request)
  2195. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2196. }
  2197. return action;
  2198. }
  2199. // No explicit databases -- pick up all known
  2200. for (db = gpreGlob.isc_databases; db; db = db->dbb_next)
  2201. if (db->dbb_runtime || !(db->dbb_flags & DBB_sqlca))
  2202. {
  2203. rdy* ready = (rdy*) MSC_alloc(RDY_LEN);
  2204. ready->rdy_next = (rdy*) action->act_object;
  2205. action->act_object = (ref*) ready;
  2206. ready->rdy_database = db;
  2207. }
  2208. if (!action->act_object)
  2209. PAR_error("no database available to READY");
  2210. else
  2211. for (rdy* ready = (rdy*) action->act_object; ready; ready = ready->rdy_next)
  2212. {
  2213. request = ready->rdy_request;
  2214. if (default_buffers && !ready->rdy_request)
  2215. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2216. // if there are any options that take host variables as arguments,
  2217. // make sure that we generate variables for the request so that the
  2218. // dpb can be extended at runtime
  2219. db = ready->rdy_database;
  2220. if (db->dbb_r_user || db->dbb_r_password || db->dbb_r_lc_messages || db->dbb_r_lc_ctype)
  2221. {
  2222. if (!request)
  2223. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2224. request->req_flags |= REQ_extend_dpb;
  2225. }
  2226. // ... and if there are compile time user or password specified,
  2227. // make sure there will be a dpb generated for them
  2228. if (!request && (db->dbb_c_user || db->dbb_c_password ||
  2229. db->dbb_c_lc_messages || db->dbb_c_lc_ctype))
  2230. {
  2231. request = PAR_set_up_dpb_info(ready, action, default_buffers);
  2232. }
  2233. }
  2234. return action;
  2235. }
  2236. //____________________________________________________________
  2237. //
  2238. // Parse a returning values clause in a STORE
  2239. // returning an action.
  2240. // Act as if we were at end_store, then set up
  2241. // for a further set of fields for returned values.
  2242. //
  2243. static act* par_returning_values()
  2244. {
  2245. if (!cur_store)
  2246. PAR_error("STORE must precede RETURNING_VALUES");
  2247. act* begin_action = (act*) MSC_pop(&cur_store);
  2248. gpre_req* request = begin_action->act_request;
  2249. // First take care of the impending store:
  2250. // Make up an assignment list for all field references and
  2251. // clone the references while we are at it
  2252. int count = 0;
  2253. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  2254. {
  2255. if (!reference->ref_master)
  2256. count++;
  2257. }
  2258. gpre_nod* assignments = MSC_node(nod_list, (SSHORT) count);
  2259. request->req_node =
  2260. MSC_ternary(nod_store, (gpre_nod*) request->req_contexts, assignments, NULL);
  2261. count = 0;
  2262. for (ref* reference = request->req_references; reference; reference = reference->ref_next)
  2263. {
  2264. ref* save_ref = MSC_reference(&begin_action->act_object);
  2265. save_ref->ref_context = reference->ref_context;
  2266. save_ref->ref_field = reference->ref_field;
  2267. save_ref->ref_source = reference;
  2268. save_ref->ref_flags = reference->ref_flags;
  2269. if (reference->ref_master)
  2270. continue;
  2271. gpre_nod* item = MSC_node(nod_assignment, 2);
  2272. item->nod_arg[0] = MSC_unary(nod_value, (gpre_nod*) save_ref);
  2273. item->nod_arg[1] = MSC_unary(nod_field, (gpre_nod*) save_ref);
  2274. assignments->nod_arg[count++] = item;
  2275. }
  2276. // Next make an updated context for post_store actions
  2277. upd* new_values = (upd*) MSC_alloc(UPD_LEN);
  2278. gpre_ctx* source = request->req_contexts;
  2279. request->req_type = REQ_store2;
  2280. gpre_ctx* new_ctx = MSC_context(request);
  2281. new_ctx->ctx_symbol = source->ctx_symbol;
  2282. new_ctx->ctx_relation = source->ctx_relation;
  2283. new_ctx->ctx_symbol->sym_object = new_ctx; // pointing to itself?
  2284. // make an update block to hold everything known about referenced fields
  2285. act* action = MSC_action(request, ACT_store2);
  2286. action->act_object = (ref*) new_values;
  2287. new_values->upd_request = request;
  2288. new_values->upd_source = source;
  2289. new_values->upd_update = new_ctx;
  2290. new_values->upd_level = ++request->req_level;
  2291. // both actions go on the cur_store stack, the store topmost
  2292. MSC_push((gpre_nod*) action, &cur_store);
  2293. MSC_push((gpre_nod*) begin_action, &cur_store);
  2294. return action;
  2295. }
  2296. //____________________________________________________________
  2297. //
  2298. // Do something about a right brace.
  2299. //
  2300. static act* par_right_brace()
  2301. {
  2302. if (--brace_count < 0)
  2303. brace_count = 0;
  2304. if (brace_count <= 0)
  2305. {
  2306. if (--namespace_count < 0)
  2307. namespace_count = 0;
  2308. }
  2309. return NULL;
  2310. }
  2311. //____________________________________________________________
  2312. //
  2313. // Parse a RELEASE_REQUEST statement.
  2314. //
  2315. static act* par_release()
  2316. {
  2317. act* action = MSC_action(0, ACT_release);
  2318. MSC_match(KW_FOR);
  2319. gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
  2320. if (symbol && (symbol->sym_type == SYM_database))
  2321. {
  2322. action->act_object = (ref*) symbol->sym_object;
  2323. PAR_get_token();
  2324. }
  2325. PAR_end();
  2326. return action;
  2327. }
  2328. //____________________________________________________________
  2329. //
  2330. // Handle a GET_SLICE or PUT_SLICE statement.
  2331. //
  2332. static act* par_slice( act_t type)
  2333. {
  2334. gpre_ctx* context;
  2335. gpre_fld* field = EXP_field(&context);
  2336. ary* info = field->fld_array_info;
  2337. if (!info)
  2338. CPR_s_error("array field");
  2339. gpre_req* request = MSC_request(REQ_slice);
  2340. slc* slice = (slc*) MSC_alloc(SLC_LEN(info->ary_dimension_count));
  2341. request->req_slice = slice;
  2342. slice->slc_dimensions = info->ary_dimension_count;
  2343. slice->slc_field = field;
  2344. slice->slc_field_ref = EXP_post_field(field, context, false);
  2345. slice->slc_parent_request = context->ctx_request;
  2346. if (!MSC_match(KW_L_BRCKET))
  2347. CPR_s_error("left bracket");
  2348. USHORT n = 0;
  2349. for (slc::slc_repeat* tail = slice->slc_rpt; ++n <= slice->slc_dimensions; ++tail)
  2350. {
  2351. tail->slc_lower = tail->slc_upper = EXP_subscript(request);
  2352. if (MSC_match(KW_COLON))
  2353. tail->slc_upper = EXP_subscript(request);
  2354. if (!MSC_match(KW_COMMA))
  2355. break;
  2356. }
  2357. if (n != slice->slc_dimensions)
  2358. PAR_error("subscript count mismatch");
  2359. if (!MSC_match(KW_R_BRCKET))
  2360. CPR_s_error("right bracket");
  2361. if (type == ACT_get_slice)
  2362. {
  2363. if (!MSC_match(KW_INTO))
  2364. CPR_s_error("INTO");
  2365. }
  2366. else if (!MSC_match(KW_FROM))
  2367. CPR_s_error("FROM");
  2368. slice->slc_array = EXP_subscript(0);
  2369. act* action = MSC_action(request, type);
  2370. action->act_object = (ref*) slice;
  2371. if (gpreGlob.sw_language == lang_c)
  2372. MSC_match(KW_SEMI_COLON);
  2373. else
  2374. PAR_end();
  2375. return action;
  2376. }
  2377. //____________________________________________________________
  2378. //
  2379. // Parse a STORE clause, returning an action.
  2380. //
  2381. static act* par_store()
  2382. {
  2383. gpre_req* request = MSC_request(REQ_store);
  2384. par_options(request, false);
  2385. act* action = MSC_action(request, ACT_store);
  2386. MSC_push((gpre_nod*) action, &cur_store);
  2387. gpre_ctx* context = EXP_context(request, 0);
  2388. gpre_rel* relation = context->ctx_relation;
  2389. request->req_database = relation->rel_database;
  2390. HSH_insert(context->ctx_symbol);
  2391. // You just inserted the context variable into the hash table.
  2392. // The current token however might be the same context variable.
  2393. // If so, get the symbol for it.
  2394. if (gpreGlob.token_global.tok_keyword == KW_none)
  2395. gpreGlob.token_global.tok_symbol = HSH_lookup(gpreGlob.token_global.tok_string);
  2396. MSC_match(KW_USING);
  2397. return action;
  2398. }
  2399. //____________________________________________________________
  2400. //
  2401. // Parse a start stream statement.
  2402. //
  2403. static act* par_start_stream()
  2404. {
  2405. gpre_req* request = MSC_request(REQ_cursor);
  2406. par_options(request, false);
  2407. act* action = MSC_action(request, ACT_s_start);
  2408. gpre_sym* cursor = PAR_symbol(SYM_dummy);
  2409. cursor->sym_type = SYM_stream;
  2410. cursor->sym_object = (gpre_ctx*) request;
  2411. MSC_match(KW_USING);
  2412. gpre_rse* rec_expr = EXP_rse(request, 0);
  2413. request->req_rse = rec_expr;
  2414. gpre_ctx* context = rec_expr->rse_context[0];
  2415. gpre_rel* relation = context->ctx_relation;
  2416. request->req_database = relation->rel_database;
  2417. gpre_ctx** ptr = rec_expr->rse_context;
  2418. for (const gpre_ctx* const* const end = ptr + rec_expr->rse_count; ptr < end; ptr++)
  2419. {
  2420. context = *ptr;
  2421. context->ctx_next = request->req_contexts;
  2422. request->req_contexts = context;
  2423. }
  2424. HSH_insert(cursor);
  2425. PAR_end();
  2426. return action;
  2427. }
  2428. //____________________________________________________________
  2429. //
  2430. // Parse a START_TRANSACTION statement, including
  2431. // transaction handle, transaction options, and
  2432. // reserving list.
  2433. //
  2434. static act* par_start_transaction()
  2435. {
  2436. act* action = MSC_action(0, ACT_start);
  2437. if (terminator())
  2438. {
  2439. PAR_end();
  2440. return action;
  2441. }
  2442. gpre_tra* trans = (gpre_tra*) MSC_alloc(TRA_LEN);
  2443. // get the transaction handle
  2444. if (!gpreGlob.token_global.tok_symbol)
  2445. trans->tra_handle = PAR_native_value(false, true);
  2446. // loop reading the various transaction options
  2447. while (gpreGlob.token_global.tok_keyword != KW_RESERVING &&
  2448. gpreGlob.token_global.tok_keyword != KW_USING && !terminator())
  2449. {
  2450. if (MSC_match(KW_READ_ONLY))
  2451. {
  2452. trans->tra_flags |= TRA_ro;
  2453. continue;
  2454. }
  2455. if (MSC_match(KW_READ_WRITE))
  2456. continue;
  2457. if (MSC_match(KW_CONSISTENCY))
  2458. {
  2459. trans->tra_flags |= TRA_con;
  2460. continue;
  2461. }
  2462. //if (MSC_match (KW_READ_COMMITTED))
  2463. //{
  2464. // trans->tra_flags |= TRA_read_committed;
  2465. // continue;
  2466. //}
  2467. if (MSC_match(KW_CONCURRENCY))
  2468. continue;
  2469. if (MSC_match(KW_NO_WAIT))
  2470. {
  2471. trans->tra_flags |= TRA_nw;
  2472. continue;
  2473. }
  2474. if (MSC_match(KW_WAIT))
  2475. continue;
  2476. if (MSC_match(KW_AUTOCOMMIT))
  2477. {
  2478. trans->tra_flags |= TRA_autocommit;
  2479. continue;
  2480. }
  2481. if (gpreGlob.sw_language == lang_cobol || gpreGlob.sw_language == lang_fortran)
  2482. break;
  2483. CPR_s_error("transaction keyword");
  2484. }
  2485. // send out for the list of reserved relations
  2486. if (MSC_match(KW_RESERVING))
  2487. {
  2488. trans->tra_flags |= TRA_rrl;
  2489. PAR_reserving(trans->tra_flags, false);
  2490. }
  2491. else if (MSC_match(KW_USING))
  2492. {
  2493. trans->tra_flags |= TRA_inc;
  2494. PAR_using_db();
  2495. }
  2496. PAR_end();
  2497. CMP_t_start(trans);
  2498. action->act_object = (ref*) trans;
  2499. return action;
  2500. }
  2501. //____________________________________________________________
  2502. //
  2503. // We have hit either a function or subroutine declaration.
  2504. // If the language is fortran, make the position with a break.
  2505. //
  2506. static act* par_subroutine()
  2507. {
  2508. if (gpreGlob.sw_language != lang_fortran)
  2509. return NULL;
  2510. act* action = MSC_action(0, ACT_routine);
  2511. action->act_flags |= ACT_mark | ACT_break;
  2512. gpreGlob.cur_routine = action;
  2513. return action;
  2514. }
  2515. //____________________________________________________________
  2516. //
  2517. // Parse a transaction termination statement: commit,
  2518. // prepare, rollback, or save (commit retaining context).
  2519. //
  2520. static act* par_trans( act_t act_op)
  2521. {
  2522. act* action = MSC_action(0, act_op);
  2523. if (!terminator())
  2524. {
  2525. const bool parens = MSC_match(KW_LEFT_PAREN);
  2526. if ((gpreGlob.sw_language == lang_fortran) && (act_op == ACT_commit_retain_context))
  2527. {
  2528. if (!MSC_match(KW_TRANSACTION_HANDLE))
  2529. return NULL;
  2530. }
  2531. else
  2532. MSC_match(KW_TRANSACTION_HANDLE);
  2533. action->act_object = (ref*) PAR_native_value(false, true);
  2534. if (parens)
  2535. EXP_match_paren();
  2536. }
  2537. if ((gpreGlob.sw_language != lang_fortran) && (gpreGlob.sw_language != lang_pascal))
  2538. MSC_match(KW_SEMI_COLON);
  2539. return action;
  2540. }
  2541. //____________________________________________________________
  2542. //
  2543. // Parse something of the form:
  2544. //
  2545. // <relation> . <field> . <something>
  2546. //
  2547. // where <something> is currently an enumerated type.
  2548. //
  2549. static act* par_type()
  2550. {
  2551. // Pick up relation
  2552. //gpre_sym* symbol;
  2553. //symbol = gpreGlob.token_global.tok_symbol;
  2554. //relation = (gpre_rel*) symbol->sym_object;
  2555. //PAR_get_token();
  2556. gpre_rel* relation = EXP_relation();
  2557. // No dot and we give up
  2558. if (!MSC_match(KW_DOT))
  2559. return NULL;
  2560. // Look for field name. No field name, punt
  2561. SQL_resolve_identifier("<Field Name>", NULL, NAME_SIZE);
  2562. gpre_fld* field = MET_field(relation, gpreGlob.token_global.tok_string);
  2563. if (!field)
  2564. return NULL;
  2565. PAR_get_token();
  2566. if (!MSC_match(KW_DOT))
  2567. CPR_s_error("period");
  2568. // Lookup type. If we can't find it, complain bitterly
  2569. SSHORT type;
  2570. if (!MET_type(field, gpreGlob.token_global.tok_string, &type))
  2571. {
  2572. TEXT s[ERROR_LENGTH];
  2573. fb_utils::snprintf(s, sizeof(s), "undefined type %s", gpreGlob.token_global.tok_string);
  2574. PAR_error(s);
  2575. }
  2576. PAR_get_token();
  2577. act* action = MSC_action(0, ACT_type_number);
  2578. action->act_object = (ref*) (IPTR) type;
  2579. return action;
  2580. }
  2581. //____________________________________________________________
  2582. //
  2583. // Parse a free reference to a database field in general
  2584. // program context.
  2585. //
  2586. static act* par_variable()
  2587. {
  2588. // Since fortran is fussy about continuations and the like,
  2589. // see if this variable token is the first thing in a statement.
  2590. const bool first = gpreGlob.token_global.tok_first;
  2591. gpre_ctx* context;
  2592. gpre_fld* field = EXP_field(&context);
  2593. gpre_fld* cast;
  2594. bool dot = MSC_match(KW_DOT);
  2595. if (dot && (cast = EXP_cast(field)))
  2596. {
  2597. field = cast;
  2598. dot = MSC_match(KW_DOT);
  2599. }
  2600. bool is_null = false;
  2601. if (dot && MSC_match(KW_NULL))
  2602. {
  2603. is_null = true;
  2604. dot = false;
  2605. }
  2606. gpre_req* request = context->ctx_request;
  2607. ref* reference = EXP_post_field(field, context, is_null);
  2608. if (field->fld_array)
  2609. EXP_post_array(reference);
  2610. act* action = MSC_action(request, ACT_variable);
  2611. if (first)
  2612. action->act_flags |= ACT_first;
  2613. if (dot)
  2614. action->act_flags |= ACT_back_token;
  2615. action->act_object = reference;
  2616. if (!is_null)
  2617. return action;
  2618. // We've got a explicit null flag reference rather than a field
  2619. // reference. If there's already a null reference for the field,
  2620. // use it; otherwise make one up.
  2621. if (reference->ref_null)
  2622. {
  2623. action->act_object = reference->ref_null;
  2624. return action;
  2625. }
  2626. // Check to see if the flag field has been allocated. If not, sigh, allocate it
  2627. ref* flag = MSC_reference(&request->req_references);
  2628. flag->ref_context = reference->ref_context;
  2629. flag->ref_field = PAR_null_field();
  2630. flag->ref_level = request->req_level;
  2631. flag->ref_master = reference;
  2632. reference->ref_null = flag;
  2633. action->act_object = flag;
  2634. return action;
  2635. }
  2636. //____________________________________________________________
  2637. //
  2638. // This is PASCAL, and we've got a function, or procedure header.
  2639. // Alas and alack, we have to decide if this is a real header or
  2640. // a forward/external declaration.
  2641. //
  2642. // Basically we scan the thing, skipping parenthesized bits,
  2643. // looking for a semi-colon. We look at the next token, which may
  2644. // be OPTIONS followed by a parenthesized list of options, or it
  2645. // may be just some options, or it may be nothing. If the options
  2646. // are EXTERN or FORWARD, we've got a reference, otherwise its a real
  2647. // routine (or possibly program or module).
  2648. //
  2649. // Fortunately all of these are of the form:
  2650. // <keyword> <name> [( blah, blah )] [: type] ; [<options>;]
  2651. //
  2652. //
  2653. static act* scan_routine_header()
  2654. {
  2655. act* action = MSC_action(0, ACT_routine);
  2656. action->act_flags |= ACT_mark;
  2657. while (!MSC_match(KW_SEMI_COLON))
  2658. {
  2659. if (!match_parentheses())
  2660. PAR_get_token();
  2661. }
  2662. if (MSC_match(KW_OPTIONS) && MSC_match(KW_LEFT_PAREN))
  2663. {
  2664. while (!MSC_match(KW_RIGHT_PAREN))
  2665. {
  2666. if (MSC_match(KW_EXTERN) || MSC_match(KW_FORWARD))
  2667. action->act_flags |= ACT_decl;
  2668. else
  2669. PAR_get_token();
  2670. }
  2671. MSC_match(KW_SEMI_COLON);
  2672. }
  2673. else
  2674. {
  2675. for (;;)
  2676. {
  2677. if (MSC_match(KW_EXTERN) || MSC_match(KW_FORWARD))
  2678. {
  2679. action->act_flags |= ACT_decl;
  2680. MSC_match(KW_SEMI_COLON);
  2681. }
  2682. else if (MSC_match(KW_INTERNAL) || MSC_match(KW_ABNORMAL) ||
  2683. MSC_match(KW_VARIABLE) || MSC_match(KW_VAL_PARAM))
  2684. {
  2685. MSC_match(KW_SEMI_COLON);
  2686. }
  2687. else
  2688. break;
  2689. }
  2690. }
  2691. return action;
  2692. }
  2693. //____________________________________________________________
  2694. //
  2695. // If this is a external declaration in
  2696. // a BASIC program, set a flag to indicate
  2697. // the situation.
  2698. //
  2699. static void set_external_flag()
  2700. {
  2701. CPR_token();
  2702. }
  2703. //____________________________________________________________
  2704. //
  2705. // Check the current token for a logical terminator. Terminators
  2706. // are semi-colon, ELSE, or ON_ERROR.
  2707. //
  2708. static bool terminator()
  2709. {
  2710. // For C, changed keyword (KW_SEMICOLON) to MSC_match (KW_SEMICOLON) to eat a
  2711. // semicolon if it is present so as to allow it to be there or not be there.
  2712. // Bug#833. mao 6/21/89
  2713. // For C, right brace ("}") must also be a terminator.
  2714. // Used reference here because MSC_match() can change tok_leyword.
  2715. kwwords_t& key = gpreGlob.token_global.tok_keyword;
  2716. switch (gpreGlob.sw_language)
  2717. {
  2718. case lang_c:
  2719. if (MSC_match(KW_SEMI_COLON) || key == KW_ELSE || key == KW_ON_ERROR || key == KW_R_BRACE)
  2720. {
  2721. return true;
  2722. }
  2723. break;
  2724. case lang_ada:
  2725. if (MSC_match(KW_SEMI_COLON) || key == KW_ELSE || key == KW_ON_ERROR)
  2726. {
  2727. return true;
  2728. }
  2729. break;
  2730. default:
  2731. if (key == KW_SEMI_COLON || key == KW_ELSE || key == KW_ON_ERROR ||
  2732. (gpreGlob.sw_language == lang_cobol && key == KW_DOT))
  2733. {
  2734. return true;
  2735. }
  2736. }
  2737. return false;
  2738. }