pdecsub.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecsub;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,symconst,symtype,symdef,symsym;
  23. type
  24. tpdflag=(
  25. pd_body, { directive needs a body }
  26. pd_implemen, { directive can be used implementation section }
  27. pd_interface, { directive can be used interface section }
  28. pd_object, { directive can be used object declaration }
  29. pd_procvar, { directive can be used procvar declaration }
  30. pd_notobject, { directive can not be used object declaration }
  31. pd_notobjintf, { directive can not be used interface declaration }
  32. pd_notprocvar { directive can not be used procvar declaration }
  33. );
  34. tpdflags=set of tpdflag;
  35. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  36. procedure calc_parast(pd:tabstractprocdef);
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var pd:tprocdef):boolean;
  39. procedure handle_calling_convention(pd:tabstractprocdef);
  40. procedure parse_parameter_dec(pd:tabstractprocdef);
  41. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  42. procedure parse_var_proc_directives(sym:tsym);
  43. procedure parse_object_proc_directives(pd:tabstractprocdef);
  44. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  45. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  46. implementation
  47. uses
  48. {$ifdef delphi}
  49. sysutils,
  50. {$else delphi}
  51. strings,
  52. {$endif delphi}
  53. { common }
  54. cutils,cclasses,
  55. { global }
  56. globtype,globals,verbose,
  57. systems,
  58. cpuinfo,
  59. { aasm }
  60. aasmbase,
  61. { symtable }
  62. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  63. { pass 1 }
  64. node,htypechk,
  65. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  66. { parser }
  67. fmodule,scanner,
  68. pbase,pexpr,ptype,pdecl,
  69. { linking }
  70. import,gendef
  71. ;
  72. const
  73. { Please leave this here, this module should NOT use
  74. these variables.
  75. Declaring it as string here results in an error when compiling (PFV) }
  76. current_procinfo = 'error';
  77. procedure insert_funcret_para(pd:tabstractprocdef);
  78. var
  79. storepos : tfileposinfo;
  80. vs : tvarsym;
  81. begin
  82. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  83. not is_void(pd.rettype.def) and
  84. paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  85. begin
  86. storepos:=akttokenpos;
  87. if pd.deftype=procdef then
  88. akttokenpos:=tprocdef(pd).fileinfo;
  89. { Generate result variable accessing function result }
  90. vs:=tvarsym.create('$result',vs_var,pd.rettype);
  91. include(vs.varoptions,vo_is_funcret);
  92. pd.parast.insert(vs);
  93. { For left to right add it at the end to be delphi compatible }
  94. if pd.proccalloption in pushleftright_pocalls then
  95. pd.concatpara(nil,vs.vartype,vs,nil,true)
  96. else
  97. pd.insertpara(vs.vartype,vs,nil,true);
  98. { Store the this symbol as funcretsym for procedures }
  99. if pd.deftype=procdef then
  100. tprocdef(pd).funcretsym:=vs;
  101. akttokenpos:=storepos;
  102. end;
  103. end;
  104. procedure insert_parentfp_para(pd:tabstractprocdef);
  105. var
  106. storepos : tfileposinfo;
  107. vs : tvarsym;
  108. begin
  109. if pd.parast.symtablelevel>normal_function_level then
  110. begin
  111. storepos:=akttokenpos;
  112. if pd.deftype=procdef then
  113. akttokenpos:=tprocdef(pd).fileinfo;
  114. { Generate result variable accessing function result }
  115. vs:=tvarsym.create('$parentfp',vs_var,voidpointertype);
  116. include(vs.varoptions,vo_is_parentfp);
  117. pd.parast.insert(vs);
  118. pd.insertpara(vs.vartype,vs,nil,true);
  119. akttokenpos:=storepos;
  120. end;
  121. end;
  122. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  123. var
  124. storepos : tfileposinfo;
  125. vs : tvarsym;
  126. tt : ttype;
  127. vsp : tvarspez;
  128. begin
  129. if (pd.deftype=procvardef) and
  130. pd.is_methodpointer then
  131. begin
  132. { Generate self variable }
  133. tt:=voidpointertype;
  134. vs:=tvarsym.create('$self',vs_value,tt);
  135. include(vs.varoptions,vo_is_self);
  136. { Insert as hidden parameter }
  137. pd.parast.insert(vs);
  138. pd.insertpara(vs.vartype,vs,nil,true);
  139. end
  140. else
  141. begin
  142. if (pd.deftype=procdef) and
  143. assigned(tprocdef(pd)._class) and
  144. (pd.parast.symtablelevel=normal_function_level) then
  145. begin
  146. storepos:=akttokenpos;
  147. akttokenpos:=tprocdef(pd).fileinfo;
  148. { Generate VMT variable for constructor/destructor }
  149. if pd.proctypeoption in [potype_constructor,potype_destructor] then
  150. begin
  151. { can't use classrefdef as type because inheriting
  152. will then always file because of a type mismatch }
  153. tt:=voidpointertype;
  154. vs:=tvarsym.create('$vmt',vs_value,tt);
  155. include(vs.varoptions,vo_is_vmt);
  156. { Insert as hidden parameter }
  157. pd.parast.insert(vs);
  158. pd.insertpara(vs.vartype,vs,nil,true);
  159. end;
  160. { Generate self variable, for classes we need
  161. to use the generic voidpointer to be compatible with
  162. methodpointers }
  163. vsp:=vs_value;
  164. if (po_staticmethod in pd.procoptions) or
  165. (po_classmethod in pd.procoptions) then
  166. begin
  167. tt.setdef(tprocdef(pd)._class);
  168. tt.setdef(tclassrefdef.create(tt));
  169. end
  170. else
  171. begin
  172. if is_object(tprocdef(pd)._class) then
  173. vsp:=vs_var;
  174. tt.setdef(tprocdef(pd)._class);
  175. end;
  176. vs:=tvarsym.create('$self',vsp,tt);
  177. include(vs.varoptions,vo_is_self);
  178. { Insert as hidden parameter }
  179. pd.parast.insert(vs);
  180. pd.insertpara(vs.vartype,vs,nil,true);
  181. akttokenpos:=storepos;
  182. end;
  183. end;
  184. end;
  185. procedure insert_funcret_local(pd:tprocdef);
  186. var
  187. storepos : tfileposinfo;
  188. vs : tvarsym;
  189. sl : tsymlist;
  190. begin
  191. { The result from constructors and destructors can't be accessed directly }
  192. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  193. not is_void(pd.rettype.def) then
  194. begin
  195. storepos:=akttokenpos;
  196. akttokenpos:=pd.fileinfo;
  197. { We always need a localsymtable }
  198. if not assigned(pd.localst) then
  199. pd.insert_localst;
  200. { We need to insert a varsym for the result in the localst
  201. when it is returning in a register }
  202. if not paramanager.ret_in_param(pd.rettype.def,pd.proccalloption) then
  203. begin
  204. vs:=tvarsym.create('$result',vs_value,pd.rettype);
  205. include(vs.varoptions,vo_is_funcret);
  206. pd.localst.insert(vs);
  207. pd.funcretsym:=vs;
  208. end;
  209. { insert the name of the procedure as alias for the function result,
  210. we can't use realname because that will not work for compilerprocs
  211. as the name is lowercase and unreachable from the code }
  212. if pd.resultname='' then
  213. pd.resultname:=pd.procsym.name;
  214. sl:=tsymlist.create;
  215. sl.addsym(sl_load,pd.funcretsym);
  216. vs:=tabsolutesym.create_ref(pd.resultname,pd.rettype,sl);
  217. include(vs.varoptions,vo_is_funcret);
  218. pd.localst.insert(vs);
  219. { insert result also if support is on }
  220. if (m_result in aktmodeswitches) then
  221. begin
  222. sl:=tsymlist.create;
  223. sl.addsym(sl_load,pd.funcretsym);
  224. vs:=tabsolutesym.create_ref('RESULT',pd.rettype,sl);
  225. include(vs.varoptions,vo_is_funcret);
  226. include(vs.varoptions,vo_is_result);
  227. pd.localst.insert(vs);
  228. end;
  229. akttokenpos:=storepos;
  230. end;
  231. end;
  232. procedure insert_hidden_para(pd:tabstractprocdef);
  233. var
  234. currpara : tparaitem;
  235. hvs : tvarsym;
  236. begin
  237. { walk from right to left, so we can insert the
  238. high parameters after the current parameter }
  239. currpara:=tparaitem(pd.para.last);
  240. while assigned(currpara) do
  241. begin
  242. { needs high parameter ? }
  243. if paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) then
  244. begin
  245. if assigned(currpara.parasym) then
  246. begin
  247. hvs:=tvarsym.create('$high'+tvarsym(currpara.parasym).name,vs_const,sinttype);
  248. include(hvs.varoptions,vo_is_high_value);
  249. tvarsym(currpara.parasym).owner.insert(hvs);
  250. end
  251. else
  252. hvs:=nil;
  253. pd.concatpara(currpara,sinttype,hvs,nil,true);
  254. end
  255. else
  256. begin
  257. { Give a warning that cdecl routines does not include high()
  258. support }
  259. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  260. paramanager.push_high_param(currpara.paratyp,currpara.paratype.def,pocall_default) then
  261. begin
  262. if is_open_string(currpara.paratype.def) then
  263. Message(parser_w_cdecl_no_openstring);
  264. if not (po_external in pd.procoptions) then
  265. Message(parser_w_cdecl_has_no_high);
  266. end;
  267. end;
  268. currpara:=tparaitem(currpara.previous);
  269. end;
  270. end;
  271. procedure check_c_para(p:tnamedindexitem;arg:pointer);
  272. begin
  273. if (tsym(p).typ<>varsym) then
  274. exit;
  275. with tvarsym(p) do
  276. begin
  277. case vartype.def.deftype of
  278. arraydef :
  279. begin
  280. if not is_variant_array(vartype.def) and
  281. not is_array_of_const(vartype.def) then
  282. begin
  283. if (varspez<>vs_var) then
  284. Message(parser_h_c_arrays_are_references);
  285. end;
  286. if is_array_of_const(vartype.def) and
  287. assigned(indexnext) and
  288. (tsym(indexnext).typ=varsym) and
  289. not(vo_is_high_value in tvarsym(indexnext).varoptions) then
  290. Message(parser_e_C_array_of_const_must_be_last);
  291. end;
  292. end;
  293. end;
  294. end;
  295. procedure parse_parameter_dec(pd:tabstractprocdef);
  296. {
  297. handle_procvar needs the same changes
  298. }
  299. var
  300. is_procvar : boolean;
  301. sc : tsinglelist;
  302. tt : ttype;
  303. arrayelementtype : ttype;
  304. vs : tvarsym;
  305. srsym : tsym;
  306. hs1 : string;
  307. varspez : Tvarspez;
  308. defaultvalue : tconstsym;
  309. defaultrequired : boolean;
  310. old_object_option : tsymoptions;
  311. currparast : tparasymtable;
  312. explicit_paraloc : boolean;
  313. locationstr : string;
  314. begin
  315. explicit_paraloc:=false;
  316. consume(_LKLAMMER);
  317. { Delphi/Kylix supports nonsense like }
  318. { procedure p(); }
  319. if try_to_consume(_RKLAMMER) and
  320. not(m_tp7 in aktmodeswitches) then
  321. exit;
  322. { parsing a proc or procvar ? }
  323. is_procvar:=(pd.deftype=procvardef);
  324. currparast:=tparasymtable(pd.parast);
  325. { reset }
  326. sc:=tsinglelist.create;
  327. defaultrequired:=false;
  328. { the variables are always public }
  329. old_object_option:=current_object_option;
  330. current_object_option:=[sp_public];
  331. inc(testcurobject);
  332. repeat
  333. if try_to_consume(_VAR) then
  334. varspez:=vs_var
  335. else
  336. if try_to_consume(_CONST) then
  337. varspez:=vs_const
  338. else
  339. if (idtoken=_OUT) and (m_out in aktmodeswitches) then
  340. begin
  341. consume(_OUT);
  342. varspez:=vs_out
  343. end
  344. else
  345. if (token=_POINTPOINTPOINT) and (m_mac in aktmodeswitches) then
  346. begin
  347. consume(_POINTPOINTPOINT);
  348. include(pd.procoptions,po_varargs);
  349. break;
  350. end
  351. else
  352. varspez:=vs_value;
  353. defaultvalue:=nil;
  354. tt.reset;
  355. { read identifiers and insert with error type }
  356. sc.reset;
  357. repeat
  358. vs:=tvarsym.create(orgpattern,varspez,generrortype);
  359. currparast.insert(vs);
  360. if assigned(vs.owner) then
  361. sc.insert(vs)
  362. else
  363. vs.free;
  364. consume(_ID);
  365. until not try_to_consume(_COMMA);
  366. locationstr:='';
  367. { read type declaration, force reading for value and const paras }
  368. if (token=_COLON) or (varspez=vs_value) then
  369. begin
  370. consume(_COLON);
  371. { check for an open array }
  372. if token=_ARRAY then
  373. begin
  374. consume(_ARRAY);
  375. consume(_OF);
  376. { define range and type of range }
  377. tt.setdef(tarraydef.create(0,-1,s32inttype));
  378. { array of const ? }
  379. if (token=_CONST) and (m_objpas in aktmodeswitches) then
  380. begin
  381. consume(_CONST);
  382. srsym:=searchsymonlyin(systemunit,'TVARREC');
  383. if not assigned(srsym) then
  384. InternalError(200404181);
  385. tarraydef(tt.def).setelementtype(ttypesym(srsym).restype);
  386. tarraydef(tt.def).IsArrayOfConst:=true;
  387. end
  388. else
  389. begin
  390. { define field type }
  391. single_type(arrayelementtype,hs1,false);
  392. tarraydef(tt.def).setelementtype(arrayelementtype);
  393. end;
  394. end
  395. else
  396. begin
  397. { open string ? }
  398. if (varspez=vs_var) and
  399. (
  400. (
  401. ((token=_STRING) or (idtoken=_SHORTSTRING)) and
  402. (cs_openstring in aktmoduleswitches) and
  403. not(cs_ansistrings in aktlocalswitches)
  404. ) or
  405. (idtoken=_OPENSTRING)) then
  406. begin
  407. consume(token);
  408. tt:=openshortstringtype;
  409. hs1:='openstring';
  410. end
  411. else
  412. begin
  413. { everything else }
  414. if (m_mac in aktmodeswitches) then
  415. try_to_consume(_UNIV); {currently does nothing}
  416. single_type(tt,hs1,false);
  417. end;
  418. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  419. begin
  420. if (idtoken=_LOCATION) then
  421. begin
  422. consume(_LOCATION);
  423. locationstr:=pattern;
  424. consume(_CSTRING);
  425. end
  426. else
  427. begin
  428. if explicit_paraloc then
  429. Message(parser_e_paraloc_all_paras);
  430. locationstr:='';
  431. end;
  432. end
  433. else
  434. locationstr:='';
  435. { default parameter }
  436. if (m_default_para in aktmodeswitches) then
  437. begin
  438. if try_to_consume(_EQUAL) then
  439. begin
  440. vs:=tvarsym(sc.first);
  441. if assigned(vs.listnext) then
  442. Message(parser_e_default_value_only_one_para);
  443. { prefix 'def' to the parameter name }
  444. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  445. if assigned(defaultvalue) then
  446. begin
  447. include(defaultvalue.symoptions,sp_internal);
  448. tprocdef(pd).parast.insert(defaultvalue);
  449. end;
  450. defaultrequired:=true;
  451. end
  452. else
  453. begin
  454. if defaultrequired then
  455. Message1(parser_e_default_value_expected_for_para,vs.name);
  456. end;
  457. end;
  458. end;
  459. end
  460. else
  461. begin
  462. {$ifndef UseNiceNames}
  463. hs1:='$$$';
  464. {$else UseNiceNames}
  465. hs1:='var';
  466. {$endif UseNiceNames}
  467. tt:=cformaltype;
  468. end;
  469. { File types are only allowed for var parameters }
  470. if (tt.def.deftype=filedef) and
  471. (varspez<>vs_var) then
  472. CGMessage(cg_e_file_must_call_by_reference);
  473. vs:=tvarsym(sc.first);
  474. while assigned(vs) do
  475. begin
  476. { update varsym }
  477. vs.vartype:=tt;
  478. { For proc vars we only need the definitions }
  479. if not is_procvar then
  480. begin
  481. if (varspez in [vs_var,vs_const,vs_out]) and
  482. paramanager.push_addr_param(varspez,tt.def,pd.proccalloption) then
  483. vs.varregable:=vr_intreg;
  484. end;
  485. pd.concatpara(nil,tt,vs,defaultvalue,false);
  486. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  487. begin
  488. if locationstr<>'' then
  489. begin
  490. if assigned(sc.first.listnext) then
  491. Message(parser_e_paraloc_only_one_para);
  492. if (pd.para.first<>pd.para.last) and not(explicit_paraloc) then
  493. Message(parser_e_paraloc_all_paras);
  494. explicit_paraloc:=true;
  495. if not(paramanager.parseparaloc(tparaitem(pd.para.last),upper(locationstr))) then
  496. message(parser_e_illegal_explicit_paraloc);
  497. end
  498. else
  499. if explicit_paraloc then
  500. Message(parser_e_paraloc_all_paras);
  501. end;
  502. vs:=tvarsym(vs.listnext);
  503. end;
  504. until not try_to_consume(_SEMICOLON);
  505. if explicit_paraloc then
  506. begin
  507. pd.has_paraloc_info:=true;
  508. include(pd.procoptions,po_explicitparaloc);
  509. end;
  510. { remove parasymtable from stack }
  511. sc.free;
  512. { reset object options }
  513. dec(testcurobject);
  514. current_object_option:=old_object_option;
  515. consume(_RKLAMMER);
  516. end;
  517. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  518. var
  519. orgsp,sp : stringid;
  520. sym : tsym;
  521. srsymtable : tsymtable;
  522. storepos,
  523. procstartfilepos : tfileposinfo;
  524. searchagain : boolean;
  525. i : longint;
  526. st : tsymtable;
  527. aprocsym : tprocsym;
  528. begin
  529. { Save the position where this procedure really starts }
  530. procstartfilepos:=akttokenpos;
  531. result:=false;
  532. pd:=nil;
  533. aprocsym:=nil;
  534. if (potype=potype_operator) then
  535. begin
  536. sp:=overloaded_names[optoken];
  537. orgsp:=sp;
  538. end
  539. else
  540. begin
  541. sp:=pattern;
  542. orgsp:=orgpattern;
  543. consume(_ID);
  544. end;
  545. { examine interface map: function/procedure iname.functionname=locfuncname }
  546. if assigned(aclass) and
  547. assigned(aclass.implementedinterfaces) and
  548. (aclass.implementedinterfaces.count>0) and
  549. try_to_consume(_POINT) then
  550. begin
  551. storepos:=akttokenpos;
  552. akttokenpos:=procstartfilepos;
  553. { get interface syms}
  554. searchsym(sp,sym,srsymtable);
  555. if not assigned(sym) then
  556. begin
  557. identifier_not_found(orgsp);
  558. sym:=generrorsym;
  559. end;
  560. akttokenpos:=storepos;
  561. { qualifier is interface? }
  562. if (sym.typ=typesym) and
  563. (ttypesym(sym).restype.def.deftype=objectdef) then
  564. i:=aclass.implementedinterfaces.searchintf(ttypesym(sym).restype.def)
  565. else
  566. i:=-1;
  567. if (i=-1) then
  568. Message(parser_e_interface_id_expected);
  569. consume(_ID);
  570. consume(_EQUAL);
  571. if (token=_ID) then
  572. aclass.implementedinterfaces.addmappings(i,sp,pattern);
  573. consume(_ID);
  574. result:=true;
  575. exit;
  576. end;
  577. { method ? }
  578. if not assigned(aclass) and
  579. (potype<>potype_operator) and
  580. (symtablestack.symtablelevel=main_program_level) and
  581. try_to_consume(_POINT) then
  582. begin
  583. { search for object name }
  584. storepos:=akttokenpos;
  585. akttokenpos:=procstartfilepos;
  586. searchsym(sp,sym,srsymtable);
  587. if not assigned(sym) then
  588. begin
  589. identifier_not_found(orgsp);
  590. sym:=generrorsym;
  591. end;
  592. akttokenpos:=storepos;
  593. { consume proc name }
  594. sp:=pattern;
  595. orgsp:=orgpattern;
  596. procstartfilepos:=akttokenpos;
  597. consume(_ID);
  598. { qualifier is class name ? }
  599. if (sym.typ=typesym) and
  600. (ttypesym(sym).restype.def.deftype=objectdef) then
  601. begin
  602. aclass:=tobjectdef(ttypesym(sym).restype.def);
  603. aprocsym:=tprocsym(aclass.symtable.search(sp));
  604. { we solve this below }
  605. if assigned(aprocsym) then
  606. begin
  607. if aprocsym.typ<>procsym then
  608. begin
  609. { we use a different error message for tp7 so it looks more compatible }
  610. if (m_fpc in aktmodeswitches) then
  611. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  612. else
  613. Message(parser_e_methode_id_expected);
  614. { rename the name to an unique name to avoid an
  615. error when inserting the symbol in the symtable }
  616. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  617. aprocsym:=nil;
  618. end;
  619. end
  620. else
  621. begin
  622. Message(parser_e_methode_id_expected);
  623. { recover by making it a normal procedure instead of method }
  624. aclass:=nil;
  625. end;
  626. end
  627. else
  628. Message(parser_e_class_id_expected);
  629. end
  630. else
  631. begin
  632. { check for constructor/destructor which is not allowed here }
  633. if (not parse_only) and
  634. (potype in [potype_constructor,potype_destructor]) then
  635. Message(parser_e_constructors_always_objects);
  636. repeat
  637. searchagain:=false;
  638. akttokenpos:=procstartfilepos;
  639. aprocsym:=tprocsym(symtablestack.search(sp));
  640. if not(parse_only) and
  641. not assigned(aprocsym) and
  642. (symtablestack.symtabletype=staticsymtable) and
  643. assigned(symtablestack.next) and
  644. (symtablestack.next.unitid=0) then
  645. begin
  646. { The procedure we prepare for is in the implementation
  647. part of the unit we compile. It is also possible that we
  648. are compiling a program, which is also some kind of
  649. implementaion part.
  650. We need to find out if the procedure is global. If it is
  651. global, it is in the global symtable.}
  652. aprocsym:=tprocsym(symtablestack.next.search(sp));
  653. end;
  654. { Check if overloaded is a procsym }
  655. if assigned(aprocsym) and
  656. (aprocsym.typ<>procsym) then
  657. begin
  658. { when the other symbol is a unit symbol then hide the unit
  659. symbol }
  660. if (aprocsym.typ=unitsym) then
  661. begin
  662. aprocsym.owner.rename(aprocsym.name,'hidden'+aprocsym.name);
  663. searchagain:=true;
  664. end
  665. else
  666. begin
  667. { we use a different error message for tp7 so it looks more compatible }
  668. if (m_fpc in aktmodeswitches) then
  669. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  670. else
  671. DuplicateSym(aprocsym);
  672. { rename the name to an unique name to avoid an
  673. error when inserting the symbol in the symtable }
  674. orgsp:=orgsp+'$'+tostr(aktfilepos.line);
  675. { generate a new aktprocsym }
  676. aprocsym:=nil;
  677. end;
  678. end;
  679. until not searchagain;
  680. end;
  681. { test again if assigned, it can be reset to recover }
  682. if not assigned(aprocsym) then
  683. begin
  684. { create a new procsym and set the real filepos }
  685. akttokenpos:=procstartfilepos;
  686. { for operator we have only one procsym for each overloaded
  687. operation }
  688. if (potype=potype_operator) then
  689. begin
  690. Aprocsym:=Tprocsym(symtablestack.search(sp));
  691. if Aprocsym=nil then
  692. Aprocsym:=tprocsym.create('$'+sp);
  693. end
  694. else
  695. aprocsym:=tprocsym.create(orgsp);
  696. symtablestack.insert(aprocsym);
  697. end;
  698. { to get the correct symtablelevel we must ignore objectsymtables }
  699. st:=symtablestack;
  700. while not(st.symtabletype in [staticsymtable,globalsymtable,localsymtable]) do
  701. st:=st.next;
  702. pd:=tprocdef.create(st.symtablelevel+1);
  703. pd._class:=aclass;
  704. pd.procsym:=aprocsym;
  705. pd.proctypeoption:=potype;
  706. { methods need to be exported }
  707. if assigned(aclass) and
  708. (
  709. (symtablestack.symtabletype=objectsymtable) or
  710. (symtablestack.symtablelevel=main_program_level)
  711. ) then
  712. include(pd.procoptions,po_public);
  713. { symbol options that need to be kept per procdef }
  714. pd.fileinfo:=procstartfilepos;
  715. pd.symoptions:=current_object_option;
  716. { parse parameters }
  717. if token=_LKLAMMER then
  718. parse_parameter_dec(pd);
  719. result:=true;
  720. end;
  721. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  722. var
  723. pd : tprocdef;
  724. hs : string;
  725. isclassmethod : boolean;
  726. begin
  727. pd:=nil;
  728. { read class method }
  729. if try_to_consume(_CLASS) then
  730. begin
  731. { class method only allowed for procedures and functions }
  732. if not(token in [_FUNCTION,_PROCEDURE]) then
  733. Message(parser_e_procedure_or_function_expected);
  734. isclassmethod:=true;
  735. end
  736. else
  737. isclassmethod:=false;
  738. case token of
  739. _FUNCTION :
  740. begin
  741. consume(_FUNCTION);
  742. if parse_proc_head(aclass,potype_none,pd) then
  743. begin
  744. { pd=nil when it is a interface mapping }
  745. if assigned(pd) then
  746. begin
  747. if try_to_consume(_COLON) then
  748. begin
  749. inc(testcurobject);
  750. single_type(pd.rettype,hs,false);
  751. pd.test_if_fpu_result;
  752. dec(testcurobject);
  753. end
  754. else
  755. begin
  756. if (
  757. parse_only and
  758. not(is_interface(pd._class))
  759. ) or
  760. (m_repeat_forward in aktmodeswitches) then
  761. begin
  762. consume(_COLON);
  763. consume_all_until(_SEMICOLON);
  764. end;
  765. end;
  766. if isclassmethod then
  767. include(pd.procoptions,po_classmethod);
  768. end;
  769. end
  770. else
  771. begin
  772. { recover }
  773. consume(_COLON);
  774. consume_all_until(_SEMICOLON);
  775. end;
  776. end;
  777. _PROCEDURE :
  778. begin
  779. consume(_PROCEDURE);
  780. if parse_proc_head(aclass,potype_none,pd) then
  781. begin
  782. { pd=nil when it is a interface mapping }
  783. if assigned(pd) then
  784. begin
  785. pd.rettype:=voidtype;
  786. if isclassmethod then
  787. include(pd.procoptions,po_classmethod);
  788. end;
  789. end;
  790. end;
  791. _CONSTRUCTOR :
  792. begin
  793. consume(_CONSTRUCTOR);
  794. parse_proc_head(aclass,potype_constructor,pd);
  795. if assigned(pd) and
  796. assigned(pd._class) then
  797. begin
  798. { Set return type, class constructors return the
  799. created instance, object constructors return boolean }
  800. if is_class(pd._class) then
  801. pd.rettype.setdef(pd._class)
  802. else
  803. pd.rettype:=booltype;
  804. end;
  805. end;
  806. _DESTRUCTOR :
  807. begin
  808. consume(_DESTRUCTOR);
  809. parse_proc_head(aclass,potype_destructor,pd);
  810. if assigned(pd) then
  811. pd.rettype:=voidtype;
  812. end;
  813. _OPERATOR :
  814. begin
  815. consume(_OPERATOR);
  816. if (token in [first_overloaded..last_overloaded]) then
  817. begin
  818. optoken:=token;
  819. end
  820. else
  821. begin
  822. Message(parser_e_overload_operator_failed);
  823. { Use the dummy NOTOKEN that is also declared
  824. for the overloaded_operator[] }
  825. optoken:=NOTOKEN;
  826. end;
  827. consume(token);
  828. parse_proc_head(aclass,potype_operator,pd);
  829. if assigned(pd) then
  830. begin
  831. if pd.parast.symtablelevel>normal_function_level then
  832. Message(parser_e_no_local_operator);
  833. if token<>_ID then
  834. begin
  835. if not(m_result in aktmodeswitches) then
  836. consume(_ID);
  837. end
  838. else
  839. begin
  840. pd.resultname:=orgpattern;
  841. consume(_ID);
  842. end;
  843. if not try_to_consume(_COLON) then
  844. begin
  845. consume(_COLON);
  846. pd.rettype:=generrortype;
  847. consume_all_until(_SEMICOLON);
  848. end
  849. else
  850. begin
  851. single_type(pd.rettype,hs,false);
  852. pd.test_if_fpu_result;
  853. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  854. ((pd.rettype.def.deftype<>orddef) or
  855. (torddef(pd.rettype.def).typ<>bool8bit)) then
  856. Message(parser_e_comparative_operator_return_boolean);
  857. if (optoken=_ASSIGNMENT) and
  858. equal_defs(pd.rettype.def,
  859. tvarsym(pd.parast.symindex.first).vartype.def) then
  860. message(parser_e_no_such_assignment)
  861. else if not isoperatoracceptable(pd,optoken) then
  862. Message(parser_e_overload_impossible);
  863. end;
  864. end
  865. else
  866. begin
  867. { recover }
  868. try_to_consume(_ID);
  869. consume(_COLON);
  870. consume_all_until(_SEMICOLON);
  871. end;
  872. end;
  873. end;
  874. { support procedure proc stdcall export; }
  875. if not(is_proc_directive(token,false)) then
  876. consume(_SEMICOLON);
  877. result:=pd;
  878. end;
  879. {****************************************************************************
  880. Procedure directive handlers
  881. ****************************************************************************}
  882. procedure pd_far(pd:tabstractprocdef);
  883. begin
  884. Message1(parser_w_proc_directive_ignored,'FAR');
  885. end;
  886. procedure pd_near(pd:tabstractprocdef);
  887. begin
  888. Message1(parser_w_proc_directive_ignored,'NEAR');
  889. end;
  890. procedure pd_export(pd:tabstractprocdef);
  891. begin
  892. if pd.deftype<>procdef then
  893. internalerror(200304264);
  894. if assigned(tprocdef(pd)._class) then
  895. Message(parser_e_methods_dont_be_export);
  896. if pd.parast.symtablelevel>normal_function_level then
  897. Message(parser_e_dont_nest_export);
  898. { only os/2 and emx need this }
  899. if target_info.system in [system_i386_os2,system_i386_emx] then
  900. begin
  901. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  902. if cs_link_deffile in aktglobalswitches then
  903. deffile.AddExport(tprocdef(pd).mangledname);
  904. end;
  905. end;
  906. procedure pd_forward(pd:tabstractprocdef);
  907. begin
  908. if pd.deftype<>procdef then
  909. internalerror(200304265);
  910. tprocdef(pd).forwarddef:=true;
  911. end;
  912. procedure pd_alias(pd:tabstractprocdef);
  913. begin
  914. if pd.deftype<>procdef then
  915. internalerror(200304266);
  916. consume(_COLON);
  917. tprocdef(pd).aliasnames.insert(get_stringconst);
  918. end;
  919. procedure pd_asmname(pd:tabstractprocdef);
  920. begin
  921. if pd.deftype<>procdef then
  922. internalerror(200304267);
  923. tprocdef(pd).setmangledname(target_info.Cprefix+pattern);
  924. if token=_CCHAR then
  925. consume(_CCHAR)
  926. else
  927. consume(_CSTRING);
  928. { we don't need anything else }
  929. tprocdef(pd).forwarddef:=false;
  930. end;
  931. procedure pd_inline(pd:tabstractprocdef);
  932. var
  933. hp : tparaitem;
  934. begin
  935. { check if there is an array of const }
  936. hp:=tparaitem(pd.para.first);
  937. while assigned(hp) do
  938. begin
  939. if assigned(hp.paratype.def) and
  940. (hp.paratype.def.deftype=arraydef) then
  941. begin
  942. with tarraydef(hp.paratype.def) do
  943. if IsVariant or IsConstructor {or IsArrayOfConst} then
  944. begin
  945. Message1(parser_w_not_supported_for_inline,'array of const');
  946. Message(parser_w_inlining_disabled);
  947. pd.proccalloption:=pocall_default;
  948. end;
  949. end;
  950. hp:=tparaitem(hp.next);
  951. end;
  952. end;
  953. procedure pd_intern(pd:tabstractprocdef);
  954. begin
  955. if pd.deftype<>procdef then
  956. internalerror(200304268);
  957. consume(_COLON);
  958. tprocdef(pd).extnumber:=get_intconst;
  959. end;
  960. procedure pd_interrupt(pd:tabstractprocdef);
  961. begin
  962. if pd.parast.symtablelevel>normal_function_level then
  963. Message(parser_e_dont_nest_interrupt);
  964. end;
  965. procedure pd_abstract(pd:tabstractprocdef);
  966. begin
  967. if pd.deftype<>procdef then
  968. internalerror(200304269);
  969. if (po_virtualmethod in pd.procoptions) then
  970. include(pd.procoptions,po_abstractmethod)
  971. else
  972. Message(parser_e_only_virtual_methods_abstract);
  973. { the method is defined }
  974. tprocdef(pd).forwarddef:=false;
  975. end;
  976. procedure pd_virtual(pd:tabstractprocdef);
  977. {$ifdef WITHDMT}
  978. var
  979. pt : tnode;
  980. {$endif WITHDMT}
  981. begin
  982. if pd.deftype<>procdef then
  983. internalerror(2003042610);
  984. if (pd.proctypeoption=potype_constructor) and
  985. is_object(tprocdef(pd)._class) then
  986. Message(parser_e_constructor_cannot_be_not_virtual);
  987. {$ifdef WITHDMT}
  988. if is_object(tprocdef(pd)._class) and
  989. (token<>_SEMICOLON) then
  990. begin
  991. { any type of parameter is allowed here! }
  992. pt:=comp_expr(true);
  993. if is_constintnode(pt) then
  994. begin
  995. include(pd.procoptions,po_msgint);
  996. pd.messageinf.i:=pt^.value;
  997. end
  998. else
  999. Message(parser_e_ill_msg_expr);
  1000. disposetree(pt);
  1001. end;
  1002. {$endif WITHDMT}
  1003. end;
  1004. procedure pd_static(pd:tabstractprocdef);
  1005. begin
  1006. if (cs_static_keyword in aktmoduleswitches) then
  1007. begin
  1008. if pd.deftype=procdef then
  1009. include(tprocdef(pd).procsym.symoptions,sp_static);
  1010. include(pd.procoptions,po_staticmethod);
  1011. end;
  1012. end;
  1013. procedure pd_override(pd:tabstractprocdef);
  1014. begin
  1015. if pd.deftype<>procdef then
  1016. internalerror(2003042611);
  1017. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1018. Message(parser_e_no_object_override);
  1019. end;
  1020. procedure pd_overload(pd:tabstractprocdef);
  1021. begin
  1022. if pd.deftype<>procdef then
  1023. internalerror(2003042612);
  1024. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1025. end;
  1026. procedure pd_message(pd:tabstractprocdef);
  1027. var
  1028. pt : tnode;
  1029. begin
  1030. if pd.deftype<>procdef then
  1031. internalerror(2003042613);
  1032. if not is_class(tprocdef(pd)._class) then
  1033. Message(parser_e_msg_only_for_classes);
  1034. { check parameter type }
  1035. if ((pd.minparacount<>1) or
  1036. (pd.maxparacount<>1) or
  1037. (TParaItem(pd.Para.first).paratyp<>vs_var)) then
  1038. Message(parser_e_ill_msg_param);
  1039. pt:=comp_expr(true);
  1040. if pt.nodetype=stringconstn then
  1041. begin
  1042. include(pd.procoptions,po_msgstr);
  1043. tprocdef(pd).messageinf.str:=strnew(tstringconstnode(pt).value_str);
  1044. end
  1045. else
  1046. if is_constintnode(pt) then
  1047. begin
  1048. include(pd.procoptions,po_msgint);
  1049. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1050. end
  1051. else
  1052. Message(parser_e_ill_msg_expr);
  1053. pt.free;
  1054. end;
  1055. procedure pd_reintroduce(pd:tabstractprocdef);
  1056. begin
  1057. if pd.deftype<>procdef then
  1058. internalerror(200401211);
  1059. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1060. Message(parser_e_no_object_reintroduce);
  1061. end;
  1062. procedure pd_syscall(pd:tabstractprocdef);
  1063. {$ifdef powerpc}
  1064. var
  1065. sym : tsym;
  1066. symtable : tsymtable;
  1067. {$endif powerpc}
  1068. begin
  1069. if pd.deftype<>procdef then
  1070. internalerror(2003042614);
  1071. tprocdef(pd).forwarddef:=false;
  1072. {$ifdef powerpc}
  1073. if target_info.system in [system_powerpc_morphos,system_m68k_amiga] then
  1074. begin
  1075. pd.has_paraloc_info:=true;
  1076. include(pd.procoptions,po_explicitparaloc);
  1077. if consume_sym(sym,symtable) then
  1078. begin
  1079. if (sym.typ=varsym) and
  1080. ((tvarsym(sym).vartype.def.deftype=pointerdef) or
  1081. is_32bitint(tvarsym(sym).vartype.def)
  1082. ) then
  1083. begin
  1084. tprocdef(pd).libsym:=sym;
  1085. pd.concatpara(nil,tvarsym(sym).vartype,tvarsym(sym),nil,true);
  1086. paramanager.parseparaloc(tparaitem(pd.para.last),'A6');
  1087. end
  1088. else
  1089. Message(parser_e_32bitint_or_pointer_variable_expected);
  1090. end;
  1091. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,calleeside);
  1092. (paramanager as tppcparamanager).create_funcret_paraloc_info(pd,callerside);
  1093. end;
  1094. {$endif powerpc}
  1095. tprocdef(pd).extnumber:=get_intconst;
  1096. end;
  1097. procedure pd_external(pd:tabstractprocdef);
  1098. {
  1099. If import_dll=nil the procedure is assumed to be in another
  1100. object file. In that object file it should have the name to
  1101. which import_name is pointing to. Otherwise, the procedure is
  1102. assumed to be in the DLL to which import_dll is pointing to. In
  1103. that case either import_nr<>0 or import_name<>nil is true, so
  1104. the procedure is either imported by number or by name. (DM)
  1105. }
  1106. var
  1107. import_dll,
  1108. import_name : string;
  1109. import_nr : word;
  1110. begin
  1111. if pd.deftype<>procdef then
  1112. internalerror(2003042615);
  1113. tprocdef(pd).forwarddef:=false;
  1114. { forbid local external procedures }
  1115. if pd.parast.symtablelevel>normal_function_level then
  1116. Message(parser_e_no_local_external);
  1117. { If the procedure should be imported from a DLL, a constant string follows.
  1118. This isn't really correct, an contant string expression follows
  1119. so we check if an semicolon follows, else a string constant have to
  1120. follow (FK) }
  1121. import_nr:=0;
  1122. import_name:='';
  1123. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1124. begin
  1125. import_dll:=get_stringconst;
  1126. if (idtoken=_NAME) then
  1127. begin
  1128. consume(_NAME);
  1129. import_name:=get_stringconst;
  1130. end;
  1131. if (idtoken=_INDEX) then
  1132. begin
  1133. {After the word index follows the index number in the DLL.}
  1134. consume(_INDEX);
  1135. import_nr:=get_intconst;
  1136. end;
  1137. { default is to used the realname of the procedure }
  1138. if (import_nr=0) and (import_name='') then
  1139. import_name:=tprocdef(pd).procsym.realname;
  1140. { create importlib if not already done }
  1141. if not(current_module.uses_imports) then
  1142. begin
  1143. current_module.uses_imports:=true;
  1144. importlib.preparelib(current_module.modulename^);
  1145. end;
  1146. importlib.importprocedure(tprocdef(pd),import_dll,import_nr,import_name);
  1147. end
  1148. else
  1149. begin
  1150. if (idtoken=_NAME) then
  1151. begin
  1152. consume(_NAME);
  1153. import_name:=get_stringconst;
  1154. tprocdef(pd).setmangledname(import_name);
  1155. end
  1156. else if (m_mac in aktmodeswitches) and (token=_SEMICOLON) then
  1157. begin
  1158. {In MacPas a single "external" has the same effect as "external name 'xxx'"}
  1159. { don't do this if the procedure is also cdecl; in that case the c-prefix }
  1160. { must also be used (handled later) }
  1161. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1162. tprocdef(pd).setmangledname(tprocdef(pd).procsym.realname);
  1163. end;
  1164. end;
  1165. end;
  1166. type
  1167. pd_handler=procedure(pd:tabstractprocdef);
  1168. proc_dir_rec=record
  1169. idtok : ttoken;
  1170. pd_flags : tpdflags;
  1171. handler : pd_handler;
  1172. pocall : tproccalloption;
  1173. pooption : tprocoptions;
  1174. mutexclpocall : tproccalloptions;
  1175. mutexclpotype : tproctypeoptions;
  1176. mutexclpo : tprocoptions;
  1177. end;
  1178. const
  1179. {Should contain the number of procedure directives we support.}
  1180. num_proc_directives=37;
  1181. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1182. (
  1183. (
  1184. idtok:_ABSTRACT;
  1185. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1186. handler : {$ifdef FPCPROCVAR}@{$endif}pd_abstract;
  1187. pocall : pocall_none;
  1188. pooption : [po_abstractmethod];
  1189. mutexclpocall : [pocall_internproc,pocall_inline];
  1190. mutexclpotype : [];
  1191. mutexclpo : [po_exports,po_interrupt,po_external]
  1192. ),(
  1193. idtok:_ALIAS;
  1194. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1195. handler : {$ifdef FPCPROCVAR}@{$endif}pd_alias;
  1196. pocall : pocall_none;
  1197. pooption : [];
  1198. mutexclpocall : [pocall_inline];
  1199. mutexclpotype : [];
  1200. mutexclpo : [po_external]
  1201. ),(
  1202. idtok:_ASMNAME;
  1203. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1204. handler : {$ifdef FPCPROCVAR}@{$endif}pd_asmname;
  1205. pocall : pocall_cdecl;
  1206. pooption : [po_external];
  1207. mutexclpocall : [pocall_internproc,pocall_inline];
  1208. mutexclpotype : [];
  1209. mutexclpo : [po_external]
  1210. ),(
  1211. idtok:_ASSEMBLER;
  1212. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1213. handler : nil;
  1214. pocall : pocall_none;
  1215. pooption : [po_assembler];
  1216. mutexclpocall : [];
  1217. mutexclpotype : [];
  1218. mutexclpo : [po_external]
  1219. ),(
  1220. idtok:_C; {same as cdecl for mode mac}
  1221. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1222. handler : nil;
  1223. pocall : pocall_cdecl;
  1224. pooption : [];
  1225. mutexclpocall : [];
  1226. mutexclpotype : [potype_constructor,potype_destructor];
  1227. mutexclpo : [po_assembler,po_external]
  1228. ),(
  1229. idtok:_CDECL;
  1230. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1231. handler : nil;
  1232. pocall : pocall_cdecl;
  1233. pooption : [];
  1234. mutexclpocall : [];
  1235. mutexclpotype : [potype_constructor,potype_destructor];
  1236. mutexclpo : [po_assembler,po_external]
  1237. ),(
  1238. idtok:_DYNAMIC;
  1239. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1240. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1241. pocall : pocall_none;
  1242. pooption : [po_virtualmethod];
  1243. mutexclpocall : [pocall_internproc,pocall_inline];
  1244. mutexclpotype : [];
  1245. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod]
  1246. ),(
  1247. idtok:_EXPORT;
  1248. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1249. handler : {$ifdef FPCPROCVAR}@{$endif}pd_export;
  1250. pocall : pocall_none;
  1251. pooption : [po_exports,po_public];
  1252. mutexclpocall : [pocall_internproc,pocall_inline];
  1253. mutexclpotype : [potype_constructor,potype_destructor];
  1254. mutexclpo : [po_external,po_interrupt]
  1255. ),(
  1256. idtok:_EXTERNAL;
  1257. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf];
  1258. handler : {$ifdef FPCPROCVAR}@{$endif}pd_external;
  1259. pocall : pocall_none;
  1260. pooption : [po_external];
  1261. mutexclpocall : [pocall_internproc,pocall_inline,pocall_syscall];
  1262. mutexclpotype : [potype_constructor,potype_destructor];
  1263. mutexclpo : [po_exports,po_interrupt,po_assembler]
  1264. ),(
  1265. idtok:_FAR;
  1266. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1267. handler : {$ifdef FPCPROCVAR}@{$endif}pd_far;
  1268. pocall : pocall_none;
  1269. pooption : [];
  1270. mutexclpocall : [pocall_internproc,pocall_inline];
  1271. mutexclpotype : [];
  1272. mutexclpo : []
  1273. ),(
  1274. idtok:_FAR16;
  1275. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1276. handler : nil;
  1277. pocall : pocall_far16;
  1278. pooption : [];
  1279. mutexclpocall : [];
  1280. mutexclpotype : [];
  1281. mutexclpo : [po_external]
  1282. ),(
  1283. idtok:_FORWARD;
  1284. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1285. handler : {$ifdef FPCPROCVAR}@{$endif}pd_forward;
  1286. pocall : pocall_none;
  1287. pooption : [];
  1288. mutexclpocall : [pocall_internproc,pocall_inline];
  1289. mutexclpotype : [];
  1290. mutexclpo : [po_external]
  1291. ),(
  1292. idtok:_OLDFPCCALL;
  1293. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1294. handler : nil;
  1295. pocall : pocall_oldfpccall;
  1296. pooption : [];
  1297. mutexclpocall : [];
  1298. mutexclpotype : [];
  1299. mutexclpo : []
  1300. ),(
  1301. idtok:_INLINE;
  1302. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1303. handler : {$ifdef FPCPROCVAR}@{$endif}pd_inline;
  1304. pocall : pocall_inline;
  1305. pooption : [];
  1306. mutexclpocall : [];
  1307. mutexclpotype : [potype_constructor,potype_destructor];
  1308. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1309. ),(
  1310. idtok:_INTERNCONST;
  1311. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1312. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1313. pocall : pocall_none;
  1314. pooption : [po_internconst];
  1315. mutexclpocall : [];
  1316. mutexclpotype : [potype_operator];
  1317. mutexclpo : []
  1318. ),(
  1319. idtok:_INTERNPROC;
  1320. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1321. handler : {$ifdef FPCPROCVAR}@{$endif}pd_intern;
  1322. pocall : pocall_internproc;
  1323. pooption : [];
  1324. mutexclpocall : [];
  1325. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1326. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1327. ),(
  1328. idtok:_INTERRUPT;
  1329. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1330. handler : {$ifdef FPCPROCVAR}@{$endif}pd_interrupt;
  1331. pocall : pocall_none;
  1332. pooption : [po_interrupt];
  1333. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1334. pocall_inline,pocall_pascal,pocall_far16,pocall_oldfpccall];
  1335. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1336. mutexclpo : [po_external]
  1337. ),(
  1338. idtok:_IOCHECK;
  1339. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1340. handler : nil;
  1341. pocall : pocall_none;
  1342. pooption : [po_iocheck];
  1343. mutexclpocall : [pocall_internproc];
  1344. mutexclpotype : [];
  1345. mutexclpo : [po_external]
  1346. ),(
  1347. idtok:_MESSAGE;
  1348. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1349. handler : {$ifdef FPCPROCVAR}@{$endif}pd_message;
  1350. pocall : pocall_none;
  1351. pooption : []; { can be po_msgstr or po_msgint }
  1352. mutexclpocall : [pocall_inline,pocall_internproc];
  1353. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1354. mutexclpo : [po_interrupt,po_external]
  1355. ),(
  1356. idtok:_NEAR;
  1357. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1358. handler : {$ifdef FPCPROCVAR}@{$endif}pd_near;
  1359. pocall : pocall_none;
  1360. pooption : [];
  1361. mutexclpocall : [pocall_internproc];
  1362. mutexclpotype : [];
  1363. mutexclpo : []
  1364. ),(
  1365. idtok:_NOSTACKFRAME;
  1366. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1367. handler : nil;
  1368. pocall : pocall_none;
  1369. pooption : [po_nostackframe];
  1370. mutexclpocall : [pocall_internproc];
  1371. mutexclpotype : [];
  1372. mutexclpo : []
  1373. ),(
  1374. idtok:_OVERLOAD;
  1375. pd_flags : [pd_implemen,pd_interface,pd_body];
  1376. handler : {$ifdef FPCPROCVAR}@{$endif}pd_overload;
  1377. pocall : pocall_none;
  1378. pooption : [po_overload];
  1379. mutexclpocall : [pocall_internproc];
  1380. mutexclpotype : [];
  1381. mutexclpo : []
  1382. ),(
  1383. idtok:_OVERRIDE;
  1384. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1385. handler : {$ifdef FPCPROCVAR}@{$endif}pd_override;
  1386. pocall : pocall_none;
  1387. pooption : [po_overridingmethod,po_virtualmethod];
  1388. mutexclpocall : [pocall_inline,pocall_internproc];
  1389. mutexclpotype : [];
  1390. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1391. ),(
  1392. idtok:_PASCAL;
  1393. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1394. handler : nil;
  1395. pocall : pocall_pascal;
  1396. pooption : [];
  1397. mutexclpocall : [];
  1398. mutexclpotype : [potype_constructor,potype_destructor];
  1399. mutexclpo : [po_external]
  1400. ),(
  1401. idtok:_PUBLIC;
  1402. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1403. handler : nil;
  1404. pocall : pocall_none;
  1405. pooption : [po_public];
  1406. mutexclpocall : [pocall_internproc,pocall_inline];
  1407. mutexclpotype : [];
  1408. mutexclpo : [po_external]
  1409. ),(
  1410. idtok:_REGISTER;
  1411. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1412. handler : nil;
  1413. pocall : pocall_register;
  1414. pooption : [];
  1415. mutexclpocall : [];
  1416. mutexclpotype : [potype_constructor,potype_destructor];
  1417. mutexclpo : [po_external]
  1418. ),(
  1419. idtok:_REINTRODUCE;
  1420. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1421. handler : {$ifdef FPCPROCVAR}@{$endif}pd_reintroduce;
  1422. pocall : pocall_none;
  1423. pooption : [po_reintroduce];
  1424. mutexclpocall : [pocall_inline,pocall_internproc];
  1425. mutexclpotype : [];
  1426. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1427. ),(
  1428. idtok:_SAFECALL;
  1429. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1430. handler : nil;
  1431. pocall : pocall_safecall;
  1432. pooption : [];
  1433. mutexclpocall : [];
  1434. mutexclpotype : [potype_constructor,potype_destructor];
  1435. mutexclpo : [po_external]
  1436. ),(
  1437. idtok:_SAVEREGISTERS;
  1438. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1439. handler : nil;
  1440. pocall : pocall_none;
  1441. pooption : [po_saveregisters];
  1442. mutexclpocall : [pocall_internproc];
  1443. mutexclpotype : [potype_constructor,potype_destructor];
  1444. mutexclpo : [po_external]
  1445. ),(
  1446. idtok:_SOFTFLOAT;
  1447. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1448. handler : nil;
  1449. pocall : pocall_softfloat;
  1450. pooption : [];
  1451. mutexclpocall : [];
  1452. mutexclpotype : [potype_constructor,potype_destructor];
  1453. { it's available with po_external because the libgcc floating point routines on the arm
  1454. uses this calling convention }
  1455. mutexclpo : []
  1456. ),(
  1457. idtok:_STATIC;
  1458. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1459. handler : {$ifdef FPCPROCVAR}@{$endif}pd_static;
  1460. pocall : pocall_none;
  1461. pooption : [po_staticmethod];
  1462. mutexclpocall : [pocall_inline,pocall_internproc];
  1463. mutexclpotype : [potype_constructor,potype_destructor];
  1464. mutexclpo : [po_external,po_interrupt,po_exports]
  1465. ),(
  1466. idtok:_STDCALL;
  1467. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1468. handler : nil;
  1469. pocall : pocall_stdcall;
  1470. pooption : [];
  1471. mutexclpocall : [];
  1472. mutexclpotype : [potype_constructor,potype_destructor];
  1473. mutexclpo : [po_external]
  1474. ),(
  1475. idtok:_SYSCALL;
  1476. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf];
  1477. handler : {$ifdef FPCPROCVAR}@{$endif}pd_syscall;
  1478. pocall : pocall_syscall;
  1479. pooption : [];
  1480. mutexclpocall : [];
  1481. mutexclpotype : [potype_constructor,potype_destructor];
  1482. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1483. ),(
  1484. idtok:_VIRTUAL;
  1485. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1486. handler : {$ifdef FPCPROCVAR}@{$endif}pd_virtual;
  1487. pocall : pocall_none;
  1488. pooption : [po_virtualmethod];
  1489. mutexclpocall : [pocall_inline,pocall_internproc];
  1490. mutexclpotype : [];
  1491. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod]
  1492. ),(
  1493. idtok:_CPPDECL;
  1494. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1495. handler : nil;
  1496. pocall : pocall_cppdecl;
  1497. pooption : [];
  1498. mutexclpocall : [];
  1499. mutexclpotype : [potype_constructor,potype_destructor];
  1500. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1501. ),(
  1502. idtok:_VARARGS;
  1503. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1504. handler : nil;
  1505. pocall : pocall_none;
  1506. pooption : [po_varargs];
  1507. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1508. pocall_inline,pocall_far16,pocall_oldfpccall];
  1509. mutexclpotype : [];
  1510. mutexclpo : [po_assembler,po_interrupt]
  1511. ),(
  1512. idtok:_COMPILERPROC;
  1513. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1514. handler : nil;
  1515. pocall : pocall_compilerproc;
  1516. pooption : [];
  1517. mutexclpocall : [];
  1518. mutexclpotype : [potype_constructor,potype_destructor];
  1519. mutexclpo : [po_interrupt]
  1520. )
  1521. );
  1522. function is_proc_directive(tok:ttoken;isprocvar:boolean):boolean;
  1523. var
  1524. i : longint;
  1525. begin
  1526. is_proc_directive:=false;
  1527. for i:=1 to num_proc_directives do
  1528. if proc_direcdata[i].idtok=idtoken then
  1529. begin
  1530. if ((not isprocvar) or
  1531. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1532. { don't eat a public directive in classes }
  1533. not((proc_direcdata[i].idtok=_PUBLIC) and (symtablestack.symtabletype=objectsymtable)) then
  1534. is_proc_directive:=true;
  1535. exit;
  1536. end;
  1537. end;
  1538. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1539. {
  1540. Parse the procedure directive, returns true if a correct directive is found
  1541. }
  1542. var
  1543. p : longint;
  1544. found : boolean;
  1545. name : stringid;
  1546. begin
  1547. parse_proc_direc:=false;
  1548. name:=tokeninfo^[idtoken].str;
  1549. found:=false;
  1550. { Hint directive? Then exit immediatly }
  1551. if (m_hintdirective in aktmodeswitches) then
  1552. begin
  1553. case idtoken of
  1554. _LIBRARY,
  1555. _PLATFORM,
  1556. _UNIMPLEMENTED,
  1557. _DEPRECATED :
  1558. exit;
  1559. end;
  1560. end;
  1561. { C directive is MAC only, because it breaks too much existing code
  1562. on other platforms (PFV) }
  1563. if (idtoken=_C) and
  1564. not(m_mac in aktmodeswitches) then
  1565. exit;
  1566. { retrieve data for directive if found }
  1567. for p:=1 to num_proc_directives do
  1568. if proc_direcdata[p].idtok=idtoken then
  1569. begin
  1570. found:=true;
  1571. break;
  1572. end;
  1573. { Check if the procedure directive is known }
  1574. if not found then
  1575. begin
  1576. { parsing a procvar type the name can be any
  1577. next variable !! }
  1578. if (pdflags * [pd_procvar,pd_object])=[] then
  1579. Message1(parser_w_unknown_proc_directive_ignored,name);
  1580. exit;
  1581. end;
  1582. { static needs a special treatment }
  1583. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1584. exit;
  1585. { Conflicts between directives ? }
  1586. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1587. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1588. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1589. begin
  1590. Message1(parser_e_proc_dir_conflict,name);
  1591. exit;
  1592. end;
  1593. { set calling convention }
  1594. if proc_direcdata[p].pocall<>pocall_none then
  1595. begin
  1596. if (po_hascallingconvention in pd.procoptions) then
  1597. begin
  1598. Message2(parser_w_proc_overriding_calling,
  1599. proccalloptionStr[pd.proccalloption],
  1600. proccalloptionStr[proc_direcdata[p].pocall]);
  1601. end;
  1602. { check if the target processor supports this calling convention }
  1603. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1604. begin
  1605. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1606. { recover }
  1607. proc_direcdata[p].pocall:=pocall_stdcall;
  1608. end;
  1609. pd.proccalloption:=proc_direcdata[p].pocall;
  1610. include(pd.procoptions,po_hascallingconvention);
  1611. end;
  1612. { check if method and directive not for object, like public.
  1613. This needs to be checked also for procvars }
  1614. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1615. (pd.owner.symtabletype=objectsymtable) then
  1616. exit;
  1617. if pd.deftype=procdef then
  1618. begin
  1619. { Check if the directive is only for objects }
  1620. if (pd_object in proc_direcdata[p].pd_flags) and
  1621. not assigned(tprocdef(pd)._class) then
  1622. exit;
  1623. { check if method and directive not for interface }
  1624. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1625. is_interface(tprocdef(pd)._class) then
  1626. exit;
  1627. end
  1628. { don't eat public in object/class declarions }
  1629. else if (pd.deftype=procvardef) and (proc_direcdata[p].idtok=_PUBLIC) and
  1630. (symtablestack.symtabletype=objectsymtable) then
  1631. exit;
  1632. { consume directive, and turn flag on }
  1633. consume(token);
  1634. parse_proc_direc:=true;
  1635. { Check the pd_flags if the directive should be allowed }
  1636. if (pd_interface in pdflags) and
  1637. not(pd_interface in proc_direcdata[p].pd_flags) then
  1638. begin
  1639. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1640. exit;
  1641. end;
  1642. if (pd_implemen in pdflags) and
  1643. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1644. begin
  1645. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1646. exit;
  1647. end;
  1648. if (pd_procvar in pdflags) and
  1649. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1650. begin
  1651. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1652. exit;
  1653. end;
  1654. { Return the new pd_flags }
  1655. if not(pd_body in proc_direcdata[p].pd_flags) then
  1656. exclude(pdflags,pd_body);
  1657. { Add the correct flag }
  1658. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1659. { Call the handler }
  1660. if pointer({$ifndef FPCPROCVAR}@{$endif}proc_direcdata[p].handler)<>nil then
  1661. proc_direcdata[p].handler(pd);
  1662. end;
  1663. procedure handle_calling_convention(pd:tabstractprocdef);
  1664. begin
  1665. { set the default calling convention if none provided }
  1666. if not(po_hascallingconvention in pd.procoptions) then
  1667. pd.proccalloption:=aktdefproccall
  1668. else
  1669. begin
  1670. if pd.proccalloption=pocall_none then
  1671. internalerror(200309081);
  1672. end;
  1673. { handle proccall specific settings }
  1674. case pd.proccalloption of
  1675. pocall_cdecl :
  1676. begin
  1677. { set mangledname }
  1678. if (pd.deftype=procdef) then
  1679. begin
  1680. if not tprocdef(pd).has_mangledname then
  1681. begin
  1682. if assigned(tprocdef(pd)._class) then
  1683. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd)._class.objrealname^+'_'+tprocdef(pd).procsym.realname)
  1684. else
  1685. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).procsym.realname);
  1686. end;
  1687. { check C cdecl para types }
  1688. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1689. end;
  1690. end;
  1691. pocall_cppdecl :
  1692. begin
  1693. { set mangledname }
  1694. if (pd.deftype=procdef) then
  1695. begin
  1696. if not tprocdef(pd).has_mangledname then
  1697. tprocdef(pd).setmangledname(target_info.Cprefix+tprocdef(pd).cplusplusmangledname);
  1698. { check C cdecl para types }
  1699. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_c_para,nil);
  1700. end;
  1701. end;
  1702. pocall_compilerproc :
  1703. begin
  1704. if (pd.deftype<>procdef) then
  1705. internalerror(200110232);
  1706. if (target_info.system<>system_i386_watcom) then
  1707. tprocdef(pd).setmangledname(lower(tprocdef(pd).procsym.name));
  1708. end;
  1709. pocall_far16 :
  1710. begin
  1711. { Temporary stub, must be rewritten to support OS/2 far16 }
  1712. Message1(parser_w_proc_directive_ignored,'FAR16');
  1713. end;
  1714. pocall_inline :
  1715. begin
  1716. if not(cs_support_inline in aktmoduleswitches) then
  1717. begin
  1718. Message(parser_e_proc_inline_not_supported);
  1719. pd.proccalloption:=pocall_default;
  1720. end;
  1721. end;
  1722. end;
  1723. { For varargs directive also cdecl and external must be defined }
  1724. if (po_varargs in pd.procoptions) then
  1725. begin
  1726. { check first for external in the interface, if available there
  1727. then the cdecl must also be there since there is no implementation
  1728. available to contain it }
  1729. if parse_only then
  1730. begin
  1731. { if external is available, then cdecl must also be available,
  1732. procvars don't need external }
  1733. if not((po_external in pd.procoptions) or
  1734. (pd.deftype=procvardef)) and
  1735. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1736. Message(parser_e_varargs_need_cdecl_and_external);
  1737. end
  1738. else
  1739. begin
  1740. { both must be defined now }
  1741. if not((po_external in pd.procoptions) or
  1742. (pd.deftype=procvardef)) or
  1743. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1744. Message(parser_e_varargs_need_cdecl_and_external);
  1745. end;
  1746. end;
  1747. { add mangledname to external list }
  1748. if (pd.deftype=procdef) and
  1749. (po_external in pd.procoptions) and
  1750. target_info.DllScanSupported then
  1751. current_module.externals.insert(tExternalsItem.create(tprocdef(pd).mangledname));
  1752. end;
  1753. procedure calc_parast(pd:tabstractprocdef);
  1754. var
  1755. currpara : tparaitem;
  1756. begin
  1757. { insert hidden high parameters }
  1758. insert_hidden_para(pd);
  1759. { insert hidden self parameter }
  1760. insert_self_and_vmt_para(pd);
  1761. { insert funcret parameter if required }
  1762. insert_funcret_para(pd);
  1763. { insert parentfp parameter if required }
  1764. insert_parentfp_para(pd);
  1765. currpara:=tparaitem(pd.para.first);
  1766. while assigned(currpara) do
  1767. begin
  1768. if not(assigned(currpara.parasym) and (currpara.parasym.typ=varsym)) then
  1769. internalerror(200304232);
  1770. { connect parasym to paraitem }
  1771. tvarsym(currpara.parasym).paraitem:=currpara;
  1772. { We need a local copy for a value parameter when only the
  1773. address is pushed. Open arrays and Array of Const are
  1774. an exception because they are allocated at runtime and the
  1775. address that is pushed is patched }
  1776. if (currpara.paratyp=vs_value) and
  1777. paramanager.push_addr_param(currpara.paratyp,currpara.paratype.def,pd.proccalloption) and
  1778. not(is_open_array(currpara.paratype.def) or
  1779. is_array_of_const(currpara.paratype.def)) then
  1780. include(tvarsym(currpara.parasym).varoptions,vo_has_local_copy);
  1781. currpara:=tparaitem(currpara.next);
  1782. end;
  1783. end;
  1784. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  1785. {
  1786. Parse the procedure directives. It does not matter if procedure directives
  1787. are written using ;procdir; or ['procdir'] syntax.
  1788. }
  1789. var
  1790. res : boolean;
  1791. begin
  1792. if (m_mac in aktmodeswitches) and (cs_externally_visible in aktlocalswitches) then
  1793. begin
  1794. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  1795. include(pd.procoptions,po_public);
  1796. end;
  1797. while token in [_ID,_LECKKLAMMER] do
  1798. begin
  1799. if try_to_consume(_LECKKLAMMER) then
  1800. begin
  1801. repeat
  1802. parse_proc_direc(pd,pdflags);
  1803. until not try_to_consume(_COMMA);
  1804. consume(_RECKKLAMMER);
  1805. { we always expect at least '[];' }
  1806. res:=true;
  1807. end
  1808. else
  1809. begin
  1810. res:=parse_proc_direc(pd,pdflags);
  1811. end;
  1812. { A procedure directive normally followed by a semicolon, but in
  1813. a const section or reading a type we should stop when _EQUAL is found,
  1814. because a constant/default value follows }
  1815. if res then
  1816. begin
  1817. if (block_type in [bt_const,bt_type]) and
  1818. (token=_EQUAL) then
  1819. break;
  1820. { support procedure proc;stdcall export; }
  1821. if not(is_proc_directive(token,(pd.deftype=procvardef))) then
  1822. consume(_SEMICOLON);
  1823. end
  1824. else
  1825. break;
  1826. end;
  1827. end;
  1828. procedure parse_var_proc_directives(sym:tsym);
  1829. var
  1830. pdflags : tpdflags;
  1831. pd : tabstractprocdef;
  1832. begin
  1833. pdflags:=[pd_procvar];
  1834. pd:=nil;
  1835. case sym.typ of
  1836. varsym :
  1837. pd:=tabstractprocdef(tvarsym(sym).vartype.def);
  1838. typedconstsym :
  1839. pd:=tabstractprocdef(ttypedconstsym(sym).typedconsttype.def);
  1840. typesym :
  1841. pd:=tabstractprocdef(ttypesym(sym).restype.def);
  1842. else
  1843. internalerror(2003042617);
  1844. end;
  1845. if pd.deftype<>procvardef then
  1846. internalerror(2003042618);
  1847. { names should never be used anyway }
  1848. parse_proc_directives(pd,pdflags);
  1849. end;
  1850. procedure parse_object_proc_directives(pd:tabstractprocdef);
  1851. var
  1852. pdflags : tpdflags;
  1853. begin
  1854. pdflags:=[pd_object];
  1855. parse_proc_directives(pd,pdflags);
  1856. end;
  1857. function proc_add_definition(var pd:tprocdef):boolean;
  1858. {
  1859. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  1860. forwarddef is found and reused it returns true
  1861. }
  1862. var
  1863. hd : tprocdef;
  1864. ad,fd : tsym;
  1865. s1,s2 : stringid;
  1866. i : cardinal;
  1867. forwardfound : boolean;
  1868. po_comp : tprocoptions;
  1869. aprocsym : tprocsym;
  1870. begin
  1871. forwardfound:=false;
  1872. aprocsym:=tprocsym(pd.procsym);
  1873. { check overloaded functions if the same function already exists }
  1874. for i:=1 to aprocsym.procdef_count do
  1875. begin
  1876. hd:=aprocsym.procdef[i];
  1877. { Skip overloaded definitions that are declared in other
  1878. units }
  1879. if hd.procsym<>aprocsym then
  1880. continue;
  1881. { check the parameters, for delphi/tp it is possible to
  1882. leave the parameters away in the implementation (forwarddef=false).
  1883. But for an overload declared function this is not allowed }
  1884. if { check if empty implementation arguments match is allowed }
  1885. (
  1886. not(m_repeat_forward in aktmodeswitches) and
  1887. not(pd.forwarddef) and
  1888. (pd.maxparacount=0) and
  1889. not(po_overload in hd.procoptions)
  1890. ) or
  1891. { check arguments }
  1892. (
  1893. (compare_paras(pd.para,hd.para,cp_none,[cpo_comparedefaultvalue])>=te_equal) and
  1894. { for operators equal_paras is not enough !! }
  1895. ((pd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1896. equal_defs(hd.rettype.def,pd.rettype.def))
  1897. ) then
  1898. begin
  1899. { Check if we've found the forwarddef, if found then
  1900. we need to update the forward def with the current
  1901. implementation settings }
  1902. if hd.forwarddef then
  1903. begin
  1904. forwardfound:=true;
  1905. { Check if the procedure type and return type are correct,
  1906. also the parameters must match also with the type }
  1907. if (hd.proctypeoption<>pd.proctypeoption) or
  1908. (
  1909. (m_repeat_forward in aktmodeswitches) and
  1910. (not((pd.maxparacount=0) or
  1911. (compare_paras(pd.para,hd.para,cp_all,[cpo_comparedefaultvalue])>=te_equal)))
  1912. ) or
  1913. (
  1914. ((m_repeat_forward in aktmodeswitches) or
  1915. not(is_void(pd.rettype.def))) and
  1916. (not equal_defs(hd.rettype.def,pd.rettype.def))) then
  1917. begin
  1918. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1919. pd.fullprocname(false));
  1920. aprocsym.write_parameter_lists(pd);
  1921. break;
  1922. end;
  1923. { Check if both are declared forward }
  1924. if hd.forwarddef and pd.forwarddef then
  1925. begin
  1926. MessagePos1(pd.fileinfo,parser_e_function_already_declared_public_forward,
  1927. pd.fullprocname(false));
  1928. end;
  1929. { internconst or internproc only need to be defined once }
  1930. if (hd.proccalloption=pocall_internproc) then
  1931. pd.proccalloption:=hd.proccalloption
  1932. else
  1933. if (pd.proccalloption=pocall_internproc) then
  1934. hd.proccalloption:=pd.proccalloption;
  1935. if (po_internconst in hd.procoptions) then
  1936. include(pd.procoptions,po_internconst)
  1937. else if (po_internconst in pd.procoptions) then
  1938. include(hd.procoptions,po_internconst);
  1939. { Check calling convention }
  1940. if (hd.proccalloption<>pd.proccalloption) then
  1941. begin
  1942. { In delphi it is possible to specify the calling
  1943. convention in the interface or implementation if
  1944. there was no convention specified in the other
  1945. part }
  1946. if (m_delphi in aktmodeswitches) then
  1947. begin
  1948. if not(po_hascallingconvention in pd.procoptions) then
  1949. pd.proccalloption:=hd.proccalloption
  1950. else
  1951. if not(po_hascallingconvention in hd.procoptions) then
  1952. hd.proccalloption:=pd.proccalloption
  1953. else
  1954. begin
  1955. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1956. aprocsym.write_parameter_lists(pd);
  1957. { restore interface settings }
  1958. pd.proccalloption:=hd.proccalloption;
  1959. end;
  1960. end
  1961. else
  1962. begin
  1963. MessagePos(pd.fileinfo,parser_e_call_convention_dont_match_forward);
  1964. aprocsym.write_parameter_lists(pd);
  1965. { restore interface settings }
  1966. pd.proccalloption:=hd.proccalloption;
  1967. end;
  1968. end;
  1969. { Check procedure options, Delphi requires that class is
  1970. repeated in the implementation for class methods }
  1971. if (m_fpc in aktmodeswitches) then
  1972. po_comp:=[po_varargs,po_methodpointer,po_interrupt]
  1973. else
  1974. po_comp:=[po_classmethod,po_methodpointer];
  1975. if ((po_comp * hd.procoptions)<>(po_comp * pd.procoptions)) then
  1976. begin
  1977. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,
  1978. pd.fullprocname(false));
  1979. aprocsym.write_parameter_lists(pd);
  1980. { This error is non-fatal, we can recover }
  1981. end;
  1982. { Check manglednames }
  1983. if (m_repeat_forward in aktmodeswitches) or
  1984. (pd.minparacount>0) then
  1985. begin
  1986. { If mangled names are equal then they have the same amount of arguments }
  1987. { We can check the names of the arguments }
  1988. { both symtables are in the same order from left to right }
  1989. ad:=tsym(hd.parast.symindex.first);
  1990. fd:=tsym(pd.parast.symindex.first);
  1991. repeat
  1992. { skip default parameter constsyms }
  1993. while assigned(ad) and (ad.typ<>varsym) do
  1994. ad:=tsym(ad.indexnext);
  1995. while assigned(fd) and (fd.typ<>varsym) do
  1996. fd:=tsym(fd.indexnext);
  1997. { stop when one of the two lists is at the end }
  1998. if not assigned(ad) or not assigned(fd) then
  1999. break;
  2000. { retrieve names, remove reg for register parameters }
  2001. s1:=ad.name;
  2002. s2:=fd.name;
  2003. { compare names }
  2004. if (s1<>s2) then
  2005. begin
  2006. MessagePos3(pd.fileinfo,parser_e_header_different_var_names,
  2007. aprocsym.name,s1,s2);
  2008. break;
  2009. end;
  2010. ad:=tsym(ad.indexnext);
  2011. fd:=tsym(fd.indexnext);
  2012. until false;
  2013. if assigned(ad) xor assigned(fd) then
  2014. internalerror(200204178);
  2015. end;
  2016. { Everything is checked, now we can update the forward declaration
  2017. with the new data from the implementation }
  2018. hd.forwarddef:=pd.forwarddef;
  2019. hd.hasforward:=true;
  2020. hd.procoptions:=hd.procoptions+pd.procoptions;
  2021. if hd.extnumber=65535 then
  2022. hd.extnumber:=pd.extnumber;
  2023. while not pd.aliasnames.empty do
  2024. hd.aliasnames.insert(pd.aliasnames.getfirst);
  2025. { update fileinfo so position references the implementation,
  2026. also update funcretsym if it is already generated }
  2027. hd.fileinfo:=pd.fileinfo;
  2028. if assigned(hd.funcretsym) then
  2029. hd.funcretsym.fileinfo:=pd.fileinfo;
  2030. { update mangledname if the implementation has a fixed mangledname set }
  2031. if pd.has_mangledname then
  2032. begin
  2033. { rename also asmsymbol first, because the name can already be used }
  2034. objectlibrary.renameasmsymbol(hd.mangledname,pd.mangledname);
  2035. hd.setmangledname(pd.mangledname);
  2036. end;
  2037. { for compilerproc defines we need to rename and update the
  2038. symbolname to lowercase }
  2039. if (pd.proccalloption=pocall_compilerproc) then
  2040. begin
  2041. { rename to lowercase so users can't access it }
  2042. aprocsym.owner.rename(aprocsym.name,lower(aprocsym.name));
  2043. { also update the realname that is stored in the ppu }
  2044. stringdispose(aprocsym._realname);
  2045. aprocsym._realname:=stringdup('$'+aprocsym.name);
  2046. { the mangeled name is already changed by the pd_compilerproc }
  2047. { handler. It must be done immediately because if we have a }
  2048. { call to a compilerproc before it's implementation is }
  2049. { encountered, it must already use the new mangled name (JM) }
  2050. end;
  2051. { the procdef will be released by the symtable, we release
  2052. at least the parast }
  2053. pd.releasemem;
  2054. pd:=hd;
  2055. end
  2056. else
  2057. begin
  2058. { abstract methods aren't forward defined, but this }
  2059. { needs another error message }
  2060. if (po_abstractmethod in hd.procoptions) then
  2061. MessagePos(pd.fileinfo,parser_e_abstract_no_definition)
  2062. else
  2063. MessagePos(pd.fileinfo,parser_e_overloaded_have_same_parameters);
  2064. end;
  2065. { we found one proc with the same arguments, there are no others
  2066. so we can stop }
  2067. break;
  2068. end;
  2069. { check for allowing overload directive }
  2070. if not(m_fpc in aktmodeswitches) then
  2071. begin
  2072. { overload directive turns on overloading }
  2073. if ((po_overload in pd.procoptions) or
  2074. (po_overload in hd.procoptions)) then
  2075. begin
  2076. { check if all procs have overloading, but not if the proc is a method or
  2077. already declared forward, then the check is already done }
  2078. if not(hd.hasforward or
  2079. assigned(pd._class) or
  2080. (pd.forwarddef<>hd.forwarddef) or
  2081. ((po_overload in pd.procoptions) and
  2082. (po_overload in hd.procoptions))) then
  2083. begin
  2084. MessagePos1(pd.fileinfo,parser_e_no_overload_for_all_procs,aprocsym.realname);
  2085. break;
  2086. end;
  2087. end
  2088. else
  2089. begin
  2090. if not(hd.forwarddef) then
  2091. begin
  2092. MessagePos(pd.fileinfo,parser_e_procedure_overloading_is_off);
  2093. break;
  2094. end;
  2095. end;
  2096. end; { equal arguments }
  2097. end;
  2098. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2099. list }
  2100. if not forwardfound then
  2101. begin
  2102. aprocsym.addprocdef(pd);
  2103. { add overloadnumber for unique naming, the overloadcount is
  2104. counted per module and 0 for the first procedure }
  2105. pd.overloadnumber:=aprocsym.overloadcount;
  2106. inc(aprocsym.overloadcount);
  2107. end;
  2108. proc_add_definition:=forwardfound;
  2109. end;
  2110. end.
  2111. {
  2112. $Log$
  2113. Revision 1.191 2004-10-08 17:09:43 peter
  2114. * tvarsym.varregable added, split vo_regable from varoptions
  2115. Revision 1.190 2004/08/29 11:28:41 peter
  2116. fixed crash with error in default value
  2117. allow assembler directive in interface
  2118. Revision 1.189 2004/08/25 15:57:19 peter
  2119. * fix for tw3261
  2120. Revision 1.188 2004/08/22 20:11:38 florian
  2121. * morphos now takes any pointer var. as libbase
  2122. * alignment for sparc fixed
  2123. * int -> double conversion on sparc fixed
  2124. Revision 1.187 2004/08/22 11:24:27 peter
  2125. * don't insert result variables for constructor/destructors
  2126. Revision 1.186 2004/08/13 17:53:37 jonas
  2127. * only set the mangled name immediately for external procedures in macpas
  2128. mode if the procedure isn't cdecl (so that the c-prefix is taken into
  2129. account, necessary for Mac OS X)
  2130. Revision 1.185 2004/08/08 12:35:09 florian
  2131. * proc. var declarations in a class doesn't eat a public anymore
  2132. Revision 1.184 2004/07/17 13:51:57 florian
  2133. * function result location for syscalls on MOS hopefully correctly set now
  2134. Revision 1.183 2004/07/14 23:19:21 olle
  2135. + added external facilities for macpas
  2136. Revision 1.182 2004/06/20 08:55:30 florian
  2137. * logs truncated
  2138. Revision 1.181 2004/06/16 20:07:09 florian
  2139. * dwarf branch merged
  2140. Revision 1.180 2004/05/23 20:54:39 peter
  2141. * fixed 3114
  2142. Revision 1.179 2004/05/23 19:06:26 peter
  2143. * expect : after function when it is a forwarddef
  2144. Revision 1.178 2004/05/12 13:21:09 karoly
  2145. * few small changes to add syscall support to M68k/Amiga target
  2146. Revision 1.177 2004/05/11 22:52:48 olle
  2147. * Moved import_implicit_external to symsym
  2148. }