~ubuntu-branches/ubuntu/precise/qtscriptgenerator/precise

« back to all changes in this revision

Viewing changes to generator/parser/declarator_compiler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2009-04-25 16:16:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090425161602-vrlxapa3fbo2k3x7
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
 
4
** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
 
5
**
 
6
** This file is part of the Qt Script Generator project on Trolltech Labs.
 
7
**
 
8
** This file may be used under the terms of the GNU General Public
 
9
** License version 2.0 as published by the Free Software Foundation
 
10
** and appearing in the file LICENSE.GPL included in the packaging of
 
11
** this file.  Please review the following information to ensure GNU
 
12
** General Public Licensing requirements will be met:
 
13
** http://www.trolltech.com/products/qt/opensource.html
 
14
**
 
15
** If you are unsure which license is appropriate for your use, please
 
16
** review the following information:
 
17
** http://www.trolltech.com/products/qt/licensing.html or contact the
 
18
** sales department at sales@trolltech.com.
 
19
**
 
20
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
21
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
22
**
 
23
****************************************************************************/
 
24
 
 
25
 
 
26
#include "declarator_compiler.h"
 
27
#include "name_compiler.h"
 
28
#include "type_compiler.h"
 
29
#include "compiler_utils.h"
 
30
#include "lexer.h"
 
31
#include "binder.h"
 
32
#include "tokens.h"
 
33
 
 
34
#include <qdebug.h>
 
35
 
 
36
DeclaratorCompiler::DeclaratorCompiler(Binder *binder)
 
37
  : _M_binder (binder), _M_token_stream (binder->tokenStream ())
 
38
{
 
39
}
 
40
 
 
41
void DeclaratorCompiler::run(DeclaratorAST *node)
 
42
{
 
43
  _M_id.clear();
 
44
  _M_parameters.clear();
 
45
  _M_array.clear();
 
46
  _M_function = false;
 
47
  _M_reference = false;
 
48
  _M_variadics = false;
 
49
  _M_indirection = 0;
 
50
 
 
51
  if (node)
 
52
    {
 
53
      NameCompiler name_cc(_M_binder);
 
54
 
 
55
      DeclaratorAST *decl = node;
 
56
      while (decl && decl->sub_declarator)
 
57
        decl = decl->sub_declarator;
 
58
 
 
59
      Q_ASSERT (decl != 0);
 
60
 
 
61
      name_cc.run(decl->id);
 
62
      _M_id = name_cc.name();
 
63
      _M_function = (node->parameter_declaration_clause != 0);
 
64
      if (node->parameter_declaration_clause && node->parameter_declaration_clause->ellipsis)
 
65
        _M_variadics = true;
 
66
 
 
67
      visitNodes(this, node->ptr_ops);
 
68
      visit(node->parameter_declaration_clause);
 
69
 
 
70
      if (const ListNode<ExpressionAST*> *it = node->array_dimensions)
 
71
        {
 
72
          it->toFront();
 
73
          const ListNode<ExpressionAST*> *end = it;
 
74
 
 
75
          do
 
76
            {
 
77
              QString elt;
 
78
              if (ExpressionAST *expr = it->element)
 
79
                {
 
80
                  const Token &start_token = _M_token_stream->token((int) expr->start_token);
 
81
                  const Token &end_token = _M_token_stream->token((int) expr->end_token);
 
82
 
 
83
                  elt += QString::fromUtf8(&start_token.text[start_token.position],
 
84
                                           (int) (end_token.position - start_token.position)).trimmed();
 
85
                }
 
86
 
 
87
              _M_array.append (elt);
 
88
 
 
89
              it = it->next;
 
90
            }
 
91
          while (it != end);
 
92
        }
 
93
    }
 
94
}
 
95
 
 
96
void DeclaratorCompiler::visitPtrOperator(PtrOperatorAST *node)
 
97
{
 
98
  std::size_t op =  _M_token_stream->kind(node->op);
 
99
 
 
100
  switch (op)
 
101
    {
 
102
      case '&':
 
103
        _M_reference = true;
 
104
        break;
 
105
      case '*':
 
106
        ++_M_indirection;
 
107
        break;
 
108
 
 
109
      default:
 
110
        break;
 
111
    }
 
112
 
 
113
  if (node->mem_ptr)
 
114
    {
 
115
#if defined(__GNUC__)
 
116
#warning "ptr to mem -- not implemented"
 
117
#endif
 
118
    }
 
119
}
 
120
 
 
121
void DeclaratorCompiler::visitParameterDeclaration(ParameterDeclarationAST *node)
 
122
{
 
123
  Parameter p;
 
124
 
 
125
  TypeCompiler type_cc(_M_binder);
 
126
  DeclaratorCompiler decl_cc(_M_binder);
 
127
 
 
128
  decl_cc.run(node->declarator);
 
129
 
 
130
  p.name = decl_cc.id();
 
131
  p.type = CompilerUtils::typeDescription(node->type_specifier, node->declarator, _M_binder);
 
132
  if (node->expression != 0)
 
133
    {
 
134
      const Token &start = _M_token_stream->token((int) node->expression->start_token);
 
135
      const Token &end = _M_token_stream->token((int) node->expression->end_token);
 
136
      int length = (int) (end.position - start.position);
 
137
 
 
138
      p.defaultValueExpression = QString();
 
139
      QString source = QString::fromUtf8(&start.text[start.position], length).trimmed();
 
140
      QStringList list = source.split("\n");
 
141
 
 
142
 
 
143
      for (int i=0; i<list.size(); ++i) {
 
144
          if (!list.at(i).startsWith("#"))
 
145
              p.defaultValueExpression += list.at(i).trimmed();
 
146
      }
 
147
 
 
148
      p.defaultValue = p.defaultValueExpression.size() > 0;
 
149
 
 
150
    }
 
151
 
 
152
  _M_parameters.append(p);
 
153
}
 
154
 
 
155
// kate: space-indent on; indent-width 2; replace-tabs on;