psub.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,
  49. {$endif newcg}
  50. temp_gen,pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and try_to_consume(_POINT) then
  109. begin
  110. storepos:=tokenpos;
  111. tokenpos:=procstartfilepos;
  112. getsym(sp,true);
  113. sym:=srsym;
  114. tokenpos:=storepos;
  115. { load proc name }
  116. sp:=pattern;
  117. realname:=orgpattern;
  118. procstartfilepos:=tokenpos;
  119. { qualifier is class name ? }
  120. if (sym^.typ<>typesym) or
  121. (ptypesym(sym)^.restype.def^.deftype<>objectdef) then
  122. begin
  123. Message(parser_e_class_id_expected);
  124. aktprocsym:=nil;
  125. consume(_ID);
  126. end
  127. else
  128. begin
  129. { used to allow private syms to be seen }
  130. aktobjectdef:=pobjectdef(ptypesym(sym)^.restype.def);
  131. procinfo^._class:=pobjectdef(ptypesym(sym)^.restype.def);
  132. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  133. consume(_ID);
  134. {The procedure has been found. So it is
  135. a global one. Set the flags to mark this.}
  136. procinfo^.flags:=procinfo^.flags or pi_is_global;
  137. aktobjectdef:=nil;
  138. { we solve this below }
  139. if not(assigned(aktprocsym)) then
  140. Message(parser_e_methode_id_expected);
  141. end;
  142. end
  143. else
  144. begin
  145. { check for constructor/destructor which is not allowed here }
  146. if (not parse_only) and
  147. (options in [potype_constructor,potype_destructor]) then
  148. Message(parser_e_constructors_always_objects);
  149. tokenpos:=procstartfilepos;
  150. aktprocsym:=pprocsym(symtablestack^.search(sp));
  151. if not(parse_only) then
  152. begin
  153. {The procedure we prepare for is in the implementation
  154. part of the unit we compile. It is also possible that we
  155. are compiling a program, which is also some kind of
  156. implementaion part.
  157. We need to find out if the procedure is global. If it is
  158. global, it is in the global symtable.}
  159. if not assigned(aktprocsym) and
  160. (symtablestack^.symtabletype=staticsymtable) then
  161. begin
  162. {Search the procedure in the global symtable.}
  163. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  164. if assigned(aktprocsym) then
  165. begin
  166. {Check if it is a procedure.}
  167. if aktprocsym^.typ<>procsym then
  168. DuplicateSym(aktprocsym);
  169. {The procedure has been found. So it is
  170. a global one. Set the flags to mark this.}
  171. procinfo^.flags:=procinfo^.flags or pi_is_global;
  172. end;
  173. end;
  174. end;
  175. end;
  176. { Create the mangledname }
  177. {$ifndef UseNiceNames}
  178. if assigned(procinfo^._class) then
  179. begin
  180. if (pos('_$$_',procprefix)=0) then
  181. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  182. else
  183. hs:=procprefix+'_$'+sp;
  184. end
  185. else
  186. begin
  187. if lexlevel=normal_function_level then
  188. hs:=procprefix+'_'+sp
  189. else
  190. hs:=procprefix+'_$'+sp;
  191. end;
  192. {$else UseNiceNames}
  193. if assigned(procinfo^._class) then
  194. begin
  195. if (pos('_5Class_',procprefix)=0) then
  196. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  197. else
  198. hs:=procprefix+'_'+tostr(length(sp))+sp;
  199. end
  200. else
  201. begin
  202. if lexlevel=normal_function_level then
  203. hs:=procprefix+'_'+tostr(length(sp))+sp
  204. else
  205. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  206. end;
  207. {$endif UseNiceNames}
  208. if assigned(aktprocsym) then
  209. begin
  210. { Check if overloading is enabled }
  211. if not(m_fpc in aktmodeswitches) then
  212. begin
  213. if aktprocsym^.typ<>procsym then
  214. begin
  215. DuplicateSym(aktprocsym);
  216. { try to recover by creating a new aktprocsym }
  217. tokenpos:=procstartfilepos;
  218. aktprocsym:=new(pprocsym,init(sp));
  219. end
  220. else
  221. begin
  222. if not(aktprocsym^.definition^.forwarddef) then
  223. Message(parser_e_procedure_overloading_is_off);
  224. end;
  225. end
  226. else
  227. begin
  228. { Check if the overloaded sym is realy a procsym }
  229. if aktprocsym^.typ<>procsym then
  230. begin
  231. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name);
  232. { try to recover by creating a new aktprocsym }
  233. tokenpos:=procstartfilepos;
  234. aktprocsym:=new(pprocsym,init(sp));
  235. end;
  236. end;
  237. end
  238. else
  239. begin
  240. { create a new procsym and set the real filepos }
  241. tokenpos:=procstartfilepos;
  242. aktprocsym:=new(pprocsym,init(sp));
  243. { for operator we have only one definition for each overloaded
  244. operation }
  245. if (options=potype_operator) then
  246. begin
  247. { the only problem is that nextoverloaded might not be in a unit
  248. known for the unit itself }
  249. if assigned(overloaded_operators[optoken]) then
  250. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  251. end;
  252. symtablestack^.insert(aktprocsym);
  253. end;
  254. st:=symtablestack;
  255. pd:=new(pprocdef,init);
  256. pd^.symtablelevel:=symtablestack^.symtablelevel;
  257. if assigned(procinfo^._class) then
  258. pd^._class := procinfo^._class;
  259. { set the options from the caller (podestructor or poconstructor) }
  260. pd^.proctypeoption:=options;
  261. { calculate the offset of the parameters }
  262. paramoffset:=8;
  263. { calculate frame pointer offset }
  264. if lexlevel>normal_function_level then
  265. begin
  266. procinfo^.framepointer_offset:=paramoffset;
  267. inc(paramoffset,target_os.size_of_pointer);
  268. { this is needed to get correct framepointer push for local
  269. forward functions !! }
  270. pd^.parast^.symtablelevel:=lexlevel;
  271. end;
  272. if assigned (procinfo^._Class) and
  273. not(procinfo^._Class^.is_class) and
  274. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  275. inc(paramoffset,target_os.size_of_pointer);
  276. { self pointer offset }
  277. { self isn't pushed in nested procedure of methods }
  278. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  279. begin
  280. procinfo^.selfpointer_offset:=paramoffset;
  281. if assigned(aktprocsym^.definition) and
  282. not(po_containsself in aktprocsym^.definition^.procoptions) then
  283. inc(paramoffset,target_os.size_of_pointer);
  284. end;
  285. { destructor flag ? }
  286. if assigned (procinfo^._Class) and
  287. procinfo^._class^.is_class and
  288. (pd^.proctypeoption=potype_destructor) then
  289. inc(paramoffset,target_os.size_of_pointer);
  290. procinfo^.para_offset:=paramoffset;
  291. pd^.parast^.datasize:=0;
  292. pd^.nextoverloaded:=aktprocsym^.definition;
  293. aktprocsym^.definition:=pd;
  294. { this is probably obsolete now PM }
  295. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  296. aktprocsym^.definition^.setmangledname(hs);
  297. aktprocsym^.definition^.procsym:=aktprocsym;
  298. if not parse_only then
  299. begin
  300. overloaded_level:=0;
  301. { we need another procprefix !!! }
  302. { count, but only those in the same unit !!}
  303. while assigned(pd) and
  304. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  305. begin
  306. { only count already implemented functions }
  307. if not(pd^.forwarddef) then
  308. inc(overloaded_level);
  309. pd:=pd^.nextoverloaded;
  310. end;
  311. if overloaded_level>0 then
  312. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  313. else
  314. procprefix:=hs+'$';
  315. end;
  316. { this must also be inserted in the right symtable !! PM }
  317. { otherwise we get subbtle problems with
  318. definitions of args defs in staticsymtable for
  319. implementation of a global method }
  320. if token=_LKLAMMER then
  321. parameter_dec(aktprocsym^.definition);
  322. { so we only restore the symtable now }
  323. symtablestack:=st;
  324. if (options=potype_operator) then
  325. overloaded_operators[optoken]:=aktprocsym;
  326. end;
  327. procedure parse_proc_dec;
  328. var
  329. hs : string;
  330. isclassmethod : boolean;
  331. begin
  332. inc(lexlevel);
  333. { read class method }
  334. if token=_CLASS then
  335. begin
  336. consume(_CLASS);
  337. isclassmethod:=true;
  338. end
  339. else
  340. isclassmethod:=false;
  341. case token of
  342. _FUNCTION : begin
  343. consume(_FUNCTION);
  344. parse_proc_head(potype_none);
  345. if token<>_COLON then
  346. begin
  347. if not(aktprocsym^.definition^.forwarddef) or
  348. (m_repeat_forward in aktmodeswitches) then
  349. begin
  350. consume(_COLON);
  351. consume_all_until(_SEMICOLON);
  352. end;
  353. end
  354. else
  355. begin
  356. consume(_COLON);
  357. inc(testcurobject);
  358. single_type(aktprocsym^.definition^.rettype,hs,false);
  359. aktprocsym^.definition^.test_if_fpu_result;
  360. dec(testcurobject);
  361. end;
  362. end;
  363. _PROCEDURE : begin
  364. consume(_PROCEDURE);
  365. parse_proc_head(potype_none);
  366. aktprocsym^.definition^.rettype.def:=voiddef;
  367. end;
  368. _CONSTRUCTOR : begin
  369. consume(_CONSTRUCTOR);
  370. parse_proc_head(potype_constructor);
  371. if assigned(procinfo^._class) and
  372. procinfo^._class^.is_class then
  373. begin
  374. { CLASS constructors return the created instance }
  375. aktprocsym^.definition^.rettype.def:=procinfo^._class;
  376. end
  377. else
  378. begin
  379. { OBJECT constructors return a boolean }
  380. {$IfDef GDB}
  381. { GDB doesn't like unnamed types !}
  382. aktprocsym^.definition^.rettype.def:=globaldef('boolean');
  383. {$else GDB}
  384. aktprocsym^.definition^.rettype.def:=new(porddef,init(bool8bit,0,1));
  385. {$Endif GDB}
  386. end;
  387. end;
  388. _DESTRUCTOR : begin
  389. consume(_DESTRUCTOR);
  390. parse_proc_head(potype_destructor);
  391. aktprocsym^.definition^.rettype.def:=voiddef;
  392. end;
  393. _OPERATOR : begin
  394. if lexlevel>normal_function_level then
  395. Message(parser_e_no_local_operator);
  396. consume(_OPERATOR);
  397. if not(token in [_PLUS..last_overloaded]) then
  398. Message(parser_e_overload_operator_failed);
  399. optoken:=token;
  400. consume(Token);
  401. procinfo^.flags:=procinfo^.flags or pi_operator;
  402. parse_proc_head(potype_operator);
  403. if token<>_ID then
  404. begin
  405. opsym:=nil;
  406. if not(m_result in aktmodeswitches) then
  407. consume(_ID);
  408. end
  409. else
  410. begin
  411. opsym:=new(pvarsym,initdef(pattern,voiddef));
  412. consume(_ID);
  413. end;
  414. if not try_to_consume(_COLON) then
  415. begin
  416. consume(_COLON);
  417. aktprocsym^.definition^.rettype.def:=generrordef;
  418. consume_all_until(_SEMICOLON);
  419. end
  420. else
  421. begin
  422. single_type(aktprocsym^.definition^.rettype,hs,false);
  423. aktprocsym^.definition^.test_if_fpu_result;
  424. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  425. ((aktprocsym^.definition^.rettype.def^.deftype<>
  426. orddef) or (porddef(aktprocsym^.definition^.
  427. rettype.def)^.typ<>bool8bit)) then
  428. Message(parser_e_comparative_operator_return_boolean);
  429. if assigned(opsym) then
  430. opsym^.vartype.def:=aktprocsym^.definition^.rettype.def;
  431. { We need to add the retrun type in the mangledname
  432. to allow overloading with just different results !! (PM) }
  433. aktprocsym^.definition^.setmangledname(
  434. aktprocsym^.definition^.mangledname+'$$'+hs);
  435. end;
  436. end;
  437. end;
  438. if isclassmethod and
  439. assigned(aktprocsym) then
  440. {$ifdef INCLUDEOK}
  441. include(aktprocsym^.definition^.procoptions,po_classmethod);
  442. {$else}
  443. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  444. {$endif}
  445. consume(_SEMICOLON);
  446. dec(lexlevel);
  447. end;
  448. {****************************************************************************
  449. Procedure directive handlers
  450. ****************************************************************************}
  451. {$ifdef tp}
  452. {$F+}
  453. {$endif}
  454. procedure pd_far(const procnames:Tstringcontainer);
  455. begin
  456. Message(parser_w_proc_far_ignored);
  457. end;
  458. procedure pd_near(const procnames:Tstringcontainer);
  459. begin
  460. Message(parser_w_proc_near_ignored);
  461. end;
  462. procedure pd_export(const procnames:Tstringcontainer);
  463. begin
  464. if assigned(procinfo^._class) then
  465. Message(parser_e_methods_dont_be_export);
  466. if lexlevel<>normal_function_level then
  467. Message(parser_e_dont_nest_export);
  468. { only os/2 needs this }
  469. if target_info.target=target_i386_os2 then
  470. begin
  471. procnames.insert(realname);
  472. procinfo^.exported:=true;
  473. if cs_link_deffile in aktglobalswitches then
  474. deffile.AddExport(aktprocsym^.definition^.mangledname);
  475. end;
  476. end;
  477. procedure pd_inline(const procnames:Tstringcontainer);
  478. begin
  479. if not(cs_support_inline in aktmoduleswitches) then
  480. Message(parser_e_proc_inline_not_supported);
  481. end;
  482. procedure pd_forward(const procnames:Tstringcontainer);
  483. begin
  484. aktprocsym^.definition^.forwarddef:=true;
  485. end;
  486. procedure pd_stdcall(const procnames:Tstringcontainer);
  487. begin
  488. end;
  489. procedure pd_safecall(const procnames:Tstringcontainer);
  490. begin
  491. end;
  492. procedure pd_alias(const procnames:Tstringcontainer);
  493. begin
  494. consume(_COLON);
  495. procnames.insert(get_stringconst);
  496. end;
  497. procedure pd_asmname(const procnames:Tstringcontainer);
  498. begin
  499. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  500. if token=_CCHAR then
  501. consume(_CCHAR)
  502. else
  503. consume(_CSTRING);
  504. { we don't need anything else }
  505. aktprocsym^.definition^.forwarddef:=false;
  506. end;
  507. procedure pd_intern(const procnames:Tstringcontainer);
  508. begin
  509. consume(_COLON);
  510. aktprocsym^.definition^.extnumber:=get_intconst;
  511. end;
  512. procedure pd_system(const procnames:Tstringcontainer);
  513. begin
  514. aktprocsym^.definition^.setmangledname(realname);
  515. end;
  516. procedure pd_abstract(const procnames:Tstringcontainer);
  517. begin
  518. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  519. {$ifdef INCLUDEOK}
  520. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  521. {$else}
  522. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  523. {$endif}
  524. else
  525. Message(parser_e_only_virtual_methods_abstract);
  526. { the method is defined }
  527. aktprocsym^.definition^.forwarddef:=false;
  528. end;
  529. procedure pd_virtual(const procnames:Tstringcontainer);
  530. begin
  531. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  532. not(aktprocsym^.definition^._class^.is_class) then
  533. Message(parser_e_constructor_cannot_be_not_virtual);
  534. end;
  535. procedure pd_static(const procnames:Tstringcontainer);
  536. begin
  537. if (cs_static_keyword in aktmoduleswitches) then
  538. begin
  539. {$ifdef INCLUDEOK}
  540. include(aktprocsym^.symoptions,sp_static);
  541. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  542. {$else}
  543. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  544. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  545. {$endif}
  546. end;
  547. end;
  548. procedure pd_override(const procnames:Tstringcontainer);
  549. begin
  550. if not(aktprocsym^.definition^._class^.is_class) then
  551. Message(parser_e_no_object_override);
  552. end;
  553. procedure pd_message(const procnames:Tstringcontainer);
  554. var
  555. pt : ptree;
  556. begin
  557. { check parameter type }
  558. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  559. ((aktprocsym^.definition^.para^.count<>1) or
  560. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  561. Message(parser_e_ill_msg_param);
  562. pt:=comp_expr(true);
  563. do_firstpass(pt);
  564. if pt^.treetype=stringconstn then
  565. begin
  566. {$ifdef INCLUDEOK}
  567. include(aktprocsym^.definition^.procoptions,po_msgstr);
  568. {$else}
  569. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  570. {$endif}
  571. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  572. end
  573. else
  574. if is_constintnode(pt) then
  575. begin
  576. {$ifdef INCLUDEOK}
  577. include(aktprocsym^.definition^.procoptions,po_msgint);
  578. {$else}
  579. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  580. {$endif}
  581. aktprocsym^.definition^.messageinf.i:=pt^.value;
  582. end
  583. else
  584. Message(parser_e_ill_msg_expr);
  585. disposetree(pt);
  586. end;
  587. procedure pd_cdecl(const procnames:Tstringcontainer);
  588. begin
  589. if aktprocsym^.definition^.deftype<>procvardef then
  590. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  591. end;
  592. procedure pd_pascal(const procnames:Tstringcontainer);
  593. var st,parast : psymtable;
  594. lastps,ps : psym;
  595. begin
  596. new(st,init(parasymtable));
  597. parast:=aktprocsym^.definition^.parast;
  598. lastps:=nil;
  599. while assigned(parast^.symindex^.first) and (lastps<>psym(parast^.symindex^.first)) do
  600. begin
  601. ps:=psym(parast^.symindex^.first);
  602. while assigned(ps^.next) and (psym(ps^.next)<>lastps) do
  603. ps:=psym(ps^.next);
  604. ps^.owner:=st;
  605. { recalculate the corrected offset }
  606. { the really_insert_in_data procedure
  607. for parasymtable should only calculateoffset PM }
  608. ps^.insert_in_data;
  609. { reset the owner correctly }
  610. ps^.owner:=parast;
  611. lastps:=ps;
  612. end;
  613. end;
  614. procedure pd_register(const procnames:Tstringcontainer);
  615. begin
  616. Message(parser_w_proc_register_ignored);
  617. end;
  618. procedure pd_syscall(const procnames:Tstringcontainer);
  619. begin
  620. aktprocsym^.definition^.forwarddef:=false;
  621. aktprocsym^.definition^.extnumber:=get_intconst;
  622. end;
  623. procedure pd_external(const procnames:Tstringcontainer);
  624. {
  625. If import_dll=nil the procedure is assumed to be in another
  626. object file. In that object file it should have the name to
  627. which import_name is pointing to. Otherwise, the procedure is
  628. assumed to be in the DLL to which import_dll is pointing to. In
  629. that case either import_nr<>0 or import_name<>nil is true, so
  630. the procedure is either imported by number or by name. (DM)
  631. }
  632. var
  633. import_dll,
  634. import_name : string;
  635. import_nr : word;
  636. begin
  637. aktprocsym^.definition^.forwarddef:=false;
  638. { If the procedure should be imported from a DLL, a constant string follows.
  639. This isn't really correct, an contant string expression follows
  640. so we check if an semicolon follows, else a string constant have to
  641. follow (FK) }
  642. import_nr:=0;
  643. import_name:='';
  644. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  645. begin
  646. import_dll:=get_stringconst;
  647. if (idtoken=_NAME) then
  648. begin
  649. consume(_NAME);
  650. import_name:=get_stringconst;
  651. end;
  652. if (idtoken=_INDEX) then
  653. begin
  654. {After the word index follows the index number in the DLL.}
  655. consume(_INDEX);
  656. import_nr:=get_intconst;
  657. end;
  658. if (import_nr=0) and (import_name='') then
  659. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  660. import_name:=aktprocsym^.definition^.mangledname
  661. else
  662. Message(parser_w_empty_import_name);}
  663. { this should work both for win32 and Linux !! PM }
  664. import_name:=realname;
  665. if not(current_module^.uses_imports) then
  666. begin
  667. current_module^.uses_imports:=true;
  668. importlib^.preparelib(current_module^.modulename^);
  669. end;
  670. if not(m_repeat_forward in aktmodeswitches) then
  671. begin
  672. { we can only have one overloaded here ! }
  673. if assigned(aktprocsym^.definition^.nextoverloaded) then
  674. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  675. import_dll,import_nr,import_name)
  676. else
  677. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  678. end
  679. else
  680. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  681. end
  682. else
  683. begin
  684. if (idtoken=_NAME) then
  685. begin
  686. consume(_NAME);
  687. import_name:=get_stringconst;
  688. aktprocsym^.definition^.setmangledname(import_name);
  689. end
  690. else
  691. begin
  692. { external shouldn't override the cdecl/system name }
  693. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  694. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  695. end;
  696. end;
  697. end;
  698. {$ifdef TP}
  699. {$F-}
  700. {$endif}
  701. {const
  702. namelength=15;}
  703. type
  704. pd_handler=procedure(const procnames:Tstringcontainer);
  705. proc_dir_rec=record
  706. idtok : ttoken;
  707. pd_flags : longint;
  708. handler : pd_handler;
  709. pocall : tproccalloptions;
  710. pooption : tprocoptions;
  711. mutexclpocall : tproccalloptions;
  712. mutexclpotype : tproctypeoptions;
  713. mutexclpo : tprocoptions;
  714. end;
  715. const
  716. {Should contain the number of procedure directives we support.}
  717. num_proc_directives=29;
  718. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  719. (
  720. (
  721. idtok:_ABSTRACT;
  722. pd_flags : pd_interface+pd_object;
  723. handler : {$ifndef TP}@{$endif}pd_abstract;
  724. pocall : [];
  725. pooption : [po_abstractmethod];
  726. mutexclpocall : [pocall_internproc,pocall_inline];
  727. mutexclpotype : [potype_constructor,potype_destructor];
  728. mutexclpo : [po_exports,po_interrupt,po_external]
  729. ),(
  730. idtok:_ALIAS;
  731. pd_flags : pd_implemen+pd_body;
  732. handler : {$ifndef TP}@{$endif}pd_alias;
  733. pocall : [];
  734. pooption : [];
  735. mutexclpocall : [pocall_inline];
  736. mutexclpotype : [];
  737. mutexclpo : [po_external]
  738. ),(
  739. idtok:_ASMNAME;
  740. pd_flags : pd_interface+pd_implemen;
  741. handler : {$ifndef TP}@{$endif}pd_asmname;
  742. pocall : [pocall_cdecl,pocall_clearstack];
  743. pooption : [po_external];
  744. mutexclpocall : [pocall_internproc];
  745. mutexclpotype : [];
  746. mutexclpo : [po_external]
  747. ),(
  748. idtok:_ASSEMBLER;
  749. pd_flags : pd_implemen+pd_body;
  750. handler : nil;
  751. pocall : [];
  752. pooption : [po_assembler];
  753. mutexclpocall : [];
  754. mutexclpotype : [];
  755. mutexclpo : [po_external]
  756. ),(
  757. idtok:_CDECL;
  758. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  759. handler : {$ifndef TP}@{$endif}pd_cdecl;
  760. pocall : [pocall_cdecl,pocall_clearstack];
  761. pooption : [po_savestdregs];
  762. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  763. mutexclpotype : [];
  764. mutexclpo : [po_assembler,po_external]
  765. ),(
  766. idtok:_DYNAMIC;
  767. pd_flags : pd_interface+pd_object;
  768. handler : {$ifndef TP}@{$endif}pd_virtual;
  769. pocall : [];
  770. pooption : [po_virtualmethod];
  771. mutexclpocall : [pocall_internproc,pocall_inline];
  772. mutexclpotype : [];
  773. mutexclpo : [po_exports,po_interrupt,po_external]
  774. ),(
  775. idtok:_EXPORT;
  776. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  777. handler : {$ifndef TP}@{$endif}pd_export;
  778. pocall : [];
  779. pooption : [po_exports];
  780. mutexclpocall : [pocall_internproc,pocall_inline];
  781. mutexclpotype : [];
  782. mutexclpo : [po_external,po_interrupt]
  783. ),(
  784. idtok:_EXTERNAL;
  785. pd_flags : pd_implemen+pd_interface;
  786. handler : {$ifndef TP}@{$endif}pd_external;
  787. pocall : [];
  788. pooption : [po_external];
  789. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  790. mutexclpotype : [];
  791. mutexclpo : [po_exports,po_interrupt,po_assembler]
  792. ),(
  793. idtok:_FAR;
  794. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  795. handler : {$ifndef TP}@{$endif}pd_far;
  796. pocall : [];
  797. pooption : [];
  798. mutexclpocall : [pocall_internproc,pocall_inline];
  799. mutexclpotype : [];
  800. mutexclpo : []
  801. ),(
  802. idtok:_FORWARD;
  803. pd_flags : pd_implemen;
  804. handler : {$ifndef TP}@{$endif}pd_forward;
  805. pocall : [];
  806. pooption : [];
  807. mutexclpocall : [pocall_internproc,pocall_inline];
  808. mutexclpotype : [];
  809. mutexclpo : [po_external]
  810. ),(
  811. idtok:_INLINE;
  812. pd_flags : pd_implemen+pd_body;
  813. handler : {$ifndef TP}@{$endif}pd_inline;
  814. pocall : [pocall_inline];
  815. pooption : [];
  816. mutexclpocall : [pocall_internproc];
  817. mutexclpotype : [potype_constructor,potype_destructor];
  818. mutexclpo : [po_exports,po_external,po_interrupt]
  819. ),(
  820. idtok:_INTERNCONST;
  821. pd_flags : pd_implemen+pd_body;
  822. handler : {$ifndef TP}@{$endif}pd_intern;
  823. pocall : [pocall_internconst];
  824. pooption : [];
  825. mutexclpocall : [];
  826. mutexclpotype : [potype_operator];
  827. mutexclpo : []
  828. ),(
  829. idtok:_INTERNPROC;
  830. pd_flags : pd_implemen;
  831. handler : {$ifndef TP}@{$endif}pd_intern;
  832. pocall : [pocall_internproc];
  833. pooption : [];
  834. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  835. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  836. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  837. ),(
  838. idtok:_INTERRUPT;
  839. pd_flags : pd_implemen+pd_body;
  840. handler : nil;
  841. pocall : [];
  842. pooption : [po_interrupt];
  843. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  844. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  845. mutexclpo : [po_external]
  846. ),(
  847. idtok:_IOCHECK;
  848. pd_flags : pd_implemen+pd_body;
  849. handler : nil;
  850. pocall : [];
  851. pooption : [po_iocheck];
  852. mutexclpocall : [pocall_internproc];
  853. mutexclpotype : [];
  854. mutexclpo : [po_external]
  855. ),(
  856. idtok:_MESSAGE;
  857. pd_flags : pd_interface+pd_object;
  858. handler : {$ifndef TP}@{$endif}pd_message;
  859. pocall : [];
  860. pooption : []; { can be po_msgstr or po_msgint }
  861. mutexclpocall : [pocall_inline,pocall_internproc];
  862. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  863. mutexclpo : [po_interrupt,po_external]
  864. ),(
  865. idtok:_NEAR;
  866. pd_flags : pd_implemen+pd_body+pd_procvar;
  867. handler : {$ifndef TP}@{$endif}pd_near;
  868. pocall : [];
  869. pooption : [];
  870. mutexclpocall : [pocall_internproc];
  871. mutexclpotype : [];
  872. mutexclpo : []
  873. ),(
  874. idtok:_OVERRIDE;
  875. pd_flags : pd_interface+pd_object;
  876. handler : {$ifndef TP}@{$endif}pd_override;
  877. pocall : [];
  878. pooption : [po_overridingmethod,po_virtualmethod];
  879. mutexclpocall : [pocall_inline,pocall_internproc];
  880. mutexclpotype : [];
  881. mutexclpo : [po_exports,po_external,po_interrupt]
  882. ),(
  883. idtok:_PASCAL;
  884. pd_flags : pd_implemen+pd_body+pd_procvar;
  885. handler : {$ifndef TP}@{$endif}pd_pascal;
  886. pocall : [pocall_leftright];
  887. pooption : [];
  888. mutexclpocall : [pocall_internproc];
  889. mutexclpotype : [];
  890. mutexclpo : [po_external]
  891. ),(
  892. idtok:_POPSTACK;
  893. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  894. handler : nil;
  895. pocall : [pocall_clearstack];
  896. pooption : [];
  897. mutexclpocall : [pocall_inline,pocall_internproc];
  898. mutexclpotype : [];
  899. mutexclpo : [po_assembler,po_external]
  900. ),(
  901. idtok:_PUBLIC;
  902. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  903. handler : nil;
  904. pocall : [];
  905. pooption : [];
  906. mutexclpocall : [pocall_internproc,pocall_inline];
  907. mutexclpotype : [];
  908. mutexclpo : [po_external]
  909. ),(
  910. idtok:_REGISTER;
  911. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  912. handler : {$ifndef TP}@{$endif}pd_register;
  913. pocall : [pocall_register];
  914. pooption : [];
  915. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  916. mutexclpotype : [];
  917. mutexclpo : [po_external]
  918. ),(
  919. idtok:_SAFECALL;
  920. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  921. handler : {$ifndef TP}@{$endif}pd_safecall;
  922. pocall : [pocall_safecall];
  923. pooption : [po_savestdregs];
  924. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  925. mutexclpotype : [];
  926. mutexclpo : [po_external]
  927. ),(
  928. idtok:_SAVEREGISTERS;
  929. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  930. handler : nil;
  931. pocall : [];
  932. pooption : [po_saveregisters];
  933. mutexclpocall : [pocall_internproc];
  934. mutexclpotype : [];
  935. mutexclpo : [po_external]
  936. ),(
  937. idtok:_STATIC;
  938. pd_flags : pd_interface+pd_object;
  939. handler : {$ifndef TP}@{$endif}pd_static;
  940. pocall : [];
  941. pooption : [po_staticmethod];
  942. mutexclpocall : [pocall_inline,pocall_internproc];
  943. mutexclpotype : [potype_constructor,potype_destructor];
  944. mutexclpo : [po_external,po_interrupt,po_exports]
  945. ),(
  946. idtok:_STDCALL;
  947. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  948. handler : {$ifndef TP}@{$endif}pd_stdcall;
  949. pocall : [pocall_stdcall];
  950. pooption : [po_savestdregs];
  951. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  952. mutexclpotype : [];
  953. mutexclpo : [po_external]
  954. ),(
  955. idtok:_SYSCALL;
  956. pd_flags : pd_interface;
  957. handler : {$ifndef TP}@{$endif}pd_syscall;
  958. pocall : [pocall_palmossyscall];
  959. pooption : [];
  960. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  961. mutexclpotype : [];
  962. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  963. ),(
  964. idtok:_SYSTEM;
  965. pd_flags : pd_implemen;
  966. handler : {$ifndef TP}@{$endif}pd_system;
  967. pocall : [pocall_clearstack];
  968. pooption : [];
  969. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  970. mutexclpotype : [];
  971. mutexclpo : [po_external,po_assembler,po_interrupt]
  972. ),(
  973. idtok:_VIRTUAL;
  974. pd_flags : pd_interface+pd_object;
  975. handler : {$ifndef TP}@{$endif}pd_virtual;
  976. pocall : [];
  977. pooption : [po_virtualmethod];
  978. mutexclpocall : [pocall_inline,pocall_internproc];
  979. mutexclpotype : [];
  980. mutexclpo : [po_external,po_interrupt,po_exports]
  981. )
  982. );
  983. function is_proc_directive(tok:ttoken):boolean;
  984. var
  985. i : longint;
  986. begin
  987. is_proc_directive:=false;
  988. for i:=1 to num_proc_directives do
  989. if proc_direcdata[i].idtok=idtoken then
  990. begin
  991. is_proc_directive:=true;
  992. exit;
  993. end;
  994. end;
  995. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  996. {
  997. Parse the procedure directive, returns true if a correct directive is found
  998. }
  999. var
  1000. p : longint;
  1001. found : boolean;
  1002. name : string;
  1003. begin
  1004. parse_proc_direc:=false;
  1005. name:=pattern;
  1006. found:=false;
  1007. for p:=1 to num_proc_directives do
  1008. if proc_direcdata[p].idtok=idtoken then
  1009. begin
  1010. found:=true;
  1011. break;
  1012. end;
  1013. { Check if the procedure directive is known }
  1014. if not found then
  1015. begin
  1016. { parsing a procvar type the name can be any
  1017. next variable !! }
  1018. if (pdflags and (pd_procvar or pd_object))=0 then
  1019. Message1(parser_w_unknown_proc_directive_ignored,name);
  1020. exit;
  1021. end;
  1022. { static needs a special treatment }
  1023. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1024. exit;
  1025. { Conflicts between directives ? }
  1026. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1027. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1028. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1029. begin
  1030. Message1(parser_e_proc_dir_conflict,name);
  1031. exit;
  1032. end;
  1033. { Check if the directive is only for objects }
  1034. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1035. not assigned(aktprocsym^.definition^._class) then
  1036. begin
  1037. exit;
  1038. end;
  1039. { check if method and directive not for object public }
  1040. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1041. assigned(aktprocsym^.definition^._class) then
  1042. begin
  1043. exit;
  1044. end;
  1045. { consume directive, and turn flag on }
  1046. consume(token);
  1047. parse_proc_direc:=true;
  1048. { Check the pd_flags if the directive should be allowed }
  1049. if ((pdflags and pd_interface)<>0) and
  1050. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1051. begin
  1052. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1053. exit;
  1054. end;
  1055. if ((pdflags and pd_implemen)<>0) and
  1056. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1057. begin
  1058. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1059. exit;
  1060. end;
  1061. if ((pdflags and pd_procvar)<>0) and
  1062. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1063. begin
  1064. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1065. exit;
  1066. end;
  1067. { Return the new pd_flags }
  1068. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1069. pdflags:=pdflags and (not pd_body);
  1070. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1071. pdflags:=pdflags or pd_global;
  1072. { Add the correct flag }
  1073. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1074. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1075. { Adjust positions of args for cdecl or stdcall }
  1076. if (aktprocsym^.definition^.deftype=procdef) and
  1077. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1078. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1079. { Call the handler }
  1080. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1081. proc_direcdata[p].handler(proc_names);
  1082. end;
  1083. {***************************************************************************}
  1084. function check_identical(var p : pprocdef) : boolean;
  1085. {
  1086. Search for idendical definitions,
  1087. if there is a forward, then kill this.
  1088. Returns the result of the forward check.
  1089. Removed from unter_dec to keep the source readable
  1090. }
  1091. var
  1092. hd,pd : Pprocdef;
  1093. storeparast : psymtable;
  1094. ad,fd : psym;
  1095. s : string;
  1096. begin
  1097. check_identical:=false;
  1098. p:=nil;
  1099. pd:=aktprocsym^.definition;
  1100. if assigned(pd) then
  1101. begin
  1102. { Is there an overload/forward ? }
  1103. if assigned(pd^.nextoverloaded) then
  1104. begin
  1105. { walk the procdef list }
  1106. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1107. begin
  1108. if not(m_repeat_forward in aktmodeswitches) or
  1109. (equal_paras(aktprocsym^.definition^.para,pd^.nextoverloaded^.para,false) and
  1110. { for operators equal_paras is not enough !! }
  1111. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1112. is_equal(pd^.nextoverloaded^.rettype.def,aktprocsym^.definition^.rettype.def))) then
  1113. begin
  1114. if pd^.nextoverloaded^.forwarddef then
  1115. { remove the forward definition but don't delete it, }
  1116. { the symtable is the owner !! }
  1117. begin
  1118. hd:=pd^.nextoverloaded;
  1119. { Check if the procedure type and return type are correct }
  1120. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1121. (not(is_equal(hd^.rettype.def,aktprocsym^.definition^.rettype.def)) and
  1122. (m_repeat_forward in aktmodeswitches)) then
  1123. begin
  1124. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1125. exit;
  1126. end;
  1127. { Check calling convention, no check for internconst,internproc which
  1128. are only defined in interface or implementation }
  1129. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1130. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1131. begin
  1132. { only trigger an error, becuase it doesn't hurt }
  1133. Message(parser_e_call_convention_dont_match_forward);
  1134. end;
  1135. { manglednames are equal? }
  1136. hd^.count:=false;
  1137. if (m_repeat_forward in aktmodeswitches) or
  1138. aktprocsym^.definition^.haspara then
  1139. begin
  1140. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1141. begin
  1142. { When overloading is not possible then we issue an error }
  1143. { This is not true, tp7/delphi don't give an error when a renamed
  1144. type is used in the other declaration (PFV)
  1145. if not(m_repeat_forward in aktmodeswitches) then
  1146. begin
  1147. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1148. exit;
  1149. end; }
  1150. if not(po_external in aktprocsym^.definition^.procoptions) then
  1151. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1152. aktprocsym^.definition^.mangledname);
  1153. { reset the mangledname of the interface part to be sure }
  1154. { this is wrong because the mangled name might have been used already !! }
  1155. if hd^.is_used then
  1156. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1157. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1158. { so we need to keep the name of interface !!
  1159. No!!!! The procedure directives can change the mangledname.
  1160. I fixed this by first calling check_identical and then doing
  1161. the proc directives, but this is not a good solution.(DM)}
  1162. { this is also wrong (PM)
  1163. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1164. end
  1165. else
  1166. begin
  1167. { If mangled names are equal, therefore }
  1168. { they have the same number of parameters }
  1169. { Therefore we can check the name of these }
  1170. { parameters... }
  1171. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1172. begin
  1173. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1174. Check_identical:=true;
  1175. { Remove other forward from the list to reduce errors }
  1176. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1177. exit;
  1178. end;
  1179. ad:=psym(hd^.parast^.symindex^.first);
  1180. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1181. if assigned(ad) and assigned(fd) then
  1182. begin
  1183. while assigned(ad) and assigned(fd) do
  1184. begin
  1185. s:=ad^.name;
  1186. if s<>fd^.name then
  1187. begin
  1188. Message3(parser_e_header_different_var_names,
  1189. aktprocsym^.name,s,fd^.name);
  1190. break;
  1191. end;
  1192. { it is impossible to have a nil pointer }
  1193. { for only one parameter - since they }
  1194. { have the same number of parameters. }
  1195. { Left = next parameter. }
  1196. ad:=psym(ad^.left);
  1197. fd:=psym(fd^.left);
  1198. end;
  1199. end;
  1200. end;
  1201. end;
  1202. { also the para_offset }
  1203. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1204. hd^.count:=true;
  1205. { remove pd^.nextoverloaded from the list }
  1206. { and add aktprocsym^.definition }
  1207. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1208. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1209. { Alert! All fields of aktprocsym^.definition that are modified
  1210. by the procdir handlers must be copied here!.}
  1211. hd^.forwarddef:=false;
  1212. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1213. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1214. if aktprocsym^.definition^.extnumber=-1 then
  1215. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1216. else
  1217. if hd^.extnumber=-1 then
  1218. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1219. { switch parast for warning in implementation PM }
  1220. if (m_repeat_forward in aktmodeswitches) or
  1221. aktprocsym^.definition^.haspara then
  1222. begin
  1223. storeparast:=hd^.parast;
  1224. hd^.parast:=aktprocsym^.definition^.parast;
  1225. aktprocsym^.definition^.parast:=storeparast;
  1226. end;
  1227. if pd=aktprocsym^.definition then
  1228. p:=nil
  1229. else
  1230. p:=pd;
  1231. aktprocsym^.definition:=hd;
  1232. check_identical:=true;
  1233. end
  1234. else
  1235. { abstract methods aren't forward defined, but this }
  1236. { needs another error message }
  1237. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1238. Message(parser_e_overloaded_have_same_parameters)
  1239. else
  1240. Message(parser_e_abstract_no_definition);
  1241. break;
  1242. end;
  1243. pd:=pd^.nextoverloaded;
  1244. end;
  1245. end
  1246. else
  1247. begin
  1248. { there is no overloaded, so its always identical with itself }
  1249. check_identical:=true;
  1250. end;
  1251. end;
  1252. { insert opsym only in the right symtable }
  1253. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1254. and not parse_only then
  1255. begin
  1256. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1257. begin
  1258. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1259. { this increases the data size }
  1260. { correct this to get the right ret $value }
  1261. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1262. { this allows to read the funcretoffset }
  1263. opsym^.address:=-4;
  1264. opsym^.varspez:=vs_var;
  1265. end
  1266. else
  1267. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1268. end;
  1269. end;
  1270. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1271. make_global,parent_has_class:boolean);
  1272. {
  1273. Compile the body of a procedure
  1274. }
  1275. var
  1276. oldexitlabel,oldexit2label : pasmlabel;
  1277. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1278. _class,hp:Pobjectdef;
  1279. { switches can change inside the procedure }
  1280. entryswitches, exitswitches : tlocalswitches;
  1281. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  1282. { code for the subroutine as tree }
  1283. {$ifdef newcg}
  1284. code:pnode;
  1285. {$else newcg}
  1286. code:ptree;
  1287. {$endif newcg}
  1288. { size of the local strackframe }
  1289. stackframe:longint;
  1290. { true when no stackframe is required }
  1291. nostackframe:boolean;
  1292. { number of bytes which have to be cleared by RET }
  1293. parasize:longint;
  1294. { filepositions }
  1295. entrypos,
  1296. savepos,
  1297. exitpos : tfileposinfo;
  1298. begin
  1299. { calculate the lexical level }
  1300. inc(lexlevel);
  1301. if lexlevel>32 then
  1302. Message(parser_e_too_much_lexlevel);
  1303. { static is also important for local procedures !! }
  1304. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1305. allow_only_static:=true
  1306. else if (lexlevel=normal_function_level) then
  1307. allow_only_static:=false;
  1308. { save old labels }
  1309. oldexitlabel:=aktexitlabel;
  1310. oldexit2label:=aktexit2label;
  1311. oldquickexitlabel:=quickexitlabel;
  1312. oldfaillabel:=faillabel;
  1313. { get new labels }
  1314. getlabel(aktexitlabel);
  1315. getlabel(aktexit2label);
  1316. { exit for fail in constructors }
  1317. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1318. begin
  1319. getlabel(faillabel);
  1320. getlabel(quickexitlabel);
  1321. end;
  1322. { reset break and continue labels }
  1323. block_type:=bt_general;
  1324. aktbreaklabel:=nil;
  1325. aktcontinuelabel:=nil;
  1326. { insert symtables for the class, by only if it is no nested function }
  1327. if assigned(procinfo^._class) and not(parent_has_class) then
  1328. begin
  1329. { insert them in the reverse order ! }
  1330. hp:=nil;
  1331. repeat
  1332. _class:=procinfo^._class;
  1333. while _class^.childof<>hp do
  1334. _class:=_class^.childof;
  1335. hp:=_class;
  1336. _class^.symtable^.next:=symtablestack;
  1337. symtablestack:=_class^.symtable;
  1338. until hp=procinfo^._class;
  1339. end;
  1340. { insert parasymtable in symtablestack}
  1341. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1342. for checking of same names used in interface and implementation !! }
  1343. if lexlevel>=normal_function_level then
  1344. begin
  1345. aktprocsym^.definition^.parast^.next:=symtablestack;
  1346. symtablestack:=aktprocsym^.definition^.parast;
  1347. symtablestack^.symtablelevel:=lexlevel;
  1348. end;
  1349. { insert localsymtable in symtablestack}
  1350. aktprocsym^.definition^.localst^.next:=symtablestack;
  1351. symtablestack:=aktprocsym^.definition^.localst;
  1352. symtablestack^.symtablelevel:=lexlevel;
  1353. { constant symbols are inserted in this symboltable }
  1354. constsymtable:=symtablestack;
  1355. { reset the temporary memory }
  1356. cleartempgen;
  1357. {$ifdef newcg}
  1358. tg.usedinproc:=[];
  1359. {$else newcg}
  1360. { no registers are used }
  1361. usedinproc:=0;
  1362. {$endif newcg}
  1363. { save entry info }
  1364. entrypos:=aktfilepos;
  1365. entryswitches:=aktlocalswitches;
  1366. localmaxfpuregisters:=aktmaxfpuregisters;
  1367. {$ifdef newcg}
  1368. { parse the code ... }
  1369. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1370. code:=convtree2node(assembler_block)
  1371. else
  1372. code:=convtree2node(block(current_module^.islibrary));
  1373. {$else newcg}
  1374. { parse the code ... }
  1375. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1376. code:=assembler_block
  1377. else
  1378. code:=block(current_module^.islibrary);
  1379. {$endif newcg}
  1380. { get a better entry point }
  1381. if assigned(code) then
  1382. entrypos:=code^.fileinfo;
  1383. { save exit info }
  1384. exitswitches:=aktlocalswitches;
  1385. exitpos:=last_endtoken_filepos;
  1386. { save current filepos }
  1387. savepos:=aktfilepos;
  1388. {When we are called to compile the body of a unit, aktprocsym should
  1389. point to the unit initialization. If the unit has no initialization,
  1390. aktprocsym=nil. But in that case code=nil. hus we should check for
  1391. code=nil, when we use aktprocsym.}
  1392. { set the framepointer to esp for assembler functions }
  1393. { but only if the are no local variables }
  1394. { already done in assembler_block }
  1395. {$ifdef newcg}
  1396. tg.setfirsttemp(procinfo^.firsttemp_offset);
  1397. {$else newcg}
  1398. setfirsttemp(procinfo^.firsttemp_offset);
  1399. {$endif newcg}
  1400. { ... and generate assembler }
  1401. { but set the right switches for entry !! }
  1402. aktlocalswitches:=entryswitches;
  1403. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  1404. aktmaxfpuregisters:=localmaxfpuregisters;
  1405. {$ifndef NOPASS2}
  1406. {$ifdef newcg}
  1407. tg.setfirsttemp(procinfo^.firsttemp_offset);
  1408. {$else newcg}
  1409. if assigned(code) then
  1410. generatecode(code);
  1411. {$endif newcg}
  1412. { set switches to status at end of procedure }
  1413. aktlocalswitches:=exitswitches;
  1414. if assigned(code) then
  1415. begin
  1416. aktprocsym^.definition^.code:=code;
  1417. { the procedure is now defined }
  1418. aktprocsym^.definition^.forwarddef:=false;
  1419. {$ifdef newcg}
  1420. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1421. {$else newcg}
  1422. aktprocsym^.definition^.usedregisters:=usedinproc;
  1423. {$endif newcg}
  1424. end;
  1425. {$ifdef newcg}
  1426. stackframe:=tg.gettempsize;
  1427. {$else newcg}
  1428. stackframe:=gettempsize;
  1429. {$endif newcg}
  1430. { first generate entry code with the correct position and switches }
  1431. aktfilepos:=entrypos;
  1432. aktlocalswitches:=entryswitches;
  1433. {$ifdef newcg}
  1434. if assigned(code) then
  1435. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1436. {$else newcg}
  1437. if assigned(code) then
  1438. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1439. {$endif newcg}
  1440. { now generate exit code with the correct position and switches }
  1441. aktfilepos:=exitpos;
  1442. aktlocalswitches:=exitswitches;
  1443. if assigned(code) then
  1444. begin
  1445. {$ifdef newcg}
  1446. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1447. {$else newcg}
  1448. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1449. {$endif newcg}
  1450. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1451. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1452. {$ifdef i386}
  1453. {$ifndef NoOpt}
  1454. if (cs_optimize in aktglobalswitches) and
  1455. { do not optimize pure assembler procedures }
  1456. ((procinfo^.flags and pi_is_assembler)=0) then
  1457. Optimize(procinfo^.aktproccode);
  1458. {$endif NoOpt}
  1459. {$endif}
  1460. { save local data (casetable) also in the same file }
  1461. if assigned(procinfo^.aktlocaldata) and
  1462. (not procinfo^.aktlocaldata^.empty) then
  1463. begin
  1464. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1465. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1466. end;
  1467. { now we can insert a cut }
  1468. if (cs_create_smart in aktmoduleswitches) then
  1469. codesegment^.concat(new(pai_cut,init));
  1470. { add the procedure to the codesegment }
  1471. codesegment^.concatlist(procinfo^.aktproccode);
  1472. end;
  1473. {$else}
  1474. if assigned(code) then
  1475. firstpass(code);
  1476. {$endif NOPASS2}
  1477. { ... remove symbol tables, for the browser leave the static table }
  1478. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1479. symtablestack^.next:=symtablestack^.next^.next
  1480. else }
  1481. if lexlevel>=normal_function_level then
  1482. symtablestack:=symtablestack^.next^.next
  1483. else
  1484. symtablestack:=symtablestack^.next;
  1485. { ... check for unused symbols }
  1486. { but only if there is no asm block }
  1487. if assigned(code) then
  1488. begin
  1489. if (Errorcount=0) then
  1490. begin
  1491. aktprocsym^.definition^.localst^.check_forwards;
  1492. aktprocsym^.definition^.localst^.checklabels;
  1493. end;
  1494. if (procinfo^.flags and pi_uses_asm)=0 then
  1495. begin
  1496. { not for unit init, becuase the var can be used in finalize,
  1497. it will be done in proc_unit }
  1498. if not(aktprocsym^.definition^.proctypeoption
  1499. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  1500. aktprocsym^.definition^.localst^.allsymbolsused;
  1501. aktprocsym^.definition^.parast^.allsymbolsused;
  1502. end;
  1503. end;
  1504. { the local symtables can be deleted, but the parast }
  1505. { doesn't, (checking definitons when calling a }
  1506. { function }
  1507. { not for a inline procedure !! (PM) }
  1508. { at lexlevel = 1 localst is the staticsymtable itself }
  1509. { so no dispose here !! }
  1510. if assigned(code) and
  1511. not(cs_browser in aktmoduleswitches) and
  1512. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1513. begin
  1514. if lexlevel>=normal_function_level then
  1515. dispose(aktprocsym^.definition^.localst,done);
  1516. aktprocsym^.definition^.localst:=nil;
  1517. end;
  1518. { only now we can remove the temps }
  1519. resettempgen;
  1520. { remove code tree, if not inline procedure }
  1521. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1522. {$ifdef newcg}
  1523. dispose(code,done);
  1524. {$else newcg}
  1525. disposetree(code);
  1526. {$endif newcg}
  1527. { remove class member symbol tables }
  1528. while symtablestack^.symtabletype=objectsymtable do
  1529. symtablestack:=symtablestack^.next;
  1530. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  1531. { restore filepos, the switches are already set }
  1532. aktfilepos:=savepos;
  1533. { restore labels }
  1534. aktexitlabel:=oldexitlabel;
  1535. aktexit2label:=oldexit2label;
  1536. quickexitlabel:=oldquickexitlabel;
  1537. faillabel:=oldfaillabel;
  1538. { reset to normal non static function }
  1539. if (lexlevel=normal_function_level) then
  1540. allow_only_static:=false;
  1541. { previous lexlevel }
  1542. dec(lexlevel);
  1543. end;
  1544. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1545. {
  1546. Parse the procedure directives. It does not matter if procedure directives
  1547. are written using ;procdir; or ['procdir'] syntax.
  1548. }
  1549. var
  1550. res : boolean;
  1551. begin
  1552. while token in [_ID,_LECKKLAMMER] do
  1553. begin
  1554. if try_to_consume(_LECKKLAMMER) then
  1555. begin
  1556. repeat
  1557. parse_proc_direc(Anames^,pdflags);
  1558. until not try_to_consume(_COMMA);
  1559. consume(_RECKKLAMMER);
  1560. { we always expect at least '[];' }
  1561. res:=true;
  1562. end
  1563. else
  1564. res:=parse_proc_direc(Anames^,pdflags);
  1565. { A procedure directive normally followed by a semicolon, but in
  1566. a const section we should stop when _EQUAL is found }
  1567. if res then
  1568. begin
  1569. if (block_type=bt_const) and
  1570. (token=_EQUAL) then
  1571. break;
  1572. consume(_SEMICOLON);
  1573. end
  1574. else
  1575. break;
  1576. end;
  1577. end;
  1578. procedure parse_var_proc_directives(var sym : psym);
  1579. var
  1580. anames : pstringcontainer;
  1581. pdflags : word;
  1582. oldsym : pprocsym;
  1583. pd : pabstractprocdef;
  1584. begin
  1585. oldsym:=aktprocsym;
  1586. anames:=new(pstringcontainer,init);
  1587. pdflags:=pd_procvar;
  1588. { we create a temporary aktprocsym to read the directives }
  1589. aktprocsym:=new(pprocsym,init(sym^.name));
  1590. case sym^.typ of
  1591. varsym :
  1592. pd:=pabstractprocdef(pvarsym(sym)^.vartype.def);
  1593. typedconstsym :
  1594. pd:=pabstractprocdef(ptypedconstsym(sym)^.typedconsttype.def);
  1595. typesym :
  1596. pd:=pabstractprocdef(ptypesym(sym)^.restype.def);
  1597. else
  1598. internalerror(994932432);
  1599. end;
  1600. if pd^.deftype<>procvardef then
  1601. internalerror(994932433);
  1602. pabstractprocdef(aktprocsym^.definition):=pd;
  1603. { names should never be used anyway }
  1604. inc(lexlevel);
  1605. parse_proc_directives(anames,pdflags);
  1606. dec(lexlevel);
  1607. aktprocsym^.definition:=nil;
  1608. dispose(aktprocsym,done);
  1609. dispose(anames,done);
  1610. aktprocsym:=oldsym;
  1611. end;
  1612. procedure parse_object_proc_directives(var sym : pprocsym);
  1613. var
  1614. anames : pstringcontainer;
  1615. pdflags : word;
  1616. begin
  1617. pdflags:=pd_object;
  1618. anames:=new(pstringcontainer,init);
  1619. inc(lexlevel);
  1620. parse_proc_directives(anames,pdflags);
  1621. dec(lexlevel);
  1622. dispose(anames,done);
  1623. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1624. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1625. Message(parser_e_self_in_non_message_handler);
  1626. end;
  1627. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1628. var
  1629. vs : pvarsym;
  1630. s : string;
  1631. begin
  1632. with pvarsym(p)^ do
  1633. begin
  1634. if copy(name,1,3)='val' then
  1635. begin
  1636. s:=Copy(name,4,255);
  1637. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1638. begin
  1639. vs:=new(Pvarsym,initdef(s,vartype.def));
  1640. vs^.fileinfo:=fileinfo;
  1641. vs^.varspez:=varspez;
  1642. aktprocsym^.definition^.localst^.insert(vs);
  1643. {$ifdef INCLUDEOK}
  1644. include(vs^.varoptions,vo_is_local_copy);
  1645. {$else}
  1646. vs^.varoptions:=vs^.varoptions+[vo_is_local_copy];
  1647. {$endif}
  1648. vs^.varstate:=vs_assigned;
  1649. localvarsym:=vs;
  1650. inc(refs); { the para was used to set the local copy ! }
  1651. { warnings only on local copy ! }
  1652. varstate:=vs_used;
  1653. end
  1654. else
  1655. begin
  1656. aktprocsym^.definition^.parast^.rename(name,s);
  1657. end;
  1658. end;
  1659. end;
  1660. end;
  1661. procedure read_proc;
  1662. {
  1663. Parses the procedure directives, then parses the procedure body, then
  1664. generates the code for it
  1665. }
  1666. var
  1667. oldprefix : string;
  1668. oldprocsym : Pprocsym;
  1669. oldprocinfo : pprocinfo;
  1670. oldconstsymtable : Psymtable;
  1671. oldfilepos : tfileposinfo;
  1672. names : Pstringcontainer;
  1673. pdflags : word;
  1674. prevdef,stdef : pprocdef;
  1675. begin
  1676. { save old state }
  1677. oldprocsym:=aktprocsym;
  1678. oldprefix:=procprefix;
  1679. oldconstsymtable:=constsymtable;
  1680. oldprocinfo:=procinfo;
  1681. { create a new procedure }
  1682. new(names,init);
  1683. {$ifdef fixLeaksOnError}
  1684. strContStack.push(names);
  1685. {$endif fixLeaksOnError}
  1686. codegen_newprocedure;
  1687. with procinfo^ do
  1688. begin
  1689. parent:=oldprocinfo;
  1690. { clear flags }
  1691. flags:=0;
  1692. { standard frame pointer }
  1693. framepointer:=frame_pointer;
  1694. { funcret_is_valid:=false; }
  1695. funcret_state:=vs_declared;
  1696. { is this a nested function of a method ? }
  1697. if assigned(oldprocinfo) then
  1698. _class:=oldprocinfo^._class;
  1699. end;
  1700. parse_proc_dec;
  1701. { set the default function options }
  1702. if parse_only then
  1703. begin
  1704. aktprocsym^.definition^.forwarddef:=true;
  1705. { set also the interface flag, for better error message when the
  1706. implementation doesn't much this header }
  1707. aktprocsym^.definition^.interfacedef:=true;
  1708. pdflags:=pd_interface;
  1709. end
  1710. else
  1711. begin
  1712. pdflags:=pd_body;
  1713. if current_module^.in_implementation then
  1714. pdflags:=pdflags or pd_implemen;
  1715. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1716. pdflags:=pdflags or pd_global;
  1717. procinfo^.exported:=false;
  1718. aktprocsym^.definition^.forwarddef:=false;
  1719. end;
  1720. { parse the directives that may follow }
  1721. inc(lexlevel);
  1722. parse_proc_directives(names,pdflags);
  1723. dec(lexlevel);
  1724. { set aktfilepos to the beginning of the function declaration }
  1725. oldfilepos:=aktfilepos;
  1726. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1727. { search for forward declarations }
  1728. if not check_identical(prevdef) then
  1729. begin
  1730. { A method must be forward defined (in the object declaration) }
  1731. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1732. Message(parser_e_header_dont_match_any_member);
  1733. { Give a better error if there is a forward def in the interface and only
  1734. a single implementation }
  1735. if (not aktprocsym^.definition^.forwarddef) and
  1736. assigned(aktprocsym^.definition^.nextoverloaded) and
  1737. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1738. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1739. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1740. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1741. else
  1742. begin
  1743. { check the global flag }
  1744. if (procinfo^.flags and pi_is_global)<>0 then
  1745. Message(parser_e_overloaded_must_be_all_global);
  1746. end
  1747. end;
  1748. { set return type here, becuase the aktprocsym^.definition can be
  1749. changed by check_identical (PFV) }
  1750. procinfo^.returntype.def:=aktprocsym^.definition^.rettype.def;
  1751. { pointer to the return value ? }
  1752. if ret_in_param(procinfo^.returntype.def) then
  1753. begin
  1754. procinfo^.return_offset:=procinfo^.para_offset;
  1755. inc(procinfo^.para_offset,target_os.size_of_pointer);
  1756. end;
  1757. { allows to access the parameters of main functions in nested functions }
  1758. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  1759. { when it is a value para and it needs a local copy then rename
  1760. the parameter and insert a copy in the localst. This is not done
  1761. for assembler procedures }
  1762. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1763. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1764. { restore file pos }
  1765. aktfilepos:=oldfilepos;
  1766. { compile procedure when a body is needed }
  1767. if (pdflags and pd_body)<>0 then
  1768. begin
  1769. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1770. names^.insert(aktprocsym^.definition^.mangledname);
  1771. { set _FAIL as keyword if constructor }
  1772. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1773. tokeninfo^[_FAIL].keyword:=m_all;
  1774. if assigned(aktprocsym^.definition^._class) then
  1775. tokeninfo^[_SELF].keyword:=m_all;
  1776. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1777. { reset _FAIL as normal }
  1778. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1779. tokeninfo^[_FAIL].keyword:=m_none;
  1780. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1781. tokeninfo^[_SELF].keyword:=m_none;
  1782. consume(_SEMICOLON);
  1783. end;
  1784. { close }
  1785. {$ifdef fixLeaksOnError}
  1786. if names <> strContStack.pop then
  1787. writeln('problem with strContStack in psub!');
  1788. {$endif fixLeaksOnError}
  1789. dispose(names,done);
  1790. codegen_doneprocedure;
  1791. { Restore old state }
  1792. constsymtable:=oldconstsymtable;
  1793. { from now on all refernece to mangledname means
  1794. that the function is already used }
  1795. aktprocsym^.definition^.count:=true;
  1796. { restore the interface order to maintain CRC values PM }
  1797. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1798. begin
  1799. stdef:=aktprocsym^.definition;
  1800. aktprocsym^.definition:=stdef^.nextoverloaded;
  1801. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1802. prevdef^.nextoverloaded:=stdef;
  1803. end;
  1804. aktprocsym:=oldprocsym;
  1805. procprefix:=oldprefix;
  1806. procinfo:=oldprocinfo;
  1807. opsym:=nil;
  1808. end;
  1809. end.
  1810. {
  1811. $Log$
  1812. Revision 1.43 2000-01-21 22:06:16 florian
  1813. * fixed for the fix of bug 793
  1814. * fpu variables modified by nested subroutines aren't regable anymore
  1815. * $maxfpuregisters doesn't modify anymore the behavior of a procedure before
  1816. Revision 1.42 2000/01/16 22:17:12 peter
  1817. * renamed call_offset to para_offset
  1818. Revision 1.41 2000/01/11 17:16:06 jonas
  1819. * removed a lot of memory leaks when an error is encountered (caused by
  1820. procinfo and pstringcontainers). There are still plenty left though :)
  1821. Revision 1.40 2000/01/07 01:14:31 peter
  1822. * updated copyright to 2000
  1823. Revision 1.39 1999/12/22 01:01:52 peter
  1824. - removed freelabel()
  1825. * added undefined label detection in internal assembler, this prevents
  1826. a lot of ld crashes and wrong .o files
  1827. * .o files aren't written anymore if errors have occured
  1828. * inlining of assembler labels is now correct
  1829. Revision 1.38 1999/12/06 18:17:09 peter
  1830. * newcg compiler compiles again
  1831. Revision 1.37 1999/11/30 10:40:48 peter
  1832. + ttype, tsymlist
  1833. Revision 1.36 1999/11/22 00:23:09 pierre
  1834. * also complain about unused functions in program
  1835. Revision 1.35 1999/11/17 17:05:02 pierre
  1836. * Notes/hints changes
  1837. Revision 1.34 1999/11/10 00:24:02 pierre
  1838. * more browser details
  1839. Revision 1.33 1999/11/09 23:43:08 pierre
  1840. * better browser info
  1841. Revision 1.32 1999/11/09 23:06:45 peter
  1842. * esi_offset -> selfpointer_offset to be newcg compatible
  1843. * hcogegen -> cgbase fixes for newcg
  1844. Revision 1.31 1999/11/06 14:34:23 peter
  1845. * truncated log to 20 revs
  1846. Revision 1.30 1999/10/26 12:30:44 peter
  1847. * const parameter is now checked
  1848. * better and generic check if a node can be used for assigning
  1849. * export fixes
  1850. * procvar equal works now (it never had worked at least from 0.99.8)
  1851. * defcoll changed to linkedlist with pparaitem so it can easily be
  1852. walked both directions
  1853. Revision 1.29 1999/10/22 10:39:35 peter
  1854. * split type reading from pdecl to ptype unit
  1855. * parameter_dec routine is now used for procedure and procvars
  1856. Revision 1.28 1999/10/13 10:37:36 peter
  1857. * moved mangledname creation of normal proc so it also handles a wrong
  1858. method proc
  1859. Revision 1.27 1999/10/12 21:20:46 florian
  1860. * new codegenerator compiles again
  1861. Revision 1.26 1999/10/03 19:38:39 peter
  1862. * fixed forward decl check for tp7/delphi
  1863. Revision 1.25 1999/10/01 10:05:44 peter
  1864. + procedure directive support in const declarations, fixes bug 232
  1865. Revision 1.24 1999/10/01 08:02:47 peter
  1866. * forward type declaration rewritten
  1867. Revision 1.23 1999/09/27 23:44:56 peter
  1868. * procinfo is now a pointer
  1869. * support for result setting in sub procedure
  1870. Revision 1.22 1999/09/20 16:39:00 peter
  1871. * cs_create_smart instead of cs_smartlink
  1872. * -CX is create smartlink
  1873. * -CD is create dynamic, but does nothing atm.
  1874. Revision 1.21 1999/09/15 20:35:42 florian
  1875. * small fix to operator overloading when in MMX mode
  1876. + the compiler uses now fldz and fld1 if possible
  1877. + some fixes to floating point registers
  1878. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1879. * .... ???
  1880. Revision 1.20 1999/09/10 18:48:09 florian
  1881. * some bug fixes (e.g. must_be_valid and procinfo^.funcret_is_valid)
  1882. * most things for stored properties fixed
  1883. Revision 1.19 1999/09/07 14:59:40 pierre
  1884. * bugfix for 237 and 244
  1885. Revision 1.18 1999/09/02 18:47:45 daniel
  1886. * Could not compile with TP, some arrays moved to heap
  1887. * NOAG386BIN default for TP
  1888. * AG386* files were not compatible with TP, fixed.
  1889. Revision 1.17 1999/08/30 10:17:57 peter
  1890. * fixed crash in psub
  1891. * ansistringcompare fixed
  1892. * support for #$0b8
  1893. Revision 1.16 1999/08/27 10:41:46 pierre
  1894. * pi_is_global was missing for global methods
  1895. + code to restore overloaded list order
  1896. (necessary to get same CRC values after interface and after
  1897. implementation)
  1898. Revision 1.15 1999/08/19 13:02:11 pierre
  1899. + label faillabel added for _FAIL support
  1900. Revision 1.14 1999/08/10 16:24:44 pierre
  1901. * linking to C code with cdecl;external; was broken
  1902. Revision 1.13 1999/08/10 12:37:44 pierre
  1903. + procsym field of tprocdef set
  1904. Revision 1.12 1999/08/05 16:53:06 peter
  1905. * V_Fatal=1, all other V_ are also increased
  1906. * Check for local procedure when assigning procvar
  1907. * fixed comment parsing because directives
  1908. * oldtp mode directives better supported
  1909. * added some messages to errore.msg
  1910. Revision 1.11 1999/08/04 13:03:01 jonas
  1911. * all tokens now start with an underscore
  1912. * PowerPC compiles!!
  1913. }